refactor orphan tx resolution.

This commit is contained in:
Christopher Jeffrey 2016-02-10 21:07:30 -08:00
parent 501abbab45
commit f26fb32027

View File

@ -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;