使用Hyperledger Composer时,如何获取和查看添加到区块链的块?
这可以使用composer-client完成。虽然没有可用于此目的的便捷方法,但可以调用底层的Hyperledger Fabric SDK API。
下面是一些示例代码,用于按编号获取块并将其打印到控制台:
const { inspect } = require('util');
const { BusinessNetworkConnection } = require('composer-client');
async function run() {
const connection = new BusinessNetworkConnection();
// Connect to the blockchain using admin credentials.
// These credentials should be available in your local keystore.
await connection.connect('admin@network');
// Native API provided through the Fabric SDK, allows much more low-level operations than Composer.
const nativeApi = connection.getNativeAPI();
// Connect to the channel where the transactions are happening, the default is "composerchannel".
const channel = nativeApi.getChannel('composerchannel');
// Grab a block by it's number
const block = await channel.queryBlock(4);
// Enter the matrix
console.log(inspect(block, { depth: null, colors: true, compact: false }));
await connection.disconnect();
}
run();
有关通过此API公开的功能的更多信息,请参阅fabric-sdk-node documentation。
Hyperledger Explorer可以做到这一点,但它不是Hyperledger Composer的一部分。这是一个原生的Fabric工具。