拆解eyfn.sh——拓展賬本增加新成員

拆解fabric-sample/first-network/拆解eyfn.sh——拓展賬本增加新成員

This tutorial serves as an extension to the Building Your First Network (BYFN) tutorial, and will demonstrate the addition of a new organization – Org3 – to the application channel (mychannel) autogenerated by BYFN. It assumes a strong understanding of BYFN, including the usage and functionality of the aforementioned utilities.
原文參考

eyfn.sh腳本主要是用來拓展前一節(jié)中的fabric網(wǎng)絡(luò)勇吊,在原來的Org1和Org2的基礎(chǔ)上汉规,新增Org3成員。
還是從eyfn.sh的執(zhí)行過程來看一下新增成員的過程中經(jīng)歷了什么针史。

腳本拆解

執(zhí)行命令

eyfn.sh腳本的執(zhí)行還是保持原來的“./eyfn.sh up”

我們傳入up以后悟民,可以看到腳本調(diào)用了networkUp方法

#Create the network using docker compose
if [ "${MODE}" == "up" ]; then
  networkUp
elif [ "${MODE}" == "down" ]; then ## Clear the network
  networkDown
elif [ "${MODE}" == "generate" ]; then ## Generate Artifacts
  generateCerts
  generateChannelArtifacts
  createConfigTx
elif [ "${MODE}" == "restart" ]; then ## Restart the network
  networkDown
  networkUp
else
  printHelp
  exit 1
fi

方法跟蹤

# Generate the needed certificates, the genesis block and start the network.
function networkUp () {
  # generate artifacts if they don't exist
  if [ ! -d "org3-artifacts/crypto-config" ]; then
    ##生成cert文件
    generateCerts
    ##生成org3的節(jié)點資料
    generateChannelArtifacts
    ##創(chuàng)建修改文件的配置
    createConfigTx
  fi
  # start org3 peers
  if [ "${IF_COUCHDB}" == "couchdb" ]; then
      IMAGE_TAG=${IMAGETAG} docker-compose -f $COMPOSE_FILE_ORG3 -f $COMPOSE_FILE_COUCH_ORG3 up -d 2>&1
  else
      IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE_ORG3 up -d 2>&1
  fi
  if [ $? -ne 0 ]; then
    echo "ERROR !!!! Unable to start Org3 network"
    exit 1
  fi
  echo
  echo "###############################################################"
  echo "############### Have Org3 peers join network ##################"
  echo "###############################################################"
  docker exec Org3cli ./scripts/step2org3.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT $VERBOSE
  if [ $? -ne 0 ]; then
    echo "ERROR !!!! Unable to have Org3 peers join network"
    exit 1
  fi
  echo
  echo "###############################################################"
  echo "##### Upgrade chaincode to have Org3 peers on the network #####"
  echo "###############################################################"
  docker exec cli ./scripts/step3org3.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT $VERBOSE
  if [ $? -ne 0 ]; then
    echo "ERROR !!!! Unable to add Org3 peers on network"
    exit 1
  fi
  # finish by running the test
  docker exec Org3cli ./scripts/testorg3.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT $VERBOSE
  if [ $? -ne 0 ]; then
    echo "ERROR !!!! Unable to run test"
    exit 1
  fi
}

方法分解

networkUp方法主要流程又分為下面幾步:

  1. 檢測是否存在證書射亏、配置文件竭业、新增部分配置文件等,如果不存在則新建未辆;
  2. 啟動Org3節(jié)點咐柜;
  3. 將Org3節(jié)點加入已有的mychannel通道;
  4. 在mychannel通道網(wǎng)絡(luò)中更新鏈碼拙友,將鏈碼版本從1.0升級到2.0;
  5. 執(zhí)行腳本測試工作辐棒;

關(guān)鍵路徑

生成并提交Org3的配置文件

# Use the CLI container to create the configuration transaction needed to add
# Org3 to the network
function createConfigTx () {
  docker exec cli scripts/step1org3.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT $VERBOSE
}

篇幅原因牍蜂,方法有刪減

這里我們看到主要流程在step1org3.sh中,該腳本的介紹頁表示了這是EYFN的第一步辐怕,創(chuàng)建并提交org3加入網(wǎng)絡(luò)的配置事物文件

This script is designed to be run in the org3cli container as the first step of the EYFN tutorial. It creates and submits a configuration transaction to add org3 to the network previously setup in the BYFN tutorial.

其中主要執(zhí)行命令如下:

# Fetch the config for the channel, writing it to config.json
fetchChannelConfig ${CHANNEL_NAME} config.json

# Modify the configuration to append the new org
jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json

# Compute a config update, based on the differences between config.json and modified_config.json, write it as a transaction to org3_update_in_envelope.pb
createConfigUpdate ${CHANNEL_NAME} config.json modified_config.json org3_update_in_envelope.pb

echo "========= Config transaction to add org3 to network created ===== "

echo "Signing config transaction"
signConfigtxAsPeerOrg 1 org3_update_in_envelope.pb

