-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
718 lines (642 loc) · 21.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
'use strict';
/**
* A node-style callback as used by {@link logic} and {@link modules}.
* @see {@link https://nodejs.org/api/errors.html#errors_node_js_style_callbacks}
* @callback nodeStyleCallback
* @param {?Error} error - Error, if any, otherwise `null`.
* @param {Data} data - Data, if there hasn't been an error.
*/
/**
* A triggered by setImmediate callback as used by {@link logic}, {@link modules} and {@link helpers}.
* Parameters formats: (cb, error, data), (cb, error), (cb).
* @see {@link https://nodejs.org/api/timers.html#timers_setimmediate_callback_args}
* @callback setImmediateCallback
* @param {function} cb - Callback function.
* @param {?Error} [error] - Error, if any, otherwise `null`.
* @param {Data} [data] - Data, if there hasn't been an error and the function should return data.
*/
/**
* Main entry point.
* Loads the lisk modules, the lisk api and run the express server as Domain master.
* CLI options available.
* @module app
*/
var async = require('async');
var checkIpInList = require('./helpers/checkIpInList.js');
var extend = require('extend');
var fs = require('fs');
var genesisblock = require('./genesisBlock.json');
var git = require('./helpers/git.js');
var https = require('https');
var Logger = require('./logger.js');
var packageJson = require('./package.json');
var path = require('path');
var program = require('commander');
var httpApi = require('./helpers/httpApi.js');
var Sequence = require('./helpers/sequence.js');
var util = require('util');
var z_schema = require('./helpers/z_schema.js');
process.stdin.resume();
var versionBuild = '';
/**
* @property {string} - Hash of last git commit.
*/
var lastCommit = '';
if (typeof gc !== 'undefined') {
setInterval(function () {
gc();
}, 60000);
}
program
.version(packageJson.version)
.option('-c, --config <path>', 'config file path')
.option('-p, --port <port>', 'listening port number')
.option('-a, --address <ip>', 'listening host name or ip')
.option('-x, --peers [peers...]', 'peers list')
.option('-l, --log <level>', 'log level')
.option('-s, --snapshot <round>', 'verify snapshot')
.parse(process.argv);
/**
* @property {object} - The default list of configuration options. Can be updated by CLI.
* @default 'config.json'
*/
var appConfig = require('./helpers/config.js')(program.config);
if (program.port) {
appConfig.port = program.port;
}
if (program.address) {
appConfig.address = program.address;
}
if (program.peers) {
if (typeof program.peers === 'string') {
appConfig.peers.list = program.peers.split(',').map(function (peer) {
peer = peer.split(':');
return {
ip: peer.shift(),
port: peer.shift() || appConfig.port
};
});
} else {
appConfig.peers.list = [];
}
}
if (program.log) {
appConfig.consoleLogLevel = program.log;
}
if (program.snapshot) {
appConfig.loading.snapshot = Math.abs(
Math.floor(program.snapshot)
);
}
if (process.env.NODE_ENV === 'test') {
appConfig.coverage = true;
}
// Define top endpoint availability
process.env.TOP = appConfig.topAccounts;
/**
* The config object to handle lisk modules and lisk api.
* It loads `modules` and `api` folders content.
* Also contains db configuration from config.json.
* @property {object} db - Config values for database.
* @property {object} modules - `modules` folder content.
* @property {object} api - `api/http` folder content.
*/
var config = {
db: appConfig.db,
cache: appConfig.redis,
cacheEnabled: appConfig.cacheEnabled,
modules: {
server: './modules/server.js',
accounts: './modules/accounts.js',
transactions: './modules/transactions.js',
blocks: './modules/blocks.js',
signatures: './modules/signatures.js',
transport: './modules/transport.js',
loader: './modules/loader.js',
system: './modules/system.js',
peers: './modules/peers.js',
delegates: './modules/delegates.js',
rounds: './modules/rounds.js',
multisignatures: './modules/multisignatures.js',
dapps: './modules/dapps.js',
crypto: './modules/crypto.js',
sql: './modules/sql.js',
cache: './modules/cache.js'
},
api: {
accounts: { http: './api/http/accounts.js' },
blocks: { http: './api/http/blocks.js' },
dapps: { http: './api/http/dapps.js' },
delegates: { http: './api/http/delegates.js' },
loader: { http: './api/http/loader.js' },
multisignatures: { http: './api/http/multisignatures.js' },
peers: { http: './api/http/peers.js' },
server: { http: './api/http/server.js' },
signatures: { http: './api/http/signatures.js' },
transactions: { http: './api/http/transactions.js' },
transport: { http: './api/http/transport.js' }
}
};
/**
* Logger holder so we can log with custom functionality.
* The Object is initialized here and pass to others as parameter.
* @property {object} - Logger instance.
*/
var logger = new Logger({ echo: appConfig.consoleLogLevel, errorLevel: appConfig.fileLogLevel, filename: appConfig.logFileName, append: appConfig.logAppend });
// Trying to get last git commit
try {
lastCommit = git.getLastCommit();
versionBuild = git.getCommitDate();
} catch (err) {
logger.debug('Cannot get last git commit', err.message);
}
/**
* Creates the express server and loads all the Modules and logic.
* @property {object} - Domain instance.
*/
var d = require('domain').create();
d.on('error', function (err) {
logger.fatal('Domain master', { message: err.message, stack: err.stack });
process.exit(0);
});
// runs domain
d.run(function () {
var modules = [];
async.auto({
/**
* Loads `payloadHash` and generate dapp password if it is empty and required.
* Then updates config.json with new random password.
* @method config
* @param {nodeStyleCallback} cb - Callback function with the mutated `appConfig`.
* @throws {Error} If failed to assign nethash from genesis block.
*/
config: function (cb) {
try {
appConfig.nethash = Buffer.from(genesisblock.payloadHash, 'hex').toString('hex');
} catch (e) {
logger.error('Failed to assign nethash from genesis block');
throw Error(e);
}
if (appConfig.dapp.masterrequired && !appConfig.dapp.masterpassword) {
var randomstring = require('randomstring');
appConfig.dapp.masterpassword = randomstring.generate({
length: 12,
readable: true,
charset: 'alphanumeric'
});
if (appConfig.loading.snapshot != null) {
delete appConfig.loading.snapshot;
}
fs.writeFileSync('./config.json', JSON.stringify(appConfig, null, 4));
cb(null, appConfig);
} else {
cb(null, appConfig);
}
},
logger: function (cb) {
cb(null, logger);
},
build: function (cb) {
cb(null, versionBuild);
},
/**
* Returns hash of last git commit.
* @method lastCommit
* @param {nodeStyleCallback} cb - Callback function with Hash of last git commit.
*/
lastCommit: function (cb) {
cb(null, lastCommit);
},
genesisblock: function (cb) {
cb(null, {
block: genesisblock
});
},
public: function (cb) {
cb(null, path.join(__dirname, 'public'));
},
schema: function (cb) {
cb(null, new z_schema());
},
/**
* Once config is completed, creates app, http & https servers & sockets with express.
* @method network
* @param {object} scope - The results from current execution,
* at least will contain the required elements.
* @param {nodeStyleCallback} cb - Callback function with created Object:
* `{express, app, server, io, https, https_io}`.
*/
network: ['config', function (scope, cb) {
var express = require('express');
var compression = require('compression');
var cors = require('cors');
var app = express();
if (appConfig.coverage) {
var im = require('istanbul-middleware');
logger.debug('Hook loader for coverage - do not use in production environment!');
im.hookLoader(__dirname);
app.use('/coverage', im.createHandler());
}
require('./helpers/request-limiter')(app, appConfig);
app.use(compression({ level: 9 }));
app.use(cors());
app.options('*', cors());
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var privateKey, certificate, https, https_io;
if (scope.config.ssl.enabled) {
privateKey = fs.readFileSync(scope.config.ssl.options.key);
certificate = fs.readFileSync(scope.config.ssl.options.cert);
https = require('https').createServer({
key: privateKey,
cert: certificate,
ciphers: 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:' + 'ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA256:HIGH:' + '!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!SRP:!CAMELLIA'
}, app);
https_io = require('socket.io')(https);
}
cb(null, {
express: express,
app: app,
server: server,
io: io,
https: https,
https_io: https_io
});
}],
dbSequence: ['logger', function (scope, cb) {
var sequence = new Sequence({
onWarning: function (current, limit) {
scope.logger.warn('DB queue', current);
}
});
cb(null, sequence);
}],
sequence: ['logger', function (scope, cb) {
var sequence = new Sequence({
onWarning: function (current, limit) {
scope.logger.warn('Main queue', current);
}
});
cb(null, sequence);
}],
balancesSequence: ['logger', function (scope, cb) {
var sequence = new Sequence({
onWarning: function (current, limit) {
scope.logger.warn('Balance queue', current);
}
});
cb(null, sequence);
}],
/**
* Once config, public, genesisblock, logger, build and network are completed,
* adds configuration to `network.app`.
* @method connect
* @param {object} scope - The results from current execution,
* at leats will contain the required elements.
* @param {function} cb - Callback function.
*/
connect: ['config', 'public', 'genesisblock', 'logger', 'build', 'network', function (scope, cb) {
var path = require('path');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var queryParser = require('express-query-int');
var randomString = require('randomstring');
scope.nonce = randomString.generate(16);
scope.network.app.engine('html', require('ejs').renderFile);
scope.network.app.use(require('express-domain-middleware'));
scope.network.app.set('view engine', 'ejs');
scope.network.app.set('views', path.join(__dirname, 'public'));
scope.network.app.use(scope.network.express.static(path.join(__dirname, 'public')));
scope.network.app.use(bodyParser.raw({limit: '2mb'}));
scope.network.app.use(bodyParser.urlencoded({extended: true, limit: '2mb', parameterLimit: 5000}));
scope.network.app.use(bodyParser.json({limit: '2mb'}));
scope.network.app.use(methodOverride());
var ignore = ['id', 'name', 'lastBlockId', 'blockId', 'transactionId', 'address', 'recipientId', 'senderId', 'previousBlock'];
scope.network.app.use(queryParser({
parser: function (value, radix, name) {
if (ignore.indexOf(name) >= 0) {
return value;
}
// Ignore conditional fields for transactions list
if (/^.+?:(blockId|recipientId|senderId)$/.test(name)) {
return value;
}
/*eslint-disable eqeqeq */
if (isNaN(value) || parseInt(value) != value || isNaN(parseInt(value, radix))) {
return value;
}
/*eslint-enable eqeqeq */
return parseInt(value);
}
}));
scope.network.app.use(require('./helpers/z_schema-express.js')(scope.schema));
scope.network.app.use(httpApi.middleware.logClientConnections.bind(null, scope.logger));
/* Instruct browser to deny display of <frame>, <iframe> regardless of origin.
*
* RFC -> https://tools.ietf.org/html/rfc7034
*/
scope.network.app.use(httpApi.middleware.attachResponseHeader.bind(null, 'X-Frame-Options', 'DENY'));
/* Set Content-Security-Policy headers.
*
* frame-ancestors - Defines valid sources for <frame>, <iframe>, <object>, <embed> or <applet>.
*
* W3C Candidate Recommendation -> https://www.w3.org/TR/CSP/
*/
scope.network.app.use(httpApi.middleware.attachResponseHeader.bind(null, 'Content-Security-Policy', 'frame-ancestors \'none\''));
scope.network.app.use(httpApi.middleware.applyAPIAccessRules.bind(null, scope.config));
cb();
}],
ed: function (cb) {
cb(null, require('./helpers/ed.js'));
},
bus: ['ed', function (scope, cb) {
var changeCase = require('change-case');
var bus = function () {
this.message = function () {
var args = [];
Array.prototype.push.apply(args, arguments);
var topic = args.shift();
var eventName = 'on' + changeCase.pascalCase(topic);
// executes the each module onBind function
modules.forEach(function (module) {
if (typeof(module[eventName]) === 'function') {
module[eventName].apply(module[eventName], args);
}
if (module.submodules) {
async.each(module.submodules, function (submodule) {
if (submodule && typeof(submodule[eventName]) === 'function') {
submodule[eventName].apply(submodule[eventName], args);
}
});
}
});
};
};
cb(null, new bus());
}],
db: function (cb) {
var db = require('./helpers/database.js');
db.connect(config.db, logger, cb);
},
/**
* It tries to connect with redis server based on config. provided in config.json file
* @param {function} cb
*/
cache: function (cb) {
var cache = require('./helpers/cache.js');
cache.connect(config.cacheEnabled, config.cache, logger, cb);
},
/**
* Once db, bus, schema and genesisblock are completed,
* loads transaction, block, account and peers from logic folder.
* @method logic
* @param {object} scope - The results from current execution,
* at leats will contain the required elements.
* @param {function} cb - Callback function.
*/
logic: ['db', 'bus', 'schema', 'genesisblock', function (scope, cb) {
var Transaction = require('./logic/transaction.js');
var Block = require('./logic/block.js');
var Account = require('./logic/account.js');
var Peers = require('./logic/peers.js');
async.auto({
bus: function (cb) {
cb(null, scope.bus);
},
db: function (cb) {
cb(null, scope.db);
},
ed: function (cb) {
cb(null, scope.ed);
},
logger: function (cb) {
cb(null, logger);
},
schema: function (cb) {
cb(null, scope.schema);
},
genesisblock: function (cb) {
cb(null, {
block: genesisblock
});
},
account: ['db', 'bus', 'ed', 'schema', 'genesisblock', 'logger', function (scope, cb) {
new Account(scope.db, scope.schema, scope.logger, cb);
}],
transaction: ['db', 'bus', 'ed', 'schema', 'genesisblock', 'account', 'logger', function (scope, cb) {
new Transaction(scope.db, scope.ed, scope.schema, scope.genesisblock, scope.account, scope.logger, cb);
}],
block: ['db', 'bus', 'ed', 'schema', 'genesisblock', 'account', 'transaction', function (scope, cb) {
new Block(scope.ed, scope.schema, scope.transaction, cb);
}],
peers: ['logger', function (scope, cb) {
new Peers(scope.logger, cb);
}]
}, cb);
}],
/**
* Once network, connect, config, logger, bus, sequence,
* dbSequence, balancesSequence, db and logic are completed,
* loads modules from `modules` folder using `config.modules`.
* @method modules
* @param {object} scope - The results from current execution,
* at leats will contain the required elements.
* @param {nodeStyleCallback} cb - Callback function with resulted load.
*/
modules: ['network', 'connect', 'config', 'logger', 'bus', 'sequence', 'dbSequence', 'balancesSequence', 'db', 'logic', 'cache', function (scope, cb) {
var tasks = {};
Object.keys(config.modules).forEach(function (name) {
tasks[name] = function (cb) {
var d = require('domain').create();
d.on('error', function (err) {
scope.logger.fatal('Domain ' + name, {message: err.message, stack: err.stack});
});
d.run(function () {
logger.debug('Loading module', name);
var moduleClass = require(config.modules[name]);
var obj = new moduleClass(cb, scope);
modules.push(obj);
});
};
});
async.parallel(tasks, function (err, results) {
cb(err, results);
});
}],
/**
* Loads api from `api` folder using `config.api`, once modules, logger and
* network are completed.
* @method api
* @param {object} scope - The results from current execution,
* at leats will contain the required elements.
* @param {function} cb - Callback function.
*/
api: ['modules', 'logger', 'network', function (scope, cb) {
Object.keys(config.api).forEach(function (moduleName) {
Object.keys(config.api[moduleName]).forEach(function (protocol) {
var apiEndpointPath = config.api[moduleName][protocol];
try {
var ApiEndpoint = require(apiEndpointPath);
new ApiEndpoint(scope.modules[moduleName], scope.network.app, scope.logger, scope.modules.cache);
} catch (e) {
scope.logger.error('Unable to load API endpoint for ' + moduleName + ' of ' + protocol, e);
}
});
});
scope.network.app.use(httpApi.middleware.errorLogger.bind(null, scope.logger));
cb();
}],
ready: ['modules', 'bus', 'logic', function (scope, cb) {
scope.bus.message('bind', scope.modules);
scope.logic.transaction.bindModules(scope.modules);
scope.logic.peers.bindModules(scope.modules);
cb();
}],
/**
* Once 'ready' is completed, binds and listens for connections on the
* specified host and port for `scope.network.server`.
* @method listen
* @param {object} scope - The results from current execution,
* at leats will contain the required elements.
* @param {nodeStyleCallback} cb - Callback function with `scope.network`.
*/
listen: ['ready', function (scope, cb) {
scope.network.server.listen(scope.config.port, scope.config.address, function (err) {
scope.logger.info('Shift started: ' + scope.config.address + ':' + scope.config.port);
if (!err) {
if (scope.config.ssl.enabled) {
scope.network.https.listen(scope.config.ssl.options.port, scope.config.ssl.options.address, function (err) {
scope.logger.info('Shift https started: ' + scope.config.ssl.options.address + ':' + scope.config.ssl.options.port);
cb(err, scope.network);
});
} else {
cb(null, scope.network);
}
} else {
cb(err, scope.network);
}
});
}]
}, function (err, scope) {
if (err) {
logger.fatal(err);
} else {
/**
* Handles app instance (acts as global variable, passed as parameter).
* @global
* @typedef {Object} scope
* @property {Object} api - Undefined.
* @property {undefined} balancesSequence - Sequence function, sequence Array.
* @property {string} build - Empty.
* @property {Object} bus - Message function, bus constructor.
* @property {Object} config - Configuration.
* @property {undefined} connect - Undefined.
* @property {Object} db - Database constructor, database functions.
* @property {function} dbSequence - Database function.
* @property {Object} ed - Crypto functions from lisk node-sodium.
* @property {Object} genesisblock - Block information.
* @property {string} lastCommit - Hash transaction.
* @property {Object} listen - Network information.
* @property {Object} logger - Log functions.
* @property {Object} logic - several logic functions and objects.
* @property {Object} modules - Several modules functions.
* @property {Object} network - Several network functions.
* @property {string} nonce
* @property {string} public - Path to lisk public folder.
* @property {undefined} ready
* @property {Object} schema - ZSchema with objects.
* @property {Object} sequence - Sequence function, sequence Array.
* @todo logic repeats: bus, ed, genesisblock, logger, schema.
* @todo description for nonce and ready
*/
scope.logger.info('Modules ready and launched');
/**
* Event reporting a cleanup.
* @event cleanup
*/
/**
* Receives a 'cleanup' signal and cleans all modules.
* @listens cleanup
*/
process.once('cleanup', function () {
scope.logger.info('Cleaning up...');
async.eachSeries(modules, function (module, cb) {
if (typeof(module.cleanup) === 'function') {
module.cleanup(cb);
} else {
setImmediate(cb);
}
}, function (err) {
if (err) {
scope.logger.error(err);
} else {
scope.logger.info('Cleaned up successfully');
}
process.exit(1);
});
});
/**
* Event reporting a SIGTERM.
* @event SIGTERM
*/
/**
* Receives a 'SIGTERM' signal and emits a cleanup.
* @listens SIGTERM
*/
process.once('SIGTERM', function () {
/**
* emits cleanup once 'SIGTERM'.
* @emits cleanup
*/
process.emit('cleanup');
});
/**
* Event reporting an exit.
* @event exit
*/
/**
* Receives an 'exit' signal and emits a cleanup.
* @listens exit
*/
process.once('exit', function () {
/**
* emits cleanup once 'exit'.
* @emits cleanup
*/
process.emit('cleanup');
});
/**
* Event reporting a SIGINT.
* @event SIGINT
*/
/**
* Receives a 'SIGINT' signal and emits a cleanup.
* @listens SIGINT
*/
process.once('SIGINT', function () {
/**
* emits cleanup once 'SIGINT'.
* @emits cleanup
*/
process.emit('cleanup');
});
}
});
});
/**
* Event reporting an uncaughtException.
* @event uncaughtException
*/
/**
* Receives a 'uncaughtException' signal and emits a cleanup.
* @listens uncaughtException
*/
process.on('uncaughtException', function (err) {
// Handle error safely
logger.fatal('System error', { message: err.message, stack: err.stack });
/**
* emits cleanup once 'uncaughtException'.
* @emits cleanup
*/
process.emit('cleanup');
});