使用 Hyperledger Composer 查看区块链块

Viewing blockchain blocks using Hyperledger Composer

如何在使用 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 工具。