From 12de841f597f6a81f34aeb3301003b665891667f Mon Sep 17 00:00:00 2001 From: Baohua Yang Date: Fri, 25 Aug 2017 13:02:44 +0800 Subject: [PATCH] Update scripts --- hyperledger/1.0/Makefile | 14 +- hyperledger/1.0/README.md | 4 +- hyperledger/1.0/scripts/func.sh | 132 +++++++++++++- hyperledger/1.0/scripts/initialize.sh | 170 +----------------- ...test_cc_invoke_query.sh => test_cc_all.sh} | 0 .../{test_1peer.sh => test_cc_peer0.sh} | 14 +- 6 files changed, 152 insertions(+), 182 deletions(-) rename hyperledger/1.0/scripts/{test_cc_invoke_query.sh => test_cc_all.sh} (100%) rename hyperledger/1.0/scripts/{test_1peer.sh => test_cc_peer0.sh} (89%) diff --git a/hyperledger/1.0/Makefile b/hyperledger/1.0/Makefile index 484af38a..cdcd30da 100644 --- a/hyperledger/1.0/Makefile +++ b/hyperledger/1.0/Makefile @@ -13,7 +13,7 @@ all: make init sleep 3 - make test + make test_cc sleep 3 make stop @@ -26,7 +26,7 @@ ready: make init sleep 3 - make test + make test_cc @echo "Now the fabric network is ready to play" @echo "run 'make cli' to enter into the fabric-cli container." @echo "run 'make stop' when done." @@ -43,9 +43,13 @@ init: # initialize the fabric network @echo "Install and instantiate cc example02 on the fabric network" docker exec -it fabric-cli bash ./scripts/initialize.sh -test: # test chaincode - @echo "Invoke and query cc example02" - docker exec -it fabric-cli bash ./scripts/test_cc_invoke_query.sh +test_cc: # test chaincode + @echo "Invoke and query cc example02 on all peers" + docker exec -it fabric-cli bash ./scripts/test_cc_all.sh + +test_peer0: # test single peer + @echo "Invoke and query cc example02 on single peer0" + docker exec -it fabric-cli bash ./scripts/test_cc_peer0.sh qscc: # test qscc quries docker exec -it fabric-cli bash ./scripts/test_qscc.sh diff --git a/hyperledger/1.0/README.md b/hyperledger/1.0/README.md index c931a2f1..261ccc54 100644 --- a/hyperledger/1.0/README.md +++ b/hyperledger/1.0/README.md @@ -23,7 +23,7 @@ tldr :) * `make start` * `make init` -* `make test` +* `make test_cc` * `make stop` Otherwise, if u wanna know more or run the command manually, then go on reading the following part. @@ -105,7 +105,7 @@ c9974dbc21d9 dev-peer0.org1.example.com-mycc-1.0 "chaincode -peer.a..." ## Test Chaincode ```bash -$ make test # test invoke and query with chaincode +$ make test_cc # test invoke and query with chaincode ``` More details, see [chaincode test](docs/chaincode_test.md). diff --git a/hyperledger/1.0/scripts/func.sh b/hyperledger/1.0/scripts/func.sh index 0d2d0926..472c8710 100644 --- a/hyperledger/1.0/scripts/func.sh +++ b/hyperledger/1.0/scripts/func.sh @@ -15,9 +15,13 @@ fi CHANNEL_NAME="$1" : ${CHANNEL_NAME:="businesschannel"} + +CC_NAME=mycc + : ${TIMEOUT:="60"} COUNTER=1 MAX_RETRY=5 + ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem verifyResult () { @@ -54,6 +58,128 @@ setGlobals () { env |grep CORE } +checkOSNAvailability() { + #Use orderer's MSP for fetching system channel config block + CORE_PEER_LOCALMSPID="OrdererMSP" + CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA + CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp + + local rc=1 + local starttime=$(date +%s) + + # continue to poll + # we either get a successful response, or reach TIMEOUT + while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 + do + sleep 3 + echo "Attempting to fetch system channel 'testchainid' ...$(($(date +%s)-starttime)) secs" + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + peer channel fetch 0 -o orderer.example.com:7050 -c "testchainid" >&log.txt + else + peer channel fetch 0 -o orderer.example.com:7050 -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt + fi + test $? -eq 0 && VALUE=$(cat log.txt | awk '/Received block/ {print $NF}') + test "$VALUE" = "0" && let rc=0 + done + cat log.txt + verifyResult $rc "Ordering Service is not available, Please try again ..." + echo "===================== Ordering Service is up and running ===================== " + echo +} + +# Use peer0/org1 to create a channel +createChannel() { + setGlobals 0 + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --timeout $TIMEOUT >&log.txt + else + peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --timeout $TIMEOUT >&log.txt + fi + res=$? + cat log.txt + verifyResult $res "Channel creation failed" + echo_g "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== " + echo +} + +updateAnchorPeers() { + PEER=$1 + setGlobals $PEER + + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt + else + peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt + fi + res=$? + cat log.txt + verifyResult $res "Anchor peer update failed" + echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " + sleep 5 + echo +} + +## Sometimes Join takes time hence RETRY atleast for 5 times +joinWithRetry () { + peer channel join -b $CHANNEL_NAME.block >&log.txt + res=$? + cat log.txt + if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then + COUNTER=` expr $COUNTER + 1` + echo_b "PEER$1 failed to join the channel, Retry after 2 seconds" + sleep 2 + joinWithRetry $1 + else + COUNTER=1 + fi + verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel" +} + +# Join given (by default all) peers into the channel +joinChannel () { + peer_to_join=$(seq 0 3) + if [ $# -gt 0 ]; then + peer_to_join=$@ + fi + for i in $peer_to_join; do + setGlobals $i + joinWithRetry $i + echo_g "===================== PEER$i joined on the channel \"$CHANNEL_NAME\" ===================== " + sleep 2 + echo + done +} + +# Install chaincode on specifized peer node +installChaincode () { + PEER=$1 + setGlobals $PEER + peer chaincode install -n $CC_NAME -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt + res=$? + cat log.txt + verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed" + echo_g "===================== Chaincode is installed on remote peer PEER$PEER ===================== " + echo +} + +# Instantiate chaincode on specifized peer node +instantiateChaincode () { + PEER=$1 + setGlobals $PEER + # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful), + # lets supply it directly as we know it using the "-o" option + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n $CC_NAME -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt + else + peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n $CC_NAME -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt + fi + res=$? + cat log.txt + verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed" + echo_g "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== " + echo +} + chaincodeQuery () { PEER=$1 echo_b "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== " @@ -67,7 +193,7 @@ chaincodeQuery () { do sleep 3 echo_b "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs" - peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt + peer chaincode query -C $CHANNEL_NAME -n $CC_NAME -c '{"Args":["query","a"]}' >&log.txt test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}') test "$VALUE" = "$2" && let rc=0 done @@ -89,9 +215,9 @@ chaincodeInvoke () { # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful), # lets supply it directly as we know it using the "-o" option if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt + peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n $CC_NAME -c '{"Args":["invoke","a","b","10"]}' >&log.txt else - peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt + peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n $CC_NAME -c '{"Args":["invoke","a","b","10"]}' >&log.txt fi res=$? cat log.txt diff --git a/hyperledger/1.0/scripts/initialize.sh b/hyperledger/1.0/scripts/initialize.sh index c4f5fe99..4bf626e5 100644 --- a/hyperledger/1.0/scripts/initialize.sh +++ b/hyperledger/1.0/scripts/initialize.sh @@ -1,14 +1,10 @@ #!/bin/bash -# Detecting whether can import the header file to render colorful cli output -if [ -f ./header.sh ]; then - source ./header.sh -elif [ -f scripts/header.sh ]; then - source scripts/header.sh -else - alias echo_r="echo" - alias echo_g="echo" - alias echo_b="echo" +# Importing useful functions for cc testing +if [ -f ./func.sh ]; then + source ./func.sh +elif [ -f scripts/func.sh ]; then + source scripts/func.sh fi echo @@ -17,164 +13,8 @@ echo " ==========initialize businesschannel========== " echo " ============================================== " echo -CHANNEL_NAME="$1" -: ${CHANNEL_NAME:="businesschannel"} -: ${TIMEOUT:="60"} -COUNTER=1 -MAX_RETRY=5 -ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem - echo_b "Channel name : "$CHANNEL_NAME -verifyResult () { - if [ $1 -ne 0 ] ; then - echo_b "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!" - echo_r "================== ERROR !!! FAILED to execute End-2-End Scenario ==================" - echo - exit 1 - fi -} - -setGlobals () { - - if [ $1 -eq 0 -o $1 -eq 1 ] ; then - CORE_PEER_LOCALMSPID="Org1MSP" - 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 - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp - if [ $1 -eq 0 ]; then - CORE_PEER_ADDRESS=peer0.org1.example.com:7051 - else - CORE_PEER_ADDRESS=peer1.org1.example.com:7051 - fi - else - CORE_PEER_LOCALMSPID="Org2MSP" - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp - if [ $1 -eq 2 ]; then - CORE_PEER_ADDRESS=peer0.org2.example.com:7051 - else - CORE_PEER_ADDRESS=peer1.org2.example.com:7051 - fi - fi - - env |grep CORE -} - -checkOSNAvailability() { - #Use orderer's MSP for fetching system channel config block - CORE_PEER_LOCALMSPID="OrdererMSP" - CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp - - local rc=1 - local starttime=$(date +%s) - - # continue to poll - # we either get a successful response, or reach TIMEOUT - while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 - do - sleep 3 - echo "Attempting to fetch system channel 'testchainid' ...$(($(date +%s)-starttime)) secs" - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - peer channel fetch 0 -o orderer.example.com:7050 -c "testchainid" >&log.txt - else - peer channel fetch 0 -o orderer.example.com:7050 -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt - fi - test $? -eq 0 && VALUE=$(cat log.txt | awk '/Received block/ {print $NF}') - test "$VALUE" = "0" && let rc=0 - done - cat log.txt - verifyResult $rc "Ordering Service is not available, Please try again ..." - echo "===================== Ordering Service is up and running ===================== " - echo -} - -createChannel() { - setGlobals 0 - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --timeout $TIMEOUT >&log.txt - else - peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --timeout $TIMEOUT >&log.txt - fi - res=$? - cat log.txt - verifyResult $res "Channel creation failed" - echo_g "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== " - echo -} - -updateAnchorPeers() { - PEER=$1 - setGlobals $PEER - - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt - else - peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt - fi - res=$? - cat log.txt - verifyResult $res "Anchor peer update failed" - echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " - sleep 5 - echo -} - -## Sometimes Join takes time hence RETRY atleast for 5 times -joinWithRetry () { - peer channel join -b $CHANNEL_NAME.block >&log.txt - res=$? - cat log.txt - if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then - COUNTER=` expr $COUNTER + 1` - echo_b "PEER$1 failed to join the channel, Retry after 2 seconds" - sleep 2 - joinWithRetry $1 - else - COUNTER=1 - fi - verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel" -} - -joinChannel () { - for ch in 0 1 2 3; do - setGlobals $ch - joinWithRetry $ch - echo_g "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== " - sleep 2 - echo - done -} - -installChaincode () { - PEER=$1 - setGlobals $PEER - peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt - res=$? - cat log.txt - verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed" - echo_g "===================== Chaincode is installed on remote peer PEER$PEER ===================== " - echo -} - -instantiateChaincode () { - PEER=$1 - setGlobals $PEER - # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful), - # lets supply it directly as we know it using the "-o" option - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt - else - peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt - fi - res=$? - cat log.txt - verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed" - echo_g "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== " - echo -} - - ## Create channel echo_b "Creating channel..." createChannel diff --git a/hyperledger/1.0/scripts/test_cc_invoke_query.sh b/hyperledger/1.0/scripts/test_cc_all.sh similarity index 100% rename from hyperledger/1.0/scripts/test_cc_invoke_query.sh rename to hyperledger/1.0/scripts/test_cc_all.sh diff --git a/hyperledger/1.0/scripts/test_1peer.sh b/hyperledger/1.0/scripts/test_cc_peer0.sh similarity index 89% rename from hyperledger/1.0/scripts/test_1peer.sh rename to hyperledger/1.0/scripts/test_cc_peer0.sh index f6d315dd..675673d7 100644 --- a/hyperledger/1.0/scripts/test_1peer.sh +++ b/hyperledger/1.0/scripts/test_cc_peer0.sh @@ -13,25 +13,25 @@ echo_b "====================1.Create channel(default newchannel) =============== createChannel echo_b "====================2.Join pee0 to the channel ======================================" -joinChannel +joinChannel 0 echo_b "====================3.set anchor peers for org1 in the channel===========================" -updateAnchorPeers +updateAnchorPeers 0 echo_b "=====================4.Install chaincode test_cc on Peer0/Org0========================" -installChaincode +installChaincode 0 echo_b "=====================5.Instantiate chaincode, this will take a while, pls waiting...===" -instantiateChaincode +instantiateChaincode 0 echo_b "====================6.Query the existing value of a====================================" -chaincodeQuery 100 +chaincodeQuery 0 100 echo_b "=====================7.Invoke a transaction to transfer 10 from a to b==================" -chaincodeInvoke +chaincodeInvoke 0 echo_b "=====================8.Check if the result of a is 90===================================" -chaincodeQuery 90 +chaincodeQuery 0 90 echo echo_g "=====================9.All GOOD, MVE Test completed ===================== "