From 55a7842f595952d619bd479a1c7c1413a2076931 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 4 Mar 2016 04:38:24 -0800 Subject: [PATCH] txid parsing fixes for txdb. --- lib/bcoin/txdb.js | 100 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index dc9a7fec..b5a2f5a2 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -261,13 +261,6 @@ TXPool.prototype.add = function add(tx, callback) { }); }; -function pad32(num) { - num = num + ''; - while (num.length < 10) - num = '0' + num; - return num; -} - // This big scary function is what a persistent tx pool // looks like. It's a semi mempool in that it can handle // receiving txs out of order. @@ -687,6 +680,78 @@ TXPool.prototype._remove = function remove(tx, map, callback) { }); }; +TXPool.prototype._confirm = function _confirm(tx, map, callback) { + var self = this; + var prefix = this.prefix + '/'; + var hash = tx.hash('hex'); + var height = tx.height; + var ts = tx.ts; + var batch; + + this.getTX(hash, function(err, existing) { + if (err) + return done(err); + + batch = self.db.batch(); + + if (!existing) + return callback(null, false); + + if (!(existing.ts === 0 && tx.ts !== 0)) + return callback(null, true); + + // Tricky - update the tx and coin in storage, + // and remove pending flag to mark as confirmed. + assert(tx.height >= 0); + assert(existing.ps > 0); + + batch.put(prefix + 't/t/' + hash, tx.toExtended()); + batch.del(prefix + 't/p/t/' + hash); + batch.put(prefix + 't/h/h/' + pad32(tx.height) + '/' + hash, DUMMY); + batch.del(prefix + 't/s/s/' + pad32(existing.ps) + '/' + hash); + batch.put(prefix + 't/s/s/' + pad32(tx.ts) + '/' + hash, DUMMY); + + map.all.forEach(function(id) { + batch.del(prefix + 't/p/a/' + id + '/' + hash); + batch.put( + prefix + 't/h/a/' + id + '/' + pad32(tx.height) + '/' + hash, DUMMY); + batch.del( + prefix + 't/s/a/' + id + '/' + pad32(existing.ps) + '/' + hash); + batch.put( + prefix + 't/s/a/' + id + '/' + pad32(tx.ts) + '/' + hash, DUMMY); + }); + + utils.forEachSerial(tx.outputs, function(output, next, i) { + self.getCoin(hash, i, function(err, coin) { + if (err) + return next(err); + + if (!coin) + return next(); + + coin.height = tx.height; + + batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw()); + + next(); + }); + }, function(err) { + if (err) + return done(err); + + batch.write(function(err) { + if (err) + return done(err); + + self.emit('confirmed', tx, map); + self.emit('tx', tx, map); + + return callback(null, true); + }); + }); + }); +}; + TXPool.prototype.unconfirm = function unconfirm(hash, callback) { var self = this; @@ -897,9 +962,9 @@ TXPool.prototype.getPendingHashes = function getPendingHashes(address, callback) } if (address) - txs.push(key.split('/')[4]); + txs.push(key.split('/')[5]); else - txs.push(key.split('/')[3]); + txs.push(key.split('/')[4]); next(); }); @@ -1008,7 +1073,10 @@ TXPool.prototype.getHeightRangeHashes = function getHeightRangeHashes(address, o }); } - txs.push(key.split('/')[4]); + if (address) + txs.push(key.split('/')[6]); + else + txs.push(key.split('/')[5]); next(); }); @@ -1058,7 +1126,10 @@ TXPool.prototype.getTimeRangeHashes = function getTimeRangeHashes(address, optio }); } - txs.push(key.split('/')[4]); + if (address) + txs.push(key.split('/')[6]); + else + txs.push(key.split('/')[5]); next(); }); @@ -1348,6 +1419,13 @@ TXPool.prototype.getBalance = function getBalance(callback) { return this.getBalanceByAddress(null, callback); }; +function pad32(num) { + num = num + ''; + while (num.length < 10) + num = '0' + num; + return num; +} + /** * Expose */