forked from duniter/duniter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
475 lines (426 loc) · 16.6 KB
/
server.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
"use strict";
var stream = require('stream');
var async = require('async');
var util = require('util');
var path = require('path');
var co = require('co');
var _ = require('underscore');
var Q = require('q');
var parsers = require('./app/lib/streams/parsers/doc');
var constants = require('./app/lib/constants');
var fileDAL = require('./app/lib/dal/fileDAL');
var jsonpckg = require('./package.json');
var router = require('./app/lib/streams/router');
var base58 = require('./app/lib/base58');
var crypto = require('./app/lib/crypto');
var signature = require('./app/lib/signature');
var directory = require('./app/lib/directory');
var dos2unix = require('./app/lib/dos2unix');
var Synchroniser = require('./app/lib/sync');
var multicaster = require('./app/lib/streams/multicaster');
var upnp = require('./app/lib/upnp');
var bma = require('./app/lib/streams/bma');
var rawer = require('./app/lib/rawer');
function Server (dbConf, overrideConf) {
stream.Duplex.call(this, { objectMode: true });
let home = directory.getHome(dbConf.name, dbConf.home);
let paramsP = directory.getHomeParams(dbConf && dbConf.memory, home);
let logger = require('./app/lib/logger')('server');
let that = this;
that.conf = null;
that.dal = null;
that.version = jsonpckg.version;
that.MerkleService = require("./app/service/MerkleService");
that.ParametersService = require("./app/service/ParametersService")();
that.IdentityService = require('./app/service/IdentityService')();
that.MembershipService = require('./app/service/MembershipService')();
that.PeeringService = require('./app/service/PeeringService')(that);
that.BlockchainService = require('./app/service/BlockchainService')();
that.TransactionsService = require('./app/service/TransactionsService')();
// Create document mapping
let documentsMapping = {
'identity': { action: that.IdentityService.submitIdentity, parser: parsers.parseIdentity },
'certification': { action: that.IdentityService.submitCertification, parser: parsers.parseCertification},
'revocation': { action: that.IdentityService.submitRevocation, parser: parsers.parseRevocation },
'membership': { action: that.MembershipService.submitMembership, parser: parsers.parseMembership },
'peer': { action: that.PeeringService.submitP, parser: parsers.parsePeer },
'transaction': { action: that.TransactionsService.processTx, parser: parsers.parseTransaction },
'block': { action: _.partial(that.BlockchainService.submitBlock, _, true, constants.NO_FORK_ALLOWED), parser: parsers.parseBlock }
};
// Unused, but made mandatory by Duplex interface
this._read = () => null;
this._write = (obj, enc, writeDone) => that.submit(obj, false, () => writeDone);
/**
* Facade method to control what is pushed to the stream (we don't want it to be closed)
* @param obj An object to be pushed to the stream.
*/
this.streamPush = (obj) => {
if (obj) {
that.push(obj);
}
};
this.plugFileSystem = () => co(function *() {
logger.debug('Plugging file system...');
let params = yield paramsP;
that.dal = fileDAL(params);
});
this.softResetData = () => co(function *() {
logger.debug('Soft data reset... [cache]');
yield that.dal.cleanCaches();
logger.debug('Soft data reset... [data]');
yield that.cleanDBData();
});
this.loadConf = (useDefaultConf) => co(function *() {
logger.debug('Loading conf...');
that.conf = yield that.dal.loadConf(overrideConf, useDefaultConf);
// Default values
var defaultValues = {
remoteipv6: that.conf.ipv6,
remoteport: that.conf.port,
cpu: constants.DEFAULT_CPU,
c: constants.CONTRACT.DEFAULT.C,
dt: constants.CONTRACT.DEFAULT.DT,
ud0: constants.CONTRACT.DEFAULT.UD0,
stepMax: constants.CONTRACT.DEFAULT.STEPMAX,
sigPeriod: constants.CONTRACT.DEFAULT.SIGPERIOD,
sigStock: constants.CONTRACT.DEFAULT.SIGSTOCK,
sigWindow: constants.CONTRACT.DEFAULT.SIGWINDOW,
sigValidity: constants.CONTRACT.DEFAULT.SIGVALIDITY,
msValidity: constants.CONTRACT.DEFAULT.MSVALIDITY,
sigQty: constants.CONTRACT.DEFAULT.SIGQTY,
idtyWindow: constants.CONTRACT.DEFAULT.IDTYWINDOW,
msWindow: constants.CONTRACT.DEFAULT.MSWINDOW,
xpercent: constants.CONTRACT.DEFAULT.X_PERCENT,
percentRot: constants.CONTRACT.DEFAULT.PERCENTROT,
blocksRot: constants.CONTRACT.DEFAULT.BLOCKSROT,
powDelay: constants.CONTRACT.DEFAULT.POWDELAY,
avgGenTime: constants.CONTRACT.DEFAULT.AVGGENTIME,
dtDiffEval: constants.CONTRACT.DEFAULT.DTDIFFEVAL,
medianTimeBlocks: constants.CONTRACT.DEFAULT.MEDIANTIMEBLOCKS,
rootoffset: 0,
forksize: constants.BRANCHES.DEFAULT_WINDOW_SIZE
};
_.keys(defaultValues).forEach(function(key){
if (that.conf[key] == undefined) {
that.conf[key] = defaultValues[key];
}
});
logger.debug('Loading crypto functions...');
// Extract key pair
let pair = null;
if (that.conf.pair) {
pair = {
publicKey: base58.decode(that.conf.pair.pub),
secretKey: base58.decode(that.conf.pair.sec)
};
}
else if (that.conf.passwd || that.conf.salt) {
pair = yield Q.nbind(crypto.getKeyPair, crypto)(that.conf.salt, that.conf.passwd);
}
else {
pair = {
publicKey: base58.decode(constants.CRYPTO.DEFAULT_KEYPAIR.pub),
secretKey: base58.decode(constants.CRYPTO.DEFAULT_KEYPAIR.sec)
};
}
if (!pair) {
throw Error('This node does not have a keypair. Use `ucoind wizard key` to fix this.');
}
that.pair = pair;
that.sign = signature.asyncSig(pair);
// Update services
[that.IdentityService, that.MembershipService, that.PeeringService, that.BlockchainService, that.TransactionsService].map((service) => {
service.setConfDAL(that.conf, that.dal, that.pair);
});
that.router().setConfDAL(that.conf, that.dal);
return that.conf;
});
this.initWithDAL = () => co(function *() {
yield that.plugFileSystem();
yield that.loadConf();
yield that.initDAL();
return that;
});
this.submit = function (obj, isInnerWrite, done) {
return co(function *() {
if (!obj.documentType) {
throw 'Document type not given';
}
try {
let action = documentsMapping[obj.documentType].action;
let res;
if (typeof action == 'function') {
// Handle the incoming object
res = yield action(obj);
} else {
throw 'Unknown document type \'' + obj.documentType + '\'';
}
if (res) {
// Only emit valid documents
that.emit(obj.documentType, _.clone(res));
that.streamPush(_.clone(res));
}
if (done) {
isInnerWrite ? done(null, res) : done();
}
return res;
} catch (err) {
logger.debug(err);
if (done) {
isInnerWrite ? done(err, null) : done();
} else {
throw err;
}
}
});
};
this.submitP = (obj, isInnerWrite) => Q.nbind(this.submit, this)(obj, isInnerWrite);
this.readConfFile = () => co(function *() {
let dal = yield fileDAL.file(home);
return yield dal.loadConf();
});
this.initDAL = () => this.dal.init();
this.start = function () {
return that.checkConfig()
.then(function (){
// Add signing & public key functions to PeeringService
logger.info('Node version: ' + that.version);
logger.info('Node pubkey: ' + that.PeeringService.pubkey);
return Q.nfcall(that.initPeer);
});
};
this.stop = function () {
that.BlockchainService.stopCleanMemory();
return that.PeeringService.stopRegular();
};
this.recomputeSelfPeer = function() {
return Q.nbind(that.PeeringService.generateSelfPeer, that.PeeringService)(that.conf, 0);
};
this.initPeer = function (done) {
async.waterfall([
function (next){
that.checkConfig().then(next).catch(next);
},
function (next){
that.PeeringService.regularCrawlPeers(next);
},
function (next){
logger.info('Storing self peer...');
that.PeeringService.regularPeerSignal(next);
},
function(next) {
that.PeeringService.regularTestPeers(next);
},
function (next){
that.PeeringService.regularSyncBlock(next);
},
function (next){
that.BlockchainService.regularCleanMemory(next);
}
], done);
};
let shouldContinue = false;
this.stopBlockComputation = function() {
shouldContinue = false;
that.BlockchainService.stopPoWThenProcessAndRestartPoW();
};
this.startBlockComputation = function() {
shouldContinue = true;
return co(function *() {
while (shouldContinue) {
try {
let block = yield that.BlockchainService.startGeneration();
if (block && shouldContinue) {
try {
let obj = parsers.parseBlock.syncWrite(dos2unix(block.getRawSigned()));
yield that.singleWritePromise(obj);
} catch (err) {
logger.warn('Proof-of-work self-submission: %s', err.message || err);
}
}
}
catch (e) {
logger.error(e);
shouldContinue = true;
}
}
logger.info('Proof-of-work computation STOPPED.');
});
};
this.checkConfig = function () {
return that.checkPeeringConf(that.conf);
};
this.checkPeeringConf = function (conf) {
return Q()
.then(function(){
if (!conf.pair && conf.passwd == null) {
throw new Error('No key password was given.');
}
if (!conf.pair && conf.salt == null) {
throw new Error('No key salt was given.');
}
if (!conf.currency) {
throw new Error('No currency name was given.');
}
if(!conf.ipv4 && !conf.ipv6){
throw new Error("No interface to listen to.");
}
if(!conf.remoteipv4 && !conf.remoteipv6 && !conf.remotehost){
throw new Error('No interface for remote contact.');
}
if (!conf.remoteport) {
throw new Error('No port for remote contact.');
}
});
};
this.resetAll = function(done) {
var files = ['stats', 'cores', 'current', 'conf', directory.UCOIN_DB_NAME, directory.UCOIN_DB_NAME + '.db', directory.WOTB_FILE];
var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
return resetFiles(files, dirs, done);
};
this.resetData = function(done) {
return co(function*(){
var files = ['stats', 'cores', 'current', directory.UCOIN_DB_NAME, directory.UCOIN_DB_NAME + '.db', directory.UCOIN_DB_NAME + '.log', directory.WOTB_FILE];
var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
yield resetFiles(files, dirs, done);
});
};
this.resetConf = function(done) {
var files = ['conf'];
var dirs = [];
return resetFiles(files, dirs, done);
};
this.resetStats = function(done) {
var files = ['stats'];
var dirs = ['ud_history'];
return resetFiles(files, dirs, done);
};
this.resetPeers = function(done) {
return that.dal.resetPeers(done);
};
this.cleanDBData = () => co(function *() {
yield _.values(that.dal.newDals).map((dal) => dal.cleanData && dal.cleanData());
that.dal.wotb.resetWoT();
var files = ['stats', 'cores', 'current'];
var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
return resetFiles(files, dirs);
});
function resetFiles(files, dirs, done) {
return co(function *() {
let params = yield paramsP;
let myFS = params.fs;
let rootPath = params.home;
for (let i = 0, len = files.length; i < len; i++) {
let fName = files[i];
// JSON file?
let existsJSON = yield myFS.exists(rootPath + '/' + fName + '.json');
if (existsJSON) {
yield myFS.remove(rootPath + '/' + fName + '.json');
} else {
// Normal file?
let normalFile = path.join(rootPath, fName);
let existsFile = yield myFS.exists(normalFile);
if (existsFile) {
yield myFS.remove(normalFile);
}
}
}
for (let i = 0, len = dirs.length; i < len; i++) {
let dirName = dirs[i];
let existsDir = yield myFS.exists(rootPath + '/' + dirName);
if (existsDir) {
yield myFS.removeTree(rootPath + '/' + dirName);
}
}
done && done();
})
.catch((err) => {
done && done(err);
throw err;
});
}
this.disconnect = function() {
return that.dal && that.dal.close();
};
this.pullBlocks = that.PeeringService.pullBlocks;
this.doMakeNextBlock = (manualValues) => that.BlockchainService.makeNextBlock(null, null, null, manualValues);
this.doCheckBlock = function(block) {
var parsed = parsers.parseBlock.syncWrite(block.getRawSigned());
return that.BlockchainService.checkBlock(parsed, false);
};
this.revert = () => this.BlockchainService.revertCurrentBlock();
this.revertTo = (number) => co(function *() {
let current = yield that.BlockchainService.current();
for (let i = 0, count = current.number - number; i < count; i++) {
yield that.BlockchainService.revertCurrentBlock();
}
if (current.number <= number) {
logger.warn('Already reached');
}
that.BlockchainService.revertCurrentBlock();
});
this.singleWritePromise = (obj) => that.submit(obj);
var theRouter;
this.router = function(active) {
if (!theRouter) {
theRouter = router(that.PeeringService, that.conf, that.dal);
}
theRouter.setActive(active !== false);
return theRouter;
};
/**
* Synchronize the server with another server.
*
* If local server's blockchain is empty, process a fast sync: **no block is verified in such a case**, unless
* you force value `askedCautious` to true.
*
* @param onHost Syncs on given host.
* @param onPort Syncs on given port.
* @param upTo Sync up to this number, if `upTo` value is a positive integer.
* @param chunkLength Length of each chunk of blocks to download. Kind of buffer size.
* @param interactive Tell if the loading bars should be used for console output.
* @param askedCautious If true, force the verification of each downloaded block. This is the right way to have a valid blockchain for sure.
* @param nopeers If true, sync will omit to retrieve peer documents.
*/
this.synchronize = (onHost, onPort, upTo, chunkLength, interactive, askedCautious, nopeers) => {
let remote = new Synchroniser(that, onHost, onPort, that.conf, interactive === true);
let syncPromise = remote.sync(upTo, chunkLength, askedCautious, nopeers);
return {
flow: remote,
syncPromise: syncPromise
};
};
/**
* Enable routing features:
* - The server will try to send documents to the network
* - The server will eventually be notified of network failures
*/
this.routing = () => {
// The router asks for multicasting of documents
this.pipe(this.router())
// The documents get sent to peers
.pipe(multicaster(this.conf))
// The multicaster may answer 'unreachable peer'
.pipe(this.router());
};
this.upnp = () => co(function *() {
let upnpAPI = yield upnp(that.conf.port, that.conf.remoteport);
that.upnpAPI = upnpAPI;
return upnpAPI;
});
this.listenToTheWeb = (showLogs) => co(function *() {
let bmapi = yield bma(that, [{
ip: that.conf.ipv4,
port: that.conf.port
}], showLogs);
return bmapi.openConnections();
});
this.rawer = rawer;
this.writeRaw = (raw, type) => co(function *() {
let parser = documentsMapping[type] && documentsMapping[type].parser;
let obj = parser.syncWrite(raw);
return yield that.singleWritePromise(obj);
});
}
util.inherits(Server, stream.Duplex);
module.exports = Server;