diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 82b1b990..5cf1a77c 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -145,7 +145,7 @@ Chain.prototype._init = function _init() { }); }; -Chain.prototype._addIndex = function _addIndex(entry) { +Chain.prototype._addIndex = function _addIndex(entry, save) { var self = this; // Already added @@ -181,7 +181,8 @@ Chain.prototype._addIndex = function _addIndex(entry) { this.tip = this.index.entries[this.index.entries.length - 1]; this.emit('tip', this.tip); - this._save(entry); + if (save) + this._save(entry.hash, entry); return Chain.codes.okay; }; @@ -194,10 +195,12 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) { assert(index >= 0); assert(checkpoint); - // This is the safest way to do it, the other possibility is to simply reset - // ignore the bad checkpoint block. The likelihood of someone carrying on an - // entire fork between to checkpoints is absurd, so this is probably _a lot_ - // of work for nothing. + // This is the safest way to do it, the other + // possibility is to simply reset ignore the + // bad checkpoint block. The likelihood of + // someone carrying on an entire fork between + // to checkpoints is absurd, so this is + // probably _a lot_ of work for nothing. this.resetHeight(checkpoint.height); }; @@ -360,7 +363,7 @@ Chain.prototype.add = function add(block, peer) { } // Attempt to add block to the chain index. - code = this._addIndex(entry); + code = this._addIndex(entry, true); // Result should never be `unchanged` since // we already verified there were no diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 8fa76929..943559e5 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -42,6 +42,7 @@ function Miner(options) { this.fee = new bn(0); this.last = this.chain.getTip(); this.block = null; + this.iterations = 0; this._begin = utils.now(); this._init(); @@ -95,6 +96,9 @@ Miner.prototype.start = function start() { // Ask our peers for mempool txs every so often. this.interval = setInterval(mempool, 60 * 1000); + // Reset iterations + this.iterations = 0; + // Create a new block and start hashing this.block = this.createBlock(); this.iterate(); @@ -126,7 +130,7 @@ Miner.prototype.addBlock = function addBlock(block) { // Somebody found the next block before // us, start over with the new target. if (block.height > this.last.height) { - this.last = block.verify + this.last = block.type === 'block' ? this.chain.getBlock(block) : block; assert(this.last); @@ -209,9 +213,7 @@ Miner.prototype.createBlock = function createBlock(tx) { // Create our block headers = { version: 4, - prevBlock: this.last.verify - ? this.last.hash('hex') - : this.last.hash, + prevBlock: this.last.hash, merkleRoot: utils.toHex(constants.zeroHash.slice()), ts: ts, bits: target, @@ -291,7 +293,7 @@ Miner.prototype.iterate = function iterate() { }; Miner.prototype.__defineGetter__('hashes', function() { - return this.block.extraNonce.muln(0xffffffff).addn(this.block.nonce); + return new bn(this.iterations).muln(0xffffffff).addn(this.block.nonce); }); Miner.prototype.__defineGetter__('rate', function() { @@ -333,6 +335,9 @@ Miner.prototype.findNonce = function findNonce() { this.sendStatus(); } + // Keep track of our iterations + this.iterations++; + // Send progress report this.sendStatus(); @@ -344,6 +349,8 @@ Miner.prototype.findNonce = function findNonce() { now = utils.now(); if (now > this.block.ts) { this.block.ts = now; + // Overflow the nonce + this.block.nonce = 0; return false; } diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 7250ad5d..a410e732 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -835,6 +835,10 @@ utils.now = function now() { return +new Date() / 1000 | 0; }; +utils.host = function host(addr) { + return addr.split(':')[0]; +}; + utils.hash = function hash(obj, enc) { if (obj == null) throw new Error('Cannot get hash of null');