Java Code Examples for org.hyperledger.fabric.sdk.BlockInfo#getBlockNumber()

The following examples show how to use org.hyperledger.fabric.sdk.BlockInfo#getBlockNumber() . 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: 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 2
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;
}
 
Example 3
Source File: ChaincodeManager.java    From fabric-net-server with Apache License 2.0 4 votes vote down vote up
private void execBlockInfo(BlockInfo blockInfo) throws InvalidArgumentException, IOException {
	final long blockNumber = blockInfo.getBlockNumber();
	log.debug("blockNumber = " + blockNumber);
	log.debug("data hash: " + Hex.encodeHexString(blockInfo.getDataHash()));
	log.debug("previous hash id: " + Hex.encodeHexString(blockInfo.getPreviousHash()));
	log.debug("calculated block hash is " + Hex.encodeHexString(SDKUtils.calculateBlockHash(blockNumber, blockInfo.getPreviousHash(), blockInfo.getDataHash())));
	
	final int envelopeCount = blockInfo.getEnvelopeCount();
	log.debug("block number " + blockNumber + " has " + envelopeCount + " envelope count:");
	
	for(EnvelopeInfo info: blockInfo.getEnvelopeInfos()) {
		final String channelId = info.getChannelId();
		log.debug("ChannelId = " + channelId);
		log.debug("Epoch = " + info.getEpoch());
		log.debug("TransactionID = " + info.getTransactionID());
		log.debug("ValidationCode = " + info.getValidationCode());
		log.debug("Timestamp = " + DateUtil.obtain().parseDateFormat(new Date(info.getTimestamp().getTime()), "yyyy年MM月dd日 HH时mm分ss秒"));
		log.debug("Type = " + info.getType());
		
		if (info.getType() == EnvelopeType.TRANSACTION_ENVELOPE) {
			BlockInfo.TransactionEnvelopeInfo txeInfo = (TransactionEnvelopeInfo) info;
			int txCount = txeInfo.getTransactionActionInfoCount();
			log.debug("Transaction number " + blockNumber + " has actions count = " + txCount);
			log.debug("Transaction number " + blockNumber + " isValid = " + txeInfo.isValid());
			log.debug("Transaction number " + blockNumber + " validation code = " + txeInfo.getValidationCode());
			
			for (int i = 0; i < txCount; i++) {
				BlockInfo.TransactionEnvelopeInfo.TransactionActionInfo txInfo = txeInfo.getTransactionActionInfo(i);
				log.debug("Transaction action " + i + " has response status " + txInfo.getResponseStatus());
                   log.debug("Transaction action " + i + " has response message bytes as string: " + printableString(new String(txInfo.getResponseMessageBytes(), "UTF-8")));
				log.debug("Transaction action " + i + " has endorsements " + txInfo.getEndorsementsCount());
				
				for (int n = 0; n < txInfo.getEndorsementsCount(); ++n) {
                       BlockInfo.EndorserInfo endorserInfo = txInfo.getEndorsementInfo(n);
                       log.debug("Endorser " + n + " signature: " + Hex.encodeHexString(endorserInfo.getSignature()));
                       log.debug("Endorser " + n + " endorser: " + new String(endorserInfo.getEndorser(), "UTF-8"));
                   }
				
                   log.debug("Transaction action " + i + " has " + txInfo.getChaincodeInputArgsCount() + " chaincode input arguments");
                   for (int z = 0; z < txInfo.getChaincodeInputArgsCount(); ++z) {
                       log.debug("Transaction action " + i + " has chaincode input argument " + z + "is: " + printableString(new String(txInfo.getChaincodeInputArgs(z), "UTF-8")));
                   }

                   log.debug("Transaction action " + i + " proposal response status: " + txInfo.getProposalResponseStatus());
                   log.debug("Transaction action " + i + " proposal response payload: " + printableString(new String(txInfo.getProposalResponsePayload())));

                   TxReadWriteSetInfo rwsetInfo = txInfo.getTxReadWriteSet();
                   if (null != rwsetInfo) {
                       log.debug("Transaction action " + i + " has " + rwsetInfo.getNsRwsetCount() +" name space read write sets");

                       for (TxReadWriteSetInfo.NsRwsetInfo nsRwsetInfo : rwsetInfo.getNsRwsetInfos()) {
                       	final String namespace = nsRwsetInfo.getNamespace();
                           KvRwset.KVRWSet rws = nsRwsetInfo.getRwset();

                           int rs = -1;
                           for (KvRwset.KVRead readList : rws.getReadsList()) {
                               rs++;

                               log.debug("Namespace " + namespace + " read set " + rs + " key " + readList.getKey() + " version [" + readList.getVersion().getBlockNum() + " : " + readList.getVersion().getTxNum() + "]");
                           }

                           rs = -1;
                           for (KvRwset.KVWrite writeList : rws.getWritesList()) {
                               rs++;
                               String valAsString = printableString(new String(writeList.getValue().toByteArray(), "UTF-8"));
                               log.debug("Namespace " + namespace + " write set " + rs + " key " + writeList.getKey() + " has value " + valAsString);
                           }
                       }
                   }
			}
		}
	}
}