-
Notifications
You must be signed in to change notification settings - Fork 519
/
chaincode.js
203 lines (163 loc) · 5.03 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
/*
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_cc1');
// 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_cc1 Init ###########');
// test the transient map support with chaincode instantiation
return this.testTransient(stub);
}
async Invoke(stub) {
logger.info('########### example_cc1 Invoke ###########');
const ret = stub.getFunctionAndParameters();
const fcn = ret.fcn;
const args = ret.params;
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 === 'echo') {
return this.echo(stub, args);
}
if (fcn === 'testTransient') {
return this.testTransient(stub);
}
logger.Errorf(`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) {
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 + 10;
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()));
return shim.success(Buffer.from('move succeed'));
} catch (e) {
return shim.error(e);
}
}
async delete(stub, args) {
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) {
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 testTransient(stub) {
let tm;
try {
tm = stub.getTransient();
} catch (e) {
logger.error('Did not find expected transient map in the proposal');
return shim.error(Buffer.from('{"Error":"Did not find expected transient map in the proposal}'));
}
const v = tm.get('test');
if (!v) {
logger.error('Did not find expected key "test" in the transient map of the proposal');
return shim.error(Buffer.from('{"Error":"Did not find expected key "test" in the transient map of the proposal}'));
}
return shim.success(v);
}
/*
* Used to return what's in the input for testing purposes
* */
async echo(stub, args) {
logger.info('Echo Response\n');
return shim.success(Buffer.from(args[0]));
}
};
shim.start(new Chaincode());