From 400680e2083f7a062dc1726808fa0ab7f0033001 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 11 Aug 2016 04:32:03 -0700 Subject: [PATCH] work --- lib/bcoin/txdb.js | 83 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index acf81e87..3e6a00c2 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -227,8 +227,7 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) { if (buf) p.writeBytes(buf); - p.writeHash(hash); - p.writeU32(index); + bcoin.outpoint(hash, index).toRaw(p); return callback(null, p.render()); }); @@ -243,17 +242,14 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) { TXDB.prototype._getOrphans = function _getOrphans(key, callback) { var self = this; + var out = []; this.db.fetch('o/' + key, function(buf) { var p = new BufferReader(buf); var orphans = []; - while (p.left()) { - orphans.push({ - hash: p.readHash('hex'), - index: p.readU32() - }); - } + while (p.left()) + orphans.push(bcoin.outpoint.fromRaw(p)); return orphans; }, function(err, orphans) { @@ -268,7 +264,7 @@ TXDB.prototype._getOrphans = function _getOrphans(key, callback) { if (err) return next(err); - orphan.tx = tx; + out.push([orphan, tx]); next(); }); @@ -276,7 +272,7 @@ TXDB.prototype._getOrphans = function _getOrphans(key, callback) { if (err) return callback(err); - return callback(null, orphans); + return callback(null, out); }); }); }; @@ -496,7 +492,8 @@ TXDB.prototype._add = function add(tx, info, callback, force) { key = prevout.hash + '/' + prevout.index; - batch.put('s/' + key, tx.hash()); + // s/[outpoint-key] -> [spender-hash]|[spender-input-index] + batch.put('s/' + key, bcoin.outpoint.fromTX(tx, i).toRaw()); if (coin) { // Add TX to inputs and spend money @@ -517,6 +514,7 @@ TXDB.prototype._add = function add(tx, info, callback, force) { } batch.del('c/' + key); + batch.put('d/' + tx.hash('hex') + '/' + pad32(i), coin.toRaw()); self.coinCache.remove(key); @@ -607,32 +605,31 @@ TXDB.prototype._add = function add(tx, info, callback, force) { return finish(); // Add input to orphan - utils.forEachSerial(orphans, function(orphan, next) { + utils.forEachSerial(orphans, function(pair, next) { if (some) return next(); + var input = pair[0]; + var orphan = pair[1]; + // Probably removed by some other means. - if (!orphan.tx) + if (!orphan) return next(); - orphan.tx.inputs[orphan.index].coin = coin; + orphan.inputs[input.index].coin = coin; - assert(orphan.tx.inputs[orphan.index].prevout.hash === hash); - assert(orphan.tx.inputs[orphan.index].prevout.index === i); + assert(orphan.inputs[input.index].prevout.hash === hash); + assert(orphan.inputs[input.index].prevout.index === i); // Verify that input script is correct, if not - add // output to unspent and remove orphan from storage - if (!self.options.verify) { + if (!self.options.verify || orphan.verifyInput(input.index)) { some = true; + batch.put('d/' + input.hash + '/' + pad32(input.index), coin.toRaw()); return next(); } - if (orphan.tx.verifyInput(orphan.index)) { - some = true; - return next(); - } - - self.lazyRemove(orphan.tx, next, true); + self.lazyRemove(orphan, next, true); }, function(err) { if (err) return next(err); @@ -821,7 +818,7 @@ TXDB.prototype.isDoubleSpend = function isDoubleSpend(tx, callback) { TXDB.prototype.isSpent = function isSpent(hash, index, callback) { var key = 's/' + hash + '/' + index; return this.db.fetch(key, function(hash) { - return hash.toString('hex'); + return hash.toString('hex', 0, 32); }, callback); }; @@ -894,8 +891,10 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback, force) { if (err) return next(err); - if (!coin) + if (!coin) { + // TODO: Update spent coin here! return next(); + } coin.height = tx.height; coin = coin.toRaw(); @@ -1055,6 +1054,7 @@ TXDB.prototype._remove = function remove(tx, info, callback, force) { coin = input.coin.toRaw(); batch.put('c/' + key, coin); + batch.del('d/' + tx.hash('hex') + '/' + pad32(i)); batch.del('s/' + key); batch.del('o/' + key); @@ -1179,8 +1179,10 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) { if (err) return next(err); - if (!coin) + if (!coin) { + // TODO: Update spent coin here return next(); + } coin.height = tx.height; coin = coin.toRaw(); @@ -1589,25 +1591,28 @@ TXDB.prototype.getCoins = function getCoins(id, callback) { TXDB.prototype.fillHistory = function fillHistory(tx, callback) { var self = this; + var hash, index, coin, input; if (tx.isCoinbase()) { callback = utils.asyncify(callback); return callback(null, tx); } - utils.forEachSerial(tx.inputs, function(input, next) { - if (input.coin) - return next(); + hash = tx.hash('hex'); - self.getTX(input.prevout.hash, function(err, tx) { - if (err) - return next(err); - - if (tx) - input.coin = bcoin.coin.fromTX(tx, input.prevout.index); - - next(); - }); + this.db.iterate({ + gte: 'd/' + hash + '/' + pad32(0), + lte: 'd/' + hash + '/' + pad32(0xffffffff), + keys: true, + values: true, + parse: function(value, key) { + index = +key.split('/')[2]; + coin = bcoin.coin.fromRaw(value); + input = tx.inputs[index]; + coin.hash = input.prevout.hash; + coin.index = input.prevout.index; + input.coin = coin; + } }, function(err) { if (err) return callback(err); @@ -2034,9 +2039,9 @@ function Details(id, tx, table) { this.block = tx.block; this.index = tx.index; this.confirmations = tx.getConfirmations(); + this.fee = tx.hasCoins() ? tx.getFee() : 0; this.ts = tx.ts; this.ps = tx.ps; - this.fee = tx.getFee(); this.tx = tx; this.inputs = []; this.outputs = [];