refactor. miner fixes.

This commit is contained in:
Christopher Jeffrey 2016-04-03 04:20:18 -07:00
parent 24060f04bb
commit fd6b315756
4 changed files with 29 additions and 18 deletions

View File

@ -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();
});

View File

@ -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);
});

View File

@ -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
*/

View File

@ -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)