Add a New Organization on Existing Hyperledger Fabric Network

Introduction

Overall Process

Review: Build Your First Network

Process of Adding Org3 into First Network

0. Bring Up First Network with byfn.sh

$ cd fabric-samples/first-network
$ ./byfn.sh up

1. Prepare Org3 artifacts

org3-crypto.yaml

// in first-network/org3-artifacts directory
$ ../../bin/cryptogen generate --config=./org3-crypto.yaml
// in first-network/org3-artifacts directory
$ cp -r ../crypto-config/ordererOrganizations crypto-config/

configtx.yaml

// in first-network/org3-artifacts directory
$ export FABRIC_CFG_PATH=$PWD && ../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json
org3.json, generated by configtxgen

2. Update the network to include Org3 configuration

The Idea

Tool: configtxlator

2.1 fetch the latest configuration block from the ledger

$ docker exec -it cli bash
# export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
# peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
Block #2 is the latest configuration block after running ./byfn.sh up script.

2.2 prepare the configuration update for Org3

# configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
Right-hand-side is the useful extraction from the block
# jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json
The three files: org3.json is inserted into config.json, resulting modified_config.json
# configtxlator proto_encode --input config.json --type common.Config --output config.pb# configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb# configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_config.pb --output org3_update.pb
# configtxlator proto_decode --input org3_update.pb --type common.ConfigUpdate | jq . > org3_update.json# echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat org3_update.json)'}}}' | jq . > org3_update_in_envelope.json# configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb
The org3_updated.json is wrapped into org3_updated_in_envelope.json
From config_block.pb (latest configuration block) to the org3_update_in_envelope.pb

2.3 sends update to Orderer and a new block containing Org3 configuration is committed back to the ledger

// a new terminal for peer0.org2$ docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer0.org2.example.com:7051 -it cli bash// perform export environment as well# export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
// cli for peer0.org1.example.com# peer channel signconfigtx -f org3_update_in_envelope.pb
// cli for peer0.org2.example.com# peer channel update -f org3_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
// cli for both peer0.org1 and peer0.org2# peer channel getinfo -c mychannel
Now our First Network has a new block containing the Org3 configuration.

3. Bring up Peers of Org3 and Join mychannel

// a new terminal for Org3cli$ docker-compose -f docker-compose-org3.yaml up -d
// Org3cli$ docker exec -it Org3cli bash# export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
// cli for peer0.org1.example.com# peer channel fetch 0 mychannel.block -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
// my localhost$ docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block .$ docker cp mychannel.block Org3cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/
// Org3cli# peer channel join -b mychannel.block
// Org3cli# CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt CORE_PEER_ADDRESS=peer1.org3.example.com:7051 peer channel join -b mychannel.block
// Org3cli# peer channel getinfo -c mychannel

4. Chaincode operations after Org3 is part of the network

// cli for peer0.org1.example.com# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/// cli for peer0.org2.example.com# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/// Org3cli# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
// cli for peer0.org1.example.com# peer chaincode upgrade -o orderer.example.com:7050 —-tls $CORE_PEER_TLS_ENABLED —-cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 2.0 -c '{"Args":["init","a","90","b","210"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"
// Org3cli# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
// Org3cli# peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'// cli for peer0.org2.example.com# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

5. Clean Up

// in first-network directory$ ./byfn.sh down

Closing

Visit http://www.ledgertech.biz/kcarticles.html for all my works. Reach me on https://www.linkedin.com/in/ktam1/ or follow me @kctheservant in Twitter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store