refactor. miner fixes.
This commit is contained in:
parent
24060f04bb
commit
fd6b315756
6
bin/node
6
bin/node
@ -8,7 +8,8 @@ var node = bcoin.fullnode({
|
||||
debug: true,
|
||||
passphrase: 'node',
|
||||
prune: process.argv.indexOf('--prune') !== -1,
|
||||
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1
|
||||
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1,
|
||||
mine: process.argv.indexOf('--mine') !== -1
|
||||
});
|
||||
|
||||
node.on('error', function(err) {
|
||||
@ -20,4 +21,7 @@ node.open(function(err) {
|
||||
throw err;
|
||||
|
||||
node.startSync();
|
||||
|
||||
if (node.options.mine)
|
||||
node.miner.start();
|
||||
});
|
||||
|
||||
@ -81,6 +81,10 @@ Fullnode.prototype._init = function _init() {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.miner.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.pool.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
@ -361,7 +361,8 @@ Miner.prototype.iterate = function iterate() {
|
||||
if (err) {
|
||||
if (err.type === 'VerifyError')
|
||||
utils.debug('Miner: %s could not be added to chain.', self.block.rhash);
|
||||
return self.emit('error', err);
|
||||
self.emit('error', err);
|
||||
return self.iterate();
|
||||
}
|
||||
|
||||
// Emit our newly found block
|
||||
@ -403,10 +404,12 @@ Miner.prototype.findNonce = function findNonce() {
|
||||
// Track how long we've been at it.
|
||||
this._begin = utils.now();
|
||||
|
||||
// assert(this.block.ts > this.last.ts);
|
||||
|
||||
// The heart and soul of the miner: match the target.
|
||||
while (this.block.nonce <= 0xffffffff) {
|
||||
// Hash and test against the next target
|
||||
if (rcmp(this.dsha256(data), target) < 0)
|
||||
if (utils.rcmp(this.dsha256(data), target) < 0)
|
||||
return true;
|
||||
|
||||
// Increment the nonce to get a different hash
|
||||
@ -453,21 +456,6 @@ Miner.prototype.findNonce = function findNonce() {
|
||||
return false;
|
||||
};
|
||||
|
||||
function rcmp(a, b) {
|
||||
var i;
|
||||
|
||||
assert(a.length === b.length);
|
||||
|
||||
for (i = a.length - 1; i >= 0; i--) {
|
||||
if (a[i] < b[i])
|
||||
return -1;
|
||||
if (a[i] > b[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose
|
||||
*/
|
||||
|
||||
@ -1444,6 +1444,21 @@ utils.cmp = function cmp(a, b) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
utils.rcmp = function rcmp(a, b) {
|
||||
var i;
|
||||
|
||||
assert(a.length === b.length);
|
||||
|
||||
for (i = a.length - 1; i >= 0; i--) {
|
||||
if (a[i] < b[i])
|
||||
return -1;
|
||||
if (a[i] > b[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
// memcmp in constant time (can only return true or false)
|
||||
// https://cryptocoding.net/index.php/Coding_rules
|
||||
// $ man 3 memcmp (see NetBSD's consttime_memequal)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user