都2021年了,fabric 2.2.x fabric java sdk 動態新增組織案例和原始碼要有了吧
阿新 • • 發佈:2021-09-04
2021 年了沒有可以直接複製貼上用fabric 2.2.x 簡單程式碼?那就自己寫,並且分享一下偷懶configtx.yaml配置,首先確保你搭建的聯盟鏈channel的配置是這個樣子,
這個配置檔名字叫 configtx.yaml
################################################################################ # # CHANNEL # # This section defines the values to encode into a config transaction or # genesis block for channel related parameters. # ################################################################################ Channel:&ChannelDefaults # Policies defines the set of policies at this level of the config tree # For Channel policies, their canonical path is # /Channel/<PolicyName> Policies: # Who may invoke the 'Deliver' API Readers: Type: ImplicitMeta Rule: "ANY Readers" # Who may invoke the 'Broadcast' API Writers: Type: ImplicitMeta Rule: "ANY Writers" # By default, who may modify elements at this config level Admins: Type: ImplicitMeta Rule: "ANY Admins" # 這點很重要,只有這樣你安裝鏈碼單節點審批就行了,建議全部偷懶全部ANY ,只要有Admin簽名就行 # Capabilities describes the channel level capabilities, see the # dedicated Capabilities section elsewhere in this file for a full # description Capabilities:<<: *ChannelCapabilities
搭建聯盟鏈這種有其他簡單教程,這裡不再贅述。
直接貼程式碼分析一下,看註釋就行了
@Test void channelConfig() throws Exception {
// 這是路徑,就是fabric加密材料的地方,預設讀取的路徑自己會拼接crypto-config
String cryptoConfig = "/home/ct/Workspace/onekey/outdir";
// OrgManage om = new OrgManage("","example.com"); OrgManage om = newOrgManage(cryptoConfig, "example.com"); om.initHFClient(); //Peer peer =; 選擇一個你要使用的使用者 OrgManage.UserContext user = om.newUser("Admin", "Org1MSP", "org1"); om.hfClientAddUser(user); om.initChannel("mychannel"); om.channelAddOrderer(om.newOrderer("orderer", "orderer.example.com", "7050")); om.channelAddPeer(om.newPeer("peer0", "org1", "peer0.org1.example.com", "7051")); om.build(); Channel channel = om.getChannel(); HFClient hfClient = om.getHfClient(); // 到這裡就建立好 channel 客戶端和 HFClient 客戶端了 byte[] configPB = channel.getChannelConfigurationBytes(); // 獲取channel配置 // configPB 寫入檔案檢測 InputStream isConfigPb = new ByteArrayInputStream(configPB); InUtils.writeStream(isConfigPb,"/home/ct/Workspace/java/sdk2x/src/test/fix/config.pb"); // 新增的新組織 String orgMSP = "Org4MSP"; // 例項化Forest配置物件 ForestConfiguration configuration = ForestConfiguration.configuration(); // 通過Forest配置物件例項化Forest請求介面 ConfigtxlatorClient myClient = configuration.createInstance(ConfigtxlatorClient.class); // 呼叫Forest請求介面,並獲取響應返回結果 這裡使用 Configtxlator 解析 PB 檔案 JSONObject configJson = myClient.decodeCommonConfig("47.105.36.27:7059", configPB, "c.pb"); // 需要獲取的資料 byte[] jsonByte = configJson.toString().getBytes(); InputStream inputStream = new ByteArrayInputStream(jsonByte); // 寫入檔案 InUtils.writeStream(inputStream, "/home/ct/Workspace/java/sdk2x/src/test/fix/config2.json"); // 讀取 org.json , 這個檔案是通過指令碼生成,可以去看原始碼 doc 裡面的指令碼,就是生成的組織4的加密材料和 configtx.yaml 檔案通過
// configtxgen -printOrg Org3MSP > ./org3.json 原始碼裡有如何生成 configtx.yaml 的摸板
JSONObject orgJson = readJson("/home/ct/Workspace/java/sdk2x/src/test/fix/org4.json"); // 檔案進行拼接產生新的 modifiedConfig String modifiedConfigStr = configJson.toJSONString(); // jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ../channel-artifacts/org3.json > modified_config.json JSONObject modifiedConfig = JSONObject.parseObject(modifiedConfigStr); modifiedConfig.getJSONObject("channel_group").getJSONObject("groups").getJSONObject("Application").getJSONObject("groups").put(orgMSP,orgJson); // 把 modifiedConfig 寫入檔案檢測是否寫入 modifiedConfigStr = modifiedConfig.toString(); InUtils.writeStream("/home/ct/Workspace/java/sdk2x/src/test/fix/modified_config.json",modifiedConfigStr); // 把 modifiedConfig 轉換成 ProtoBuffer 檔案 byte[] modifiedConfigJsonPB = myClient.encodeCommonConfig("47.105.36.27:7059", modifiedConfigStr.getBytes(), "c.pb"); // 寫入檔案檢測 InputStream isModifiedConfigJsonPB = new ByteArrayInputStream(modifiedConfigJsonPB); InUtils.writeStream(isModifiedConfigJsonPB,"/home/ct/Workspace/java/sdk2x/src/test/fix/modified_config.pb"); ConfigtxlatorHttpClient configtxlatorHttpClient = new ConfigtxlatorHttpClient(); String channelName = "mychannel"; // org4_update.pb byte[] orgUpdatePb = configtxlatorHttpClient.computeUpdate("47.105.36.27:7059",channelName,configPB,modifiedConfigJsonPB); InUtils.writeStream(new ByteArrayInputStream(orgUpdatePb),"/home/ct/Workspace/java/sdk2x/src/test/fix/org4_update.pb"); // 建立 update -f org3_update_in_envelope.pb UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration(orgUpdatePb); // 建立 OrdererMSP 使用者 , 實際生產自己肯定沒有儲存這麼多證書,可以通過 p2p 網路發過來 Signature 而不是證書 OrgManage.UserContext ordererAdmin = om.newUser("Admin", "OrdererMSP"); // 建立 adminOrg2 OrgManage.UserContext adminOrg2 = om.newUser("Admin", "Org2MSP", "org2"); // 建立 adminOrg3 OrgManage.UserContext adminOrg3 = om.newUser("Admin", "Org3MSP", "org3"); // 提交簽名,這裡確實會自己新增頭部 '{"payload":{"header":{"channel_header":{"channel_id":...
// 超過半數簽名 channel.updateChannelConfiguration(updateChannelConfiguration, hfClient.getUpdateChannelConfigurationSignature(updateChannelConfiguration,ordererAdmin), // hfClient.getUpdateChannelConfigurationSignature(updateChannelConfiguration,user), hfClient.getUpdateChannelConfigurationSignature(updateChannelConfiguration,adminOrg2), hfClient.getUpdateChannelConfigurationSignature(updateChannelConfiguration,adminOrg3)); byte[] configPBNew = channel.getChannelConfigurationBytes(); // configPB 寫入檔案檢測 InputStream isConfigPbNew = new ByteArrayInputStream(configPBNew); InUtils.writeStream(isConfigPbNew,"/home/ct/Workspace/java/sdk2x/src/test/fix/configAddOrg4.pb"); }
詳細程式碼直接去原始碼test裡找就好了