Observing Channel and Chaincode in Hyperledger Fabric

Overview

Quick Review on Test Network Script

Bring Up Containers

./network.sh up -s couchdb

Create Channels

./network.sh createChannel -c <channel_name>
./network.sh up createChannel

Deploy Chaincodes

./network.sh deployCC -ccn <chaincode name> -ccp <chaincode path> -c <channel_name>

Tear Down Test Network

./network.sh down

Prepare Two Terminals

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
export PATH=$PATH:$PWD/../bin/export FABRIC_CFG_PATH=$PWD/../config/
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
export PATH=$PATH:$PWD/../bin/export FABRIC_CFG_PATH=$PWD/../config/
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
source terminalorg1
source terminalorg2

Baseline Setup: One Channel and One Chaincode

Bring up Test Network and mychannel

cd fabric-samples/test-network
./network.sh up createChannel -s couchdb
Five containers are running: one orderer, two peers, and one Couch DB for each peer
peer0.org1.example.com joined mychannel
peer0.org2.example.com joined mychannel

Deploy chaincode

./network.sh deployCC -ccn mycc -ccp ../chaincode/sacc

Test chaincode

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C mychannel -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Alice"]}'
Invoke chaincode function to set value for “name”
peer chaincode query -C mychannel -n mycc -c '{"Args":["get","name"]}'
peer0.org1.example.com
peer0.org2.example.com

Observation

Two chaincode containers are running
Each chaincode container is pairing to a peer, associated to the chaincode name.
One database handling the state for one channel and one chaincode

Clean up deployment

./network.sh down

Two channels with the same chaincode

Bring up Test Network and two channels

cd fabric-samples/test-network
./network.sh up -s couchdb
./network.sh createChannel -c channelone
./network.sh createChannel -c channeltwo
peer0.org1.example.com joined channelone and channeltwo
peer0.org2.example.com joined channelone and channeltwo

Deploy chaincode to the two channels

./network.sh deployCC -ccn mycc -ccp ../chaincode/sacc -c channelone
peer lifecycle chaincode queryinstalled
peer lifecycle chaincode approveformyorg --tls --cafile $ORDERER_CA -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID channeltwo --name mycc --version 1 --sequence 1 --waitForEvent --package-id mycc_1.0:366663ea5345d035a3a8aca36db3abe45806c5be6cce5f30fdf9ce81410d7fa5
Approval for Org1 (on Terminal for Org1)
Approval for Org2 (on Terminal for Org2)
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile $ORDERER_CA --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations//peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --channelID channeltwo --name mycc --version 1 --sequence 1
Commit chaincode to channeltwo
peer lifecycle chaincode querycommitted --channelID channelone --name mycc --output jsonpeer lifecycle chaincode querycommitted --channelID channeltwo --name mycc --output json
For channelone
For channeltwo

Test chaincode

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C channelone -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Alice"]}'
peer chaincode query -C channelone -n mycc -c '{"Args":["get","name"]}'
Terminal for Org1
Terminal for Org2
peer chaincode query -C channeltwo -n mycc -c '{"Args":["get","name"]}'
Terminal for Org1
Terminal for Org2
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C channeltwo -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Bob"]}'
peer chaincode query -C channelone -n mycc -c '{"Args":["get","name"]}'peer chaincode query -C channeltwo -n mycc -c '{"Args":["get","name"]}'
Terminal for Org1
Terminal for Org2

Observation

Two chaincode containers are running
Each chaincode container is pairing to a peer, associated to the chaincode name, without channel specific information
Two databases handling the state when two channels are created

Clean up deployment

./network.sh down

Same chaincode deployed twice in a channel

Bring up Test Network and mychannel

cd fabric-samples/test-network
./network.sh up createChannel -s couchdb
./network.sh deployCC -ccn mycc1 -ccp ../chaincode/sacc./network.sh deployCC -ccn mycc2 -ccp ../chaincode/sacc

Test chaincode

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C mychannel -n mycc1 --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Alice"]}'peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C mychannel -n mycc2 --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Bob"]}'
peer chaincode query -C mychannel -n mycc1 -c '{"Args":["get","name"]}'peer chaincode query -C mychannel -n mycc2 -c '{"Args":["get","name"]}'
Terminal for Org1
Terminal for Org2

Observation

Four chaincode containers are running
Each chaincode container is pairing to a peer and a chaincode name. Therefore we have four here.
Two databases handling the state when two chaincodes are deployed

Clean up deployment

./network.sh down

Summary

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