Book Image

Hyperledger Cookbook

By : Xun (Brian) Wu, Chuanfeng Zhang, Zhibin (Andrew) Zhang
Book Image

Hyperledger Cookbook

By: Xun (Brian) Wu, Chuanfeng Zhang, Zhibin (Andrew) Zhang

Overview of this book

Hyperledger is an open-source project and creates private blockchain applications for a range of domains. This book will be your desk reference as you explore common and not-so-common challenges faced while building blockchain networks using Hyperledger services. We'll work through all Hyperledger platform modules to understand their services and features and build end-to-end blockchain applications using various frameworks and tools supported by Hyperledger. This book's independent, recipe-based approach (packed with real-world examples) will familiarize you with the blockchain development cycle. From modeling a business network to integrating with various tools, you will cover it all. We'll cover common and not-so-common challenges faced in the blockchain life cycle. Later, we'll delve into how we can interact with the Hyperledger Fabric blockchain, covering all the principles you need to master, such as chaincode, smart contracts, and much more. We'll also address the scalability and security issues currently faced in blockchain development. By the end of this book, you will be able to implement each recipe to plan, design, and create a full-fledged, private, decentralized application to meet organizational needs.
Table of Contents (12 chapters)

Adding an organization to a channel

This recipe serves as an extension to the BYFN recipe. We will demonstrate how to add a new organization Org3 to the application's channel (mychannel).

Getting ready...

To run this recipe, you need complete the Reviewing the Hyperledger Fabric architecture and components recipe in this chapter to install Hyperledger Fabric with samples and binaries on the AWS EC2 instance.

How to do it...

Since we need add the new organization, Org3, to BYFN, we will first bring up the BYFN network. Follow these steps:

  1. Bring up the first network using the following command:
        $ cd ~
$ cd fabric-samples/first-network
$ sudo ./byfn.sh generate
$ sudo ./byfn.sh up

  1. Execute the script to add Org3 into the mychannel channel:
    $ cd ~
$ cd fabric-samples/first-network
$ sudo ./eyfn.sh up

The following screenshot confirms org3 is added to mychannel successfully:

We can test this by running a query against Org3 peer0.

  1. To shut down and clean up the network, execute the following:
        $ cd fabric-samples/first-network
$ sudo ./eyfn.sh down
$ sudo ./byfn.sh down

How it works...

Like what we did in the Building the Fabric network recipe, the eyfn.sh script is a good resource to understand how things work.

We will also look into the command-line steps to see the internal building blocks to add an organization to a channel:

  1. Generate the org3 certificates:
        $ cryptogen generate --config=./org3-crypto.yaml
  1. Generate the org3 configuration materials:
        $ configtxgen -printOrg Org3MSP
  1. Generate and submit the transaction configuration for organization 3:
         $ peer channel fetch config config_block.pb -o 
orderer.example.com:7050 -c mychannel --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
$ configtxlator proto_encode --input config.json
--type common.Config
$ configtxlator proto_encode --input modified_config.json
--type common.Config
$ configtxlator compute_update --channel_id mychannel
--original original_config.pb --updated modified_config.pb
$ configtxlator proto_decode --input config_update.pb
--type common.ConfigUpdate

  1. Configure the transaction to add org3, which has been created:
        $ peer channel signconfigtx -f org3_update_in_envelope.pb
  1. Submit the transaction from a different peer (peer0.org2), who also signs it:
        $ peer channel update -f org3_update_in_envelope.pb -c mychannel -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
  1. Get the org3 peer to join the network:
        $ peer channel fetch 0 mychannel.block -o orderer.example.com:7050 
-c mychannel --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
$ peer channel join
-b mychannel.blockcd fabric-samples/first-network
  1. Install and update the chaincode:
         $ peer chaincode install -n mycc -v 2.0 -l golang -p    
github.com/chaincode/chaincode_example02/go/
$ peer chaincode upgrade -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
mychannel -n mycc -v 2.0 -c
'{"Args":["init","a","90","b","210"]}'

-P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'',
'\''Org3MSP.peer'\'')'
  1. Query peer0 org3:
        $ peer chaincode query -C mychannel -n mycc 
-c '{"Args":["query","a"]}'
  1. Invoke the transaction to move 10 from a to b again on a different peer:
        $ 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
mychannel -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:7051

--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/
fabric/peer/
crypto/peerOrganizations/org2.example.com/peers/
peer0.org2.example.com/tls/ca.crt
--peerAddresses peer0.org3.example.com:7051
--tlsRootCertFiles/opt/gopath/src/github.com/hyperledger/
fabric/
peer/crypto/peerOrganizations/org3.example.com/peers/
peer0.org3.example.com/tls/ca.crt
-c '{"Args":["invoke","a","b","10"]}'


This concludes how to add an organization to an existing network in a channel. We will look at how to use CouchDB to review transactions in the next recipe.

Following all the previous steps will create our first network, which consists of two organizations, two peers per organization, and single Solo ordering service. In this recipe, we showed you how to add a third organization to an application channel with its own peers to an already running first network, and then join it to the new channel.

When you view the log file, you will be able to see details in the following order:

  • Generating Org3 config material
  • Generating and submitting config tx to add Org3
  • Creating config transaction to add Org3 to the network
  • Installing jq
  • Config transaction to add Org3 to the network
  • Signing the config transaction
  • Submitting the transaction from a different peer (peer0.org2), which also signs it
  • Configure transaction to add Org3 to network submitted
  • Having Org3 peers join the network
  • Getting Org3 on to your first network
  • Fetching the channel config block from orderer
  • peer0.org3 joined the mychannel channel
  • peer1.org3 joined the mychannel channel
  • Installing chaincode 2.0 on peer0.org3
  • Upgrading chaincode to have Org3 peers on the network
  • Finishing adding Org3 to your first network
  • Chaincode is installed on peer0.org1
  • Chaincode is installed on peer0.org2
  • Chaincode is upgraded on peer0.org1 on the mychannel channel
  • Finished adding Org3 to your first network!

Updating modification policies or altering batch sizes or any other channel configuration can be updated using the same approach but for now we will focus solely on the integration of a new organization.

There's more...

The following block shows the org3-crypto.yaml section for Org3:

# --------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org3
# ---------------------------------------------------------------------------
- Name: Org3
Domain: org3.example.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1

The following block shows the configtx.yaml section for Org3:

#################################################################################
# Section: Organizations
#
# - This section defines the different organizational identities which will
# be referenced later in the configuration.
#
################################################################################
Organizations:
- &Org3
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org3MSP
# ID to load the MSP definition as
ID: Org3MSP
MSPDir: crypto-config/peerOrganizations/org3.example.com/msp
AnchorPeers:
# AnchorPeers defines the location of peers which can be used for cross org gossip #communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org3.example.com
Port: 7051

In the next recipe, we will look at how smart contracts work with CouchDB.