This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmoveSim.js
288 lines (255 loc) · 14.5 KB
/
moveSim.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Invoke the telco-roaming-contract to move a SubscriberSim to a new location.
* node moveSim.js <simPublicKey> <location>
* eg. node moveSim.js sim1 European\ Union
*
*
* 1. call moveSim to move the sim to a new location.
* 2. call authentication to verify that the sim is Active (and not a Fraud). Only then will the following calls be performed.
* 3. call discovery to update the roamingPartner details.
* 4. call updateRate to update the roaming and overage rates for the sim.
*
* Will throw an error and processing will stop at step 2 if the sim has isValid = Fraud.
* Otherwise, should successfully complete without errors and the ledger should show that the sim has moved to a new location.
* If the new location is different from home location, then the sim should have the correct RoamingPartner specified.
* If the new location is the home location, the RoamingPartner for the sim will be blank.
* The roaming and overage rates will be updated to reflect those specified by the roaming partner.
*/
let util = require('util');
const fs = require('fs');
const path = require('path');
// Bring Fabric SDK network class
const { FileSystemWallet, Gateway } = require('fabric-network');
// A wallet stores a collection of identities for use
let walletDir = path.join(path.dirname(require.main.filename),'fabric/_idwallet');
const wallet = new FileSystemWallet(walletDir);
const configDirectory = path.join(process.cwd(), 'fabric');
const configPath = path.join(configDirectory, 'config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
let channelName = config.channel_name;
let smartContractName = config.smart_contract_name;
let connection_file = config.connection_file;
let appAdmin = config.appAdmin;
let peerAddr = config.peerName;
let gatewayDiscoveryEnabled = 'enabled' in config.gatewayDiscovery?config.gatewayDiscovery.enabled:true;
let gatewayDiscoveryAsLocalhost = 'asLocalhost' in config.gatewayDiscovery?config.gatewayDiscovery.asLocalhost:true;
const ccpPath = path.join(configDirectory, connection_file);
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
const gateway = new Gateway();
let promises = [];
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('An unhandled rejection was found - ', error.message);
return process.exit(1);
});
async function main() {
if(process.argv.length !== 4){
throw new Error('Process argv length is ' + process.argv.length + '. It should be 4');
}
let simPublicKey = process.argv[2];
let newLocation = process.argv[3];
// A gateway defines the peers used to access Fabric networks
await gateway.connect(ccp, { wallet, identity: appAdmin , discovery: {enabled: gatewayDiscoveryEnabled, asLocalhost:gatewayDiscoveryAsLocalhost }});
// eslint-disable-next-line no-unused-vars
const network = await gateway.getNetwork(channelName);
const client = gateway.getClient();
const channel = client.getChannel(channelName);
let event_hub = channel.newChannelEventHub(peerAddr);
// get a transaction id object based on the current user assigned to fabric client
let tx_id = client.newTransactionID(true);
let fcn = 'moveSim';
console.log(`Sending transaction proposal for ${fcn} with transaction id ${tx_id._transaction_id}`);
// must send the proposal to endorsing peers
let request = {
//targets: let default to the peer assigned to the client
chaincodeId: smartContractName,
fcn: fcn,
args: [simPublicKey, newLocation],
chainId: channelName,
txId: tx_id
};
// send the transaction proposal to the peers
channel.sendTransactionProposal(request).then((results) => {
let proposalResponses = results[0];
let proposal = results[1];
let isProposalGood = false;
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
isProposalGood = true;
console.log('Transaction proposal was good');
} else {
console.error('Transaction proposal was bad');
}
if (isProposalGood) {
console.log(util.format(
'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
proposalResponses[0].response.status, proposalResponses[0].response.message));
// build up the request for the orderer to have the transaction committed
let request = {
proposalResponses: proposalResponses,
proposal: proposal
};
let sendPromise = channel.sendTransaction(request);
promises.push(sendPromise); //we want the send transaction first, so that we know where to check status
console.log(`Created Promise - ${fcn} for ` + simPublicKey);
}
//console.log("Created eventhub - ", event_hub);
event_hub.connect(true);
//console.log("connected to eventhub");
let regid = event_hub.registerChaincodeEvent('telco-roaming-contract', 'MoveEvent-'+simPublicKey, function() {
console.log('Found MoveEvent');
//console.log(event);
//console.log(util.format("Custom event received, payload: %j\n", event.payload.toString()));
event_hub.unregisterChaincodeEvent(regid);
// get a transaction id object based on the current user assigned to fabric client
tx_id = client.newTransactionID(true);
fcn = 'discovery';
console.log(`Sending transaction proposal for ${fcn} with transaction id ${tx_id._transaction_id}`);
// must send the proposal to endorsing peers
request = {
//targets: let default to the peer assigned to the client
chaincodeId: smartContractName,
fcn: fcn,
args: [simPublicKey],
chainId: channelName,
txId: tx_id
};
// send the transaction proposal to the peers
channel.sendTransactionProposal(request).then((results) =>{
proposalResponses = results[0];
proposal = results[1];
isProposalGood = false;
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
isProposalGood = true;
console.log('Transaction proposal was good');
} else {
console.error('Transaction proposal was bad');
}
if (isProposalGood) {
console.log(util.format(
'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
proposalResponses[0].response.status, proposalResponses[0].response.message));
// build up the request for the orderer to have the transaction committed
let request = {
proposalResponses: proposalResponses,
proposal: proposal
};
let sendPromise = channel.sendTransaction(request);
promises.push(sendPromise); //we want the send transaction first, so that we know where to check status
console.log(`Created Promise - ${fcn} for ` + simPublicKey);
}
event_hub.connect(true);
//console.log("connected to eventhub");
let regid = event_hub.registerChaincodeEvent('telco-roaming-contract', 'DiscoveryEvent-'+simPublicKey, function(event) {
console.log('Found discovery event');
//console.log(event);
//console.log(util.format("Custom event received, payload: %j\n", event.payload.toString()));
let eventPayload = JSON.parse(event.payload.toString());
let operatorName = eventPayload.localOperator;
event_hub.unregisterChaincodeEvent(regid);
// get a transaction id object based on the current user assigned to fabric client
tx_id = client.newTransactionID(true);
fcn = 'authentication';
console.log(`Sending transaction proposal for ${fcn} with transaction id ${tx_id._transaction_id}`);
// must send the proposal to endorsing peers
request = {
//targets: let default to the peer assigned to the client
chaincodeId: smartContractName,
fcn: fcn,
args: [simPublicKey],
chainId: channelName,
txId: tx_id
};
// send the transaction proposal to the peers
channel.sendTransactionProposal(request).then((results) =>{
proposalResponses = results[0];
proposal = results[1];
isProposalGood = false;
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
isProposalGood = true;
console.log('Transaction proposal was good');
} else {
console.error('Transaction proposal was bad');
}
if (isProposalGood) {
console.log(util.format(
'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
proposalResponses[0].response.status, proposalResponses[0].response.message));
// build up the request for the orderer to have the transaction committed
let request = {
proposalResponses: proposalResponses,
proposal: proposal
};
let sendPromise = channel.sendTransaction(request);
promises.push(sendPromise); //we want the send transaction first, so that we know where to check status
console.log(`Created Promise - ${fcn} for ` + simPublicKey);
}
event_hub.connect(true);
//console.log("connected to eventhub");
let regid = event_hub.registerChaincodeEvent('telco-roaming-contract', 'AuthenticationEvent-'+simPublicKey, function() {
console.log('Found authentication event');
//console.log(event);
//console.log(util.format("Custom event received, payload: %j\n", event.payload.toString()));
event_hub.unregisterChaincodeEvent(regid);
// get a transaction id object based on the current user assigned to fabric client
tx_id = client.newTransactionID(true);
fcn = 'updateRate';
console.log(`Sending transaction proposal for ${fcn} with transaction id ${tx_id._transaction_id}`);
// must send the proposal to endorsing peers
request = {
//targets: let default to the peer assigned to the client
chaincodeId: smartContractName,
fcn: fcn,
args: [simPublicKey, operatorName],
chainId: channelName,
txId: tx_id
};
// send the transaction proposal to the peers
channel.sendTransactionProposal(request).then((results) =>{
proposalResponses = results[0];
proposal = results[1];
isProposalGood = false;
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
isProposalGood = true;
console.log('Transaction proposal was good');
} else {
console.error('Transaction proposal was bad');
}
if (isProposalGood) {
console.log(util.format(
'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
proposalResponses[0].response.status, proposalResponses[0].response.message));
// build up the request for the orderer to have the transaction committed
let request = {
proposalResponses: proposalResponses,
proposal: proposal
};
let sendPromise = channel.sendTransaction(request);
promises.push(sendPromise); //we want the send transaction first, so that we know where to check status
console.log(`Created Promise - ${fcn} for ` + simPublicKey);
}
event_hub.connect(true);
//console.log("connected to eventhub");
let regid = event_hub.registerChaincodeEvent('telco-roaming-contract', 'UpdateRateEvent-'+simPublicKey, function() {
console.log('Found updateRate event');
//console.log(event);
//console.log(util.format("Custom event received, payload: %j\n", event.payload.toString()));
event_hub.unregisterChaincodeEvent(regid);
return process.exit(0);
});
});
});
});
});
});
});
});
}
main();