-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-smart-home-repl-chaincode.sh
232 lines (196 loc) · 12.4 KB
/
deploy-smart-home-repl-chaincode.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
#!/bin/bash
. ./scripts/utils.sh
export FABRIC_CFG_PATH=$PWD/../config/
#CC_NAME=$1
#PATH=$2
CC_NAME="smart_home_replenishment_chaincode"
CC_PATH="./smart-home-replenishment-chaincode/"
CC_SEQUENCE=1
CC_VERSION=1.0
infoln "1. Create the chaincode package."
peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_PATH} --lang node --label ${CC_NAME}_${CC_VERSION}
# ----------------------------- START: INSTALL PROCESS ----------------------------- #
infoln "\n\n2. Install chaincode package on all three peers."
CHANNEL1="channel1"
CHANNEL2="channel2"
infoln "\n2.1 Installing on organisation 1..."
ORG=1
ADRESS="localhost:7051"
source ./set-org.sh ${ORG} ${ADRESS}
peer lifecycle chaincode install ./smart_home_replenishment_chaincode.tar.gz
infoln "\n--> Installed chaincodes on peer 1:"
peer lifecycle chaincode queryinstalled
infoln "\n--> Copy the package ID, paste it into the next line and press enter to continue..."
read CC_PACKAGE_ID
infoln "\n--> Approving chaincode definition with ID ${CC_PACKAGE_ID} with org ${ORG}, channel 1..."
# Only need to approve for one peer since chaincode definitions are approved on a organisational level.
# The other peers will get synched by the gossip protocol.
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL1} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--package-id ${CC_PACKAGE_ID} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
# ----------------------------- START: APPROVE ----------------------------- #
infoln "\n--> Approving chaincode definition with ID ${CC_PACKAGE_ID} with org ${ORG}, channel 2..."
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL2} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--package-id ${CC_PACKAGE_ID} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
# ----------------------------- END: APPROVE ----------------------------- #
infoln "\n2.2 Installing on organisation 2..."
ORG=2
ADRESS="localhost:9051"
source ./set-org.sh ${ORG} ${ADRESS}
peer lifecycle chaincode install ./smart_home_replenishment_chaincode.tar.gz
infoln "\n--> Installed chaincodes on peer 2:"
peer lifecycle chaincode queryinstalled
infoln "\n--> Copy the package ID, paste it into the next line and press enter to continue..."
read CC_PACKAGE_ID
# ----------------------------- START: APPROVE ----------------------------- #
infoln "\n--> Approving chaincode definition with ID ${CC_PACKAGE_ID} with org ${ORG}, channel 1..."
# Only need to approve for one peer since chaincode definitions are approved on a organisational level.
# The other peers will get synched by the gossip protocol.
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL1} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--package-id ${CC_PACKAGE_ID} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
# ----------------------------- END: APPROVE ----------------------------- #
infoln "\n2.3 Installing on organisation 3..."
ORG=3
ADRESS="localhost:11051"
source ./set-org.sh ${ORG} ${ADRESS}
peer lifecycle chaincode install ./smart_home_replenishment_chaincode.tar.gz
infoln "\n--> Installed chaincodes on peer 3:"
peer lifecycle chaincode queryinstalled
infoln "\n--> Copy the package ID, paste it into the next line and press enter to continue..."
read CC_PACKAGE_ID
# ----------------------------- START: APPROVE ----------------------------- #
infoln "\n--> Approving chaincode definition with ID ${CC_PACKAGE_ID} with org ${ORG}, channel 2..."
# Only need to approve for one peer since chaincode definitions are approved on a organisational level.
# The other peers will get synched by the gossip protocol.
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL2} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--package-id ${CC_PACKAGE_ID} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
# ----------------------------- END: APPROVE ----------------------------- #
# ----------------------------- END: INSTALL PROCESS ----------------------------- #
# ----------------------------- START: COMMIT PROCESS ----------------------------- #
infoln "\n\n3. Committing chaincode to both channels after they have been approved..."
# Here only one organisation is required to commit the chaincode definition to the channel since
# it already has been approved by a sufficient amount of channel members. However, the commit has to be done by
# an organisation admin.
# ----------------------------- START: CHECK COMMIT READINESS ----------------------------- #
ORG=2
ADRESS="localhost:9051"
source ./set-org.sh ${ORG} ${ADRESS}
infoln "\n--> Commmit readiness for channel ${CHANNEL1}, package ID ${CC_PACKAGE_ID}, as org ${ORG} with adress ${ADRESS}"
peer lifecycle chaincode checkcommitreadiness --channelID ${CHANNEL1} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json
ORG=3
ADRESS="localhost:11051"
source ./set-org.sh ${ORG} ${ADRESS}
infoln "\n--> Commmit readiness for channel ${CHANNEL2}, package ID ${CC_PACKAGE_ID}, as org ${ORG} with adress ${ADRESS}"
peer lifecycle chaincode checkcommitreadiness --channelID ${CHANNEL2} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json
# ----------------------------- END: CHECK COMMIT READINESS ----------------------------- #
# ----------------------------- START: COMMIT TO CHANNELS ----------------------------- #
ORG=2
ADRESS="localhost:9051"
source ./set-org.sh ${ORG} ${ADRESS}
infoln "\n--> Commiting for channel ${CHANNEL1}, package ID ${CC_PACKAGE_ID}, as org ${ORG} with adress ${ADRESS}"
# In the smart home replenishment setup, org 1 and org 2 are in channel 1.
# This installs the chaincode on both peers within the channel.
peer lifecycle chaincode commit \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL1} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
--peerAddresses localhost:7051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \
--peerAddresses localhost:9051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
#infoln "\n--> Checking successfull commitment to the channel ${CHANNEL1}..."
#peer lifecycle chaincode querycommitted \
# --channelID ${CHANNEL1} \
# --name ${CC_NAME} \
# --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
ORG=3
ADRESS="localhost:11051"
source ./set-org.sh ${ORG} ${ADRESS}
infoln "\n--> Commiting for channel ${CHANNEL2}, package ID ${CC_PACKAGE_ID}, as org ${ORG} with adress ${ADRESS}"
# In the smart home replenishment setup, org 1 and org 3 are in channel 2.
# This installs the chaincode on both peers within the channel.
peer lifecycle chaincode commit \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID ${CHANNEL2} \
--name ${CC_NAME} \
--version ${CC_VERSION} \
--sequence ${CC_SEQUENCE} \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
--peerAddresses localhost:7051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \
--peerAddresses localhost:11051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt"
#infoln "\n--> Checking successfull commitment to the channel ${CHANNEL2}..."
#peer lifecycle chaincode querycommitted \
# --channelID ${CHANNEL2} \
# --name ${CC_NAME} \
# --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
# ----------------------------- END: COMMIT TO CHANNELS ----------------------------- #
# ----------------------------- END: COMMIT PROCESS ----------------------------- #
# EXAMPLES ON HOW TO INVOKE THE SMART HOME REPLENISHMENT CHAINCODE:
# Placeholder for needed parameters:
# - channel1 (channel)
# - smart_home_replenishment_chaincode (name)
# - on org1 and org2 peers
# - function parameters = '{"function":"InitLedger","Args":[]}'
# Be sure to call ./setorg <ORG_NUMBER> <ORG_ADRESS> before running the command below. This will determine, which peer will be used to execute that command.
#
# Add a product:
# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C channel1 -n smart_home_replenishment_chaincode --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"addProduct","Args":["water", "2", "Just water", "2", "org2"]}'
#
# Get specific product that you created
# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C channel1 -n smart_home_replenishment_chaincode --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"getProductByNameAndOrg","Args":["water", "org2"]}'
#
# List all products with org x
# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C channel1 -n smart_home_replenishment_chaincode --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"listProducts","Args":["org2"]}'
#
# Order a product
# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C channel1 -n smart_home_replenishment_chaincode --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"orderProduct","Args":["consumer1", "water", "org2"]}'