forked from Zilliqa/kaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
284 lines (251 loc) · 9.18 KB
/
app.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
/*
This file is part of kaya.
Copyright (c) 2018 - present Zilliqa Research Pvt. Ltd.
kaya is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
kaya is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
kaya. If not, see <http://www.gnu.org/licenses/>.
*/
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const fs = require('fs');
const rimraf = require('rimraf');
const yargs = require('yargs');
const expressjs = express();
const config = require('./config');
const logic = require('./logic');
const wallet = require('./components/wallet/wallet');
const { prepareDirectories, logVerbose, consolePrint,
getDateTimeString, getDataFromDir, loadData, loadDataToDir } = require('./utilities');
const init = require('./argv');
const logLabel = 'App.js';
expressjs.use(bodyParser.json({ extended: false }));
const argv = init(yargs).argv;
const makeResponse = (id, jsonrpc, data, isErr) => {
const responseObj = { id, jsonrpc };
responseObj.result = isErr ? { Error: data } : data;
return responseObj;
}
const wrapAsync = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
if (argv.d.trim() === 'saved/') {
throw new Error('Saved dir is reserved for saved files');
}
// Stores all the option flags and configurations
// Console defined flag will override the config settings
let options = {
fixtures: argv.f,
numAccts: argv.n,
dataPath: argv.d,
remote: argv.r,
verbose: argv.v,
save: argv.s,
load: argv.l
}
consolePrint(`Running from ${options.remote ? 'remote' : 'local'} interpreter`)
if (options.remote) { consolePrint(config.scilla.url) };
consolePrint('='.repeat(80));
prepareDirectories(options.dataPath); // prepare the directories required
if (options.save) {
logVerbose(logLabel, 'Save enabled. Data files from this session will be saved');
}
if (options.load) {
// loading option specified
logVerbose(logLabel, 'Loading option specified. Loading files now...');
// loads file into dbPath from the given bootstrap file
const importedData = loadData(options.load);
wallet.loadAccounts(importedData.accounts);
logic.loadData(importedData.transactions, importedData.createdContractsByUsers);
loadDataToDir(options.dataPath, importedData);
logVerbose(logLabel, 'Load completed');
}
if (process.env.NODE_ENV === 'test') {
options.fixtures = 'test/account-fixtures.json';
}
/*
* Account creation/loading based on presets given
* @dev : Only create wallets if the user does not supply any load file
*/
if(!options.load) {
if (options.fixtures) {
logVerbose(logLabel, `Bootstrapping from account fixture files: ${options.fixtures}`);
const accountsPath = options.fixtures;
if (!fs.existsSync(accountsPath)) {
throw new Error('Account Path Invalid');
}
const accounts = JSON.parse(fs.readFileSync(accountsPath, 'utf-8'));
wallet.loadAccounts(accounts);
} else {
/* Create Dummy Accounts */
wallet.createWallets(options.numAccts);
}
}
wallet.printWallet();
// cross region settings with Env
if (process.env.NODE_ENV === 'dev') {
expressjs.use(cors());
logVerbose(logLabel, 'CORS Enabled');
}
expressjs.get('/', (req, res) => {
res.status(200).send('Kaya RPC Server');
});
// Method handling logic for incoming POST request
const handler = async (req, res) => {
const { body } = req;
let data = {};
let result;
let addr;
logVerbose(logLabel, `Method specified ${body.method}`);
switch (body.method) {
case 'GetBalance':
// [addr, ... ] = body.params;
addr = body.params[0];
if (typeof addr === 'object') {
addr = JSON.stringify(addr);
}
logVerbose(logLabel, `Getting balance for ${addr}`);
try {
data = wallet.getBalance(addr);
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetNetworkId':
data = makeResponse(body.id, body.jsonrpc, 'Testnet', false);
res.status(200).send(data);
break;
case 'GetSmartContractCode':
try {
result = logic.processGetDataFromContract(body.params, options.dataPath, 'code');
data = result;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetSmartContractState':
try {
result = logic.processGetDataFromContract(body.params, options.dataPath, 'state');
data = result;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetSmartContractInit':
try {
result = logic.processGetDataFromContract(body.params, options.dataPath, 'init');
data = result;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetSmartContracts':
try {
result = logic.processGetSmartContracts(body.params, options.dataPath);
data = result;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'CreateTransaction':
try {
const txnId = await logic.processCreateTxn(body.params, options);
data = txnId;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetTransaction':
try {
const obj = logic.processGetTransaction(body.params);
data = obj;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
case 'GetRecentTransactions':
try {
const obj = logic.processGetRecentTransactions();
data = obj;
} catch (err) {
data = err.message;
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, true));
break;
}
res.status(200).send(makeResponse(body.id, body.jsonrpc, data, false));
break;
default:
data = { Error: 'Unsupported Method' };
res.status(404).send(data);
}
logVerbose(logLabel, 'Sending response back to client');
};
expressjs.post('/', wrapAsync(handler));
// Function below handles the end of the session due to SIGINT. It will save
// data files if the `-s` flag is toggled and will remove all files from the data directory
process.on('SIGINT', function () {
consolePrint("Gracefully shutting down from SIGINT (Ctrl-C)");
// If `save` is enabled, store files under the saved/ directory
if (options.save) {
console.log(`Save mode enabled. Extracting data now..`);
const dir = config.savedFilesDir;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// Saved files will be prefixed with the timestamp when the user decides to end the session
const timestamp = getDateTimeString();
const outputData = `${dir}${timestamp}`;
const targetFilePath = `${outputData}_data.json`;
consolePrint(`Files will be saved at ${targetFilePath}`);
// Prepares Data to be exported
consolePrint('Extracting data...');
const data = logic.exportData();
data.accounts = wallet.getAccounts();
consolePrint(`[1/5] Transactions and account data extracted`);
data.states = getDataFromDir(options.dataPath, 'state.json');
consolePrint(`[2/5] Contract state data extracted`);
data.init = getDataFromDir(options.dataPath, 'init.json');
consolePrint(`[3/5] Contract init data extracted`);
data.codes = getDataFromDir(options.dataPath, 'code.scilla');
consolePrint(`[4/5] Contract code data extracted`);
// Writing to the final exported data file in JSON format
fs.writeFileSync(targetFilePath, JSON.stringify(data));
consolePrint(`[5/5] Data file written to ${targetFilePath}`);
consolePrint(`Save successful`)
}
// remove files from the db_path
rimraf.sync(`${options.dataPath}*`);
console.log(`Files from ${options.dataPath} removed. Shutting down now.`);
process.exit(0);
})
module.exports = {
expressjs,
wallet,
};