Skip to content

Commit f30aa5e

Browse files
committed
[FAB-9843] Improve e2e_cli multi-endorse
This CR removes the hard-coded invoke operation against peer0.org1 and peer0.org2, updating chaincodeInvoke to accept pairs of peer and org parameters to specify the peers to request endorsements from. This CR also ports a number of cleanup items from the BYFN scripts to make the script more readable (each peer operation now takes in a peer and org argument) and make the output more consistent. Change-Id: Idf41f642006af5a9d5d13e9311b03b4b2469a965 Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent 3f782c9 commit f30aa5e

File tree

1 file changed

+128
-85
lines changed

1 file changed

+128
-85
lines changed

examples/e2e_cli/scripts/script.sh

+128-85
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ verifyResult () {
3333
}
3434

3535
setGlobals () {
36-
37-
if [ $1 -eq 0 -o $1 -eq 1 ] ; then
36+
PEER=$1
37+
ORG=$2
38+
if [ $ORG -eq 1 ] ; then
3839
CORE_PEER_LOCALMSPID="Org1MSP"
3940
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
4041
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
41-
if [ $1 -eq 0 ]; then
42+
if [ $PEER -eq 0 ]; then
4243
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
4344
else
4445
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
@@ -47,7 +48,7 @@ setGlobals () {
4748
CORE_PEER_LOCALMSPID="Org2MSP"
4849
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
4950
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
50-
if [ $1 -eq 2 ]; then
51+
if [ $PEER -eq 0 ]; then
5152
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
5253
else
5354
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
@@ -58,7 +59,7 @@ setGlobals () {
5859
}
5960

6061
checkOSNAvailability() {
61-
#Use orderer's MSP for fetching system channel config block
62+
# Use orderer's MSP for fetching system channel config block
6263
CORE_PEER_LOCALMSPID="OrdererMSP"
6364
CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA
6465
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp
@@ -87,7 +88,7 @@ checkOSNAvailability() {
8788
}
8889

8990
createChannel() {
90-
setGlobals 0
91+
setGlobals 0 1
9192
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
9293
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
9394
else
@@ -96,67 +97,75 @@ createChannel() {
9697
res=$?
9798
cat log.txt
9899
verifyResult $res "Channel creation failed"
99-
echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
100+
echo "===================== Channel '$CHANNEL_NAME' created ===================== "
100101
echo
101102
}
102103

103104
updateAnchorPeers() {
104-
PEER=$1
105-
setGlobals $PEER
105+
PEER=$1
106+
ORG=$2
107+
setGlobals $PEER $ORG
106108

107-
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
109+
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
108110
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
109111
else
110112
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls --cafile $ORDERER_CA >&log.txt
111113
fi
112114
res=$?
113115
cat log.txt
114116
verifyResult $res "Anchor peer update failed"
115-
echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
117+
echo "===================== Anchor peers updated for org '$CORE_PEER_LOCALMSPID' on channel '$CHANNEL_NAME' ===================== "
116118
sleep 5
117119
echo
118120
}
119121

120122
## Sometimes Join takes time hence RETRY atleast for 5 times
121-
joinWithRetry () {
123+
joinChannelWithRetry () {
124+
PEER=$1
125+
ORG=$2
126+
setGlobals $PEER $ORG
127+
122128
peer channel join -b $CHANNEL_NAME.block >&log.txt
123129
res=$?
124130
cat log.txt
125131
if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
126132
COUNTER=` expr $COUNTER + 1`
127-
echo "PEER$1 failed to join the channel, Retry after 2 seconds"
133+
echo "peer${PEER}.org${ORG} failed to join the channel, Retry after 2 seconds"
128134
sleep 2
129-
joinWithRetry $1
135+
joinChannelWithRetry $1
130136
else
131137
COUNTER=1
132138
fi
133-
verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
139+
verifyResult $res "After $MAX_RETRY attempts, peer${PEER}.org${ORG} has failed to join channel '$CHANNEL_NAME' "
134140
}
135141

136142
joinChannel () {
137-
for ch in 0 1 2 3; do
138-
setGlobals $ch
139-
joinWithRetry $ch
140-
echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
141-
sleep 2
142-
echo
143+
for org in 1 2; do
144+
for peer in 0 1; do
145+
joinChannelWithRetry $peer $org
146+
echo "===================== peer${peer}.org${org} joined channel '$CHANNEL_NAME' ===================== "
147+
sleep 2
148+
echo
149+
done
143150
done
144151
}
145152

146153
installChaincode () {
147154
PEER=$1
148-
setGlobals $PEER
155+
ORG=$2
156+
setGlobals $PEER $ORG
149157
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/example02/cmd >&log.txt
150158
res=$?
151159
cat log.txt
152-
verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
153-
echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
160+
verifyResult $res "Chaincode installation on peer peer${PEER}.org${ORG} has Failed"
161+
echo "===================== Chaincode is installed on peer${PEER}.org${ORG} ===================== "
154162
echo
155163
}
156164

157165
instantiateChaincode () {
158166
PEER=$1
159-
setGlobals $PEER
167+
ORG=$2
168+
setGlobals $PEER $ORG
160169
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
161170
# lets supply it directly as we know it using the "-o" option
162171
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
@@ -166,100 +175,134 @@ instantiateChaincode () {
166175
fi
167176
res=$?
168177
cat log.txt
169-
verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
170-
echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
178+
verifyResult $res "Chaincode instantiation on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' failed"
179+
echo "===================== Chaincode is instantiated on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' ===================== "
171180
echo
172181
}
173182

174183
chaincodeQuery () {
175-
PEER=$1
176-
echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
177-
setGlobals $PEER
178-
local rc=1
179-
local starttime=$(date +%s)
180-
181-
# continue to poll
182-
# we either get a successful response, or reach TIMEOUT
183-
while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
184-
do
185-
sleep 3
186-
echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
187-
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
188-
test $? -eq 0 && VALUE=$(cat log.txt | egrep '^[0-9]+$')
189-
test "$VALUE" = "$2" && let rc=0
190-
done
191-
echo
192-
cat log.txt
193-
if test $rc -eq 0 ; then
194-
echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
195-
else
196-
echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
197-
echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
184+
PEER=$1
185+
ORG=$2
186+
setGlobals $PEER $ORG
187+
EXPECTED_RESULT=$3
188+
echo "===================== Querying on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME'... ===================== "
189+
local rc=1
190+
local starttime=$(date +%s)
191+
192+
# continue to poll
193+
# we either get a successful response, or reach TIMEOUT
194+
while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
195+
do
196+
sleep 3
197+
echo "Attempting to Query peer${PEER}.org${ORG} ...$(($(date +%s)-starttime)) secs"
198+
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
199+
test $? -eq 0 && VALUE=$(cat log.txt | egrep '^[0-9]+$')
200+
test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
201+
done
198202
echo
199-
exit 1
200-
fi
203+
cat log.txt
204+
if test $rc -eq 0 ; then
205+
echo "===================== Query successful on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' ===================== "
206+
else
207+
echo "!!!!!!!!!!!!!!! Query result on peer${PEER}.org${ORG} is INVALID !!!!!!!!!!!!!!!!"
208+
echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
209+
echo
210+
exit 1
211+
fi
212+
}
213+
214+
# parsePeerConnectionParameters $@
215+
# Helper function that takes the parameters from a chaincode operation
216+
# (e.g. invoke, query, instantiate) and checks for an even number of
217+
# peers and associated org, then sets $PEER_CONN_PARMS and $PEERS
218+
parsePeerConnectionParameters() {
219+
# check for uneven number of peer and org parameters
220+
if [ $(( $# % 2 )) -ne 0 ]; then
221+
exit 1
222+
fi
223+
224+
PEER_CONN_PARMS=""
225+
PEERS=""
226+
while [ "$#" -gt 0 ]; do
227+
PEER="peer$1.org$2"
228+
PEERS="$PEERS $PEER"
229+
PEER_CONN_PARMS="$PEER_CONN_PARMS --peerAddresses $PEER.example.com:7051"
230+
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "true" ]; then
231+
TLSINFO=$(eval echo "--tlsRootCertFiles \$PEER$1_ORG$2_CA")
232+
PEER_CONN_PARMS="$PEER_CONN_PARMS $TLSINFO"
233+
fi
234+
# shift by two to get the next pair of peer/org parameters
235+
shift; shift
236+
done
237+
# remove leading space for output
238+
PEERS="$(echo -e "$PEERS" | sed -e 's/^[[:space:]]*//')"
201239
}
202240

241+
# chaincodeInvoke <peer> <org> ...
242+
# Accepts as many peer/org pairs as desired and requests endorsement from each
203243
chaincodeInvoke () {
204-
PEER=$1
205-
setGlobals $PEER
206-
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
207-
# lets supply it directly as we know it using the "-o" option
244+
parsePeerConnectionParameters $@
245+
res=$?
246+
verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
247+
248+
# while 'peer chaincode' command can get the orderer endpoint from the
249+
# peer (if join was successful), let's supply it directly as we know
250+
# it using the "-o" option
208251
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
209-
peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:7051 -c '{"Args":["invoke","a","b","10"]}' >&log.txt
252+
peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}' >&log.txt
210253
else
211-
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $PEER0_ORG1_CA --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles $PEER0_ORG2_CA -c '{"Args":["invoke","a","b","10"]}' >&log.txt
254+
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}' >&log.txt
212255
fi
213256
res=$?
214257
cat log.txt
215258
verifyResult $res "Invoke execution on PEER$PEER failed "
216-
echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
259+
echo "===================== Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME' ===================== "
217260
echo
218261
}
219262

220-
## Check for orderering service availablility
263+
# Check for orderering service availablility
221264
echo "Check orderering service availability..."
222265
checkOSNAvailability
223266

224-
## Create channel
267+
# Create channel
225268
echo "Creating channel..."
226269
createChannel
227270

228-
## Join all the peers to the channel
271+
# Join all the peers to the channel
229272
echo "Having all peers join the channel..."
230273
joinChannel
231274

232-
## Set the anchor peers for each org in the channel
275+
# Set the anchor peers for each org in the channel
233276
echo "Updating anchor peers for org1..."
234-
updateAnchorPeers 0
277+
updateAnchorPeers 0 1
235278
echo "Updating anchor peers for org2..."
236-
updateAnchorPeers 2
279+
updateAnchorPeers 0 2
237280

238-
## Install chaincode on Peer0/Org1 and Peer2/Org2
239-
echo "Installing chaincode on org1/peer0..."
240-
installChaincode 0
241-
echo "Install chaincode on org2/peer0..."
242-
installChaincode 2
281+
# Install chaincode on peer0.org1 and peer2.org2
282+
echo "Installing chaincode on peer0.org1..."
283+
installChaincode 0 1
284+
echo "Install chaincode on peer0.org2..."
285+
installChaincode 0 2
243286

244-
#Instantiate chaincode on Peer2/Org2
245-
echo "Instantiating chaincode on org2/peer2..."
246-
instantiateChaincode 2
287+
# Instantiate chaincode on peer0.org2
288+
echo "Instantiating chaincode on peer0.org2..."
289+
instantiateChaincode 0 2
247290

248-
#Query on chaincode on Peer0/Org1
249-
echo "Querying chaincode on org1/peer0..."
250-
chaincodeQuery 0 100
291+
# Query on chaincode on peer0.org1
292+
echo "Querying chaincode on peer0.org1..."
293+
chaincodeQuery 0 1 100
251294

252-
#Invoke on chaincode on Peer0/Org1
253-
echo "Sending invoke transaction on org1/peer0..."
254-
chaincodeInvoke 0
295+
# Invoke on chaincode on peer0.org1 and peer0.org2
296+
echo "Sending invoke transaction on peer0.org1 and peer0.org2..."
297+
chaincodeInvoke 0 1 0 2
255298

256-
## Install chaincode on Peer3/Org2
257-
echo "Installing chaincode on org2/peer3..."
258-
installChaincode 3
299+
# Install chaincode on peer1.org2
300+
echo "Installing chaincode on peer1.org2..."
301+
installChaincode 1 2
259302

260-
#Query on chaincode on Peer3/Org2, check if the result is 90
261-
echo "Querying chaincode on org2/peer3..."
262-
chaincodeQuery 3 90
303+
# Query on chaincode on peer1.org2, check if the result is 90
304+
echo "Querying chaincode on peer1.org2..."
305+
chaincodeQuery 1 2 90
263306

264307
echo
265308
echo "===================== All GOOD, End-2-End execution completed ===================== "

0 commit comments

Comments
 (0)