Hyperledger fabric 使用 configtxlator 工具解析区块

解析区块可以分为2步进行:

  • 把区块从账本里取出来,区块格式为 protobuf
  • protobuf 格式的区块转换为 JSON 格式

一、获取区块

1.进入 peer0.org1.example.com 的容器中

[jack@localhost blockchain_Server]$ docker exec -it peer0.org1.example.com bash

 结果

root@a01d1f70bc8c:/opt/gopath/src/github.com/hyperledger/fabric/peer#

注意:以上的 a01d1f70bc8c 是 peer0.org1.example.com 这个容器的 id,后面拷贝区块文件的时候会用到

2.peer channel fetch 命令

peer channel fetch 命令能够拉取指定通道最新、最老以及特定高度的区块,不过得到的区块是protobuf格式的,不转换无法知道内容,具体的命令参数说明如下:

root@a01d1f70bc8c:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel fetch -h
Fetch a specified block, writing it to a file.

Usage:
  peer channel fetch <newest|oldest|config|(number)> [outputfile] [flags]

Flags:
      --bestEffort         Whether fetch requests should ignore errors and return blocks on a best effort basis
  -c, --channelID string   In case of a newChain command, the channel ID to create. It must be all lower case, less than 250 characters long and match the regular expression: [a-z][a-z0-9.-]*
  -h, --help               help for fetch

Global Flags:
      --cafile string                       Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint
      --certfile string                     Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint
      --clientauth                          Use mutual TLS when communicating with the orderer endpoint
      --connTimeout duration                Timeout for client to connect (default 3s)
      --keyfile string                      Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint
  -o, --orderer string                      Ordering service endpoint
      --ordererTLSHostnameOverride string   The hostname override to use when validating the TLS connection to the orderer.
      --tls                                 Use TLS when communicating with the orderer endpoint

3.拉取区块

root@a01d1f70bc8c:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel fetch 2 mychannel.block  -c mychannel
2019-11-29 07:24:35.154 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-29 07:24:35.156 UTC [cli.common] readBlock -> INFO 002 Received block: 2

4.ls一下

root@a01d1f70bc8c:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
mychannel.block

二、将区块文件拷贝到本机

命令构成:docker cp <容器id>:<容器中区块文件存放的路径> <本机路径>

[jack@localhost ~]$ docker cp a01d1f70bc8c:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block /home/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin

注意:<本机路径> 与 configtxlator 工具所在的路径最好一致,这样比较方便,我的fabric版本是1.4.3,configtxlator 工具路径如下

/home/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin

三、使用 configtxlator 工具解析区块

1.进入 configtxlator 工具所在路径

[jack@localhost ~]$ cd workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin
[jack@localhost bin]$

2.使用 configtxlator 工具把区块从 protobuf 格式解析成 JSON 格式

[jack@localhost bin]$ configtxlator proto_decode  --type common.Block --input mychannel.block > mychannel.block.json

3.成功后cat 一下

[jack@localhost bin]$ cat mychannel.block.json

 以下是部分截图

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值