10.6 configtxlator转换配置

通过之前小节的讲解可以知道,configtxgen工具可以用来生成通道相关的配置交易和系统通道初始区块等并进行简 单的查看。但是如果想对这些配置进行修改,就比较困难了,因为无论配置交易文件还是初始区块文件都是二进制格式(严格来说,是Protobuf消息数据结 构导出到本地文件),无法直接进行编辑。

configtxlator工具可以将这些配置文件在二进制格式和方便阅读编辑的Json格式之间进行转换,方便用户更新通道的配置。

configtxlator工具自身是个比较简单的RESTful服务程序,启动后默认监听在7059端口。支持通过--hostname=<addr>来指定服务监听地址,通过--port int来指定服务端口。

例如采用如下命令启动configtxlator服务,并且监听在7059端口:


$ configtxlator start --hostname="0.0.0.0" --port 7059
[configtxlator] main -> INFO 001 Serving HTTP requests on 0.0.0.0:7059

10.6.1 RESTful接口

目前支持三个功能接口,分别进行解码、编码或者计算配置更新量:

·解码:接口地址为/protolator/decode/{msgName},支持POST操作,将二进制格式数据解码为Json格式,其中{msgName}需要指定Fabric中定义的对应Protobuf消息结构的名称;

·编码:接口地址为/protolator/encode/{msgName},支持POST操作,将Json格式的数据编码为二进制格式,其中{msgName}需要指定Protobuf消息类型名称;

·计算配置更新量:接口地址为/configtxlator/compute/update-from-configs,支持POST操作,计算两个配置(common.Config消息结构)之间的二进制格式更新量。

10.6.2 解码为Json格式

支持将二进制格式的文件转换为Json格式,方便用户使用。

例如,下面命令将本地使用configtxgen生成的Orderer系统通道初始区块,转换为Json格式。注意区块为common.Block消息结构:


$ curl -X POST \
   --data-binary @orderer_genesis.block \
   http://127.0.0.1:7059/protolator/decode/common.Block \
   > ./orderer_genesis.json
 % Total  % Received % Xferd  Average Speed   Time    Time     Time  Current
                              Dload  Upload   Total   Spent    Left  Speed
100 25862  0 19117  100  6745  1075k   379k --:--:-- --:--:-- --:--:-- 1098k

此时,用户可以很方便的查看其内容,大致结构如图10-8所示。

10.6 configtxlator转换配置

图10-8 Orderer初始区块转换为Json结构

注意JSON.data.data[0].payload.data.config域内数据代表了完整的通道配置信息,为common.Config结构,用户可以对这些配置内容进行修改。修改后保存为orderer_genesis_new.json文件。

读者也可以尝试对新建通道配置交易文件进行转换和查看,注意消息类型指定为对应的common.Envelope:


$ curl -X POST \
   --data-binary @channel.tx \
   http://127.0.0.1:7059/protolator/decode/common.Envelope \
   > channel.json

生成配置交易的Json结构如图10-9所示。


10.6 configtxlator转换配置

图10-9 新建通道配置交易转换为Json结构

10.6.3 编码为二进制格式

反过来,利用修改后的orderer_genesis_new.json文件,用户也可以将其转换为二进制格式的初始区块文件:


$ curl -X POST \
   --data-binary @./orderer_genesis.json \
   http://127.0.0.1:7059/protolator/encode/common.Block \
   > orderer_genesis_new.block
% Total  % Received % Xferd  Average Speed   Time    Time     Time  Current
                            Dload  Upload   Total   Spent    Left  Speed
100 25862  0  6745  100 19117  630k  1786k --:--:-- --:--:-- --:--:-- 1866k

生成的orderer_genesis_new.block文件即为更新配置后的二进制格式的初始区块文件。

10.6.4 计算配置更新量

对于给定的两个配置(common.Config结构),configtxlater还可以比对它们的不同,计算出更新到新配置时的更新量(common.ConfigUpdate结构)。

首先,通过如下命令获取已创建的应用通道businesschannel的配置区块:


