Exploring Gossip when Starting a Fabric Network

Introduction

This is another “behind the scene” demonstration about Hyperledger Fabric. Gossip plays an important role in Hyperledger Fabric. In this article, we will inspect gossip when a fabric network is started, by breaking down the starting process and observing the logs of individual components. We will come across some concepts in fabric, such as leader and anchor peer, and see how gossip helps building a scalable consortium platform in Hyperledger Fabric.

Overview

Many of us begin practicing Hyperledger Fabric with sample networks like First Network. First Network provides a script, byfn.sh, showing us a typical flow of bringing up a network:

  1. bringing up network components (orderers, peers, etc)
  2. joining of peers to channel
  3. updating anchor peers

Gossip Bootstrap Configuration

Gossip bootstrap is configured in each peer. It contains one or a list of peers within an organization. When a peer is up and running, it will reach that list of peers for gossiping.

Image for post
Image for post
Configuration of gossip bootstrap

Leader

Leader in an organization is responsible for receiving blocks from ordering service, and distributing blocks to other peers in the same organization. Remember that a fabric network requires all peers in all channel members (organizations) receiving the new block from ordering service. The introduction of “leader” optimizes the overall process as ordering service only sends to the leader of each organization, and leader to other peers in this organization.

Image for post
Image for post
Leader setting in peers

Anchor Peer

Anchor peer is needed in a network for peers to learn channel members outside its own organization. Without an anchor peer, knowledge of channel members is only limited to just one’s organization. For example, without an anchor peer, peers in Org1 only know peers in Org1 as channel members, but no knowledge of peers in Org2. At least one anchor peer is needed on a channel such that any peers in every organization know all other members in this channel.

First Network and Script byfn.sh

