如何使用 Hyperledger Fabric SDK 从节点查询值 java
How to query a value from a peer using Hyperledger Fabric SDK java
我已经按照构建您的第一个网络的步骤设置了一个 HyperLedger Fabric V1.0 网络,其中有 2 个组织,每个组织都有 2 个对等节点和一个排序节点。
我已经使用“sh byfn.sh -m up”启动了网络并编译并执行了 returns 查询值 'a' 作为 90.
我想使用 Java Fabric SDK 编写代码,仅 查询 使用 docker 网络容器启动的值的对等点。
能否请您分享相同的代码段或给我参考
谢谢
// 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());
我已经按照构建您的第一个网络的步骤设置了一个 HyperLedger Fabric V1.0 网络,其中有 2 个组织,每个组织都有 2 个对等节点和一个排序节点。
我已经使用“sh byfn.sh -m up”启动了网络并编译并执行了 returns 查询值 'a' 作为 90.
我想使用 Java Fabric SDK 编写代码,仅 查询 使用 docker 网络容器启动的值的对等点。
能否请您分享相同的代码段或给我参考 谢谢
// 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());