org.hyperledger.fabric.sdk.BlockchainInfo Java Examples

The following examples show how to use org.hyperledger.fabric.sdk.BlockchainInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FabricContext.java    From spring-fabric-gateway with MIT License 6 votes vote down vote up
public FabricQueryResponse<FabricLedger> queryBlockchainInfo() {
	try {
		if (network == null) {
			getContract();
		}
		Channel channel = network.getChannel();
		BlockchainInfo info = channel.queryBlockchainInfo();

		FabricLedger ledger = new FabricLedger();
		ledger.setChannel(channel.getName());
		ledger.setHeight(info.getHeight());
		ledger.setCurrentHash(Hex.encodeHexString(info.getCurrentBlockHash()));
		ledger.setPreviousHash(Hex.encodeHexString(info.getPreviousBlockHash()));
		ledger.setName(properties.getName());
		ledger.setOrgs(properties.getOrganizations());
		ledger.setChaincode(properties.getChaincode().getIdentify());
		ledger.setChaincodeName(properties.getChaincode().getName());
		ledger.setPeers(properties.getPeers());
		return FabricQueryResponse.success(ledger);
	} catch (Exception e) {
		e.printStackTrace();
		return FabricQueryResponse.failure(e.getLocalizedMessage());
	}
}
 
Example #2
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private static void generateTbBlock(Channel channel, BigInteger blockNumber, BlockInfo lastestblockInfo, BlockInfo blockInfo, TbBlock tbBlock) throws InvalidArgumentException, ProposalException, InvalidProtocolBufferException {
    if (blockNumber.longValue() != lastestblockInfo.getBlockNumber()) {
        BlockInfo nextBlockInfo = channel.queryBlockByNumber(blockNumber.longValue() + 1);
        tbBlock.setPkHash(Hex.encodeHexString(nextBlockInfo.getPreviousHash()));
    } else {
        BlockchainInfo blockchainInfo = channel.queryBlockchainInfo();
        tbBlock.setPkHash(Hex.encodeHexString(blockchainInfo.getCurrentBlockHash()));
    }
    if (blockInfo.getEnvelopeCount() > 0) {
        tbBlock.setBlockTimestamp(DataTypeUtils.getTimestamp(blockInfo.getEnvelopeInfo(0).getTimestamp()));
    }
    tbBlock.setTransCount(blockInfo.getEnvelopeCount());
    tbBlock.setBlockNumber(blockNumber);
}
 
Example #3
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private static List<TbBlock> getTbBlocKList(Channel channel, List<Long> blockNums, BlockchainInfo blockchainInfo) throws ExecutionException, InterruptedException {
    List<CompletableFuture<TbBlock>> futureList = new ArrayList<>();
    blockNums.forEach(blockNumber -> {
        CompletableFuture<TbBlock> future = CompletableFuture.supplyAsync(() -> {
            TbBlock tbBlock = new TbBlock();
            try {
                BlockInfo blockInfo = channel.queryBlockByNumber(blockNumber);
                if (blockNumber != (blockchainInfo.getHeight() - 1)) {
                    BlockInfo nextBlockInfo = channel.queryBlockByNumber(blockNumber + 1);
                    tbBlock.setPkHash(Hex.encodeHexString(nextBlockInfo.getPreviousHash()));
                } else {
                    tbBlock.setPkHash(Hex.encodeHexString(blockchainInfo.getCurrentBlockHash()));
                }
                if (blockInfo.getEnvelopeCount() > 0) {
                    tbBlock.setBlockTimestamp(DataTypeUtils.getTimestamp(blockInfo.getEnvelopeInfo(0).getTimestamp()));
                }
                tbBlock.setBlockNumber(new BigInteger(String.valueOf(blockNumber)));
                tbBlock.setTransCount(blockInfo.getTransactionCount());
            } catch (InvalidArgumentException | ProposalException | InvalidProtocolBufferException e) {
                log.error("query block by blockNumber failed, e:", e);
                return null;
            }

            return tbBlock;
        });

        futureList.add(future);
    });

    return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]))
            .thenApply(v -> futureList.stream().map(CompletableFuture::join).collect(Collectors.toList())).get();
}
 
Example #4
Source File: ChaincodeServiceImpl.java    From balance-transfer-java with Apache License 2.0 5 votes vote down vote up
/**
 * gives blockchain info
 * 
 */
