__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

HyperLedger Fabric - Setting and Testing Raft based Orderer

BY: @devrajsinghrawat | CREATED: June 20, 2019, 3:17 p.m. | VOTES: 5 | PAYOUT: $0.00 | [ VOTE ]

[IMAGE: https://ipfs.busy.org/ipfs/QmWC1qPozFQn5EEXxwAJEroF23TjpNsQsXQ83jxcpW2NDb]
Pic Credits : https://raft.github.io/

Hola Friends,

Recently Hyperledger Fabric team had release v2.0 Alpha and introduced few exciting features like

Fabric chaincode lifecycle
FabToken
Alpine images
Raft ordering service
Check for more details

In this blog, I will be demonstrating "Setup and Testing" for RAFT based Orderer in HLF network. The whole writeup will be divided into 7 Sections as mentioned below

1- Generating basic building blocks
2- Bringing Up the network ( 2 Org, 2 Peer each, 5 Orderer)
3- Create Channel, Peers to join channel & Update Anchor Peers
4- Install Chain Code, Approve Chain Code & Commit Chain code
5- Invoke Chain Code
6- Query Chain Code
7- Mock the Orderer Crash and Test the Network

1) Generating basic building blocks

 ../bin/cryptogen generate --config=./crypto-config.yaml
 export FABRIC_CFG_PATH=$PWD
../bin/configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
export CHANNEL_NAME=mychannel && ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

2) Bring UP the network with RAFT Orderer Configuration

docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml up -d

Network is up with 5 orderer nodes
[IMAGE: https://ipfs.busy.org/ipfs/QmRxfMqk5XVWYapiC83wZE9pxrBfsbXuSG1cfQTxr1DnST]

Orderer Node log
[IMAGE: https://ipfs.busy.org/ipfs/QmdkqEBFzdc4QT2DdeNEvjatCwWdSMaJnuUpJaBssAsxiP]

Issues I had faced at this stage

Orderer2, 3,4, 5 were exiting
All Orderer were exiting
Channel were not able to create due to Access and some times Endorsement reasons

> If you face issue regarding TLS Handshake as mentioned in my separate log file in this repo https://github.com/devrajsinghrawat/HLF then retry using below command and restart the steps

> docker network prune

If everything goes fine then move to next steps

3) Create Channel using CLI container

docker exec -it cli bash
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp

CORE_PEER_ADDRESS=peer0.org1.example.com:7051

CORE_PEER_LOCALMSPID="Org1MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CHANNEL_NAME=mychannel

[IMAGE: https://ipfs.busy.org/ipfs/QmYCevpGWM47Uosan81xCSxvARZanrA3TiFwHy3iY3QwMS]

(Please note: All paths mentioned in env variables are local to CLI container)

CLI local artifacts view

[IMAGE: https://ipfs.busy.org/ipfs/QmUXzrJ9PYYx1DFzpcFFWUrzs3T1oz3ZzCzT3y4JN6EzxE]

peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Now let’s join peer0.org1.example.com to the channel

peer channel join -b mychannel.block
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:9051

CORE_PEER_LOCALMSPID="Org2MSP"

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

Now let’s join peer0.org2.example.com to the channel

peer channel join -b mychannel.block
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:9051 CORE_PEER_LOCALMSPID="Org2MSP" 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
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

4) Install and define a chaincode

( Note: Fabric chaincode lifecycle introduced in the v2.0 Alpha release)
Applications interact with the blockchain ledger through chaincode. Therefore we need to install a chaincode on every peer that will execute and endorse our transactions. However, before we can interact with our chaincode, the members of the channel need to agree on a chaincode definition that establishes chaincode governance.

This packages a Golang chaincode.

peer lifecycle chaincode package mycc.tar.gz --path github.com/hyperledger/fabric-samples/chaincode/abstore/go/ --lang golang --label mycc_1

This packages a Node.js chaincode

peer lifecycle chaincode package mycc.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode/abstore/node/ --lang node --label mycc_1

This packages a java chaincode

peer lifecycle chaincode package mycc.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode/abstore/java/ --lang java --label mycc_1
peer lifecycle chaincode install mycc.tar.gz

this returns the details of the packages installed on your peers

peer lifecycle chaincode queryinstalled

This will return Package ID..Installed chaincodes on peer:Package ID: mycc_1:1dbc43a27115483a31ee8bd78d88d5e33619b6d2ef6506832e91242a737081b4, Label: mycc_1

export CC_PACKAGE_ID=mycc_1:1dbc43a27115483a31ee8bd78d88d5e33619b6d2ef6506832e91242a737081b4

The endorsement policy of mycc will be set to require endorsements from a peer in both Org1 and Org2. Therefore, we also need to install the chaincode on a peer in Org2.

Environment variables for PEER0 in Org2

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:9051 CORE_PEER_LOCALMSPID="Org2MSP" 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
peer lifecycle chaincode install mycc.tar.gz

Because we set the environment variables to operate as Org2 in previous step, we can use the following command to approve a definition of the mycc chaincode for Org2.

this approves a chaincode definition for your org, make note of the --package-id flag that provides the package ID, use the --init-required flag to request the Init function be invoked to initialize the chaincode

peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name mycc --version 1.0 --init-required --package-id $CC_PACKAGE_ID --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent

All organizations need to agree on the definition before they can use the chaincode. Modify the following four environment variables to operate as Org1:

Environment variables for PEER0 ORG1

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org1MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

You can now approve a definition for the mycc chaincode as Org1. Chaincode is approved at the organization level. You can issue the command once even if you have multiple peers.

peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name mycc --version 1.0 --init-required --package-id $CC_PACKAGE_ID --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent
peer lifecycle chaincode queryapprovalstatus --channelID $CHANNEL_NAME --name mycc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

The command will produce as output a JSON map showing if the organizations in the channel have approved the chaincode definition

[IMAGE: https://ipfs.busy.org/ipfs/QmYrZaDSJ2ZnYcBHTnAMkGiTMJkuVBQwssnSNBcsyKDaCn]

peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID $CHANNEL_NAME --name mycc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --waitForEvent

5) Invoking the chaincode

peer chaincode invoke -o orderer.example.com:7050 --isInit --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["Init","a","100","b","100"]}' --waitForEvent

6) Query

peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

[IMAGE: https://ipfs.busy.org/ipfs/QmaH5N22vpNPk4oq9c8LqtXBdAi4xLR1kZWbA49N7qi9uv]

Complete log for installed chaincode container.
[IMAGE: https://ipfs.busy.org/ipfs/QmPcQj5AcdsA5oqJLKKpjFu5F6LCzRzLmf1TfpBWVKxXj1]

One of Raft based Orderer node log on successful transaction
[IMAGE: https://ipfs.busy.org/ipfs/QmVC7B2X283BvuBQeKJ1MXNWoWjk44tjGn5t5zgkMmav6p]

7) Test the RAFT protocol Fault tolerance

RAFT protocol should keep working until 2n+1 majority rule

docker stop orderer5.example.com

[IMAGE: https://ipfs.busy.org/ipfs/QmXHHc9LeqB1XTcZdyuZ6aKtfCjv2Z5v8gw3NjhiBcd9Ez]

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' --waitForEvent

[IMAGE: https://ipfs.busy.org/ipfs/QmWnLhrxiHWamBrLKVjqb6jarbPUoVnV7peUtr9bFYyVDe]

docker logs orderer4.example.com

[IMAGE: https://ipfs.busy.org/ipfs/QmV81hUF8DDY3R7JZwbRr4inAktBHiX2oZTNdDipCWD8R7]

docker stop orderer4.example.com orderer3.example.com 

[IMAGE: https://ipfs.busy.org/ipfs/QmY616R9hFJAaJ566bj2kXpNntSBV7PAhPnXtXThxtL44u]

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' --waitForEvent

Error: error sending transaction for invoke: got unexpected status: SERVICE_UNAVAILABLE -- no Raft leader - proposal response: version:1 response: payload:
[IMAGE: https://ipfs.busy.org/ipfs/QmZy6cqTVsnBEyKT6PmU4AfyUvq3taKKiAYYDrFdURf5tJ]

peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

[IMAGE: https://ipfs.busy.org/ipfs/QmUeMNz2SkJNVpWXg3AZRrN3gyktdbamCNd6AX3So3qVGJ]

 docker start orderer3.example.com

Now total 3 orderers are up and running.

Let's invoke the transaction again.

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' --waitForEvent

[IMAGE: https://ipfs.busy.org/ipfs/Qmai9Lgsf38ZbjVAYEGUmVCfqPsxWe6HSVyC5n3d5SJscS]

[IMAGE: https://ipfs.busy.org/ipfs/QmQh8mWZKCbUixrC2Nrq7F2B7Mk11KanXRpvT1VzEiAa9X]

Yhoo ! It was a success !

I hope you must have enjoyed the blog,

hyperledger-fabric #raft #orderer #steemit

TAGS: [ #hyperledger ] [ #fabric ] [ #busy ] [ #blog ] [ #blockchain ]

Replies

@steemitboard | June 20, 2019, 6 p.m. | Votes: 0 | [ VOTE ]

Congratulations @devrajsinghrawat! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published more than 350 posts. Your next target is to reach 400 posts.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!SteemitBoard - Witness UpdateDo not miss the coming Rocky Mountain Steem Meetup and get a new community badge!

> You can upvote this notification to help all Steem users. Learn how here!

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>