-
Notifications
You must be signed in to change notification settings - Fork 519
/
chaincode.js
273 lines (219 loc) · 6.87 KB
/
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
266
267
268
269
270
271
272
273
/*
Copyright IBM Corp. 2016 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.
*/
// This is a node-js version of example_02.go
const shim = require('fabric-shim');
// An log4js logger instance
const logger = shim.newLogger('example_cc0');
// The logger level can also be set by environment variable 'CORE_CHAINCODE_LOGGING_SHIM'
// to CRITICAL, ERROR, WARNING, DEBUG
logger.level = 'info';
const Chaincode = class {
async Init(stub) {
logger.info('########### example_cc0 Init ###########');
const ret = stub.getFunctionAndParameters();
let A, B; // Entities
let Aval, Bval; // Asset holdings
const args = ret.params;
if (args.length === 4) {
A = args[0];
B = args[2];
Aval = parseInt(args[1]);
if (isNaN(Aval)) {
return shim.error('Expecting integer value for asset holding');
}
Bval = parseInt(args[3]);
if (isNaN(Bval)) {
return shim.error('Expecting integer value for asset holding');
}
logger.info(`Aval = ${Aval}, Bval = ${Bval}`);
try {
// Write the state to the ledger
await stub.putState(A, Buffer.from(Aval.toString()));
await stub.putState(B, Buffer.from(Bval.toString()));
return shim.success();
} catch (e) {
return shim.error(e);
}
} else {
return shim.error('init expects 4 args');
}
}
async Invoke(stub) {
logger.info('########### example_cc0 Invoke ###########');
const ret = stub.getFunctionAndParameters();
const fcn = ret.fcn;
const args = ret.params;
logger.info('-stub.getFunctionAndParameters(): ', JSON.stringify(ret));
try {
if (fcn === 'delete') {
return this.delete(stub, args);
}
if (fcn === 'query') {
return this.query(stub, args);
}
if (fcn === 'returnError') {
return this.returnError(stub, args);
}
if (fcn === 'throwError') {
return this.throwError(stub, args);
}
if (fcn === 'move') {
return this.move(stub, args);
}
if (fcn === 'call') {
return this.call(stub, args);
}
if (fcn === 'getTransient') {
return this.getTransient(stub, args);
}
if (fcn === 'echo') {
return this.echo(stub, args);
}
if (fcn === 'init') {
return this.Init(stub); // Use this when you wish to manage the initialization
// return shim.error('Chaincode has not been initialized correctly');
}
} catch (error) {
return shim.error(error.toString());
}
logger.error(`Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: ${fcn}`);
return shim.error(`Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: ${fcn}`);
}
async move(stub, args) {
logger.info('########### example_cc0 move ###########');
let Aval, Bval;
if (args.length !== 3) {
return shim.error('Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value');
}
const A = args[0];
const B = args[1];
try {
const Avalbytes = await stub.getState(A);
if (!Avalbytes) {
return shim.error('Entity A not found');
}
Aval = Avalbytes.toString();
Aval = parseInt(Aval);
} catch (e) {
return shim.error('Failed to get state A');
}
try {
const Bvalbytes = await stub.getState(B);
if (!Bvalbytes) {
return shim.error('Entity B not found');
}
Bval = Bvalbytes.toString();
Bval = parseInt(Bval);
} catch (e) {
return shim.error('Failed to get state B');
}
// Perform the execution
const X = parseInt(args[2]);
if (isNaN(X)) {
return shim.error('Invalid transaction amount, expecting a integer value');
}
Aval = Aval - X;
Bval = Bval + X;
logger.info(`Aval = ${Aval}, Bval = ${Bval}`);
// Write the state back to the ledger
try {
await stub.putState(A, Buffer.from(Aval.toString()));
await stub.putState(B, Buffer.from(Bval.toString()));
logger.info(' example_cc0 - move succeed');
return shim.success(Buffer.from('move succeed'));
} catch (e) {
return shim.error(e);
}
}
async delete(stub, args) {
logger.info('########### example_cc0 delete ###########');
if (args.length !== 1) {
return shim.error('Incorrect number of arguments. Expecting 1');
}
const A = args[0];
try {
await stub.deleteState(A);
} catch (e) {
return shim.error('Failed to delete state');
}
return shim.success();
}
async query(stub, args) {
logger.info('########### example_cc0 query ###########');
if (args.length !== 1) {
return shim.error('Incorrect number of arguments. Expecting name of the person to query');
}
const A = args[0];
let Aval;
// Get the state from the ledger
try {
const Avalbytes = await stub.getState(A);
if (!Avalbytes) {
return shim.error('Entity A not found');
}
Aval = Avalbytes.toString();
} catch (e) {
return shim.error('Failed to get state A');
}
const jsonResp = {
Name: A,
Amount: Aval
};
logger.info('Query Response:%s\n', JSON.stringify(jsonResp));
return shim.success(Buffer.from(Aval.toString()));
}
async returnError(stub, args) {
return shim.error(new Error(args[0] || 'returnError: chaincode error response'));
}
async throwError(stub, args) {
throw new Error(args[0] || 'throwError: chaincode error thrown');
}
async call(stub, args) {
logger.info('########### example_cc0 call ###########');
if (args.length < 2) {
return shim.error('Incorrect number of arguments. Expecting name of the chaincode and function to call');
}
const chaincode_name = args.shift().toString();
logger.info('Calling chaincode:%s with function:%s argument 1:%s \n', chaincode_name, args[0].toString(), parseInt(args[1]));
let results = null;
// call the other chaincode
try {
results = await stub.invokeChaincode(chaincode_name, args);
logger.info(' example_cc0 - call succeeded %s', results);
} catch (e) {
logger.error('Failed to call chaincode ' + e);
}
if (results) {
return shim.success(Buffer.from('Success'));
}
return shim.error('Failed to complete the call to ' + chaincode_name);
}
async getTransient(stub) {
const transientMap = stub.getTransient();
const result = {};
transientMap.forEach((value, key) => {
result[key] = value.toString('utf8');
});
const payload = Buffer.from(JSON.stringify(result));
return shim.success(payload);
}
async echo(stub, args) {
stub.setEvent('echo', Buffer.from('content'));
if (args.length > 0) {
return shim.success(Buffer.from(args[0]));
} else {
return shim.success();
}
}
};
shim.start(new Chaincode());