From f26fb320278dfde59721e3c2fa444891537adec3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 10 Feb 2016 21:07:30 -0800 Subject: [PATCH] refactor orphan tx resolution. --- lib/bcoin/tx-pool.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index ae41432e..e1e743f1 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -74,7 +74,7 @@ TXPool.prototype._init = function init() { TXPool.prototype.add = function add(tx, noWrite, strict) { var hash = tx.hash('hex'); var updated = false; - var i, input, output, coin, unspent, index, orphan; + var i, j, input, output, coin, unspent, index, orphan; var key, orphans, some; this._wallet.fillPrevout(tx); @@ -148,23 +148,6 @@ TXPool.prototype.add = function add(tx, noWrite, strict) { this._orphans[key] = [orphan]; } - function checkOrphan(orphan) { - orphan.tx.inputs[orphan.index].output = coin; - - assert(orphan.tx.inputs[orphan.index].prevout.hash === hash); - assert(orphan.tx.inputs[orphan.index].prevout.index === i); - - // Verify that input script is correct, if not - add output to unspent - // and remove orphan from storage - if (!orphan.tx.verify(orphan.index)) { - this._removeTX(orphan.tx, noWrite); - return false; - } - - this._addInput(orphan.tx, orphan.index); - return true; - } - // Add unspent outputs or fullfill orphans for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; @@ -182,12 +165,32 @@ TXPool.prototype.add = function add(tx, noWrite, strict) { // Add input to orphan if (orphans) { - some = orphans.some(checkOrphan, this); + some = false; + + for (j = 0; j < orphans.length; j++) { + orphan = orphans[j]; + orphan.tx.inputs[orphan.index].output = coin; + + assert(orphan.tx.inputs[orphan.index].prevout.hash === hash); + assert(orphan.tx.inputs[orphan.index].prevout.index === i); + + // Verify that input script is correct, if not - add + // output to unspent and remove orphan from storage + if (orphan.tx.verify(orphan.index)) { + this._addInput(orphan.tx, orphan.index); + some = true; + break; + } + + this._removeTX(orphan.tx, noWrite); + } + if (!some) orphans = null; } delete this._orphans[key]; + if (!orphans) { this._unspent[key] = coin; updated = true;