public void blockchainInfo(Org sampleOrg, Channel channel) {

	try {
		checkConfig();

		String channelName = channel.getName();
		Set<Peer> peerSet = sampleOrg.getPeers();
		// Peer queryPeer = peerSet.iterator().next();
		// out("Using peer %s for channel queries", queryPeer.getName());

		BlockchainInfo channelInfo = channel.queryBlockchainInfo();
		logger.info("Channel info for : " + channelName);
		logger.info("Channel height: " + channelInfo.getHeight());

		String chainCurrentHash = Hex.encodeHexString(channelInfo.getCurrentBlockHash());
		String chainPreviousHash = Hex.encodeHexString(channelInfo.getPreviousBlockHash());
		logger.info("Chain current block hash: " + chainCurrentHash);
		logger.info("Chainl previous block hash: " + chainPreviousHash);

		// Query by block number. Should return latest block, i.e. block
		// number 2
		BlockInfo returnedBlock = channel.queryBlockByNumber(channelInfo.getHeight() - 1);
		String previousHash = Hex.encodeHexString(returnedBlock.getPreviousHash());
		logger.info("queryBlockByNumber returned correct block with blockNumber " + returnedBlock.getBlockNumber()
		+ " \n previous_hash " + previousHash);

		// Query by block hash. Using latest block's previous hash so should
		// return block number 1
		byte[] hashQuery = returnedBlock.getPreviousHash();
		returnedBlock = channel.queryBlockByHash(hashQuery);
		logger.info("queryBlockByHash returned block with blockNumber " + returnedBlock.getBlockNumber());

	} catch (Exception e) {
		logger.error("ChaincodeServiceImpl | blockchainInfo | "+ e.getMessage());
	}
}
 
Example #5
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public static ListPage<TbBlock> queryBlockList(FabricConfig fabricConfig,
                                               Channel channel,
                                               BigInteger blockNumber,
                                               String blockHash,
                                               Integer pageIndex,
                                               Integer pageSize) throws ProposalException, InvalidArgumentException, ExecutionException, InterruptedException, DecoderException, InvalidProtocolBufferException {
    ListPage<TbBlock> tbBlockListPage = new ListPage<>();
    List<TbBlock> tbBlocks = new CopyOnWriteArrayList<>();
    int blockTotalCount;

    BlockInfo lastestblockInfo = getBlockInfo(fabricConfig, channel, null);
    BlockInfo blockInfo;

    TbBlock tbBlock = new TbBlock();
    if (!StringUtils.isBlank(blockHash)) {
        blockInfo = channel.queryBlockByHash(Hex.decodeHex(blockHash.toCharArray()));
        generateTbBlock(channel, BigInteger.valueOf(blockInfo.getBlockNumber()), lastestblockInfo, blockInfo, tbBlock);
        tbBlocks.add(tbBlock);
        blockTotalCount = 1;
    } else if (blockNumber != null) {
        blockInfo = getBlockInfo(fabricConfig, channel, blockNumber);
        generateTbBlock(channel, blockNumber, lastestblockInfo, blockInfo, tbBlock);
        tbBlocks.add(tbBlock);
        blockTotalCount = 1;
    } else {
        BlockchainInfo blockchainInfo = channel.queryBlockchainInfo();
        blockInfo = getBlockInfo(fabricConfig, channel, null);
        long lastestblcokNum = blockInfo.getBlockNumber();

        int blockSize = ((int) lastestblcokNum <= pageIndex * pageSize) ? ((int) lastestblcokNum - ((pageIndex - 1) * pageSize)) : pageSize;
        long blockNumberIndex = (long) pageSize * (pageIndex - 1) + 1;

        List<Long> blockNums = new ArrayList<>();
        for (int i = 0; i < blockSize; i++) {
            blockNums.add(blockNumberIndex);
            blockNumberIndex++;
        }

        tbBlocks = getTbBlocKList(channel, blockNums, blockchainInfo);
        blockTotalCount = Integer.parseInt(String.valueOf(lastestblockInfo.getBlockNumber()));
        tbBlocks.sort((arg0, arg1) -> arg1.getBlockNumber().compareTo(arg0.getBlockNumber()));
    }

    tbBlockListPage.setPageSize(pageSize);
    tbBlockListPage.setPageIndex(pageIndex);
    tbBlockListPage.setTotal(blockTotalCount);
    tbBlockListPage.setPageData(tbBlocks);
    return tbBlockListPage;
}