简体   繁体   中英

How to query a value from a peer using Hyperledger Fabric SDK java

I have setup a HyperLedger Fabric V1.0 network with 2 organisations each having 2 peer and an orderer by following the steps Building Your First Network.

I have started the network using " sh byfn.sh -m up " and compiled and executed chaincode that returns value for querying 'a' as 90.

I want to write a code using Java Fabric SDK to only query a peer for a value using the docker network container started .

Could you pls share the code segment for the same or give me references Thanks

// Channel queries

// We can only send channel queries to peers that are in the same org as the SDK user context
// Get the peers from the current org being used and pick one randomly to send the queries to.
Set<Peer> peerSet = sampleOrg.getPeers();
//  Peer queryPeer = peerSet.iterator().next();
//   out("Using peer %s for channel queries", queryPeer.getName());

BlockchainInfo channelInfo = channel.queryBlockchainInfo();
out("Channel info for : " + channelName);
out("Channel height: " + channelInfo.getHeight());
String chainCurrentHash = Hex.encodeHexString(channelInfo.getCurrentBlockHash());
String chainPreviousHash = Hex.encodeHexString(channelInfo.getPreviousBlockHash());
out("Chain current block hash: " + chainCurrentHash);
out("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());
out("queryBlockByNumber returned correct block with blockNumber " + returnedBlock.getBlockNumber()
        + " \n previous_hash " + previousHash);
assertEquals(channelInfo.getHeight() - 1, returnedBlock.getBlockNumber());
assertEquals(chainPreviousHash, 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);
out("queryBlockByHash returned block with blockNumber " + returnedBlock.getBlockNumber());
assertEquals(channelInfo.getHeight() - 2, returnedBlock.getBlockNumber());

// Query block by TxID. Since it's the last TxID, should be block 2
returnedBlock = channel.queryBlockByTransactionID(testTxID);
out("queryBlockByTxID returned block with blockNumber " + returnedBlock.getBlockNumber());
assertEquals(channelInfo.getHeight() - 1, returnedBlock.getBlockNumber());

// query transaction by ID
TransactionInfo txInfo = channel.queryTransactionByID(testTxID);
out("QueryTransactionByID returned TransactionInfo: txID " + txInfo.getTransactionID()
        + "\n     validation code " + txInfo.getValidationCode().getNumber());

This is end to end test sample using Java SDK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM