-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabric
executable file
·238 lines (199 loc) · 8.06 KB
/
fabric
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
#!/usr/bin/env bash
set -euo pipefail
PROJECT_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )"/../.. &> /dev/null && pwd )"
DISABLE_BLOCKCHAIN_EXPLORER=${DISABLE_BLOCKCHAIN_EXPLORER:-0}
DISABLE_LOGSPOUT=${DISABLE_LOGSPOUT:-0}
FABRIC_VERSION="${FABRIC_VERSION:-2.4.1}"
FABRIC_CA_VERSION="${FABRIC_CA_VERSION:-1.5.2}"
FABRIC_ROOT=${FABRIC_ROOT:-$PROJECT_ROOT/.fabric}
BLOCKCHAIN_EXPLORER_VERSION=${BLOCKCHAIN_EXPLORER_VERSION:-1.1.8}
BLOCKCHAIN_EXPLORER_ROOT=${BLOCKCHAIN_EXPLORER_ROOT:-$PROJECT_ROOT/.explorer}
FABRIC_CHANNEL=mychannel
FABRIC_CHAINCODE_NAME=iotservice
function downloadBlockchainExplorer() {
URI_ROOT="https://raw.githubusercontent.com/hyperledger/blockchain-explorer/v${BLOCKCHAIN_EXPLORER_VERSION}"
mkdir -p ${BLOCKCHAIN_EXPLORER_ROOT}
cd ${BLOCKCHAIN_EXPLORER_ROOT}
wget -q --show-progress ${URI_ROOT}/examples/net1/config.json
wget -q --show-progress ${URI_ROOT}/examples/net1/connection-profile/test-network.json -P connection-profile
wget -q --show-progress ${URI_ROOT}/docker-compose.yaml
sed -i 's/name: net_test/name: fabric_test/' docker-compose.yaml
sed -i "s/:latest/:${BLOCKCHAIN_EXPLORER_VERSION}/" docker-compose.yaml
sed -i 's/\/examples\/net1//' docker-compose.yaml
sed -i "s/\/fabric-path\/fabric-samples\/test-network/${FABRIC_ROOT//\//\\/}\/test-network/" docker-compose.yaml
sed -i "s/mychannel/${FABRIC_CHANNEL}/" connection-profile/test-network.json
sed -i "s/[email protected]/cert.pem/" connection-profile/test-network.json
cd - &> /dev/null
}
function downloadFabricSamples() {
mkdir -p ${FABRIC_ROOT}
cd ${FABRIC_ROOT}
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/v${FABRIC_VERSION}/scripts/bootstrap.sh \
| bash -s -- ${FABRIC_VERSION} ${FABRIC_CA_VERSION}
mv fabric-samples/bin .
mv fabric-samples/config .
mv fabric-samples/test-network .
rm -rf fabric-samples
cd - &> /dev/null
}
function bringUpNetwork() {
if [[ ! -d ${FABRIC_ROOT}/test-network ]]; then
downloadFabricSamples
fi
cd ${FABRIC_ROOT}/test-network
./network.sh up createChannel -ca -c ${FABRIC_CHANNEL}
cd ${FABRIC_ROOT}/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore
cp * priv_sk
cd ${FABRIC_ROOT}/test-network/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp/keystore
cp * priv_sk
if [[ ${DISABLE_BLOCKCHAIN_EXPLORER} -eq 0 ]]; then
if [[ ! -d ${BLOCKCHAIN_EXPLORER_ROOT} ]]; then
downloadBlockchainExplorer
fi
cd ${BLOCKCHAIN_EXPLORER_ROOT}
docker-compose up -d
echo "Blockchain explorer started at http://0.0.0.0:8080 with username 'exploreradmin' and password 'exploreradminpw'."
fi
if [[ ${DISABLE_LOGSPOUT} -eq 0 ]]; then
docker run -d --name=fabric_logspout \
--volume=/var/run/docker.sock:/var/run/docker.sock \
--publish=0.0.0.0:8081:80 \
--network fabric_test \
gliderlabs/logspout
echo "Logspout started at http://0.0.0.0:8081/logs."
fi
cd - &> /dev/null
}
function bringDownNetwork() {
if [[ -d ${FABRIC_ROOT}/test-network ]]; then
cd ${FABRIC_ROOT}/test-network
./network.sh down
cd - &> /dev/null
fi
if [[ ${DISABLE_BLOCKCHAIN_EXPLORER} -eq 0 ]]; then
if [[ -d ${BLOCKCHAIN_EXPLORER_ROOT} ]]; then
cd ${BLOCKCHAIN_EXPLORER_ROOT}
docker-compose down -v
cd - &> /dev/null
fi
fi
if [[ ${DISABLE_LOGSPOUT} -eq 0 ]]; then
docker kill fabric_logspout 2> /dev/null 1>&2 || true
docker rm fabric_logspout 2> /dev/null 1>&2 || true
fi
}
function deployChaincode() {
CHAINCODE_VERSION=${1:-1.0}; if [ ! $# -lt 1 ]; then shift; fi
CHAINCODE_SEQUENCE=${1:-1}; if [ ! $# -lt 1 ]; then shift; fi
cd ${FABRIC_ROOT}/test-network
./network.sh deployCC \
-c ${FABRIC_CHANNEL} \
-ccl go \
-ccn ${FABRIC_CHAINCODE_NAME} \
-ccp ${PROJECT_ROOT}/chaincode \
-ccv ${CHAINCODE_VERSION} \
-ccs ${CHAINCODE_SEQUENCE}
cd - &> /dev/null
}
function runAsOrgUser() {
FABRIC_ORG=${FABRIC_ORG:-Org1}
FABRIC_USER=${FABRIC_USER:-User1}
FABRIC_DOMAIN="$(echo ${FABRIC_ORG} | awk '{print tolower($0)}').example.com"
FABRIC_ORG_ROOT="${FABRIC_ROOT}/test-network/organizations/peerOrganizations/${FABRIC_DOMAIN}"
export PATH=${FABRIC_ROOT}/bin:$PATH
export FABRIC_CFG_PATH=${FABRIC_ROOT}/config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="${FABRIC_ORG}MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${FABRIC_ORG_ROOT}/peers/peer0.${FABRIC_DOMAIN}/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${FABRIC_ORG_ROOT}/users/${FABRIC_USER}@${FABRIC_DOMAIN}/msp
export CORE_PEER_ADDRESS=localhost:7051
"$@"
}
function queryChaincode() {
PAYLOAD=$@
runAsOrgUser peer chaincode query -C ${FABRIC_CHANNEL} -n ${FABRIC_CHAINCODE_NAME} -c "${PAYLOAD}"
}
function invokeChaincode() {
PAYLOAD=$@
runAsOrgUser peer chaincode invoke \
-o localhost:7050 \
-C ${FABRIC_CHANNEL} \
-n ${FABRIC_CHAINCODE_NAME} \
--ordererTLSHostnameOverride orderer.example.com \
--peerAddresses localhost:7051 \
--peerAddresses localhost:9051 \
--tls \
--tlsRootCertFiles "${FABRIC_ROOT}/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \
--tlsRootCertFiles "${FABRIC_ROOT}/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \
--cafile "${FABRIC_ROOT}/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
-c "${PAYLOAD}"
}
function createOrgUser() {
FABRIC_ORG=${1}
FABRIC_USER=${2}
FABRIC_ORG_LOWER=$(echo ${FABRIC_ORG} | awk '{print tolower($0)}')
FABRIC_USER_LOWER=$(echo ${FABRIC_USER} | awk '{print tolower($0)}')
FABRIC_USER_PASSWORD="${FABRIC_USER_LOWER}pw"
FABRIC_DOMAIN="$(echo ${FABRIC_ORG} | awk '{print tolower($0)}').example.com"
FABRIC_ORG_ROOT="${FABRIC_ROOT}/test-network/organizations/peerOrganizations/${FABRIC_DOMAIN}"
FABRIC_TLS_CERT="${FABRIC_ROOT}/test-network/organizations/fabric-ca/${FABRIC_ORG_LOWER}/tls-cert.pem"
FABRIC_CA_SERVER_PORT=7054
if [ "$FABRIC_ORG_LOWER" == "org2" ]; then
FABRIC_CA_SERVER_PORT=8054
fi
export PATH=${FABRIC_ROOT}/bin:$PATH
export FABRIC_CA_CLIENT_HOME=${FABRIC_ORG_ROOT}/
fabric-ca-client register \
--caname "ca-${FABRIC_ORG_LOWER}" \
--id.name ${FABRIC_USER_LOWER} \
--id.secret ${FABRIC_USER_PASSWORD} \
--id.type client \
--tls.certfiles ${FABRIC_TLS_CERT}
fabric-ca-client enroll -u "https://${FABRIC_USER_LOWER}:${FABRIC_USER_PASSWORD}@localhost:${FABRIC_CA_SERVER_PORT}" \
--caname "ca-${FABRIC_ORG_LOWER}" \
-M "${FABRIC_ORG_ROOT}/users/${FABRIC_USER}@${FABRIC_DOMAIN}/msp" \
--tls.certfiles ${FABRIC_TLS_CERT}
cp "${FABRIC_ORG_ROOT}/msp/config.yaml" "${FABRIC_ORG_ROOT}/users/${FABRIC_USER}@${FABRIC_DOMAIN}/msp/config.yaml"
}
COMMAND=$1; shift
case $COMMAND in
download)
downloadFabricSamples
if [[ ${DISABLE_BLOCKCHAIN_EXPLORER} -eq 0 ]]; then
downloadBlockchainExplorer
fi
;;
network)
SUBCOMMAND=$1; shift
case $SUBCOMMAND in
up)
bringUpNetwork
;;
down)
bringDownNetwork
;;
esac
;;
chaincode)
SUBCOMMAND=$1; shift
case $SUBCOMMAND in
deploy)
deployChaincode $@
;;
query)
queryChaincode $@
;;
invoke)
invokeChaincode $@
;;
esac
;;
user)
SUBCOMMAND=$1; shift
case $SUBCOMMAND in
create)
createOrgUser $@
;;
esac
;;
esac