-
Notifications
You must be signed in to change notification settings - Fork 4
/
hello-world.js
185 lines (174 loc) · 6.95 KB
/
hello-world.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
/**
* Copyright 2016 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.
*/
// An end-to-end sample program on Fabric V1.0
'use strict';
process.env.GOPATH = __dirname;
var path = require('path');
var hfc = require('.');
var util = require('util');
var fs = require('fs');
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
var chain = hfc.newChain(config.chainName);
var webUser;
var chaincodeID = "hello-world";
chain.setKeyValueStore(hfc.newKeyValueStore({
path: path.join(__dirname, "/" + config.KeyValueStore)
}));
chain.setMemberServicesUrl(config.ca.ca_url);
chain.setOrderer(config.orderer.orderer_url);
var targets = hfc.getPeer(config.peers[0].peer_url, config.peers[1].peer_url);
chain.enroll(config.users[0].username, config.users[0].secret)
.then(
function(admin) {
console.log('Successfully enrolled user \'admin\'');
webUser = admin;
var args = getArgs(config.deployRequest.args);
// send proposal to endorser
var request = {
//TODO: replace this to a single object
targets: targets,
chaincodePath: config.deployRequest.chaincodePath,
chaincodeId: chaincodeID,
fcn: config.deployRequest.functionName,
args: args
};
return admin.sendDeploymentProposal(request);
},
function(err) {
console.log('Failed to enroll user \'admin\'. ' + err);
process.exit();
}
).then(
function(results) {
var proposalResponses = results[0];
var proposal = results[1];
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
console.log(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));
//chaincodeID = proposalResponse.chaincodeId;
return webUser.sendTransaction(proposalResponses, proposal);
} else {
console.log('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
process.exit(0);
}
},
function(err) {
console.log('Failed to send deployment proposal due to error: ' + err.stack ? err.stack : err);
process.exit(0);
}
).then(
function(response) {
if (response.Status === 'SUCCESS') {
console.log('Successfully ordered deployment endorsement.');
console.log(' need to wait now for the committer to catch up');
return sleep(parseInt(config.deployWaitTime));
} else {
console.log('Failed to order the deployment endorsement. Error code: ' + response.status);
process.exit();
}
},
function(err) {
console.log('Failed to send deployment e due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function() {
var args = getArgs(config.invokeRequest.args);
// send proposal to endorser
var request = {
targets: targets,
chaincodeId: chaincodeID,
fcn: config.invokeRequest.functionName,
args: args
};
return webUser.sendTransactionProposal(request);
},
function(err) {
console.log('Failed to wait due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function(results) {
var proposalResponses = results[0];
var proposal = results[1];
if (proposalResponses[0].response.status === 200) {
console.log('Successfully obtained transaction endorsement.' + JSON.stringify(proposalResponses));
return webUser.sendTransaction(proposalResponses, proposal);
} else {
console.log('Failed to obtain transaction endorsement. Error code: ' + status);
process.exit();
}
},
function(err) {
console.log('Failed to send transaction proposal due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function(response) {
if (response.Status === 'SUCCESS') {
console.log('Successfully ordered endorsement transaction.');
console.log(' need to wait now for the committer to catch up');
return sleep(20000);
return sleep(parseInt(config.transactionWaitTime));
} else {
console.log('Failed to order the endorsement of the transaction. Error code: ' + response.status);
process.exit();
}
},
function(err) {
console.log('Failed to send transaction proposal due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function() {
var args = getArgs(config.queryRequest.args);
// send query
var request = {
targets: targets,
chaincodeId: chaincodeID,
fcn: config.queryRequest.functionName,
args: args
};
return webUser.queryByChaincode(request);
},
function(err) {
console.log('Failed to wait-- error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function(response_payload) {
console.log('User b now has ', response_payload.toString('utf8'));
process.exit();
},
function(err) {
console.log('Failed to send query due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).catch(
function(err) {
console.log('Failed to end to end test with error:' + err.stack ? err.stack : err);
process.exit();
}
);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getArgs(requestArgs) {
var args = [];
for (var i = 0; i < requestArgs.length; i++) {
args.push(requestArgs[i]);
}
return args;
}