$ peer channel fetch config businesschannel.block \
   -c businesschannel \
   -o orderer:7050

按照之前所述命令,将其转化为Json格式:


$ curl -X POST --data-binary @businesschannel.block http://127.0.0.1:7059/protolator/
   decode/common.Block > ./businesschannel.json

提取出.data.data[0].payload.data.config域,保存为businesschannel_config文件,该文件中只包括了common.Config结构相关数据:


$ jq .data.data[0].payload.data.config businesschannel.json > businesschannel_
   config.json

对businesschannel_config.json文件中的配置项(如区块最大交易消息数 Orderer.values.Batch-Size.value.max_message_count或调整通道中组织等)进行修改,另存为 businesschannel_config_new.json。利用两个配置文件编码成为二进制格式:


$ curl -X POST \
   --data-binary @businesschannel_config.json \
   http://127.0.0.1:7059/protolator/encode/common.Config \
   > businesschannel_config.config

$ curl -X POST \
   --data-binary @businesschannel_config_new.json \
   http://127.0.0.1:7059/protolator/encode/common.Config \
   > businesschannel_config_new.config

利用这两个配置文件,通过configtxlator提供的接口,计算出更新配置时的更新量信息,为common.ConfigUpdate结构的二进制文件。并根据二进制文件生成更新量的Json格式:


$ curl -X POST \
   -F [email protected]_config.config \
   -F [email protected]_config_new.config \
   http://127.0.0.1:7059/configtxlator/compute/update-from-configs \
   -F channel=businesschannel \
   > config_update.config
$ curl -X POST \
   --data-binary @config_update.config \
   http://127.0.0.1:7059/protolator/decode/common.ConfigUpdate \
   > config_update.json

10.6.5 更新通道配置

通过计算更新量,可以得到common.ConfigUpdate结构的更新信息。而对通道配置进行更新时,还需要封装 为common.Envelope结构的配置更新交易。因此需要将common.ConfigUpdate结构数据进行补全,补全后的文件命名为 config_update_envelope.json。

可以通过如下命令进行补全:


echo '{"payload":{"header":{"channel_header":{"channel_id":"businesschannel", "type":2}},
   "data":{"config_update":'$(cat config_update.json)'}}}' > config_update_
   envelope.json

补全后的common.Envelope结构的config_update_envelope.json文件内容类似如下结构:


{
   "payload":{
       "header":{
           "channel_header":{
           "channel_id":"businesschannel",
           "type":2
           }
       },
       "data":{
           "config_update": {
               ...
           }
       }
   }
}

利用该common.Envelope结构编码为二进制交易配置文件,并用它对应用通道进行更新。需要注意,更新配置需要指定相应的权限(OderMSP的Admin身份):


$ curl -X POST \
   --data-binary @config_update_envelope.json \
   http://127.0.0.1:7059/protolator/encode/common.Envelope \
   > config_update_envelope.tx
$ CORE_PEER_LOCALMSPID=OrderMSP
$ CORE_PEER_MSPCONFIGPATH=crypto-config/ordererOrganizations/example.com/users/
   [email protected]/msp
$ peer channel update \
   -o 127.0.0.1:7050 \
   -f config_update_envelope.tx \
   -c businesschannel

本章首先剖析了Peer节点和Orderer节点的核心配置文件,并对其中的关键配置进行了详细讲解和说明。

为了启动一个相对复杂的Fabric网络,往往需要依赖多个启动配置文件,包括初始区块、配置交易文件、身份证书等。这 些文件可以使用cryptogen工具和configtxgen工具进行生成,并使用configtxlator工具进行解析和操作。本章详细讲解了围绕 这三个工具的配置说明和使用操作。通过本章内容的学习,相信读者对于Fabric网络的配置有了更深入的理解,可以掌握Fabric网络配置和管理的相关 工具和技能。


来源:我是码农,转载请保留出处和链接!

本文链接:http://www.54manong.com/?id=917

'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646208", container: s }); })();
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646147", container: s }); })();