As we are using First Network, a network of two peer organizations, Org1 and Org2, each is configured with two peers (peer0 and peer1). A channel mychannel is created and joined by all four peers. Finally peer0 of both orgs are configured as anchor peers.

  1. Bring up all network components (containers) with docker-compose (Step 2–5)
  2. Create genesis block (block #0) for mychannel (Step 6)
  3. Join all four peers (from both organizations) to mychannel (Step 7–10)
  4. Update anchor peer configuration for both organizations (Step 11–12)
Image for post
Image for post
Each peer knows other channel members, in its own organization and others

Demonstration

Step 0: Preparation

A new directory is directly copied from first-network/.

cd fabric-samples
cp -r first-network kc-first-network
Image for post
Image for post
Comment out the depends_on

Step 1: Generate crypto material and channel artifacts

We leverage byfn.sh to generate the required crypto material and channel artifacts for the network.

cd kc-first-network
./byfn.sh generate
Image for post
Image for post

Step 2: Bring up Orderer and CLI

docker-compose -f docker-compose-cli.yaml up -d orderer.example.com cli
Image for post
Image for post

Step 3: Bring Up peer0.org1

docker-compose -f docker-compose-cli.yaml up -d peer0.org1.example.com
docker logs -f peer0.org1.example.com
Image for post
Image for post
peer0.org1.example.com: gossip instance started
Image for post
Image for post
peer0.org1.example.com: reaching gossip bootstrap

Step 4: Bring Up peer1.org1

docker-compose -f docker-compose-cli.yaml up -d peer1.org1.example.com
Image for post
Image for post
peer1.org1.example.com: gossip instance started
Image for post
Image for post
peer1.org1.example.com: it reaches gossip bootstrap
  • peer0.org1.example.com reached peer1.org1.example.com in 06:39:05.892 (below)
Image for post
Image for post
peer0.org1.example.com: it locates the gossip bootstrap

Step 5: Bring up Peers of Org2

To keep the demo flow of byfn.sh script, we bring up peer0.org2.example.com and peer1.org2.example.com as well. We observe similar behaviour.

docker-compose -f docker-compose-cli.yaml up -d peer0.org2.example.com peer1.org2.example.com

Step 6: Prepare Genesis Block for mychannel

docker exec cli peer channel create -o orderer.example.com:7050 --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 -c mychannel -f ./channel-artifacts/channel.tx
Image for post
Image for post

Step 7: Join peer0.org1 to mychannel

Note the default setting of CLI targets to peer0.org1.example.com using Admin of Org1.

docker exec cli peer channel join -b mychannel.block
  • Commit block #0
  • Join gossip network for mychannel (with 2 organizations learnt from block #0)
  • Find no configuration of anchor peers from the two organizations yet (will be done in final step)
  • Become a leader, and elected as a leader
Image for post
Image for post
peer0.org1.example.com: elected as leader after joining the channel

Step 8: Join peer1.org1 to mychannel

docker exec -e CORE_PEER_ADDRESS=peer1.org1.example.com:8051 -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt cli peer channel join -b mychannel.block
Image for post
Image for post
peer1.org1.example.com: it sees another leader after joining the channel

Step 9: Peers of Org1 Learn Channel Membership

After leadership of an organization is done, we immediately see logs from both peers showing what they learn about channel membership.

Image for post
Image for post
peer0.org1.example.com: learn peers in its own organization as channel members
Image for post
Image for post
peer1.org1.example.com: learn peers in its own organization as channel members

Step 10: Joining Peers of Org2 to mychannel

docker exec -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:9051 -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 cli peer channel join -b mychannel.blockdocker exec -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=peer1.org2.example.com:10051 -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/peer1.org2.example.com/tls/ca.crt cli peer channel join -b mychannel.block
Image for post
Image for post
peer0.org2.example.com: elected as leader and learn peers in its own organization
Image for post
Image for post
peer1.org2.example.com: not a leader, and also learn peers in its own organization
Image for post
Image for post
Without anchor peer, peer has no knowledge of cross-organization channel members

Step 11: Update Anchor Peer for Org1

docker exec cli peer channel update -o orderer.example.com:7050 --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 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx
Image for post
Image for post
tcpdump: connection between orderer to both peer0.org1 and peer0.org2, leaders of organizations
Image for post
Image for post
peer0.org1.example.com
Image for post
Image for post
peer1.org1.example.com
Image for post
Image for post
peer0.org2.example.com
Image for post
Image for post
peer1.org2.example.com
Image for post
Image for post
peer0.org1.example.com: learn all channel members
Image for post
Image for post
peer1.org1.example.com: learn all channel members
Image for post
Image for post
peer0.org2.example.com: learn all channel members
Image for post
Image for post
peer1.org2.example.com: learn all channel members
Image for post
Image for post
With just one anchor peer, peer now knows all channel members

Step 12: Update Anchor Peer for Org2

We see just one anchor peer in a channel makes the network ready for use. It is highly recommended to have an anchor peer per organization.

docker exec -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:9051 -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 cli peer channel update -o orderer.example.com:7050 --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 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx

Variation

For learning purposes, we will do some observations by varying some steps. This helps us to understand more how the change affects the fabric network.

Removing Gossip Bootstrap from Peer Configuration

The gossip bootstrap is set in base/docker-compose-base.yaml. Comment the variables CORE_PEER_GOSSIP_BOOTSTRAP on each peer.

Image for post
Image for post
peer0.org1.example.com
Image for post
Image for post
peer1.org1.example.com
Image for post
Image for post
peer0.org2.example.com
Image for post
Image for post
peer1.org2.example.com
Image for post
Image for post
peer0.org1.example.com: know all channel members
Image for post
Image for post
peer1.org1.example.com: know all channel members
Image for post
Image for post
peer0.org2.example.com: know all channel members
Image for post
Image for post
peer1.org2.example.com: know all channel members
Image for post
Image for post
peer0.org1.example.com stops being leader
Image for post
Image for post
peer0.org2.example.com stops being leader

Having Anchor Peer Update before Joining another Organization

In byfn.sh the anchor update (Step 11) is done after peers in Org2 join the channel (Step 10). Here we observe what happens if we swap these two steps. That is to say, mychannel already has an anchor peer before peers of Org2 join mychannel.

  • Commit block #0
  • Join gossip network for mychannel (with 2 organizations learnt from block #0)
  • Find no configuration of anchor peers from the two organizations yet (will be done in final step)
  • Become a leader, and elected as a leader
Image for post
Image for post
peer0.org2.example.com
Image for post
Image for post
peer0.org2.example.com: learn channel member in Org1 as anchor peer already exists in channel
Image for post
Image for post
peer0.org1.example.com: learn peer0.org2.example.com
Image for post
Image for post
peer1.org1.example.com: learn peer0.org2.example.com

Summary

In this article we examined the role of gossip in bringing up a fabric network. We followed the script byfn.sh from First Network by breaking down into steps, and observed from the logs the behaviours when bringing up a peer, joining a peer to channel, the role of anchor peer in cross-organization membership learning. We also made some variations to explore more on gossip and fabric network. Hope you can gain some more ideas on Hyperledger Fabric.

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

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