解析区块可以分为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
以下是部分截图