diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 86512a5e..121ef3fd 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -205,36 +205,72 @@ Mempool.prototype.addBlock = function addBlock(block, callback, force) { callback = utils.wrap(callback, unlock); - utils.forEachSerial(block.txs, function(tx, next) { - var hash = tx.hash('hex'); - var copy; + // We add the txs we haven't seen to + // the mempool first to potentially + // resolve orphans. + utils.forEachSerial(block.txs, function(tx, next) { + var hash, copy; + + if (!self.chain.isFull()) + return next(); if (tx.isCoinbase()) return next(); - self.getTX(hash, function(err, existing) { + hash = tx.hash('hex'); + + self.hasTX(hash, function(err, exists) { if (err) - return callback(err); + return next(err); - if (!existing) - return self.removeOrphan(hash, next); + if (exists) + return next(); - copy = tx.clone(); - copy.ts = existing.ts; - copy.block = existing.block; - copy.height = existing.height; - copy.ps = existing.ps; - - self.removeUnchecked(copy, function(err) { + self.removeOrphan(hash, function(err) { if (err) return next(err); - self.emit('confirmed', tx, block); + copy = tx.clone(); + copy.unsetBlock(); - return next(); + self.addUnchecked(tx, next); }); }); - }, callback); + }, function(err) { + if (err) + return callback(err); + + utils.forEachSerial(block.txs.slice().reverse(), function(tx, next) { + var hash = tx.hash('hex'); + var copy; + + if (tx.isCoinbase()) + return next(); + + self.getTX(hash, function(err, existing) { + if (err) + return next(err); + + if (!existing) + return self.removeOrphan(hash, next); + + copy = tx.clone(); + copy.ts = existing.ts; + copy.block = existing.block; + copy.height = existing.height; + copy.ps = existing.ps; + + self.removeUnchecked(copy, function(err) { + if (err) + return next(err); + + self.emit('confirmed', tx, block); + + return next(); + }); + }); + }, callback); + }); }; /** @@ -253,7 +289,7 @@ Mempool.prototype.removeBlock = function removeBlock(block, callback, force) { callback = utils.wrap(callback, unlock); - utils.forEachSerial(block.txs.slice().reverse(), function(tx, next) { + utils.forEachSerial(block.txs, function(tx, next) { var copy; if (tx.isCoinbase()) diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index e15d6e88..b8ccbb11 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -509,7 +509,7 @@ testnet.block = { pruneAfterHeight: 1000, // maxTipAge: 0x7fffffff maxTipAge: 24 * 60 * 60, - slowHeight: 500000 + slowHeight: 750000 }; testnet.witness = false;