From 7bf912f570013a436ddc747801aa29c20768ada8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 2 Mar 2016 03:12:28 -0800 Subject: [PATCH] do not store orphan txs. --- lib/bcoin/txdb.js | 79 +++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 51fa0660..ee5082c5 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -233,7 +233,11 @@ TXPool.prototype._add = function add(tx, callback, force) { + '/' + input.prevout.index); } - batch.del(p + 'u/t/' + input.prevout.hash + '/' + input.prevout.index); + batch.del( + p + 'u/t/' + + input.prevout.hash + + '/' + input.prevout.index); + return next(); } @@ -260,7 +264,7 @@ TXPool.prototype._add = function add(tx, callback, force) { if (orphans) { try { - orphans = JSON.parse(orphans.toString('utf8')); + orphans = JSON.parse(orphans.toString('ascii')); } catch (e) { return done(e); } @@ -269,11 +273,11 @@ TXPool.prototype._add = function add(tx, callback, force) { } orphans.push({ - tx: tx.toExtended(true).toString('hex'), + tx: hash, index: i }); - orphans = new Buffer(JSON.stringify(orphans), 'utf8'); + orphans = new Buffer(JSON.stringify(orphans), 'ascii'); batch.put(p + 'o/' + key, orphans); @@ -307,43 +311,46 @@ TXPool.prototype._add = function add(tx, callback, force) { if (err && err.type !== 'NotFoundError') return done(err); - if (orphans) { - try { - orphans = JSON.parse(orphans.toString('utf8')); - orphans = orphans.map(function(orphan) { - var tx = new Buffer(orphan.tx, 'hex'); - orphan.tx = bcoin.tx.fromExtended(tx, true); - return orphan; - }); - } catch (e) { - return next(e); - } - } - // Add input to orphan if (orphans) { some = false; + try { + orphans = JSON.parse(new Buffer(orphans, 'ascii')); + } catch (e) { + return done(e); + } + utils.forEachSerial(orphans, function(orphan, next, j) { if (some) return next(); - 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)) { - some = true; - return next(); - } - - self.remove(orphan.tx, function(err) { + self.getTX(orphan.tx, function(err, otx) { if (err) - return next(err); - return next(); + return done(err); + + // Probably removed by some other means. + if (!otx) + return next(); + + orphan.tx = otx; + 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)) { + some = true; + return next(); + } + + self.remove(orphan.tx, function(err) { + if (err) + return next(err); + return next(); + }); }); }, function(err) { if (err) @@ -371,8 +378,12 @@ TXPool.prototype._add = function add(tx, callback, force) { if (type === 'pubkey' || type === 'multisig') address = null; - if (address) - batch.put(p + 'u/a/' + address + '/' + hash + '/' + i, new Buffer([])); + if (address) { + batch.put( + p + 'u/a/' + address + + '/' + hash + '/' + i, + new Buffer([])); + } batch.put(p + 'u/t/' + hash + '/' + i, coin.toRaw()); updated = true;