Hyperledger Fabric:使用 JsonPath 将新组织添加到现有通道

Posted

技术标签:

【中文标题】Hyperledger Fabric:使用 JsonPath 将新组织添加到现有通道【英文标题】:Hyperledger Fabric: Add new organization to existing channel using JsonPath 【发布时间】:2020-12-23 14:36:25 【问题描述】:

我想向 Hyperledger Fabric 中的现有通道添加一个新组织。

首先我使用以下代码sn-p获取JSON格式的通道配置:

byte[] configBytes = channel.getChannelConfigurationBytes();
String configtxlator = props.getProperty("configtxlator");
HttpPost httppost = new HttpPost(configtxlator + "/protolator/decode/common.Config");
httppost.setEntity(new ByteArrayEntity(configBytes));

HttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httppost);
int statuscode = response.getStatusLine().getStatusCode();
if (statuscode == 200) 
    return EntityUtils.toString(response.getEntity());
 else 
    return "";

结果是以下 JSON 字符串:


"channel_group": 
    "groups": 
        "Application": 
            "groups": 
                "Org1MSP": 
                    "groups": ,
                    "mod_policy": "Admins",
                    "policies": 
                        "Admins": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "ADMIN"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        ,
                        "Readers": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "ADMIN"
                                            ,
                                            "principal_classification": "ROLE"
                                        ,
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "PEER"
                                            ,
                                            "principal_classification": "ROLE"
                                        ,
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "CLIENT"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                ,
                                                
                                                    "signed_by": 1
                                                ,
                                                
                                                    "signed_by": 2
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        ,
                        "Writers": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "ADMIN"
                                            ,
                                            "principal_classification": "ROLE"
                                        ,
                                        
                                            "principal": 
                                                "msp_identifier": "Org1MSP",
                                                "role": "CLIENT"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                ,
                                                
                                                    "signed_by": 1
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        
                    ,
                    "values": 
                        "AnchorPeers": 
                            "mod_policy": "Admins",
                            "value": 
                                "anchor_peers": [
                                    
                                        "host": "peer1.org1.isprint.com",
                                        "port": 7051
                                    ,
                                    
                                        "host": "peer2.org1.isprint.com",
                                        "port": 7051
                                    
                                ]
                            ,
                            "version": "0"
                        ,
                        "MSP": 
                            "mod_policy": "Admins",
                            "value": 
                                "config": 
                                    "admins": [],
                                    "crypto_config": 
                                        "identity_identifier_hash_function": "SHA256",
                                        "signature_hash_family": "SHA2"
                                    ,
                                    "fabric_node_ous": 
                                        "admin_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "admin"
                                        ,
                                        "client_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "client"
                                        ,
                                        "enable": true,
                                        "orderer_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "orderer"
                                        ,
                                        "peer_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "peer"
                                        
                                    ,
                                    "intermediate_certs": [],
                                    "name": "Org1MSP",
                                    "organizational_unit_identifiers": [],
                                    "revocation_list": [],
                                    "root_certs": [
                                        "..."
                                    ],
                                    "signing_identity": null,
                                    "tls_intermediate_certs": [],
                                    "tls_root_certs": [
                                        "..."
                                    ]
                                ,
                                "type": 0
                            ,
                            "version": "0"
                        
                    ,
                    "version": "1"
                
            ,
            "mod_policy": "Admins",
            "policies": 
                "Admins": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "MAJORITY",
                            "sub_policy": "Admins"
                        
                    ,
                    "version": "0"
                ,
                "Readers": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "ANY",
                            "sub_policy": "Readers"
                        
                    ,
                    "version": "0"
                ,
                "Writers": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "ANY",
                            "sub_policy": "Writers"
                        
                    ,
                    "version": "0"
                
            ,
            "values": 
                "Capabilities": 
                    "mod_policy": "Admins",
                    "value": 
                        "capabilities": 
                            "V1_4_2": 
                        
                    ,
                    "version": "0"
                
            ,
            "version": "1"
        ,
        "Orderer": 
            "groups": 
                "OrdererOrg": 
                    "groups": ,
                    "mod_policy": "Admins",
                    "policies": 
                        "Admins": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "OrdererMSP",
                                                "role": "ADMIN"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        ,
                        "Readers": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "OrdererMSP",
                                                "role": "MEMBER"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        ,
                        "Writers": 
                            "mod_policy": "Admins",
                            "policy": 
                                "type": 1,
                                "value": 
                                    "identities": [
                                        
                                            "principal": 
                                                "msp_identifier": "OrdererMSP",
                                                "role": "MEMBER"
                                            ,
                                            "principal_classification": "ROLE"
                                        
                                    ],
                                    "rule": 
                                        "n_out_of": 
                                            "n": 1,
                                            "rules": [
                                                
                                                    "signed_by": 0
                                                
                                            ]
                                        
                                    ,
                                    "version": 0
                                
                            ,
                            "version": "0"
                        
                    ,
                    "values": 
                        "MSP": 
                            "mod_policy": "Admins",
                            "value": 
                                "config": 
                                    "admins": [],
                                    "crypto_config": 
                                        "identity_identifier_hash_function": "SHA256",
                                        "signature_hash_family": "SHA2"
                                    ,
                                    "fabric_node_ous": 
                                        "admin_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "admin"
                                        ,
                                        "client_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "client"
                                        ,
                                        "enable": true,
                                        "orderer_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "orderer"
                                        ,
                                        "peer_ou_identifier": 
                                            "certificate": "...",
                                            "organizational_unit_identifier": "peer"
                                        
                                    ,
                                    "intermediate_certs": [],
                                    "name": "OrdererMSP",
                                    "organizational_unit_identifiers": [],
                                    "revocation_list": [],
                                    "root_certs": [
                                        "..."
                                    ],
                                    "signing_identity": null,
                                    "tls_intermediate_certs": [],
                                    "tls_root_certs": [
                                        "..."
                                    ]
                                ,
                                "type": 0
                            ,
                            "version": "0"
                        
                    ,
                    "version": "0"
                
            ,
            "mod_policy": "Admins",
            "policies": 
                "Admins": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "MAJORITY",
                            "sub_policy": "Admins"
                        
                    ,
                    "version": "0"
                ,
                "BlockValidation": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "ANY",
                            "sub_policy": "Writers"
                        
                    ,
                    "version": "0"
                ,
                "Readers": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "ANY",
                            "sub_policy": "Readers"
                        
                    ,
                    "version": "0"
                ,
                "Writers": 
                    "mod_policy": "Admins",
                    "policy": 
                        "type": 3,
                        "value": 
                            "rule": "ANY",
                            "sub_policy": "Writers"
                        
                    ,
                    "version": "0"
                
            ,
            "values": 
                "BatchSize": 
                    "mod_policy": "Admins",
                    "value": 
                        "absolute_max_bytes": 103809024,
                        "max_message_count": 100,
                        "preferred_max_bytes": 524288
                    ,
                    "version": "0"
                ,
                "BatchTimeout": 
                    "mod_policy": "Admins",
                    "value": 
                        "timeout": "30s"
                    ,
                    "version": "0"
                ,
                "Capabilities": 
                    "mod_policy": "Admins",
                    "value": 
                        "capabilities": 
                            "V1_4_2": 
                        
                    ,
                    "version": "0"
                ,
                "ChannelRestrictions": 
                    "mod_policy": "Admins",
                    "value": null,
                    "version": "0"
                ,
                "ConsensusType": 
                    "mod_policy": "Admins",
                    "value": 
                        "metadata": 
                            "consenters": [
                                
                                    "client_tls_cert": "...",
                                    "host": "orderer1.isprint.com",
                                    "port": 7050,
                                    "server_tls_cert": "..."
                                ,
                                
                                    "client_tls_cert": "...",
                                    "host": "orderer2.isprint.com",
                                    "port": 7050,
                                    "server_tls_cert": "..."
                                ,
                                
                                    "client_tls_cert": "...",
                                    "host": "orderer3.isprint.com",
                                    "port": 7050,
                                    "server_tls_cert": "..."
                                
                            ],
                            "options": 
                                "election_tick": 10,
                                "heartbeat_tick": 1,
                                "max_inflight_blocks": 5,
                                "snapshot_interval_size": 20971520,
                                "tick_interval": "500ms"
                            
                        ,
                        "state": "STATE_NORMAL",
                        "type": "etcdraft"
                    ,
                    "version": "0"
                
            ,
            "version": "0"
        
    ,
    "mod_policy": "Admins",
    "policies": 
        "Admins": 
            "mod_policy": "Admins",
            "policy": 
                "type": 3,
                "value": 
                    "rule": "MAJORITY",
                    "sub_policy": "Admins"
                
            ,
            "version": "0"
        ,
        "Readers": 
            "mod_policy": "Admins",
            "policy": 
                "type": 3,
                "value": 
                    "rule": "ANY",
                    "sub_policy": "Readers"
                
            ,
            "version": "0"
        ,
        "Writers": 
            "mod_policy": "Admins",
            "policy": 
                "type": 3,
                "value": 
                    "rule": "ANY",
                    "sub_policy": "Writers"
                
            ,
            "version": "0"
        
    ,
    "values": 
        "BlockDataHashingStructure": 
            "mod_policy": "Admins",
            "value": 
                "width": 4294967295
            ,
            "version": "0"
        ,
        "Capabilities": 
            "mod_policy": "Admins",
            "value": 
                "capabilities": 
                    "V1_4_3": 
                
            ,
            "version": "0"
        ,
        "Consortium": 
            "mod_policy": "Admins",
            "value": 
                "name": "SampleConsortium"
            ,
            "version": "0"
        ,
        "HashingAlgorithm": 
            "mod_policy": "Admins",
            "value": 
                "name": "SHA256"
            ,
            "version": "0"
        ,
        "OrdererAddresses": 
            "mod_policy": "/Channel/Orderer/Admins",
            "value": 
                "addresses": [
                    "orderer1.isprint.com:7050",
                    "orderer2.isprint.com:7050",
                    "orderer3.isprint.com:7050"
                ]
            ,
            "version": "0"
        
    ,
    "version": "0"
