fix and improve miner.
This commit is contained in:
parent
b26961d68a
commit
24060f04bb
@ -416,7 +416,7 @@ BST.prototype.batch = function batch(ops, options, callback) {
|
|||||||
batch = new Batch(this, options);
|
batch = new Batch(this, options);
|
||||||
|
|
||||||
if (ops) {
|
if (ops) {
|
||||||
batch.ops = ops;
|
batch.ops = ops.slice();
|
||||||
return batch.write(callback);
|
return batch.write(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,8 @@ Fullnode.prototype._init = function _init() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var options;
|
var options;
|
||||||
|
|
||||||
|
this.wallet = null;
|
||||||
|
|
||||||
this.chain = new bcoin.chain(this, {
|
this.chain = new bcoin.chain(this, {
|
||||||
preload: false,
|
preload: false,
|
||||||
spv: false,
|
spv: false,
|
||||||
@ -142,10 +144,10 @@ Fullnode.prototype._init = function _init() {
|
|||||||
utils.debug('Node is loaded.');
|
utils.debug('Node is loaded.');
|
||||||
}
|
}
|
||||||
|
|
||||||
options = {
|
options = utils.merge({
|
||||||
id: 'primary',
|
id: 'primary',
|
||||||
passphrase: this.options.passphrase
|
passphrase: this.options.passphrase
|
||||||
};
|
}, this.options.wallet || {});
|
||||||
|
|
||||||
utils.serial([
|
utils.serial([
|
||||||
this.chain.open.bind(this.chain),
|
this.chain.open.bind(this.chain),
|
||||||
@ -166,6 +168,8 @@ Fullnode.prototype._init = function _init() {
|
|||||||
if (!self.miner.address)
|
if (!self.miner.address)
|
||||||
self.miner.address = wallet.getAddress();
|
self.miner.address = wallet.getAddress();
|
||||||
|
|
||||||
|
self.wallet = wallet;
|
||||||
|
|
||||||
load();
|
load();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -203,6 +207,7 @@ Fullnode.prototype.open = function open(callback) {
|
|||||||
|
|
||||||
Fullnode.prototype.close =
|
Fullnode.prototype.close =
|
||||||
Fullnode.prototype.destroy = function destroy(callback) {
|
Fullnode.prototype.destroy = function destroy(callback) {
|
||||||
|
this.wallet.destroy();
|
||||||
utils.serial([
|
utils.serial([
|
||||||
this.http.close.bind(this.http),
|
this.http.close.bind(this.http),
|
||||||
this.walletdb.close.bind(this.walletdb),
|
this.walletdb.close.bind(this.walletdb),
|
||||||
|
|||||||
@ -110,8 +110,14 @@ Miner.prototype.start = function start() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// Wait for `tip`.
|
// Wait for `tip`.
|
||||||
if (!this.last)
|
this.last = this.last || this.chain.tip;
|
||||||
|
if (!this.last) {
|
||||||
|
this.chain.on('tip', function(tip) {
|
||||||
|
self.last = tip;
|
||||||
|
self.start();
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.stop();
|
this.stop();
|
||||||
|
|
||||||
@ -214,7 +220,7 @@ Miner.prototype.createBlock = function createBlock(callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
ts = Math.max(utils.now(), this.last.ts + 1);
|
ts = Math.max(utils.now(), self.last.ts + 1);
|
||||||
|
|
||||||
target = self.chain.getTarget(self.last, ts);
|
target = self.chain.getTarget(self.last, ts);
|
||||||
|
|
||||||
@ -354,7 +360,7 @@ Miner.prototype.iterate = function iterate() {
|
|||||||
self.chain.add(self.block, function(err) {
|
self.chain.add(self.block, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.type === 'VerifyError')
|
if (err.type === 'VerifyError')
|
||||||
utils.debug('Miner: %s could not be added to chain.', block.rhash);
|
utils.debug('Miner: %s could not be added to chain.', self.block.rhash);
|
||||||
return self.emit('error', err);
|
return self.emit('error', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,7 +374,7 @@ Miner.prototype.iterate = function iterate() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Miner.prototype.__defineGetter__('hashes', function() {
|
Miner.prototype.__defineGetter__('hashes', function() {
|
||||||
return new bn(this.iterations).muln(0xffffffff).addn(this.block.nonce);
|
return new bn(this.iterations).mul(utils.U32).addn(this.block.nonce);
|
||||||
});
|
});
|
||||||
|
|
||||||
Miner.prototype.__defineGetter__('rate', function() {
|
Miner.prototype.__defineGetter__('rate', function() {
|
||||||
@ -391,6 +397,7 @@ Miner.prototype.sendStatus = function sendStatus() {
|
|||||||
|
|
||||||
Miner.prototype.findNonce = function findNonce() {
|
Miner.prototype.findNonce = function findNonce() {
|
||||||
var data = this.block.abbr();
|
var data = this.block.abbr();
|
||||||
|
var target = this.block.target.toBuffer('le', 32);
|
||||||
var now;
|
var now;
|
||||||
|
|
||||||
// Track how long we've been at it.
|
// Track how long we've been at it.
|
||||||
@ -399,7 +406,7 @@ Miner.prototype.findNonce = function findNonce() {
|
|||||||
// The heart and soul of the miner: match the target.
|
// The heart and soul of the miner: match the target.
|
||||||
while (this.block.nonce <= 0xffffffff) {
|
while (this.block.nonce <= 0xffffffff) {
|
||||||
// Hash and test against the next target
|
// Hash and test against the next target
|
||||||
if (utils.testTarget(this.block.target, this.dsha256(data)))
|
if (rcmp(this.dsha256(data), target) < 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Increment the nonce to get a different hash
|
// Increment the nonce to get a different hash
|
||||||
@ -446,6 +453,21 @@ Miner.prototype.findNonce = function findNonce() {
|
|||||||
return false;
|
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
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -34,6 +34,8 @@ SPVNode.prototype._init = function _init() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var options;
|
var options;
|
||||||
|
|
||||||
|
this.wallet = null;
|
||||||
|
|
||||||
this.chain = new bcoin.chain(this, {
|
this.chain = new bcoin.chain(this, {
|
||||||
preload: this.options.preload,
|
preload: this.options.preload,
|
||||||
spv: true,
|
spv: true,
|
||||||
@ -108,10 +110,10 @@ SPVNode.prototype._init = function _init() {
|
|||||||
utils.debug('Node is loaded.');
|
utils.debug('Node is loaded.');
|
||||||
}
|
}
|
||||||
|
|
||||||
options = {
|
options = utils.merge({
|
||||||
id: 'primary',
|
id: 'primary',
|
||||||
passphrase: this.options.passphrase
|
passphrase: this.options.passphrase
|
||||||
};
|
}, this.options.wallet || {});
|
||||||
|
|
||||||
// Create or load the primary wallet.
|
// Create or load the primary wallet.
|
||||||
utils.serial([
|
utils.serial([
|
||||||
@ -126,6 +128,8 @@ SPVNode.prototype._init = function _init() {
|
|||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
|
self.wallet = wallet;
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -163,6 +167,7 @@ SPVNode.prototype.open = function open(callback) {
|
|||||||
|
|
||||||
SPVNode.prototype.close =
|
SPVNode.prototype.close =
|
||||||
SPVNode.prototype.destroy = function destroy(callback) {
|
SPVNode.prototype.destroy = function destroy(callback) {
|
||||||
|
this.wallet.destroy();
|
||||||
utils.parallel([
|
utils.parallel([
|
||||||
this.http.close.bind(this.http),
|
this.http.close.bind(this.http),
|
||||||
this.pool.close.bind(this.pool),
|
this.pool.close.bind(this.pool),
|
||||||
|
|||||||
@ -1420,7 +1420,7 @@ utils.sizePush = function sizePush(num) {
|
|||||||
return 5;
|
return 5;
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.cmp = function(a, b) {
|
utils.cmp = function cmp(a, b) {
|
||||||
var len, i;
|
var len, i;
|
||||||
|
|
||||||
if (a.compare)
|
if (a.compare)
|
||||||
@ -1449,7 +1449,7 @@ utils.cmp = function(a, b) {
|
|||||||
// $ man 3 memcmp (see NetBSD's consttime_memequal)
|
// $ man 3 memcmp (see NetBSD's consttime_memequal)
|
||||||
// This protects us against timing attacks when
|
// This protects us against timing attacks when
|
||||||
// comparing an input against a secret string.
|
// comparing an input against a secret string.
|
||||||
utils.ccmp = function(a, b) {
|
utils.ccmp = function ccmp(a, b) {
|
||||||
var res = 0;
|
var res = 0;
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user