解析區塊可以分爲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
以下是部分截圖