forked from HyperledgerHandsOn/trade-finance-logistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstantiate-chaincode.js
265 lines (220 loc) · 9 KB
/
instantiate-chaincode.js
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
/*
* Copyright 2018 IBM All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var utils = require('fabric-client/lib/utils.js');
var logger = utils.getLogger('instantiate-chaincode');
var util = require('util');
var path = require('path');
var fs = require('fs');
var Client = require('fabric-client');
var Constants = require('./constants.js');
var ClientUtils = require('./clientUtils.js');
//
// Construct instantiation or upgrade proposal
//
function buildChaincodeProposal(client, user_handle, chaincode_path, version, funcName, argList) {
var tx_id = client.newTransactionID();
// send proposal to endorser
var request = {
chaincodePath: chaincode_path,
chaincodeId: Constants.CHAINCODE_ID,
chaincodeVersion: version,
fcn: funcName,
args: argList,
txId: tx_id,
'endorsement-policy': Constants.TRANSACTION_ENDORSEMENT_POLICY
};
return request;
}
//
// Send request for chaincode instantiation on the channel to the orderer
//
function instantiateOrUpgradeChaincode(userOrg, chaincode_path, version, funcName, argList, upgrade, constants) {
if (constants) {
Constants = constants;
}
ClientUtils.init(Constants);
var ORGS = JSON.parse(fs.readFileSync(path.join(__dirname, Constants.networkConfig)))[Constants.networkId];
var channel_name = Client.getConfigSetting('E2E_CONFIGTX_CHANNEL_NAME', Constants.CHANNEL_NAME);
var targets = [];
var type = 'instantiate';
if(upgrade) type = 'upgrade';
var client = new Client();
var channel = client.newChannel(channel_name);
var user_handle = null;
var tx_id = null;
var orgName = ORGS[userOrg].name;
var cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: ClientUtils.storePathForOrg(orgName)}));
client.setCryptoSuite(cryptoSuite);
var caRootsPath = ORGS.orderer.tls_cacerts;
let data = fs.readFileSync(path.join(__dirname, caRootsPath));
let caroots = Buffer.from(data).toString();
channel.addOrderer(
client.newOrderer(
ORGS.orderer.url,
{
'pem': caroots,
'ssl-target-name-override': ORGS.orderer['server-hostname']
}
)
);
return Client.newDefaultKeyValueStore({
path: ClientUtils.storePathForOrg(orgName)
}).then((store) => {
client.setStateStore(store);
return ClientUtils.getSubmitter(client, true /* use peer org admin*/, userOrg);
}).then((admin) => {
console.log('Successfully enrolled user \'admin\'');
user_handle = admin;
for(let org in ORGS) {
if (ORGS[org].hasOwnProperty('peer1')) {
let key = 'peer1';
let data = fs.readFileSync(path.join(__dirname, ORGS[org][key]['tls_cacerts']));
logger.debug(' create new peer %s', ORGS[org][key].requests);
let peer = client.newPeer(
ORGS[org][key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org][key]['server-hostname']
}
);
targets.push(peer);
channel.addPeer(peer);
}
}
// an event listener can only register with a peer in its own org
logger.debug(' create new eventhub %s', ORGS[userOrg]['peer1'].events);
let data = fs.readFileSync(path.join(__dirname, ORGS[userOrg]['peer1']['tls_cacerts']));
let eh = client.newEventHub();
eh.setPeerAddr(
ORGS[userOrg]['peer1'].events,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[userOrg]['peer1']['server-hostname']
}
);
eh.connect();
ClientUtils.eventhubs.push(eh);
// read the config block from the orderer for the channel
// and initialize the verify MSPs based on the participating
// organizations
return channel.initialize();
}, (err) => {
console.log('Failed to enroll user \'admin\'. ' + err);
throw new Error('Failed to enroll user \'admin\'. ' + err);
}).then(() => {
logger.debug(' orglist:: ', channel.getOrganizations());
let request = buildChaincodeProposal(client, user_handle, chaincode_path, version, funcName, argList);
tx_id = request.txId;
if (upgrade) {
logger.debug(util.format(
'Upgrading chaincode "%s" at path "%s" to version "%s" by passing args "%s" to method "%s" in transaction "%s"',
request.chaincodeId,
request.chaincodePath,
request.chaincodeVersion,
request.args,
request.fcn,
request.txId.getTransactionID()
));
// This process could take a while, so we set a very long timeout
return channel.sendUpgradeProposal(request, 300000);
} else {
// This process could take a while, so we set a very long timeout
return channel.sendInstantiateProposal(request, 300000);
}
}, (err) => {
console.log(util.format('Failed to initialize the channel. %s', err.stack ? err.stack : err));
throw new Error('Failed to initialize the channel');
}).then((results) => {
var proposalResponses = results[0];
var proposal = results[1];
var all_good = true;
for(var i in proposalResponses) {
let one_good = false;
if (proposalResponses && proposalResponses[i].response && proposalResponses[i].response.status === 200) {
one_good = true;
logger.info(type +' proposal was good from peer %s', targets[i].getUrl());
} else {
logger.error(type +' proposal was bad from peer %s', targets[i].getUrl());
}
all_good = all_good & one_good;
}
if (all_good) {
console.log('Successfully sent Proposal and received', proposalResponses.length, 'ProposalResponses');
logger.debug(util.format('Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s", metadata - "%s", endorsement signature: %s', proposalResponses[0].response.status, proposalResponses[0].response.message, proposalResponses[0].response.payload, proposalResponses[0].endorsement.signature));
var request = {
proposalResponses: proposalResponses,
proposal: proposal
};
// set the transaction listener and set a timeout of 30sec
// if the transaction did not get committed within the timeout period, fail
var deployId = tx_id.getTransactionID();
var eventPromises = [];
ClientUtils.eventhubs.forEach((eh) => {
let txPromise = new Promise((resolve, reject) => {
let handle = setTimeout(reject, 300000);
eh.registerTxEvent(deployId.toString(), (tx, code) => {
console.log('The chaincode ' + type + ' transaction has been committed on peer '+ eh.getPeerAddr());
clearTimeout(handle);
eh.unregisterTxEvent(deployId);
if (code !== 'VALID') {
console.log('The chaincode ' + type + ' transaction was invalid, code = ' + code);
reject();
} else {
console.log('The chaincode ' + type + ' transaction was valid.');
resolve();
}
});
});
logger.debug('register eventhub %s with tx=%s',eh.getPeerAddr(),deployId);
eventPromises.push(txPromise);
});
var sendPromise = channel.sendTransaction(request);
return Promise.all([sendPromise].concat(eventPromises))
.then((results) => {
logger.debug('Transaction and event promises all complete');
return results[0]; // just first results are from orderer, the rest are from the peer events
}).catch((err) => {
console.log('Failed to send ' + type + ' transaction and get notifications within the timeout period:', err);
throw new Error('Failed to send ' + type + ' transaction and get notifications within the timeout period.');
});
} else {
console.log('Failed to send ' + type + ' Proposal or receive valid response. Response null or status is not 200. exiting...');
throw new Error('Failed to send ' + type + ' Proposal or receive valid response. Response null or status is not 200. exiting...');
}
}, (err) => {
console.log('Failed to send ' + type + ' proposal due to error: ' + err.stack ? err.stack : err);
throw new Error('Failed to send ' + type + ' proposal due to error: ' + err.stack ? err.stack : err);
}).then((response) => {
if (!(response instanceof Error) && response.status === 'SUCCESS') {
console.log('Successfully sent ' + type + 'transaction to the orderer.');
if (type === 'upgrade') {
console.log('Successfully upgraded chaincode on channel');
} else {
console.log('Successfully instantiated chaincode on channel');
}
return true;
} else {
console.log('Failed to order the ' + type + 'transaction. Error code: ' + response.status);
Promise.reject(new Error('Failed to order the ' + type + 'transaction. Error code: ' + response.status));
}
}, (err) => {
console.log('Failed to send ' + type + ' due to error: ' + err.stack ? err.stack : err);
Promise.reject(new Error('Failed to send instantiate due to error: ' + err.stack ? err.stack : err));
});
};
module.exports.instantiateOrUpgradeChaincode = instantiateOrUpgradeChaincode;