@@ -33,12 +33,13 @@ verifyResult () {
33
33
}
34
34
35
35
setGlobals () {
36
-
37
- if [ $1 -eq 0 -o $1 -eq 1 ] ; then
36
+ PEER=$1
37
+ ORG=$2
38
+ if [ $ORG -eq 1 ] ; then
38
39
CORE_PEER_LOCALMSPID=" Org1MSP"
39
40
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
40
41
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
42
43
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
43
44
else
44
45
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
@@ -47,7 +48,7 @@ setGlobals () {
47
48
CORE_PEER_LOCALMSPID=" Org2MSP"
48
49
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
49
50
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
51
52
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
52
53
else
53
54
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
@@ -58,7 +59,7 @@ setGlobals () {
58
59
}
59
60
60
61
checkOSNAvailability () {
61
- # Use orderer's MSP for fetching system channel config block
62
+ # Use orderer's MSP for fetching system channel config block
62
63
CORE_PEER_LOCALMSPID=" OrdererMSP"
63
64
CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA
64
65
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() {
87
88
}
88
89
89
90
createChannel () {
90
- setGlobals 0
91
+ setGlobals 0 1
91
92
if [ -z " $CORE_PEER_TLS_ENABLED " -o " $CORE_PEER_TLS_ENABLED " = " false" ]; then
92
93
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >& log.txt
93
94
else
@@ -96,67 +97,75 @@ createChannel() {
96
97
res=$?
97
98
cat log.txt
98
99
verifyResult $res " Channel creation failed"
99
- echo " ===================== Channel \" $CHANNEL_NAME \" is created successfully ===================== "
100
+ echo " ===================== Channel ' $CHANNEL_NAME ' created ===================== "
100
101
echo
101
102
}
102
103
103
104
updateAnchorPeers () {
104
- PEER=$1
105
- setGlobals $PEER
105
+ PEER=$1
106
+ ORG=$2
107
+ setGlobals $PEER $ORG
106
108
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
108
110
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID} anchors.tx >& log.txt
109
111
else
110
112
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
111
113
fi
112
114
res=$?
113
115
cat log.txt
114
116
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 ' ===================== "
116
118
sleep 5
117
119
echo
118
120
}
119
121
120
122
# # 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
+
122
128
peer channel join -b $CHANNEL_NAME .block >& log.txt
123
129
res=$?
124
130
cat log.txt
125
131
if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
126
132
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"
128
134
sleep 2
129
- joinWithRetry $1
135
+ joinChannelWithRetry $1
130
136
else
131
137
COUNTER=1
132
138
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 ' "
134
140
}
135
141
136
142
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
143
150
done
144
151
}
145
152
146
153
installChaincode () {
147
154
PEER=$1
148
- setGlobals $PEER
155
+ ORG=$2
156
+ setGlobals $PEER $ORG
149
157
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/example02/cmd >& log.txt
150
158
res=$?
151
159
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} ===================== "
154
162
echo
155
163
}
156
164
157
165
instantiateChaincode () {
158
166
PEER=$1
159
- setGlobals $PEER
167
+ ORG=$2
168
+ setGlobals $PEER $ORG
160
169
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
161
170
# lets supply it directly as we know it using the "-o" option
162
171
if [ -z " $CORE_PEER_TLS_ENABLED " -o " $CORE_PEER_TLS_ENABLED " = " false" ]; then
@@ -166,100 +175,134 @@ instantiateChaincode () {
166
175
fi
167
176
res=$?
168
177
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 ' ===================== "
171
180
echo
172
181
}
173
182
174
183
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
198
202
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:]]*//' ) "
201
239
}
202
240
241
+ # chaincodeInvoke <peer> <org> ...
242
+ # Accepts as many peer/org pairs as desired and requests endorsement from each
203
243
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
208
251
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
210
253
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
212
255
fi
213
256
res=$?
214
257
cat log.txt
215
258
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 ' ===================== "
217
260
echo
218
261
}
219
262
220
- # # Check for orderering service availablility
263
+ # Check for orderering service availablility
221
264
echo " Check orderering service availability..."
222
265
checkOSNAvailability
223
266
224
- # # Create channel
267
+ # Create channel
225
268
echo " Creating channel..."
226
269
createChannel
227
270
228
- # # Join all the peers to the channel
271
+ # Join all the peers to the channel
229
272
echo " Having all peers join the channel..."
230
273
joinChannel
231
274
232
- # # Set the anchor peers for each org in the channel
275
+ # Set the anchor peers for each org in the channel
233
276
echo " Updating anchor peers for org1..."
234
- updateAnchorPeers 0
277
+ updateAnchorPeers 0 1
235
278
echo " Updating anchor peers for org2..."
236
- updateAnchorPeers 2
279
+ updateAnchorPeers 0 2
237
280
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
243
286
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
247
290
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
251
294
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
255
298
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
259
302
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
263
306
264
307
echo
265
308
echo " ===================== All GOOD, End-2-End execution completed ===================== "
0 commit comments