matiru

Blockchain-Based Supply Chain Transparency for Agricultural Produce

fabric/network/scripts/createChannel.sh (2544B)


  1#!/usr/bin/env bash
  2
  3. scripts/envVar.sh
  4. scripts/utils.sh
  5
  6CHANNEL_NAME="$1"
  7DELAY="$2"
  8MAX_RETRY="$3"
  9VERBOSE="$4"
 10: ${CHANNEL_NAME:="mychannel"}
 11: ${DELAY:="3"}
 12: ${MAX_RETRY:="5"}
 13: ${VERBOSE:="false"}
 14
 15if [ ! -d "channel-artifacts" ]; then
 16	mkdir channel-artifacts
 17fi
 18
 19createChannelGenesisBlock() {
 20	which configtxgen
 21	if [ "$?" -ne 0 ]; then
 22		fatalln "configtxgen tool not found."
 23	fi
 24	set -x
 25	configtxgen -profile FourOrgsApplicationGenesis -outputBlock ./channel-artifacts/${CHANNEL_NAME}.block -channelID $CHANNEL_NAME
 26	res=$?
 27	{ set +x; } 2>/dev/null
 28  verifyResult $res "Failed to generate channel configuration transaction..."
 29}
 30
 31createChannel() {
 32	setGlobals 1
 33	local rc=1
 34	local COUNTER=1
 35	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
 36		sleep $DELAY
 37		set -x
 38		osnadmin channel join --channelID $CHANNEL_NAME --config-block ./channel-artifacts/${CHANNEL_NAME}.block -o localhost:7053 --ca-file "$ORDERER_CA" --client-cert "$ORDERER_ADMIN_TLS_SIGN_CERT" --client-key "$ORDERER_ADMIN_TLS_PRIVATE_KEY" >&log.txt
 39		res=$?
 40		{ set +x; } 2>/dev/null
 41		let rc=$res
 42		COUNTER=$(expr $COUNTER + 1)
 43	done
 44	cat log.txt
 45	verifyResult $res "Channel creation failed"
 46}
 47
 48joinChannel() {
 49  FABRIC_CFG_PATH=$PWD/../config/
 50  ORG=$1
 51  setGlobals $ORG
 52	local rc=1
 53	local COUNTER=1
 54	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
 55    sleep $DELAY
 56    set -x
 57    peer channel join -b $BLOCKFILE >&log.txt
 58    res=$?
 59    { set +x; } 2>/dev/null
 60		let rc=$res
 61		COUNTER=$(expr $COUNTER + 1)
 62	done
 63	cat log.txt
 64	verifyResult $res "After $MAX_RETRY attempts, peer0.org${ORG} has failed to join channel '$CHANNEL_NAME' "
 65}
 66
 67setAnchorPeer() {
 68  ORG=$1
 69  docker exec cli ./scripts/setAnchorPeer.sh $ORG $CHANNEL_NAME 
 70}
 71
 72FABRIC_CFG_PATH=${PWD}/configtx
 73
 74infoln "Generating channel genesis block '${CHANNEL_NAME}.block'"
 75createChannelGenesisBlock
 76
 77FABRIC_CFG_PATH=$PWD/../config/
 78BLOCKFILE="./channel-artifacts/${CHANNEL_NAME}.block"
 79
 80infoln "Creating channel ${CHANNEL_NAME}"
 81createChannel
 82successln "Channel '$CHANNEL_NAME' created"
 83
 84infoln "Joining org1 peer to the channel..."
 85joinChannel 1
 86infoln "Joining org2 peer to the channel..."
 87joinChannel 2
 88infoln "Joining org3 peer to the channel..."
 89joinChannel 3
 90infoln "Joining org4 peer to the channel..."
 91joinChannel 4
 92
 93infoln "Setting anchor peer for org1..."
 94setAnchorPeer 1
 95infoln "Setting anchor peer for org2..."
 96setAnchorPeer 2
 97infoln "Setting anchor peer for org3..."
 98setAnchorPeer 3
 99infoln "Setting anchor peer for org4..."
100setAnchorPeer 4
101
102successln "Channel '$CHANNEL_NAME' joined"