Once we have started the Fabric network, we need to execute a series of commands to interact with the blockchain. You may already notice that we have a command configuration in the CLI section of the docker-compose-cli.yaml:
/bin/bash -c './scripts/script.sh ${CHANNEL_NAME} ${DELAY}; sleep $TIMEOUT'
Since there are six peers and three organizations, when we call the above commands, we don’t want to hardcode the peers and organizations inside any functions. To avoid ending with a cumbersome script, we can simply pass the peer and organization parameters to a script function and trigger the peer-related Fabric command. Here is an example of scripts/script.sh to join all peers to a channel and install Chaincode for the consumer and retailer peer nodes.
#check script.sh utils.sh
joinChannel () {
for org in 1 2 3; do
for peer in 0 1; do
joinChannelWithRetry $peer $org
echo "======= peer${peer}.org${org} joined on the channel \"$CHANNEL_NAME\" ============ "
sleep $DELAY
echo
done
done
}
joinChannelWithRetry () {
PEER=$1
ORG=$2
setGlobals $PEER $ORG
peer channel join -b $CHANNEL_NAME.block >&log.txt
..
}
echo "Installing Chaincode on consumer peer: peer0.org1..."
installChaincode 0 1
echo "Installing Chaincode on retailer peer: peer1.org1..."
installChaincode 1 1
..
#function for install Chaincode, check utils.sh
installChaincode () {
PEER=$1
ORG=$2
setGlobals $PEER $ORG
peer Chaincode install -n fsccc -v 1.0 -l ${LANGUAGE} -p github.com/Chaincode/foodcontract >&log.txt
res=$?
cat log.txt
verifyResult $res "Chaincode installation on peer${PEER}.org${ORG} has Failed"
echo "================== Chaincode is installed on remote peer${PEER}.org${ORG} ================= "
echo
}
We can also define additional important environment variables such as the following (an example when interacting with Peer 0 of Organization 2):
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.fsc.com/users/Admin@org2.fsc.com/msp
CORE_PEER_ADDRESS=peer0.org2.fsc.com:7051
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.fsc.com/peers/peer0.org2.fsc.com/tls/ca.crt
However, to automate the process and to avoid handling environment variables from the CLI, I have defined a utils.sh script located under the scripts/ folder which we will be using later in this chapter.