Hyperledger Fabric “Building Your First Network” Tutorial — Part 3

Sam LAM @ OriginBit
2 min readFeb 6, 2019

--

Hyperledger Fabric Logo

In part 1 and 2, I had introduced files in BYFN. In this part 3, I am going to continue practical steps!

3. Bring Up the Network

3.1 Switch to BYFN Directory

cd fabric-samples/first-network

3.2 Bring Up the Network

./byfn.sh up

After running this script, you might need to wait for a moment…

Also, remember to start Docker, before you run the command above.

*In case you make something wrongly, you can run the following commands to turn down the network, and bring up again (and be careful):

./byfn.sh down
./byfn.sh up

Remember what it does?

  1. Generate certificates and keys based on crypto-config.yaml
  2. Generate channel artifacts based on configtx.yaml, outputs will be stored in channel-artifacts/
  3. Bring up the (virtual) Fabric network based on docker-compose-cli.yaml
  4. Create Hyperledger Fabric channel, mychannel
  5. Join Peer nodes into the channel
  6. Install the Smart Contract (Chaincode) in Peer nodes
  7. Instantiate the Smart Contract (Chaincode) in one of the Peer nodes

After the byfn.sh is finished, you should also find that there are files generated in channel-artifacts/.

And If you execute command “docker ps”, you should see that there are some running containers, which are those nodes in the Blockchain network.

4. Verification

How could we verify that the network is up?

4.1 Access the Cli

There is a cli Docker container created automatically, it is a command line interface to control the nodes.

Let’s get access to the cli:

docker exec -it cli bash

Then, set up environment variables used by some programs:

export CHANNEL_NAME=mychannel
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export 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 could just copy all of them, then paste to your terminal and press “enter”.

To verify, we will make a transaction. Let’s check a’s account balance:

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

You should see 90. Now we transfer $10 from a to b:

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 -c '{"Args":["invoke","a","b","10"]}'

Then, we check a’s account balance again:

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

You should see 80 (90 - 10 = 80). The Blockchain network is working!

Now exit the cil and turn down the network:

exit
./byfn.sh down

Later, to develop Smart Contract (Chaincode), we could use this BYFN to test the functionalities. And we could take advantage of such a virtual Docker network — turn up and down easily.

Thanks! If you like my stories, please follow me for new updates!

--

--

Sam LAM @ OriginBit

OriginBit Founder | HSUHK Lecturer | Gov Advisory Committee Member(SCSDITE) | GDG Organizer | MBuddhStud(HKU) | BEng(HKUST) | MCSE | MCP | CCNP | CEH