echo "========= Submitting transaction from a different peer (peer0.org2) which also signs it ========= "
peer channel update -f org3_update_in_envelope.pb -c ${CHANNEL_NAME} -o orderer.example.com:7050 --tls --cafile ${ORDERER_CA}

分別看一下上面用到的幾個方法和命令
  • fetchChannelConfig

    The reason why we have to pull the latest version of the config is because channel config elements are versioned.. Versioning is important for several reasons. It prevents config changes from being repeated or replayed (for instance, reverting to a channel config with old CRLs would represent a security risk). Also it helps ensure concurrency (if you want to remove an Org from your channel, for example, after a new Org has been added, versioning will help prevent you from removing both Orgs, instead of just the Org you want to remove).

    官方說之所以必須提取配置的最新版本寄疏,是因為通道配置元素的版本化了,版本控制非常重要妖泄,它可以防止配置更改被重復(fù)或重播艘策,還有助于確保并發(fā)性

    用命令獲取最新的區(qū)塊信息

    peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL --cafile $ORDERER_CA
    

    使用configtxlator proto_decode對區(qū)塊信息進行解碼操作

    configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config >"${OUTPUT}"
    

    可以使用這個命令對所有的pb文件進行解密得到原文查看,官方也解釋了這個命令:

    This command saves the binary protobuf channel configuration block to config_block.pb. Note that the choice of name and file extension is arbitrary. However, following a convention which identifies both the type of object being represented and its encoding (protobuf or JSON) is recommended.

  • createConfigUpdate

    Takes an original and modified config, and produces the config update tx which transitions between the two

    根據(jù)原始和修改的config生成新的配置文件

    configtxlator proto_encode --input "${ORIGINAL}" --type common.Config >original_config.pb
    configtxlator proto_encode --input "${MODIFIED}" --type common.Config >modified_config.pb
    configtxlator compute_update --channel_id "${CHANNEL}" --original original_config.pb --updated modified_config.pb >config_update.pb
    configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate >config_update.json
    configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope >"${OUTPUT}"
    
    
  • signConfigtxAsPeerOrg

    peer channel signconfigtx -f "${TX}"
    

    根據(jù)前一步生成的org3_update_in_envelope.pb文件進行簽名操作,官方默認的修改策略是需要“MAJORITY”(超過半數(shù))節(jié)點進行簽名方可在賬本中進行配置文件生效驯妄,由于我們demo中只有Org1和Org2一共2個節(jié)點,所以需要2個節(jié)點都需要簽名才行源织。

    However, we need signatures from the requisite Admin users before the config can be written to the ledger. The modification policy (mod_policy) for our channel Application group is set to the default of “MAJORITY”, which means that we need a majority of existing org admins to sign it. Because we have only two orgs – Org1 and Org2 – and the majority of two is two, we need both of them to sign. Without both signatures, the ordering service will reject the transaction for failing to fulfill the policy.

  • peer channel update
    上一步中提到的需要2個節(jié)點進行簽名微猖,在上一步中我們使用了Org1進行簽名,這一步主要是用peer channel update命令使Org2進行簽名操作
    peer channel update -f org3_update_in_envelope.pb -c ${CHANNEL_NAME} -o orderer.example.com:7050 --tls --cafile ${ORDERER_CA}
    

后續(xù)的啟動Org3節(jié)點侠仇、將Org3節(jié)點加入已有的mychannel通道犁珠、在mychannel通道網(wǎng)絡(luò)中更新鏈碼,將鏈碼版本從1.0升級到2.0犁享、執(zhí)行腳本測試工作與之前的byfn.sh腳本中類似饼疙,只是將Org從1和2改成了3,將鏈碼版本號從1.0改為2.0其余均一樣窑眯。

謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炊林,一起剝皮案震驚了整個濱河市卷要,隨后出現(xiàn)的幾起案子独榴,更是在濱河造成了極大的恐慌奕枝,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件症歇,死亡現(xiàn)場離奇詭異谭梗,居然都是意外死亡,警方通過查閱死者的電腦和手機设塔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門远舅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人钞护,你說我怎么就攤上這事爆办】问幔” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵跨算,是天一觀的道長椭懊。 經(jīng)常有香客問我,道長氧猬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任漠魏,我火速辦了婚禮柱锹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘禁熏。我一直安慰自己,他們只是感情好隙笆,可當(dāng)我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布升筏。 她就那樣靜靜地躺著,像睡著了一般铅忿。 火紅的嫁衣襯著肌膚如雪灵汪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天峻凫,我揣著相機與錄音览露,去河邊找鬼。 笑死差牛,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的脐恩。 我是一名探鬼主播侦讨,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼骗污!你這毒婦竟也來了怜俐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤贴谎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后擅这,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡痹扇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年鲫构,在試婚紗的時候發(fā)現(xiàn)自己被綠了玫坛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡炕吸,死狀恐怖勉痴,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蒸矛,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布廓脆,位于F島的核電站磁玉,受9級特大地震影響驾讲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜吮铭,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望掠拳。 院中可真熱鬧纸肉,春花似錦喊熟、人聲如沸姐刁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柏靶。三九已至,卻和暖如春屎蜓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背控汉。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工返吻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人测僵。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓捍靠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親榨婆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容