From c830f5b6cda88963e77b0354f622e78d62c8e3ff Mon Sep 17 00:00:00 2001 From: Chris Elder Date: Fri, 6 Oct 2023 14:24:13 -0400 Subject: [PATCH] Add chaincode utilities This change is meant to help cover end user functionality that was previously covered by the blockchain VSCode Plugin. Functions added: - cc mode with package, list chaincode, invoke and query functions - auto sequencing for chaincode deployment - move variables into config file Signed-off-by: Chris Elder --- test-network/network.config | 58 ++++++++++ test-network/network.sh | 140 ++++++++++++++++++------ test-network/scripts/ccutils.sh | 172 +++++++++++++++++++++++++++++- test-network/scripts/deployCC.sh | 79 ++------------ test-network/scripts/packageCC.sh | 103 ++++++++++++++++++ test-network/scripts/utils.sh | 91 ++++++++++++++-- 6 files changed, 533 insertions(+), 110 deletions(-) create mode 100644 test-network/network.config create mode 100755 test-network/scripts/packageCC.sh diff --git a/test-network/network.config b/test-network/network.config new file mode 100644 index 0000000000..ec107bbe62 --- /dev/null +++ b/test-network/network.config @@ -0,0 +1,58 @@ +# default image tag, example: "2.5.4". "default" will download the latest. (-i) +IMAGETAG="default" + +# default ca image tag, example: "1.5.7". "default" will download the latest. (-cai) +CA_IMAGETAG="default" + +# Using crpto vs CA. default is cryptogen +CRYPTO="cryptogen" + +# max number of retries before giving up (-r) +MAX_RETRY=5 + +# default for delay between commands (-d) +CLI_DELAY=3 + +# channel name defaults to "mychannel" (-c) +CHANNEL_NAME="mychannel" + +# default database (-s) +DATABASE="leveldb" + +# default org (-org) +ORG=1 + +# chaincode language defaults to "NA" (-ccl) +CC_SRC_LANGUAGE="go" + +# Chaincode version (-ccv) +CC_VERSION="1.0.1" + +# chaincode name defaults to "NA" (-ccn) +CC_NAME="basic" + +# default to running the docker commands for the CCAAS (-ccaasdocker) +CCAAS_DOCKER_RUN=true + +# chaincode path defaults to "NA" (-ccp) +CC_SRC_PATH="../asset-transfer-basic/chaincode-go" + +# endorsement policy defaults to "NA". This would allow chaincodes to use the majority default policy. (-ccep) +CC_END_POLICY="NA" + +# collection configuration defaults to "NA" (-cccg) +CC_COLL_CONFIG="NA" + +# chaincode init function defaults to "NA" (-cci) +CC_INIT_FCN="NA" + +# Chaincode definition sequence, this should be an integer or auto (-ccs) +CC_SEQUENCE=auto + +# Default constructor for testing a chaincode invoke (-ccic) +CC_INVOKE_CONSTRUCTOR=''{\"Args\":[\"InitLedger\"]}'' + +# Default constructor for testing a chaincode query (-cciq) +CC_QUERY_CONSTRUCTOR=''{\"Args\":[\"GetAllAssets\"]}'' + + diff --git a/test-network/network.sh b/test-network/network.sh index c790f1ae45..e673855336 100755 --- a/test-network/network.sh +++ b/test-network/network.sh @@ -351,6 +351,66 @@ function deployCCAAS() { fi } +## Call the script to package the chaincode +function packageChaincode() { + + infoln "Packaging chaincode" + + scripts/packageCC.sh $CC_NAME $CC_SRC_PATH $CC_SRC_LANGUAGE $CC_VERSION true + + if [ $? -ne 0 ]; then + fatalln "Packaging the chaincode failed" + fi + +} + +## Call the script to list installed and committed chaincode on a peer +function listChaincode() { + + export FABRIC_CFG_PATH=${PWD}/../config + + . scripts/envVar.sh + . scripts/ccutils.sh + + setGlobals $ORG + + println + queryInstalledOnPeer + println + + listAllCommitted + +} + +## Call the script to invoke +function invokeChaincode() { + + export FABRIC_CFG_PATH=${PWD}/../config + + . scripts/envVar.sh + . scripts/ccutils.sh + + setGlobals $ORG + + chaincodeInvoke $ORG $CHANNEL_NAME $CC_NAME $CC_INVOKE_CONSTRUCTOR + +} + +## Call the script to query chaincode +function queryChaincode() { + + export FABRIC_CFG_PATH=${PWD}/../config + + . scripts/envVar.sh + . scripts/ccutils.sh + + setGlobals $ORG + + chaincodeQuery $ORG $CHANNEL_NAME $CC_NAME $CC_QUERY_CONSTRUCTOR + +} + + # Tear down running network function networkDown() { local temp_compose=$COMPOSE_FILE_BASE @@ -396,25 +456,8 @@ function networkDown() { fi } -# Using crpto vs CA. default is cryptogen -CRYPTO="cryptogen" -# timeout duration - the duration the CLI should wait for a response from -# another container before giving up -MAX_RETRY=5 -# default for delay between commands -CLI_DELAY=3 -# channel name defaults to "mychannel" -CHANNEL_NAME="mychannel" -# chaincode name defaults to "NA" -CC_NAME="NA" -# chaincode path defaults to "NA" -CC_SRC_PATH="NA" -# endorsement policy defaults to "NA". This would allow chaincodes to use the majority default policy. -CC_END_POLICY="NA" -# collection configuration defaults to "NA" -CC_COLL_CONFIG="NA" -# chaincode init function defaults to "NA" -CC_INIT_FCN="NA" +. ./network.config + # use this as the default docker-compose yaml definition COMPOSE_FILE_BASE=compose-test-net.yaml # docker-compose.yaml file if you are using couchdb @@ -428,16 +471,6 @@ COMPOSE_FILE_ORG3_COUCH=compose-couch-org3.yaml # certificate authorities compose file COMPOSE_FILE_ORG3_CA=compose-ca-org3.yaml # -# chaincode language defaults to "NA" -CC_SRC_LANGUAGE="NA" -# default to running the docker commands for the CCAAS -CCAAS_DOCKER_RUN=true -# Chaincode version -CC_VERSION="1.0" -# Chaincode definition sequence -CC_SEQUENCE=1 -# default database -DATABASE="leveldb" # Get docker sock path from environment variable SOCK="${DOCKER_HOST:-/var/run/docker.sock}" @@ -457,15 +490,29 @@ else shift fi -# parse a createChannel subcommand if used +## if no parameters are passed, show the help for cc +if [ "$MODE" == "cc" ] && [[ $# -lt 1 ]]; then + printHelp $MODE + exit 0 +fi + +# parse subcommands if used if [[ $# -ge 1 ]] ; then key="$1" + # check for the createChannel subcommand if [[ "$key" == "createChannel" ]]; then export MODE="createChannel" shift + # check for the cc command + elif [[ "$MODE" == "cc" ]]; then + if [ "$1" != "-h" ]; then + export SUBCOMMAND=$key + shift + fi fi fi + # parse flags while [[ $# -ge 1 ]] ; do @@ -539,6 +586,26 @@ while [[ $# -ge 1 ]] ; do -verbose ) VERBOSE=true ;; + -org ) + ORG="$2" + shift + ;; + -i ) + IMAGETAG="$2" + shift + ;; + -cai ) + CA_IMAGETAG="$2" + shift + ;; + -ccic ) + CC_INVOKE_CONSTRUCTOR="$2" + shift + ;; + -ccqc ) + CC_QUERY_CONSTRUCTOR="$2" + shift + ;; * ) errorln "Unknown flag: $key" printHelp @@ -566,7 +633,10 @@ else fi # Determine mode of operation and printing out what we asked for -if [ "$MODE" == "up" ]; then +if [ "$MODE" == "prereq" ]; then + infoln "Installing binaries and fabric images. Fabric Version: ${IMAGETAG} Fabric CA Version: ${CA_IMAGETAG}" + installPrereqs +elif [ "$MODE" == "up" ]; then infoln "Starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE}' ${CRYPTO_MODE}" networkUp elif [ "$MODE" == "createChannel" ]; then @@ -586,6 +656,14 @@ elif [ "$MODE" == "deployCC" ]; then elif [ "$MODE" == "deployCCAAS" ]; then infoln "deploying chaincode-as-a-service on channel '${CHANNEL_NAME}'" deployCCAAS +elif [ "$MODE" == "cc" ] && [ "$SUBCOMMAND" == "package" ]; then + packageChaincode +elif [ "$MODE" == "cc" ] && [ "$SUBCOMMAND" == "list" ]; then + listChaincode +elif [ "$MODE" == "cc" ] && [ "$SUBCOMMAND" == "invoke" ]; then + invokeChaincode +elif [ "$MODE" == "cc" ] && [ "$SUBCOMMAND" == "query" ]; then + queryChaincode else printHelp exit 1 diff --git a/test-network/scripts/ccutils.sh b/test-network/scripts/ccutils.sh index 3ad183f400..bc7f3bb0a3 100644 --- a/test-network/scripts/ccutils.sh +++ b/test-network/scripts/ccutils.sh @@ -1,6 +1,5 @@ #!/bin/bash - # installChaincode PEER ORG function installChaincode() { ORG=$1 @@ -173,4 +172,175 @@ function chaincodeQuery() { else fatalln "After $MAX_RETRY attempts, Query result on peer0.org${ORG} is INVALID!" fi +} + +function resolveSequence() { + + #if the sequence is not "auto", then use the provided sequence + if [[ "${CC_SEQUENCE}" != "auto" ]]; then + return 0 + fi + + local rc=1 + local COUNTER=1 + # first, find the sequence number of the committed chaincode + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + set -x + COMMITTED_CC_SEQUENCE=$(peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME} | sed -n "/Version:/{s/.*Sequence: //; s/, Endorsement Plugin:.*$//; p;}") + res=$? + { set +x; } 2>/dev/null + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + + # if there are no committed versions, then set the sequence to 1 + if [ -z $COMMITTED_CC_SEQUENCE ]; then + CC_SEQUENCE=1 + return 0 + fi + + rc=1 + COUNTER=1 + # next, find the sequence number of the approved chaincode + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + set -x + APPROVED_CC_SEQUENCE=$(peer lifecycle chaincode queryapproved --channelID $CHANNEL_NAME --name ${CC_NAME} | sed -n "/sequence:/{s/^sequence: //; s/, version:.*$//; p;}") + res=$? + { set +x; } 2>/dev/null + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + + # if the committed sequence and the approved sequence match, then increment the sequence + # otherwise, use the approved sequence + if [ $COMMITTED_CC_SEQUENCE == $APPROVED_CC_SEQUENCE ]; then + CC_SEQUENCE=$((COMMITTED_CC_SEQUENCE+1)) + else + CC_SEQUENCE=$APPROVED_CC_SEQUENCE + fi + +} + +#. scripts/envVar.sh + +queryInstalledOnPeer() { + + local rc=1 + local COUNTER=1 + # continue to poll + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + #sleep $DELAY + #infoln "Attempting to list on peer0.org${ORG}, Retry after $DELAY seconds." + peer lifecycle chaincode queryinstalled >&log.txt + res=$? + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + cat log.txt +} + +queryCommittedOnChannel() { + CHANNEL=$1 + local rc=1 + local COUNTER=1 + # continue to poll + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + #sleep $DELAY + #infoln "Attempting to list on peer0.org${ORG}, Retry after $DELAY seconds." + peer lifecycle chaincode querycommitted -C $CHANNEL >&log.txt + res=$? + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + cat log.txt + if test $rc -ne 0; then + fatalln "After $MAX_RETRY attempts, Failed to retrieve committed chaincode!" + fi + +} + +## Function to list chaincodes installed on the peer and committed chaincode visible to the org +listAllCommitted() { + + local rc=1 + local COUNTER=1 + # continue to poll + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + CHANNEL_LIST=$(peer channel list | sed '1,1d') + res=$? + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + if test $rc -eq 0; then + for channel in $CHANNEL_LIST + do + queryCommittedOnChannel "$channel" + done + else + fatalln "After $MAX_RETRY attempts, Failed to retrieve committed chaincode!" + fi + +} + +chaincodeInvoke() { + ORG=$1 + CHANNEL=$2 + CC_NAME=$3 + CC_INVOKE_CONSTRUCTOR=$4 + + infoln "Invoking on peer0.org${ORG} on channel '$CHANNEL_NAME'..." + local rc=1 + local COUNTER=1 + # continue to poll + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + sleep $DELAY + infoln "Attempting to Invoke on peer0.org${ORG}, Retry after $DELAY seconds." + set -x + peer chaincode invoke -o localhost:7050 -C $CHANNEL_NAME -n ${CC_NAME} -c ${CC_INVOKE_CONSTRUCTOR} --tls --cafile $ORDERER_CA --peerAddresses localhost:7051 --tlsRootCertFiles $PEER0_ORG1_CA --peerAddresses localhost:9051 --tlsRootCertFiles $PEER0_ORG2_CA >&log.txt + res=$? + { set +x; } 2>/dev/null + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + cat log.txt + if test $rc -eq 0; then + successln "Invoke successful on peer0.org${ORG} on channel '$CHANNEL_NAME'" + else + fatalln "After $MAX_RETRY attempts, Invoke result on peer0.org${ORG} is INVALID!" + fi +} + +chaincodeQuery() { + ORG=$1 + CHANNEL=$2 + CC_NAME=$3 + CC_QUERY_CONSTRUCTOR=$4 + + infoln "Querying on peer0.org${ORG} on channel '$CHANNEL_NAME'..." + local rc=1 + local COUNTER=1 + # continue to poll + # we either get a successful response, or reach MAX RETRY + while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do + sleep $DELAY + infoln "Attempting to Query peer0.org${ORG}, Retry after $DELAY seconds." + set -x + peer chaincode query -C $CHANNEL_NAME -n ${CC_NAME} -c ${CC_QUERY_CONSTRUCTOR} >&log.txt + res=$? + { set +x; } 2>/dev/null + let rc=$res + COUNTER=$(expr $COUNTER + 1) + done + cat log.txt + if test $rc -eq 0; then + successln "Query successful on peer0.org${ORG} on channel '$CHANNEL_NAME'" + else + fatalln "After $MAX_RETRY attempts, Query result on peer0.org${ORG} is INVALID!" + fi } \ No newline at end of file diff --git a/test-network/scripts/deployCC.sh b/test-network/scripts/deployCC.sh index 7dbfa67f9d..31e66925fc 100755 --- a/test-network/scripts/deployCC.sh +++ b/test-network/scripts/deployCC.sh @@ -29,66 +29,6 @@ println "- DELAY: ${C_GREEN}${DELAY}${C_RESET}" println "- MAX_RETRY: ${C_GREEN}${MAX_RETRY}${C_RESET}" println "- VERBOSE: ${C_GREEN}${VERBOSE}${C_RESET}" -FABRIC_CFG_PATH=$PWD/../config/ - -#User has not provided a name -if [ -z "$CC_NAME" ] || [ "$CC_NAME" = "NA" ]; then - fatalln "No chaincode name was provided. Valid call example: ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go" - -# User has not provided a path -elif [ -z "$CC_SRC_PATH" ] || [ "$CC_SRC_PATH" = "NA" ]; then - fatalln "No chaincode path was provided. Valid call example: ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go" - -# User has not provided a language -elif [ -z "$CC_SRC_LANGUAGE" ] || [ "$CC_SRC_LANGUAGE" = "NA" ]; then - fatalln "No chaincode language was provided. Valid call example: ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go" - -## Make sure that the path to the chaincode exists -elif [ ! -d "$CC_SRC_PATH" ] && [ ! -f "$CC_SRC_PATH" ]; then - fatalln "Path to chaincode does not exist. Please provide different path." -fi - -CC_SRC_LANGUAGE=$(echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:]) - -# do some language specific preparation to the chaincode before packaging -if [ "$CC_SRC_LANGUAGE" = "go" ]; then - CC_RUNTIME_LANGUAGE=golang - - infoln "Vendoring Go dependencies at $CC_SRC_PATH" - pushd $CC_SRC_PATH - GO111MODULE=on go mod vendor - popd - successln "Finished vendoring Go dependencies" - -elif [ "$CC_SRC_LANGUAGE" = "java" ]; then - CC_RUNTIME_LANGUAGE=java - - rm -rf $CC_SRC_PATH/build/install/ - infoln "Compiling Java code..." - pushd $CC_SRC_PATH - ./gradlew installDist - popd - successln "Finished compiling Java code" - CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME - -elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then - CC_RUNTIME_LANGUAGE=node - -elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then - CC_RUNTIME_LANGUAGE=node - - infoln "Compiling TypeScript code into JavaScript..." - pushd $CC_SRC_PATH - npm install - npm run build - popd - successln "Finished compiling TypeScript code into JavaScript" - -else - fatalln "The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script. Supported chaincode languages are: go, java, javascript, and typescript" - exit 1 -fi - INIT_REQUIRED="--init-required" # check if the init fcn should be called if [ "$CC_INIT_FCN" = "NA" ]; then @@ -107,21 +47,12 @@ else CC_COLL_CONFIG="--collections-config $CC_COLL_CONFIG" fi +FABRIC_CFG_PATH=$PWD/../config/ + # import utils . scripts/envVar.sh . scripts/ccutils.sh -packageChaincode() { - set -x - peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt - res=$? - PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid ${CC_NAME}.tar.gz) - { set +x; } 2>/dev/null - cat log.txt - verifyResult $res "Chaincode packaging has failed" - successln "Chaincode is packaged" -} - function checkPrereqs() { jq --version > /dev/null 2>&1 @@ -138,7 +69,9 @@ function checkPrereqs() { checkPrereqs ## package the chaincode -packageChaincode +./scripts/packageCC.sh $CC_NAME $CC_SRC_PATH $CC_SRC_LANGUAGE $CC_VERSION + +PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid ${CC_NAME}.tar.gz) ## Install chaincode on peer0.org1 and peer0.org2 infoln "Installing chaincode on peer0.org1..." @@ -146,6 +79,8 @@ installChaincode 1 infoln "Install chaincode on peer0.org2..." installChaincode 2 +resolveSequence + ## query whether the chaincode is installed queryInstalled 1 diff --git a/test-network/scripts/packageCC.sh b/test-network/scripts/packageCC.sh new file mode 100755 index 0000000000..1b3d6b4091 --- /dev/null +++ b/test-network/scripts/packageCC.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +#!/bin/bash + +source scripts/utils.sh + +CC_NAME=${1} +CC_SRC_PATH=${2} +CC_SRC_LANGUAGE=${3} +CC_VERSION=${4} +CC_PACKAGE_ONLY=${5:-false} + +println "executing with the following" +println "- CC_NAME: ${C_GREEN}${CC_NAME}${C_RESET}" +println "- CC_SRC_PATH: ${C_GREEN}${CC_SRC_PATH}${C_RESET}" +println "- CC_SRC_LANGUAGE: ${C_GREEN}${CC_SRC_LANGUAGE}${C_RESET}" +println "- CC_VERSION: ${C_GREEN}${CC_VERSION}${C_RESET}" + +FABRIC_CFG_PATH=$PWD/../config/ + +#User has not provided a name +if [ -z "$CC_NAME" ] || [ "$CC_NAME" = "NA" ]; then + fatalln "No chaincode name was provided. Valid call example: ./network.sh packageCC -ccn basic -ccp chaincode/asset-transfer-basic/chaincode-go -ccv 1.0.0 -ccl go" + +# User has not provided a path +elif [ -z "$CC_SRC_PATH" ] || [ "$CC_SRC_PATH" = "NA" ]; then + fatalln "No chaincode path was provided. Valid call example: ./network.sh packageCC -ccn basic -ccp chaincode/asset-transfer-basic/chaincode-go -ccv 1.0.0 -ccl go" + +# User has not provided a language +elif [ -z "$CC_SRC_LANGUAGE" ] || [ "$CC_SRC_LANGUAGE" = "NA" ]; then + fatalln "No chaincode language was provided. Valid call example: ./network.sh packageCC -ccn basic -ccp chaincode/asset-transfer-basic/chaincode-go -ccv 1.0.0 -ccl go" + +## Make sure that the path to the chaincode exists +elif [ ! -d "$CC_SRC_PATH" ]; then + fatalln "Path to chaincode does not exist. Please provide different path." +fi + +CC_SRC_LANGUAGE=$(echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:]) + +# do some language specific preparation to the chaincode before packaging +if [ "$CC_SRC_LANGUAGE" = "go" ]; then + CC_RUNTIME_LANGUAGE=golang + + infoln "Vendoring Go dependencies at $CC_SRC_PATH" + pushd $CC_SRC_PATH + GO111MODULE=on go mod vendor + popd + successln "Finished vendoring Go dependencies" + +elif [ "$CC_SRC_LANGUAGE" = "java" ]; then + CC_RUNTIME_LANGUAGE=java + + infoln "Compiling Java code..." + pushd $CC_SRC_PATH + ./gradlew installDist + popd + successln "Finished compiling Java code" + CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME + +elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then + CC_RUNTIME_LANGUAGE=node + +elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then + CC_RUNTIME_LANGUAGE=node + + infoln "Compiling TypeScript code into JavaScript..." + pushd $CC_SRC_PATH + npm install + npm run build + popd + successln "Finished compiling TypeScript code into JavaScript" + +else + fatalln "The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script. Supported chaincode languages are: go, java, javascript, and typescript" + exit 1 +fi + +verifyResult() { + if [ $1 -ne 0 ]; then + fatalln "$2" + fi +} + +packageChaincode() { + set -x + if [ ${CC_PACKAGE_ONLY} = true ] ; then + mkdir -p packagedChaincode + peer lifecycle chaincode package packagedChaincode/${CC_NAME}_${CC_VERSION}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt + else + peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt + fi + res=$? + { set +x; } 2>/dev/null + cat log.txt + PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid ${CC_NAME}.tar.gz) + verifyResult $res "Chaincode packaging has failed" + successln "Chaincode is packaged" +} + +## package the chaincode +packageChaincode + +exit 0 diff --git a/test-network/scripts/utils.sh b/test-network/scripts/utils.sh index 968654d8a6..73db6efb89 100755 --- a/test-network/scripts/utils.sh +++ b/test-network/scripts/utils.sh @@ -9,7 +9,18 @@ C_YELLOW='\033[1;33m' # Print the usage message function printHelp() { USAGE="$1" - if [ "$USAGE" == "up" ]; then + if [ "$USAGE" == "prereq" ]; then + println "Usage: " + println " network.sh [Flags]" + println " Modes:" + println " \033[0;32mprereq\033[0m - Install Fabric binaries and docker images" + println + println " Flags:" + println " Used with \033[0;32mnetwork.sh prereq\033[0m:" + println " -i FabricVersion (default: '2.5.4')" + println " -cai Fabric CA Version (default: '1.5.7')" + println + elif [ "$USAGE" == "up" ]; then println "Usage: " println " network.sh \033[0;32mup\033[0m [Flags]" println @@ -62,7 +73,7 @@ function printHelp() { println " -ccn - Chaincode name." println " -ccl - Programming language of chaincode to deploy: go, java, javascript, typescript" println " -ccv - Chaincode version. 1.0 (default), v2, version3.x, etc" - println " -ccs - Chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc" + println " -ccs - Chaincode definition sequence. Must be auto (default) or an integer, 1 , 2, 3, etc" println " -ccp - File path to the chaincode." println " -ccep - (Optional) Chaincode endorsement policy using signature policy syntax. The default policy requires an endorsement from Org1 and Org2" println " -cccg - (Optional) File path to private data collections configuration file" @@ -84,7 +95,7 @@ function printHelp() { println " -c - Name of channel to deploy chaincode to" println " -ccn - Chaincode name." println " -ccv - Chaincode version. 1.0 (default), v2, version3.x, etc" - println " -ccs - Chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc" + println " -ccs - Chaincode definition sequence. Must be auto (default) or an integer, 1 , 2, 3, etc" println " -ccp - File path to the chaincode. (used to find the dockerfile for building the docker image only)" println " -ccep - (Optional) Chaincode endorsement policy using signature policy syntax. The default policy requires an endorsement from Org1 and Org2" println " -cccg - (Optional) File path to private data collections configuration file" @@ -98,18 +109,59 @@ function printHelp() { println println " Examples:" println " network.sh deployCCAAS -ccn basicj -ccp ../asset-transfer-basic/chaincode-java" - println " network.sh deployCCAAS -ccn basict -ccp ../asset-transfer-basic/chaincode-typescript -ccaasdocker false" + println " network.sh deployCCAAS -ccn basict -ccp ../asset-transfer-basic/chaincode-typescript -ccaasdocker false" + elif [ "$USAGE" == "cc" ] ; then + println "Usage: " + println " network.sh cc [Flags]" + println + println " Modes:" + println " \033[0;32mlist\033[0m - list chaincodes installed on a peer and committed on a channel" + println " \033[0;32mpackage\033[0m - package a chaincode in tar format. Stores in directory packagedChaincode" + println " \033[0;32minvoke\033[0m - execute an invoke operation" + println " \033[0;32mquery\033[0m - execute an query operation" + println + println " Flags:" + println " -org - Org number for the executing the command (1,2,etc) (default is 1)." + println " -c - Name of channel" + println " -ccn - Chaincode name." + println " -ccl - Programming language of chaincode to deploy: go, java, javascript, typescript" + println " -ccv - Chaincode version. 1.0 (default), v2, version3.x, etc" + println " -ccp - File path to the chaincode." + println " -ccic - Chaincode invoke constructor." + println " -ccqc - Chaincode query constructor." + println " -h - Print this message" + println + println " Possible Mode and flag combinations" + println " \033[0;32mcc list\033[0m -org -verbose" + println " \033[0;32mcc package\033[0m -ccn -ccl -ccv -ccp -verbose" + println " \033[0;32mcc invoke\033[0m -org -c -ccic -verbose" + println " \033[0;32mcc query\033[0m -org -c -ccqc -verbose" + println + println " Examples:" + println " network.sh cc list -org 1" + println " network.sh cc package -ccn basic -ccp chaincode/asset-transfer-basic/go -ccv 1.0.0 -ccl go" + println " network.sh cc invoke -c channel1 -ccic '{\"Args\":[\"CreateAsset\",\"asset1\",\"red\",\"10\",\"fred\",\"500\"]}'" + println " network.sh cc query -c channel1 -ccqc '{\"Args\":[\"ReadAsset\",\"asset1\"]}'" + println + println " NOTE: Default settings can be changed in network.config" + println else println "Usage: " println " network.sh [Flags]" println " Modes:" + println " \033[0;32mprereq\033[0m - Install Fabric binaries and docker images" println " \033[0;32mup\033[0m - Bring up Fabric orderer and peer nodes. No channel is created" println " \033[0;32mup createChannel\033[0m - Bring up fabric network with one channel" println " \033[0;32mcreateChannel\033[0m - Create and join a channel after the network is created" println " \033[0;32mdeployCC\033[0m - Deploy a chaincode to a channel (defaults to asset-transfer-basic)" + println " \033[0;32mcc\033[0m - chaincode functions, use \"network.sh cc -h\" for options" println " \033[0;32mdown\033[0m - Bring down the network" println println " Flags:" + println " Used with \033[0;32mnetwork.sh prereq\033[0m" + println " -i FabricVersion (default: '2.5.4')" + println " -cai Fabric CA Version (default: '1.5.7')" + println println " Used with \033[0;32mnetwork.sh up\033[0m, \033[0;32mnetwork.sh createChannel\033[0m:" println " -ca - Use Certificate Authorities to generate network crypto material" println " -cfssl - Use CFSSL CA to generate network crypto material" @@ -120,12 +172,12 @@ function printHelp() { println " -d - CLI delays for a certain number of seconds (defaults to 3)" println " -verbose - Verbose mode" println - println " Used with \033[0;32mnetwork.sh deployCC\033[0m" + println " Used with \033[0;32mnetwork.sh deployCC\033[0m:" println " -c - Name of channel to deploy chaincode to" println " -ccn - Chaincode name." println " -ccl - Programming language of the chaincode to deploy: go, java, javascript, typescript" println " -ccv - Chaincode version. 1.0 (default), v2, version3.x, etc" - println " -ccs - Chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc" + println " -ccs - Chaincode definition sequence. Must be auto (default) or an integer, 1 , 2, 3, etc" println " -ccp - File path to the chaincode." println " -ccep - (Optional) Chaincode endorsement policy using signature policy syntax. The default policy requires an endorsement from Org1 and Org2" println " -cccg - (Optional) File path to private data collections configuration file" @@ -146,7 +198,34 @@ function printHelp() { println " network.sh createChannel -c channelName" println " network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-javascript/ -ccl javascript" println " network.sh deployCC -ccn mychaincode -ccp ./user/mychaincode -ccv 1 -ccl javascript" + println + println " NOTE: Default settings can be changed in network.config" + fi +} + +function installPrereqs() { + + infoln "installing prereqs" + + FILE=../install-fabric.sh + if [ ! -f $FILE ]; then + curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh + cp install-fabric.sh .. fi + + IMAGE_PARAMETER="" + if [ "$IMAGETAG" != "default" ]; then + IMAGE_PARAMETER="-f ${IMAGETAG}" + fi + + CA_IMAGE_PARAMETER="" + if [ "$CA_IMAGETAG" != "default" ]; then + CA_IMAGE_PARAMETER="-c ${CA_IMAGETAG}" + fi + + cd .. + ./install-fabric.sh ${IMAGE_PARAMETER} ${CA_IMAGE_PARAMETER} docker binary + } # println echos string