/
hi.js
683 lines (587 loc) · 24.8 KB
/
hi.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
/*
* Copyright (c) 2017-present, Block Collider developers, All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/
const { writeFileSync } = require('fs');
const { inspect } = require('util');
const fkill = require('fkill');
const crypto = require('crypto');
const BN = require('bn.js');
const debug = require('debug')('bcnode:mining:officer');
const { max, mean, all, equals, flatten, fromPairs, last, range, values } = require('ramda');
const { prepareWork, prepareNewBlock, getUniqueBlocks } = require('./primitives');
const { getLogger } = require('../logger');
const { Block, BcBlock, BlockchainHeaders } = require('../protos/core_pb');
const { MinerRequest, MinerResponseResult } = require('../protos/miner_pb');
const { RpcClient } = require('../rpc');
const { isDebugEnabled, ensureDebugPath } = require('../debug');
const { validateRoveredSequences, isValidBlock } = require('../bc/validation');
const { getBlockchainsBlocksCount } = require('../bc/helper');
const { WorkerPool } = require('./pool');
const ts = require('../utils/time').default; // ES6 default export
const { parseBoolean } = require('../utils/config');
const { txCreateCoinbase, getMaxBlockSize, COINBASE_TX_ESTIMATE_SIZE } = require('../core/txUtils');
const MIN_HEALTH_NET = process.env.MIN_HEALTH_NET === 'true';
const DISABLE_IPH_TEST = parseBoolean(process.env.DISABLE_IPH_TEST);
const BC_RUST_MINER = parseBoolean(process.env.BC_RUST_MINER);
const keyOrMethodToChain = keyOrMethod => keyOrMethod.replace(/^get|set/, '').replace(/List$/, '').toLowerCase();
// const chainToSet = (chain: string) => `set${chain[0].toUpperCase() + chain.slice(1)}List` // UNUSED
const chainToGet = chain => `get${chain[0].toUpperCase() + chain.slice(1)}List`;
class MiningOfficer {
constructor(pubsub, persistence, workerPool, txPendingPool, opts) {
this._logger = getLogger(__filename);
this._minerKey = opts.minerKey;
this._pubsub = pubsub;
this._persistence = persistence;
this._knownRovers = opts.rovers;
this._unfinished = [];
this._workerPool = workerPool;
this._speedResults = [];
this._collectedBlocks = {};
for (let roverName of this._knownRovers) {
this._collectedBlocks[roverName] = 0;
}
this._canMine = false;
this._timers = {};
this._timerResults = {};
this._unfinishedBlockData = { block: undefined, lastPreviousBlock: undefined, currentBlocks: {}, timeDiff: undefined, iterations: undefined };
this._paused = false;
this._blockTemplates = [];
this._rpc = new RpcClient();
this._txPendingPool = txPendingPool;
}
get persistence() {
return this._persistence;
}
get pubsub() {
return this._pubsub;
}
get paused() {
return this._paused;
}
set paused(paused) {
this._paused = paused;
}
// TODO: PING
async newRoveredBlock(rovers, block, blockCache) {
let iph;
debug(`DISABLE_IPH_TEST: ${DISABLE_IPH_TEST}`);
if (DISABLE_IPH_TEST === false) {
iph = await this._persistence.get('bc.sync.initialpeerheader');
} else {
// allows mining to continue by force passing the IPH test
this._logger.warn('SECURITY ALERT: IPH test is has been overridden by DISABLE_IPH_TEST');
iph = 'complete';
}
this._collectedBlocks[block.getBlockchain()] += 1;
this._logger.info('[] <- [] ' + 'rovered ' + block.getBlockchain() + ' block ' + block.getHeight() + ' ' + block.getHash());
// TODO: Adjust minimum count of collected blocks needed to trigger mining
if (!this._canMine && all(numCollected => numCollected >= 1, values(this._collectedBlocks))) {
this._canMine = true;
}
if (this._canMine === true && MIN_HEALTH_NET === false) {
try {
const quorum = await this._persistence.get('bc.dht.quorum');
if (quorum === null) {
this._canMine = false;
this._logger.error('quorum state is not persisted on disk');
return Promise.reject(new Error('critical error -> restart application'));
}
if (parseInt(quorum, 10) < 1 && this._canMine === true) {
this._logger.info('peer waypoint discovery in progress');
this._canMine = false;
return Promise.resolve(false);
}
} catch (err) {
this._canMine = false;
this._logger.error('quorum state is not persisted on disk');
this._logger.error(err);
return Promise.reject(new Error('critical error -> restart application'));
}
}
// make sure the miner has at least two blocks of the depth
if (this._canMine === true && MIN_HEALTH_NET === false) {
try {
const parent = await this._persistence.get('bc.block.parent');
// DISABLED UNTIL AT
// const latest = await this._persistence.get('bc.block.latest')
// if (!latest || parent.getHash() !== latest.getPreviousHash()) {
// this._logger.warn('after resync approval rovers must complete new multiverse')
// this._canMine = false
// return Promise.resolve(false)
// }
if (!parent || parent.getHeight() === '1') {
this._logger.warn('searching for additional blocks before initiating mining process');
this._canMine = false;
}
} catch (err) {
this._canMine = false;
this._logger.error('unable to assert parent block of highest block');
return Promise.reject(new Error('crtical error -> restart application'));
}
}
// Check if _canMine
if (!this._canMine || iph !== 'complete') {
if (iph !== 'complete') {
this._logger.info('holding for iph "all clear" status before mining');
}
const keys = Object.keys(this._collectedBlocks);
const values = '[' + keys.reduce((all, a, i) => {
const val = this._collectedBlocks[a];
if (i === keys.length - 1) {
all = all + a + ':' + val;
} else {
all = all + a + ':' + val + ' ';
}
return all;
}, '') + ']';
const totalBlocks = keys.reduce((all, key) => {
return all + this._collectedBlocks[key];
}, 0);
this._logger.info('constructing multiverse with blockchains ' + values);
this._logger.info('multiverse depth ' + totalBlocks);
return Promise.resolve(false);
}
// Check if peer is syncing
if (this.paused) {
this._logger.info(`mining and ledger updates disabled until initial multiverse threshold is met`);
return Promise.resolve(false);
}
// Check if all rovers are enabled
if (equals(new Set(this._knownRovers), new Set(rovers)) === false) {
this._logger.info(`consumed blockchains manually overridden, mining services disabled, active multiverse rovers: ${JSON.stringify(rovers)}, known: ${JSON.stringify(this._knownRovers)})`);
return Promise.resolve(false);
}
// $FlowFixMe
return this.startMining(rovers, block, blockCache).then(res => {
this._logger.info('mining cycle initiated');
return Promise.resolve(res);
}).catch(err => {
this._logger.error(err);
return Promise.reject(err);
});
}
stop() {
this.stopMining();
this._cleanUnfinishedBlock();
}
startTimer(name) {
this._timers[name] = Math.floor(Date.now() * 0.001);
}
// TODO: will have to remake for overline, no boundaries
stopTimer(name) {
if (this._timers[name] !== undefined) {
const startTime = this._timers[name];
delete this._timers[name];
const elapsed = Math.floor(Date.now() * 0.001) - startTime;
if (this._timerResults[name] === undefined) {
this._timerResults[name] = [];
}
this._timerResults[name].push(elapsed);
return elapsed;
}
return 0;
}
getTimerResults(name) {
if (this._timerResults[name] !== undefined && this._timerResults[name].length > 4) {
return mean(this._timerResults[name]);
}
}
// TODO: PING
async startMining(rovers, block, blockCache) {
// get latest block from each child blockchain
//
// ////////////// --> runs everytime
const lastPreviousBlock = await this.persistence.get('bc.block.latest');
if (!lastPreviousBlock) {
const msg = 'Could not fetch bc.block.latest';
this._logger.warn(msg);
throw new Error(msg);
}
this._logger.info(`local persisted block height: ${lastPreviousBlock.getHeight()}`);
// [eth.block.latest,btc.block.latest,neo.block.latest...]
const latestRoveredHeadersKeys = this._knownRovers.map(chain => `${chain}.block.latest`);
const latestBlockHeaders = await this.persistence.getBulk(latestRoveredHeadersKeys);
// { eth: 200303, btc:2389, neo:933 }
const latestBlockHeadersHeights = fromPairs(latestBlockHeaders.map(header => [header.getBlockchain(), header.getHeight()]));
this._logger.debug(`latestBlockHeadersHeights: ${inspect(latestBlockHeadersHeights)}`);
// collider is starting up
// if (lastPreviousBlock.getHeight() === 1 && blockCache.length > 0) {
// this._logger.info('rovered block indexed')
// blockCache.push(block)
// return Promise.resolve(true)
// } else if (lastPreviousBlock.getTimestamp() + 6 > Math.floor(Date.now() * 0.001) &&
// blockCache.length > 0) {
// this._logger.info('rovered block indexed')
// blockCache.push(block)
// return Promise.resolve(true)
/// / otherwise reset the cache
// } else if (blockCache.length > 0) {
// blockCache.length = 0
// }
// prepare a list of keys of headers to pull from persistence
const newBlockHeadersKeys = flatten(Object.keys(lastPreviousBlock.getBlockchainHeaders().toObject()).map(listKey => {
this._logger.debug('assembling minimum heights for ' + listKey);
const chain = keyOrMethodToChain(listKey);
const lastHeaderInPreviousBlock = last(lastPreviousBlock.getBlockchainHeaders()[chainToGet(chain)]());
let from;
let to;
if (lastPreviousBlock.getHeight() === 1) {
// genesis
// just pick the last known block for genesis
from = latestBlockHeadersHeights[chain]; // TODO check, seems correct
to = from;
} else {
if (!lastHeaderInPreviousBlock) {
throw new Error(`previous NRG block ${lastPreviousBlock.getHeight()} failed minimum "${chain}" state changes`);
}
from = lastHeaderInPreviousBlock.getHeight() + 1;
to = latestBlockHeadersHeights[chain];
}
this._logger.info(`newBlockHeadersKeys, heights nrg: ${lastPreviousBlock.getHeight()}, ${chain} ln: ${from}, ${to}`);
if (from === to) {
return [`${chain}.block.${from}`];
}
if (from > to) {
return [];
}
if (to === undefined) {
to = from + 1;
}
from = max(to - 15, from);
this._logger.info('chain: ' + chain + 'from: ' + from + ' to: ' + to);
return [range(from, to + 1).map(height => `${chain}.block.${height}`)];
}));
this._logger.debug(`loading ${inspect(newBlockHeadersKeys)}`);
// get latest known BC block
try {
const currentBlocks = await this.persistence.getBulk(newBlockHeadersKeys);
if (!currentBlocks) {
throw new Error(`Could not fetch current rovered block headers: ${newBlockHeadersKeys}`);
}
this._logger.info(`preparing new block`);
const currentTimestamp = ts.nowSeconds();
if (this._unfinishedBlock !== undefined && getBlockchainsBlocksCount(this._unfinishedBlock) >= 6) {
this._cleanUnfinishedBlock();
}
const hi = await getMaxBlockSize(this._minerKey, this.persistence)
const maxBlockSize = hi - COINBASE_TX_ESTIMATE_SIZE;
const maxNumberOfTx = Math.floor(maxBlockSize / COINBASE_TX_ESTIMATE_SIZE);
const candidateTxs = await this._txPendingPool.loadBestPendingTxs(maxNumberOfTx);
console.log('xxxxxxxxxx', candidateTxs.length)
let txsSizeSoFar = 0;
const txsToMine = [];
for (let tx of candidateTxs) {
const thisTxSize = tx.serializeBinary().length;
console.log('xxxxxxxxxx', candidateTxs.length, thisTxSize, maxBlockSize, hi, COINBASE_TX_ESTIMATE_SIZE)
if (txsSizeSoFar + thisTxSize > maxBlockSize) {
break;
}
txsSizeSoFar += thisTxSize;
txsToMine.push(tx);
}
const coinbaseTx = await txCreateCoinbase(lastPreviousBlock.getHeight(), this.persistence, txsToMine, this._minerKey);
const allTxs = [coinbaseTx].concat(txsToMine);
this._logger.info(`Txs to mine, length: ${allTxs.length}, size: ${txsSizeSoFar}, maxBlockSize: ${maxBlockSize}`);
const [newBlock, finalTimestamp] = prepareNewBlock(currentTimestamp, lastPreviousBlock, currentBlocks, block, allTxs, this._minerKey, this._unfinishedBlock);
const workId = lastPreviousBlock.getHeight() + '@' + crypto.randomBytes(16).toString('hex');
const work = prepareWork(lastPreviousBlock.getHash(), newBlock.getBlockchainHeaders());
newBlock.setTimestamp(finalTimestamp);
this._unfinishedBlock = newBlock;
this._unfinishedBlockData = {
lastPreviousBlock,
currentBlocks: newBlock.getBlockchainHeaders(),
block,
iterations: undefined,
timeDiff: undefined
};
this._unfinished.push({
workId: workId,
unfinishedBlock: newBlock,
unfinishedBlockData: {
lastPreviousBlock,
currentBlocks: newBlock.getBlockchainHeaders(),
block,
iterations: undefined,
timeDiff: undefined
}
});
if (this._unfinished.length > 30) {
this._unfinished.shift();
}
this.setCurrentMiningHeaders(newBlock.getBlockchainHeaders());
// if blockchains block count === 5 we will create a block with 6 blockchain blocks (which gets bonus)
// if it's more, do not restart mining and start with new ones
// if (this._workerProcess) {
// this._logger.info(`new rovered block -> accepted`)
// this.stopMining()
// }
const update = {
workId: workId,
currentTimestamp,
offset: ts.offset,
work,
minerKey: this._minerKey,
merkleRoot: newBlock.getMerkleRoot(),
newestChildBlock: block.toObject(),
difficulty: newBlock.getDifficulty(),
difficultyData: {
currentTimestamp,
lastPreviousBlock: lastPreviousBlock.serializeBinary(),
// $FlowFixMe
newBlockHeaders: newBlock.getBlockchainHeaders().serializeBinary()
}
};
if (BC_RUST_MINER) {
const minerRequest = new MinerRequest();
minerRequest.setWorkId(workId);
minerRequest.setCurrentTimestamp(currentTimestamp);
minerRequest.setOffset(ts.offset || 1);
minerRequest.setWork(work);
minerRequest.setMinerKey(this._minerKey);
minerRequest.setMerkleRoot(newBlock.getMerkleRoot());
minerRequest.setDifficulty(newBlock.getDifficulty());
minerRequest.setLastPreviousBlock(lastPreviousBlock);
minerRequest.setNewBlockHeaders(newBlock.getBlockchainHeaders());
this._rpc.miner.mine(minerRequest, (err, response) => {
if (err) {
this._logger.error('Native mining request failed', err);
return;
}
if (response.getResult() === MinerResponseResult.CANCELED) {
this._logger.info('Mining restarted because of new work');
return;
}
this._logger.info('Got response from rust miner', response.toObject());
const transformed = _extends({}, response.toObject(), {
workId
});
this._workerPool.emitter.emit('mined', transformed);
});
} else {
// this._workerPool.emitter.once('mined', (data) => {
// this._handleWorkerFinishedMessage(data)
// })
/* eslint-disable */
try {
await fkill('bcworker', { force: true });
} catch (err) {
this._logger.debug(err);
}
this._workerPool.updateWorkers({ type: 'work', data: update, workId: workId, newestChildBlock: block });
}
return Promise.resolve(true);
} catch (err) {
this._logger.error(err);
this._logger.warn(`Error while getting last previous BC block, reason: ${err.message}`);
return Promise.reject(err);
}
}
/// **
//* Manages the current most recent block template used by the miner
//* @param blockTemplate
//* /
setCurrentMiningHeaders(blockTemplate) {
if (this._blockTemplates.length > 0) {
this._blockTemplates.pop();
}
this._blockTemplates.push(blockTemplate);
}
/**
* Accessor for block templates
*/
getCurrentMiningHeaders() {
if (this._blockTemplates.length < 1) return;
return this._blockTemplates[0];
}
stopMining(pool) {
debug('stop mining');
if (pool !== undefined) {
pool.updateWorkers({ type: 'reset' });
} else {
this._workerPool.updateWorkers({ type: 'reset' });
}
try {
return fkill('bcworker', { force: true });
} catch (err) {
this._logger.debug(err);
}
return Promise.resolve(true);
}
/*
* Alias for validation module
*/
validateRoveredSequences(blocks) {
return validateRoveredSequences(blocks);
}
/*
* Restarts the miner by merging any unused rover blocks into a new block
*/
async rebaseMiner() {
// if (this._canMine !== true) return Promise.resolve(false)
this._logger.info('rebase miner request');
try {
const stopped = this.stopMining();
this._logger.info(`miner rebased, result: ${inspect(stopped)}`);
const latestRoveredHeadersKeys = this._knownRovers.map(chain => `${chain}.block.latest`);
this._logger.info(latestRoveredHeadersKeys);
const currentRoveredBlocks = await this.persistence.getBulk(latestRoveredHeadersKeys);
const lastPreviousBlock = await this.persistence.get('bc.block.latest');
const previousHeaders = lastPreviousBlock.getBlockchainHeaders();
this._logger.info(currentRoveredBlocks);
if (lastPreviousBlock === null) {
return Promise.resolve(false);
}
if (currentRoveredBlocks !== null && currentRoveredBlocks.length > 0) {
const perc = currentRoveredBlocks.length / Object.keys(previousHeaders.toObject()).length * 100;
this._logger.info('multiverse sync state: ' + perc + '%');
}
if (currentRoveredBlocks.length !== Object.keys(previousHeaders.toObject()).length) {
return Promise.resolve(false);
}
const uniqueBlocks = getUniqueBlocks(previousHeaders, currentRoveredBlocks).sort((a, b) => {
if (a.getHeight() > b.getHeight()) {
return -1;
}
if (a.getHeight() < b.getHeight()) {
return 1;
}
return 0;
});
this._logger.info('stale branch blocks: ' + uniqueBlocks.length);
if (uniqueBlocks.length < 1) {
this._logger.info(uniqueBlocks.length + ' state changes ');
return Promise.resolve(false);
}
return this.startMining(this._knownRovers, uniqueBlocks.shift());
} catch (err) {
return Promise.reject(err);
}
}
restartMining() {
debug('Restarting mining', this._knownRovers);
// this.stopMining()
// if (this._rawBlock.length > 0) {
// return this.startMining(this._knownRovers, this._rawBlock.pop())
// .then(res => {
// return Promise.resolve(!res)
// })
// } else {
// return Promise.resolve(true)
// }
return this.stopMining();
}
_handleWorkerFinishedMessage(solution) {
let unfinishedBlock = this._unfinishedBlock;
let unfinishedBlockData = this._unfinishedBlockData;
let workId = solution.workId;
this._logger.debug('loading work id ' + workId + ' from imperforate blocks ' + this._unfinished.length);
/* eslint-disable */
//if (!unfinishedBlock || unfinishedBlock.workId !== workId) {
if (workId !== undefined) {
const candidates = this._unfinished.filter(b => {
//this._logger.info(JSON.stringify(b, null, 2) + ' ------------>>> ')
if (b.workId === workId) {
return b;
}
});
this._logger.debug('loaded work id: ' + workId + ' candidates ' + candidates.length);
if (candidates.length > 0) {
const unfinishedBlockObject = candidates.shift();
unfinishedBlock = unfinishedBlockObject.unfinishedBlock;
unfinishedBlockData = unfinishedBlockObject.unfinishedBlockData;
const remainingUnfinished = this._unfinished.filter(b => {
if (b.workId !== workId) {
return b;
}
});
this._unfinished = remainingUnfinished;
} else {
// this._logger.warn('There is not an unfinished block to use solution for')
return;
}
}
//}
const { nonce, distance, timestamp, difficulty, iterations, timeDiff } = solution;
this._logger.info(' nonce: ' + nonce);
this._logger.info(' distance: ' + distance);
this._logger.info(' timestamp: ' + timestamp);
this._logger.info(' difficulty: ' + difficulty);
this._logger.info(' iterations: ' + iterations);
this._logger.info(' timeDiff: ' + timeDiff);
this._logger.info(`The calculated block difficulty was ${unfinishedBlock.getDifficulty()}, actual mining difficulty was ${difficulty}`);
const chainWeight = unfinishedBlock.getDistance();
unfinishedBlock.setNonce(nonce);
unfinishedBlock.setDistance(distance);
// this is likely the problem
unfinishedBlock.setTotalDistance(new BN(unfinishedBlock.getTotalDistance()).add(new BN(chainWeight)).add(new BN(unfinishedBlock.getDifficulty())).toString());
unfinishedBlock.setTimestamp(timestamp);
this._logger.info('total distance with unfinished block <- ' + unfinishedBlock.getTotalDistance());
if (unfinishedBlockData) {
unfinishedBlockData.iterations = iterations;
unfinishedBlockData.timeDiff = timeDiff;
}
this._logger.info('about to test if block is valid');
if (!isValidBlock(unfinishedBlock)) {
this._logger.info(`candidate block is stale`);
this._cleanUnfinishedBlock();
//this._workerPool.emitter.emit('blockCacheRebase')
return;
}
if (unfinishedBlock !== undefined && isDebugEnabled()) {
this._writeMiningData(unfinishedBlock, solution);
}
this._cleanUnfinishedBlock();
this.pubsub.publish('miner.block.new', { unfinishedBlock, solution });
//return this.stopMining()
}
_handleWorkerError(error) {
this._logger.error(error);
this._logger.warn(`mining worker process errored, reason: ${error.message}`);
this._cleanUnfinishedBlock();
//return this.stopMining()
}
_handleWorkerExit(code, signal) {
//this.stopMining()
this._logger.info('miner pending new work');
if (code === 0 || code === null) {
// 0 means worker exited on it's own correctly, null that is was terminated from engine
this._logger.info(`mining worker finished its work (code: ${code})`);
} else {
this._logger.warn(`mining worker process exited with code ${code}, signal ${signal}`);
this._cleanUnfinishedBlock();
}
}
_cleanUnfinishedBlock() {
debug('cleaning unfinished block');
this._unfinishedBlock = undefined;
this._unfinishedBlockData = undefined;
}
_writeMiningData(newBlock, solution) {
// block_height, block_difficulty, block_distance, block_total_distance, block_timestamp, iterations_count, mining_duration_ms, btc_confirmation_count, btc_current_timestamp, eth_confirmation_count, eth_current_timestamp, lsk_confirmation_count, lsk_current_timestamp, neo_confirmation_count, neo_current_timestamp, wav_confirmation_count, wav_current_timestamp
const row = [newBlock.getHeight(), newBlock.getDifficulty(), newBlock.getDistance(), newBlock.getTotalDistance(), newBlock.getTimestamp(), solution.iterations, solution.timeDiff];
this._knownRovers.forEach(roverName => {
if (this._unfinishedBlockData && this._unfinishedBlockData.currentBlocks) {
const methodNameGet = `get${roverName[0].toUpperCase() + roverName.slice(1)}List`; // e.g. getBtcList
// $FlowFixMe - flow does not now about methods of protobuf message instances
const blocks = this._unfinishedBlockData.currentBlocks[methodNameGet]();
row.push(blocks.map(block => block.getBlockchainConfirmationsInParentCount()).join('|'));
row.push(blocks.map(block => block.getTimestamp() / 1000 << 0).join('|'));
}
});
row.push(getBlockchainsBlocksCount(newBlock));
const dataPath = ensureDebugPath(`bc/mining-data.csv`);
writeFileSync(dataPath, `${row.join(',')}\r\n`, { encoding: 'utf8', flag: 'a' });
}
}
exports.MiningOfficer = MiningOfficer;
//# sourceMappingURL=officer.js.map