From 8977e99906e81aa59db1bb986a366ef53a62bc3e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 1 Mar 2017 15:12:35 -0800 Subject: [PATCH] mempool: extra sanity checking for mining. --- lib/mempool/mempool.js | 9 +++++++++ lib/mining/miner.js | 3 +++ 2 files changed, 12 insertions(+) diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index e9e48f2e..f952059b 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -88,6 +88,7 @@ function Mempool(options) { this.freeCount = 0; this.lastTime = 0; this.lastFlush = 0; + this.tip = this.network.genesis.hash; this.waiting = {}; this.orphans = {}; @@ -150,6 +151,8 @@ Mempool.prototype._open = co(function* open() { } } + this.tip = this.chain.tip.hash; + this.logger.info('Mempool loaded (maxsize=%dkb).', size); }); @@ -227,6 +230,8 @@ Mempool.prototype._addBlock = co(function* addBlock(block, txs) { yield this.cache.flush(); + this.tip = block.hash; + if (entries.length === 0) return; @@ -291,6 +296,8 @@ Mempool.prototype._removeBlock = co(function* removeBlock(block, txs) { yield this.cache.flush(); + this.tip = block.prevBlock; + if (total === 0) return; @@ -345,6 +352,8 @@ Mempool.prototype._reset = co(function* reset() { yield this.cache.wipe(); this.cache.clear(); } + + this.tip = this.chain.tip.hash; }); /** diff --git a/lib/mining/miner.js b/lib/mining/miner.js index bb5db248..cfd633da 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -389,6 +389,9 @@ Miner.prototype.build = function build(attempt) { if (!this.mempool) return []; + assert(this.mempool.tip === this.chain.tip.hash, + 'Mempool/chain tip mismatch! Unsafe to create block.'); + hashes = this.mempool.getSnapshot(); for (i = 0; i < hashes.length; i++) {