forked from AschPlatform/asch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
239 lines (206 loc) · 6.57 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
var assert = require('assert');
var crypto = require('crypto');
var program = require('commander');
var path = require('path');
var fs = require('fs');
var async = require('async');
var Logger = require('./src/logger');
var init = require('./src/init');
function verifyGenesisBlock(scope, block) {
try {
var payloadHash = crypto.createHash('sha256');
var payloadLength = 0;
for (var i = 0; i < block.transactions.length; ++i) {
var trs = block.transactions[i];
var bytes = scope.base.transaction.getBytes(trs);
payloadLength += bytes.length;
payloadHash.update(bytes);
}
var id = scope.base.block.getId(block);
assert.equal(payloadLength, block.payloadLength, 'Unexpected payloadLength');
assert.equal(payloadHash.digest().toString('hex'), block.payloadHash, 'Unexpected payloadHash');
assert.equal(id, block.id, 'Unexpected block id');
// assert.equal(id, '11839820784468442760', 'Block id is incorrect');
} catch (e) {
assert(false, 'Failed to verify genesis block: ' + e);
}
}
function main() {
process.stdin.resume();
var version = '1.3.1';
program
.version(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('-b, --blockchain <path>', 'Blockchain db path')
.option('-g, --genesisblock <path>', 'Genesisblock path')
.option('-x, --peers [peers...]', 'Peers list')
.option('-l, --log <level>', 'Log level')
.option('-d, --daemon', 'Run asch node as daemon')
.option('-e, --execute <path>', 'exe')
.option('--dapps <dir>', 'DApps directory')
.option('--base <dir>', 'Base directory')
.parse(process.argv);
var baseDir = program.base || './';
var pidFile = path.join(baseDir, 'asch.pid');
if (fs.existsSync(pidFile)) {
console.log('Failed: asch server already started');
return;
}
var appConfigFile = path.join(baseDir, 'config.json');
if (program.config) {
appConfigFile = path.resolve(process.cwd(), program.config);
}
var appConfig = JSON.parse(fs.readFileSync(appConfigFile, 'utf8'));
if (!appConfig.dapp.masterpassword) {
var randomstring = require("randomstring");
appConfig.dapp.masterpassword = randomstring.generate({
length: 12,
readable: true,
charset: 'alphanumeric'
});
fs.writeFileSync(appConfigFile, JSON.stringify(appConfig, null, 2), "utf8");
}
appConfig.version = version;
appConfig.baseDir = baseDir;
appConfig.buildVersion = 'development';
appConfig.netVersion = process.env.NET_VERSION || 'localnet';
appConfig.publicDir = path.join(baseDir, 'public', 'dist');
appConfig.dappsDir = program.dapps || path.join(baseDir, 'dapps')
global.Config = appConfig;
var genesisblockFile = path.join(baseDir, 'genesisBlock.json');
if (program.genesisblock) {
genesisblockFile = path.resolve(process.cwd(), program.genesisblock);
}
var genesisblock = JSON.parse(fs.readFileSync(genesisblockFile, 'utf8'));
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 (appConfig.netVersion === 'mainnet') {
var seeds = [
757137132,
1815983436,
759980934,
759980683,
1807690192,
1758431015,
1760474482,
1760474149,
759110497,
757134616
];
var ip = require('ip');
for (var i = 0; i < seeds.length; ++i) {
appConfig.peers.list.push({ ip: ip.fromLong(seeds[i]), port: 80 });
}
}
if (program.log) {
appConfig.logLevel = program.log;
}
var protoFile = path.join(baseDir, 'proto', 'index.proto');
if (!fs.existsSync(protoFile)) {
console.log('Failed: proto file not exists!');
return;
}
if (program.daemon) {
console.log('Asch server started as daemon ...');
require('daemon')({cwd: process.cwd()});
fs.writeFileSync(pidFile, process.pid, 'utf8');
}
var logger = new Logger({
filename: path.join(baseDir, 'logs', 'debug.log'),
echo: program.deamon ? null : appConfig.logLevel,
errorLevel: appConfig.logLevel
});
var options = {
dbFile: program.blockchain || path.join(baseDir, 'blockchain.db'),
appConfig: appConfig,
genesisblock: genesisblock,
logger: logger,
protoFile: protoFile
};
if (program.reindex) {
appConfig.loading.verifyOnLoading = true;
}
global.featureSwitch = {}
global.state = {}
init(options, function (err, scope) {
if (err) {
scope.logger.fatal(err);
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
}
process.exit(1);
return;
}
verifyGenesisBlock(scope, scope.genesisblock.block);
if (program.execute) {
// only for debug use
// require(path.resolve(program.execute))(scope);
}
scope.bus.message('bind', scope.modules);
global.modules = scope.modules
scope.logger.info('Modules ready and launched');
if (!scope.config.publicIp) {
scope.logger.warn('Failed to get public ip, block forging MAY not work!');
}
process.once('cleanup', function () {
scope.logger.info('Cleaning up...');
async.eachSeries(scope.modules, function (module, cb) {
if (typeof (module.cleanup) == 'function') {
module.cleanup(cb);
} else {
setImmediate(cb);
}
}, function (err) {
if (err) {
scope.logger.error('Error while cleaning up', err);
} else {
scope.logger.info('Cleaned up successfully');
}
scope.dbLite.close();
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
}
process.exit(1);
});
});
process.once('SIGTERM', function () {
process.emit('cleanup');
})
process.once('exit', function () {
scope.logger.info('process exited');
});
process.once('SIGINT', function () {
process.emit('cleanup');
});
process.on('uncaughtException', function (err) {
// handle the error safely
scope.logger.fatal('uncaughtException', { message: err.message, stack: err.stack });
process.emit('cleanup');
});
if (typeof gc !== 'undefined') {
setInterval(function () {
gc();
}, 60000);
}
});
}
main();