forked from pavankramadugu/CSE598-EBA-Project2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_chaincode.sh
More file actions
executable file
·136 lines (109 loc) · 6.95 KB
/
deploy_chaincode.sh
File metadata and controls
executable file
·136 lines (109 loc) · 6.95 KB
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
#!/bin/bash -e
# Check if the path to the smart contract code is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <path-to-code>"
echo "<path-to-code> is the directory containing the Go smart contract code after running 'go mod tidy' and 'go mod vendor'"
exit 1
fi
# Set the path to the smart contract code
SMART_CONTRACT_PATH="$1"
# Log file
LOG_FILE="deployment_log.txt"
# Function to log a message
log() {
echo "$(date): $1" | tee -a "$LOG_FILE"
}
# Bring up a fresh test network
log "Bringing up a fresh test network..."
./network.sh down && ./network.sh up
# Check running Docker containers
log "Checking running Docker containers..."
docker ps -a | grep "hyperledger/fabric" | tee -a "$LOG_FILE"
# Create a channel and have the two example orgs join it
log "Creating a channel and having the two example orgs join it..."
./network.sh createChannel | tee -a "$LOG_FILE"
# Set environment variables
log "Setting environment variables..."
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
# Clean up and Package the smart contract
log "Cleaning up old chaincode packages..."
rm -f supplyChain.tar.gz
log "Packaging the smart contract..."
peer lifecycle chaincode package supplyChain.tar.gz --path "$SMART_CONTRACT_PATH" --lang golang --label supplyChain 2>&1 | tee -a "$LOG_FILE"
# Check if the package has been created
log "Checking if the package has been created..."
ls | grep supplyChain.tar.gz | tee -a "$LOG_FILE"
# Install the chaincode package on Org1
log "Installing the chaincode package on Org1..."
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode install supplyChain.tar.gz 2>&1 | tee -a "$LOG_FILE"
# Install the chaincode package on Org2
log "Installing the chaincode package on Org2..."
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
peer lifecycle chaincode install supplyChain.tar.gz 2>&1 | tee -a "$LOG_FILE"
# Approve the chaincode for Org1
log "Approving the chaincode for Org1..."
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
CC_PACKAGE_ID_ORG1=$(peer lifecycle chaincode queryinstalled | grep -E "supplyChain:[0-9a-f]+" -o)
if [ -z "$CC_PACKAGE_ID_ORG1" ]; then
echo "Could not find Package ID for Org1!" | tee -a "$LOG_FILE"
exit 1
fi
echo "Package ID for Org1: $CC_PACKAGE_ID_ORG1" | tee -a "$LOG_FILE"
peer lifecycle chaincode approveformyorg -o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name supplyChain \
--version 1.0 \
--package-id $CC_PACKAGE_ID_ORG1 \
--sequence 1 \
--tls true \
--cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 2>&1 | tee -a "$LOG_FILE"
# Approve the chaincode for Org2
log "Approving the chaincode for Org2..."
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
CC_PACKAGE_ID_ORG2=$(peer lifecycle chaincode queryinstalled | grep -E "supplyChain:[0-9a-f]+" -o)
if [ -z "$CC_PACKAGE_ID_ORG2" ]; then
echo "Could not find Package ID for Org2!" | tee -a "$LOG_FILE"
exit 1
fi
echo "Package ID for Org2: $CC_PACKAGE_ID_ORG2" | tee -a "$LOG_FILE"
peer lifecycle chaincode approveformyorg -o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name supplyChain \
--version 1.0 \
--package-id $CC_PACKAGE_ID_ORG2 \
--sequence 1 \
--tls true \
--cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 2>&1 | tee -a "$LOG_FILE"
# Check if the chaincode is ready to be committed
log "Checking if the chaincode is ready to be committed..."
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name supplyChain --version 1.0 --sequence 1 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json 2>&1 | tee -a "$LOG_FILE"
# Commit the chaincode to the channel
log "Committing the chaincode to the channel..."
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name supplyChain --version 1.0 --sequence 1 --tls true --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 2>&1 | tee -a "$LOG_FILE"
# Check if the chaincode is committed
log "Checking if the chaincode committed to the channel..."
peer lifecycle chaincode querycommitted --channelID mychannel --name supplyChain --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 2>&1 | tee -a "$LOG_FILE"
# Invoke the initLedger function
log "Invoking the initLedger function..."
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n supplyChain --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":"initLedger","Args":[]}' 2>&1 | tee -a "$LOG_FILE"
# Get All Products from the Ledger
log "Calling the GetAllProducts function..."
peer chaincode query -C mychannel -n supplyChain -c '{"Args":["GetAllProducts"]}' 2>&1 | tee -a "$LOG_FILE"