zoukankan      html  css  js  c++  java
  • hyperledge工具-configtxlator

    参考:http://www.blockchainbrother.com/article/1337

    1.作用:

    因为无论配置交易文件 .tx和初始区块文件 .block都是二进制格式,用户无法直接编辑。

    configtxlator工具将这些配置文件在二进制格式和方便阅读的json格式之间进行转换。

    开发 configtxlator 工具是为了支持独立于SDK来进行重新配置。configtxlator 工具被设计为提供一个API让任意一个SDK的用户都能够与之交互来更新配置。

    工具的名称是 configtx 和 translator 的拼接,意在传达该工具简单地在不同的等效数据之间进行转换。 它不产生配置。也不提交或撤回配置。它不修改配置本身,只是简单地提供一些配置格式的不同的双射展现。

    2.配置该工具的方法有:

    该工具的源码在github.com/hyperledger/fabric/common/tools/configtxlator

    1)在github.com/hyperledger/fabric目录下运行:

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ make configtxlator
    .build/bin/configtxlator
    CGO_CFLAGS=" " GOBIN=/opt/gopath/src/github.com/hyperledger/fabric/.build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/tools/configtxlator/metadata.CommitSHA=325999f" github.com/hyperledger/fabric/common/tools/configtxlator
    Binary available as .build/bin/configtxlator

    然后会生成./build/bin/configtxlator可执行文件

    2)在github.com/hyperledger/fabric/common/tools/configtxlator目录下运行go build

    3.标准用法:

    • 首先使用SDK取出最新的配置
    • 然后使用configtxlator 工具产生将二进制文件转成可读版本的配置文件
    • 这样用户或者应用就可以编辑配置文件
    • 接着使用 configtxlator 工具计算更新的配置与原有配置的差异
    • 最后使用SDK提交配置以及签名

    configtxlator 工具暴露一个完全无状态的 REST API 接口用来和配置匀速进行交互。 这些 REST 组件支持本地的配置和可读的JSON格式配置文件进行相互转换, 同时根据配置文件的差异计算配置的更新。

    因为 configtxlator 工具特意没有包含任何密码工具和密钥信息, 所有它没有任何权限控制。 预计的典型部署方式是运行在沙盒容器中, 所以在本地的应用中, 有一个专用的 configtxlator 进程给每一个使用者。

     

    4.如何使用该工具

    1)查看该工具能够使用的命令:

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator --help
    usage: configtxlator [<flags>] <command> [<args> ...]
    
    用于生成Hyperledger Fabric通道配置的实用程序
    
    Flags:
      --help  显示上下文敏感的帮助信息 (also try --help-long and --help-man).
    
    Commands:
      help [<command>...]
        显示帮助信息.
    
      start [<flags>]
        开启configtxlator REST服务端
    
      proto_encode --type=TYPE [<flags>]
        转换JSON文件成指定的protobuf格式
    
      proto_decode --type=TYPE [<flags>]
        转换proto信息为JSON格式
    
      compute_update --channel_id=CHANNEL_ID [<flags>]
        带两个编码的common.Config消息,并计算在两者之间转换的配置更新。
      version
        显示版本信息

    1》start — 开启服务端

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator help start
    usage: configtxlator start [<flags>]
      开启configtxlator REST服务端
    
    Flags:
      --help                显示上下文敏感的帮助信息(also try --help-long and
                            --help-man).
      --hostname="0.0.0.0"  REST服务端坚挺的主机名或IP地址,默认为0.0.0.0
      --port=7059           REST服务端坚挺的端口,默认为7059
      --CORS=CORS ...       容许的CORS(跨来源资源共享)域名, 如'*' or 'www.example.com'(may be repeated).

    工具启动一个服务器监听指定的端口且等待处理请求,如在本地8080端口启动rest服务

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator start --hostname="127.0.0.1" --port=8080
    2019-03-10 01:37:26.715 UTC [configtxlator] startServer -> INFO 001 Serving HTTP requests on 127.0.0.1:8080

    这是开启了一个REST服务端,但是其实我们也可以不用启动这个服务端,直接使用的命令行进行操作

    2》proto解码

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator help proto_decode
    usage: configtxlator proto_decode --type=TYPE [<flags>]
    
    将proto信息转换成JSON格式
    
    Flags:
      --help                显示上下文敏感的帮助信息 (also try --help-long and
                            --help-man).
      --type=TYPE           要解码的protobuf结构的类型。比如'common.Config'
      --input=/dev/stdin    指定包含proto信息的文件,,默认为/dev/stdin,即控制台输入
      --output=/dev/stdout  指定要将解码所得的JSON信息写入的文件默认为/dev/stdout,即控制台输出

    configtxlator命令用来将fabric的数据结构在protobuf和JSON 之间进行转换,也可以用来创建配置更新。

    • 该命令可以启动一个REST 服务来通过HTTP暴露服务接口,
    • 也可以直接在命令行使用

    1> 如果使用的是启动一个REST 服务来通过HTTP暴露服务接口的方法,那么需要运行configtxlator start命令开启服务,如上面进行的操作

    要转换原型到可读的 JSON 格式,只要发送二进制原型到 rest 目标http://$SERVER:$PORT/protolator/decode/<message.Name>,<message.Name> 是合法原型名的全称,合法原型名如下:

    • common.Block:区块结构;
    • common.Envelope:带有效载荷和数字签名的数字信封,区块的数据部分就是序列化后的数字信封;
    • common.ConfigEnvelope:包含链配置的数字信封,内容包含ConfigUpdateEnvelope;
    • common.ConfigUpdateEnvelope:提交给排序节点的配置数字信封;
    • common.Config:ConfigEnvelope的配置部分;
    • common.ConfigUpdate:ConfigUpdateEnvelope的一部分。

    然后进行解码。例如,为了解析一个存储为 configuration_block.pb 的配置区块,执行命令:

    curl -X POST --data-binary @configuration_block.pb http://127.0.0.1:8080/protolator/decode/common.Block

     2>另一种方式就是不启动服务端,直接使用命令进行操作,上面的例子可以写成:

    configtxlator proto_decode --input configuration_block.pb --type common.Block

    3》proto编码

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator help proto_encode
    usage: configtxlator proto_encode --type=TYPE [<flags>]
    
    将JSON文件转移成protobuf.
    
    Flags:
      --help                显示上下文敏感的帮助信息(also try --help-long and
                            --help-man).
      --type=TYPE           指定要编码成的protobuf结构的类型,比如'common.Config'.
      --input=/dev/stdin    指定要转换的JSON文件,默认为/dev/stdin,即控制台输入
      --output=/dev/stdout  指定将输出写如的文件,默认为/dev/stdout,即控制台输出

    1>通过HTTP暴露服务接口编码,举例将控制台输入的json格式的policy,转换为protobuf格式并存入文件policy.pb:

    curl -X POST --data-binary /dev/stdin http://127.0.0.1:8080/protolator/encode/common.Policy > policy.pb

    2>命令行格式为:

    configtxlator proto_encode --type common.Policy --output policy.pb

    4》compute_update

    vagrant@ubuntu-xenial:/opt/gopath/src/github.com/hyperledger/fabric$ configtxlator help compute_update
    usage: configtxlator compute_update --channel_id=CHANNEL_ID [<flags>]
    
      计算生成两个common.Config消息之间的配置更新
    
    Flags:
      --help                   显示上下文敏感的帮助信息(also try --help-long and
                               --help-man).
      --original=ORIGINAL      原始的配置信息
      --updated=UPDATED        更新后的配置信息下
      --channel_id=CHANNEL_ID  这次更新的通道的名字T
      --output=/dev/stdout     指明将JSON文件写入的文件,默认为/dev/stdout,即控制台输出

    1>通过HTTP暴露服务接口编码,举例计算从original_config.pb到modified_config.pb的配置更新,并在stdout输出JSON解码后的数据:

    configtxlator compute_update --channel_id testchan --original original_config.pb --updated modified_config.pb | configtxlator proto_decode --type common.ConfigUpdate

    2>命令行格式为:

    curl -X POST -F channel=testchan -F "original=@original_config.pb" -F "updated=@modified_config.pb" "http://127.0.0.1:8080/configtxlator/compute/update-from-configs" | curl -X POST --data-binary /dev/stdin "http://127.0.0.1:8080/protolator/encode/common.ConfigUpdate"
  • 相关阅读:
    【linux之web服务器apache】
    【linux工具之sed实例】
    【linux工具之strace】
    【linux工具之iptables 脚本】
    【linux工具之iptables】
    【Linux抓包工具之tcpdump】
    【抓包工具之wireshark】
    【Linux 基础服务之DNS】
    c语言
    sf
  • 原文地址:https://www.cnblogs.com/wanghui-garcia/p/10497415.html
Copyright © 2011-2022 走看看