-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_statebasedep.sh
456 lines (402 loc) · 15.9 KB
/
example_statebasedep.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/bash
set -e
source $(dirname $0)/common.sh
curDir=$PWD
trap cleanup EXIT
function cleanup {
cd $curDir
}
ChannelName=my-ch
CCName=my-cc3
CollName=my-collection
fabricDir=$GOPATH/src/github.com/hyperledger/fabric/
artifactsDir=/tmp/$(basename $0).mat/
if test -d $artifactsDir
then
rm -rf $artifactsDir
fi
mkdir $artifactsDir
ordererOrg=myordererorg
applicationOrg=myapplicationorg
newApplicationOrg=mynewapplicationorg
ordererOrgDir=$artifactsDir/crypto-config/ordererOrganizations/$ordererOrg
applicationOrgDir=$artifactsDir/crypto-config/peerOrganizations/$applicationOrg
newApplicationOrgDir=$artifactsDir/crypto-config/peerOrganizations/$newApplicationOrg
genBlockMain=$artifactsDir/$ChannelName.block
ordBlockMain=$artifactsDir/$ChannelName-orderer.block
genTransMain=$artifactsDir/$ChannelName-channel.tx
CONFIGTXGEN_CMD=$fabricDir/.build/bin/configtxgen
CONFIGTXLTR_CMD=$fabricDir/.build/bin/configtxlator
PEER_CMD=$fabricDir/.build/bin/peer
CRYPTOGEN_CMD=$fabricDir/.build/bin/cryptogen
BLOCKPARSER_CMD=$(readlink -f $(dirname $0))/blockparser
configtxgenFile=$artifactsDir/configtx.yaml
cat <<- EOF > $configtxgenFile
---
Organizations:
- &OrdererOrg
Name: $ordererOrg
ID: $ordererOrg
MSPDir: $ordererOrgDir/msp
Policies:
Readers:
Type: Signature
Rule: "OR('$ordererOrg.member')"
Writers:
Type: Signature
Rule: "OR('$ordererOrg.member')"
Admins:
Type: Signature
Rule: "OR('$ordererOrg.admin')"
- &$applicationOrg
Name: $applicationOrg
ID: $applicationOrg
MSPDir: $applicationOrgDir/msp
Policies:
Readers:
Type: Signature
Rule: "OR('$applicationOrg.admin', '$applicationOrg.peer', '$applicationOrg.client')"
Writers:
Type: Signature
Rule: "OR('$applicationOrg.admin', '$applicationOrg.client')"
Admins:
Type: Signature
Rule: "OR('$applicationOrg.admin')"
AnchorPeers:
- Host: $applicationOrg
Port: 7051
- &$newApplicationOrg
Name: $newApplicationOrg
ID: $newApplicationOrg
MSPDir: $newApplicationOrgDir/msp
Policies:
Readers:
Type: Signature
Rule: "OR('$newApplicationOrg.admin', '$newApplicationOrg.peer', '$newApplicationOrg.client')"
Writers:
Type: Signature
Rule: "OR('$newApplicationOrg.admin', '$newApplicationOrg.client')"
Admins:
Type: Signature
Rule: "OR('$newApplicationOrg.admin')"
AnchorPeers:
- Host: $newApplicationOrg
Port: 7051
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_1: true
V1_2: true
V1_3: true
Application: &ApplicationDefaults
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ApplicationCapabilities
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- $ordererOrg:7050
BatchTimeout: 10ms
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"
Capabilities:
<<: *OrdererCapabilities
Channel: &ChannelDefaults
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ChannelCapabilities
Profiles:
TwoOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *$applicationOrg
- *$newApplicationOrg
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *$applicationOrg
- *$newApplicationOrg
EOF
cryptogenCfgFile=$artifactsDir/crypto-config.yaml
cat <<- EOF > $cryptogenCfgFile
OrdererOrgs:
- Name: $ordererOrg
Domain: $ordererOrg
Specs:
- Hostname: orderer
CommonName: orderer
PeerOrgs:
- Name: $applicationOrg
Domain: $applicationOrg
EnableNodeOUs: true
Specs:
- Hostname: peer
CommonName: peer
- Hostname: peer1
CommonName: peer1
Users:
Count: 1
- Name: $newApplicationOrg
Domain: $newApplicationOrg
EnableNodeOUs: true
Specs:
- Hostname: peer
CommonName: peer
Users:
Count: 1
EOF
dockerComposeFile=$artifactsDir/network.yaml
cat <<- EOF > $dockerComposeFile
version: '3.5'
services:
$ordererOrg:
container_name: $ordererOrg
image: hyperledger/fabric-orderer
networks:
- fabric-net
environment:
- ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_LOCALMSPID=$ordererOrg
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/genesis.block
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- $ordBlockMain:/var/hyperledger/orderer/genesis.block
- $ordererOrgDir/orderers/orderer/msp:/var/hyperledger/orderer/msp
ports:
- 7050:7050
$applicationOrg:
container_name: $applicationOrg
image: hyperledger/fabric-peer
networks:
- fabric-net
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric-net
- CORE_PEER_TLS_ENABLED=false
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_ID=$applicationOrg
- CORE_PEER_ADDRESS=$applicationOrg:7051
- CORE_PEER_CHAINCODEADDRESS=$applicationOrg:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_LOCALMSPID=$applicationOrg
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=$applicationOrg:7051
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- $applicationOrgDir/peers/peer/msp:/etc/hyperledger/fabric/msp
ports:
- 7051:7051
- 7052:7052
- 7053:7053
${applicationOrg}1:
container_name: ${applicationOrg}1
image: hyperledger/fabric-peer
networks:
- fabric-net
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric-net
- CORE_PEER_TLS_ENABLED=false
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_ID=$applicationOrg
- CORE_PEER_ADDRESS=${applicationOrg}1:7051
- CORE_PEER_CHAINCODEADDRESS=${applicationOrg}1:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_LOCALMSPID=$applicationOrg
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=${applicationOrg}1:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=$applicationOrg:7051
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- $applicationOrgDir/peers/peer1/msp:/etc/hyperledger/fabric/msp
ports:
- 8051:7051
- 8052:7052
- 8053:7053
$newApplicationOrg:
container_name: $newApplicationOrg
image: hyperledger/fabric-peer
networks:
- fabric-net
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric-net
- CORE_PEER_TLS_ENABLED=false
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_ID=$newApplicationOrg
- CORE_PEER_ADDRESS=$newApplicationOrg:7051
- CORE_PEER_CHAINCODEADDRESS=$newApplicationOrg:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_LOCALMSPID=$newApplicationOrg
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- $newApplicationOrgDir/peers/peer/msp:/etc/hyperledger/fabric/msp
ports:
- 9051:7051
- 9052:7052
- 9053:7053
networks:
fabric-net:
name: fabric-net
EOF
collectionConfigFile=$artifactsDir/collectionconfig.json
cat <<- EOF > $collectionConfigFile
[
{
"name": "$CollName",
"policy": "OR('$applicationOrg.member', '$applicationOrg.admin')",
"requiredPeerCount": 1,
"maxPeerCount": 1,
"blockToLive":1000000
}
]
EOF
cd $artifactsDir
$CRYPTOGEN_CMD generate --config=$cryptogenCfgFile
cd $curDir
# generating genesis block (main channel)
$CONFIGTXGEN_CMD -profile TwoOrgsOrdererGenesis -outputBlock $ordBlockMain --configPath $artifactsDir
$CONFIGTXGEN_CMD -profile TwoOrgsChannel -outputCreateChannelTx $genTransMain -channelID $ChannelName --configPath $artifactsDir
killall -9 peer || true
docker ps -a | awk '{print $1}' | xargs docker kill || true
docker ps -a | awk '{print $1}' | xargs docker rm || true
docker network rm fabric-net || true
docker image ls | grep $CCName | awk '{print $3}' | xargs docker rmi -f || true
docker-compose -f $dockerComposeFile up -d
sleep .2
# creating channel
echo creating channel
env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD channel create -o 127.0.0.1:7050 -c $ChannelName -f $genTransMain --outputBlock $genBlockMain
# joining channel
echo peer 1 joining channel
env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD channel join -b $genBlockMain
# joining channel
echo peer 2 joining channel
env CORE_PEER_ADDRESS=127.0.0.1:8051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD channel join -b $genBlockMain
# joining channel
echo peer 3 joining channel
env CORE_PEER_ADDRESS=127.0.0.1:9051 CORE_PEER_LOCALMSPID=$newApplicationOrg CORE_PEER_MSPCONFIGPATH=$newApplicationOrgDir/users/Admin@mynewapplicationorg/msp/ \
$PEER_CMD channel join -b $genBlockMain
# install chaincode
echo peer 1 installing cc
env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD chaincode install -n $CCName -v 1 -p github.com/hyperledger/fabric/examples/chaincode/go/keylevelep
# install chaincode
echo peer 2 installing cc
env CORE_PEER_ADDRESS=127.0.0.1:8051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD chaincode install -n $CCName -v 1 -p github.com/hyperledger/fabric/examples/chaincode/go/keylevelep
# install chaincode
echo peer 3 installing cc
env CORE_PEER_ADDRESS=127.0.0.1:9051 CORE_PEER_LOCALMSPID=$newApplicationOrg CORE_PEER_MSPCONFIGPATH=$newApplicationOrgDir/users/Admin@mynewapplicationorg/msp/ \
$PEER_CMD chaincode install -n $CCName -v 1 -p github.com/hyperledger/fabric/examples/chaincode/go/keylevelep
# instantiate chaincode
echo instantiating cc
env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/Admin@myapplicationorg/msp/ \
$PEER_CMD chaincode instantiate -C $ChannelName -n $CCName -v 1 -c '{"Args":[""]}' --collections-config $collectionConfigFile -o 127.0.0.1:7050
sleep 1
# invoke chaincode
echo peer1 invoke addorgs
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["addorgs","mynewapplicationorg"]}' -o 127.0.0.1:7050
# query chaincode
echo peer1 invoke listorgs
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode query -n $CCName -C $ChannelName -c'{"Args":["listorgs"]}' -o 127.0.0.1:7050
# invoke chaincode
echo peer1 invoke setval - should not succeed
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["setval","newfoo1"]}' -o 127.0.0.1:7050
# query chaincode
echo peer1 invoke getval
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode query -n $CCName -C $ChannelName -c'{"Args":["getval"]}' -o 127.0.0.1:7050
# invoke chaincode
echo peer2 invoke setval - should succeed
runStep env CORE_PEER_ADDRESS=127.0.0.1:9051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["setval","newfoo2"]}' -o 127.0.0.1:7050
# query chaincode
echo peer1 invoke getval
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode query -n $CCName -C $ChannelName -c'{"Args":["getval"]}' -o 127.0.0.1:7050
# invoke chaincode
echo peer1 invoke addorgs - should not succeed
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["addorgs","myapplicationorg"]}' -o 127.0.0.1:7050
# invoke chaincode
echo peer2 invoke addorgs - should succeed
runStep env CORE_PEER_ADDRESS=127.0.0.1:9051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["addorgs","myapplicationorg"]}' -o 127.0.0.1:7050
# invoke chaincode
echo peer1 invoke delorgs - should not succeed
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode invoke -n $CCName -C $ChannelName -c '{"Args":["delorgs","mynewapplicationorg"]}' -o 127.0.0.1:7050
# query chaincode
echo peer1 invoke listorgs
runStep env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD chaincode query -n $CCName -C $ChannelName -c'{"Args":["listorgs"]}' -o 127.0.0.1:7050
echo "env CORE_PEER_ADDRESS=127.0.0.1:7051 CORE_PEER_LOCALMSPID=$applicationOrg CORE_PEER_MSPCONFIGPATH=$applicationOrgDir/users/User1@myapplicationorg/msp/ \
$PEER_CMD channel fetch 1 /dev/stdout -c my-ch | $BLOCKPARSER_CMD"
exit 0