,
"sequence": "2"

我已经使用 JsonPath 编写了一个代码 sn-p:

DocumentContext context = JsonPath.parse(json);
String pathOrg1MSP = "$.channel_group.groups.Application.groups.Org1MSP";
Map<String,Object> mapOrg1MSP = context.read(pathOrg1MSP);
Map<String,Object> mapOrg2MSP = new LinkedHashMap<String,Object>();
mapOrg1MSP.forEach((k,v) -> 
        mapOrg2MSP.put(k, v);
);
String pathOrg2MSP = "$.channel_group.groups.Application.groups";
context.put(pathOrg2MSP, "Org2MSP", mapOrg2MSP);

目的是首先创建一个精确的克隆,然后更改需要更改的任何值(对等点、证书等),然后将其拼接到原始 Json 中。

    这能实现我的目标吗? 为了防止与现有组织发生任何可能的冲突,我需要更改哪些价值观?

【问题讨论】:

【参考方案1】:
    为 Org2 生成加密材料 生成特定于 Org2 的配置材料 从 orderer 中获取最新的配置块,将其修剪并转换为 JSON(我相信你已经完成了这一步) 向 JSON 添加特定于 Org2 的配置材料 将原始 JSON 和新编辑的 JSON 转换为 protobuf 格式 计算新块和原始块(.pb 文件)之间的差异 将其转换为 JSON 格式并添加回标头 将其转换为 protobuf 格式 Org1 签署交易

详细的命令可以参考这个官方文档。 https://hyperledger-fabric.readthedocs.io/en/release-1.4/channel_update_tutorial.html

【讨论】:

以上是关于Hyperledger Fabric:使用 JsonPath 将新组织添加到现有通道的主要内容,如果未能解决你的问题,请参考以下文章

HyperLedger/Fabric SDK使用Docker容器镜像快速部署上线

搭建基于hyperledger fabric的联盟社区 --启动Fabric网络

安装hyperledger fabric V1.0.0(维护中)

Hyperledger Fabric-身份

Hyperledger Fabric简介(6.1)

HyperLedger Fabric中fabric-samples 安装