From c056b0d8abceaa4b16c41becf107f01665f86be5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 12 Jun 2016 13:48:02 -0700 Subject: [PATCH] refactor. --- lib/bcoin/chain.js | 3 ++- lib/bcoin/chaindb.js | 31 ++++++++++++++++++------------- lib/bcoin/coins.js | 20 ++++++++++++++++++-- lib/bcoin/coinview.js | 14 +++++++------- lib/bcoin/tx.js | 9 --------- 5 files changed, 45 insertions(+), 32 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index c2e9b847..4ab9e0a2 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -890,7 +890,8 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, state, callbac } } - view.add(tx.toCoins()); + // Add new coins. + view.addTX(tx); return next(); }); diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index bdae44e3..074f8e89 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -842,16 +842,16 @@ ChainDB.prototype.removeBlock = function removeBlock(hash, batch, callback) { ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callback) { var undo = new BufferWriter(); - var i, j, tx, input, output, key, addresses, address, hash, coins, raw; + var i, j, tx, input, output, prev, addresses, address, hash, coins, raw; if (this.options.spv) { this.emit('add block', block); - return utils.nextTick(callback); + return utils.asyncify(callback)(null, block); } // Genesis block's coinbase is unspendable. if (this.chain.isGenesis(block)) - return utils.nextTick(callback); + return utils.asyncify(callback)(null, block); for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; @@ -870,7 +870,6 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb for (j = 0; j < tx.inputs.length; j++) { input = tx.inputs[j]; - key = input.prevout.hash + '/' + input.prevout.index; if (tx.isCoinbase()) break; @@ -879,8 +878,10 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb if (this.options.indexAddress) { address = input.getHash(); - if (address) - batch.del(layout.C(address, input.prevout.hash, input.prevout.index)); + if (address) { + prev = input.prevout; + batch.del(layout.C(address, prev.hash, prev.index)); + } } Framer.coin(input.coin, false, undo); @@ -935,10 +936,10 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callback) { var self = this; - var i, j, tx, input, output, key, addresses, address, hash, coins, raw; + var i, j, tx, input, output, prev, addresses, address, hash, coins, raw; if (this.options.spv) - return utils.nextTick(callback); + return utils.asyncify(callback)(null, block); this.getUndoView(block, function(err, view) { if (err) @@ -961,7 +962,6 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb for (j = 0; j < tx.inputs.length; j++) { input = tx.inputs[j]; - key = input.prevout.hash + '/' + input.prevout.index; if (tx.isCoinbase()) break; @@ -970,16 +970,20 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb if (self.options.indexAddress) { address = input.getHash(); - if (address) - batch.put(layout.C(address, input.prevout.hash, input.prevout.index), DUMMY); + if (address) { + prev = input.prevout; + batch.put(layout.C(address, prev.hash, prev.index), DUMMY); + } } } - view.add(tx.toCoins()); + // Add all of the coins we are about to + // remove. This is to ensure they appear + // in the view array below. + view.addTX(tx); for (j = 0; j < tx.outputs.length; j++) { output = tx.outputs[j]; - key = hash + '/' + j; if (output.script.isUnspendable()) continue; @@ -990,6 +994,7 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb batch.del(layout.C(address, hash, j)); } + // Spend added coin. view.spend(hash, j); } } diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index 2981bafe..14b074a7 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -246,7 +246,7 @@ Coins.parseRaw = function parseRaw(data, hash, index) { i++; } - assert(index == null, 'Bad index.'); + assert(index == null, 'Bad coin index.'); return coins; }; @@ -260,7 +260,7 @@ Coins.parseRaw = function parseRaw(data, hash, index) { */ Coins.parseCoin = function parseCoin(data, hash, index) { - assert(index != null, 'Bad index.'); + assert(index != null, 'Bad coin index.'); return Coins.parseRaw(data, hash, index); }; @@ -323,11 +323,21 @@ Coins.fromTX = function fromTX(tx) { */ function DeferredCoin(offset, size, raw) { + if (!(this instanceof DeferredCoin)) + return new DeferredCoin(offset, size, raw); + this.offset = offset; this.size = size; this.raw = raw; } +/** + * Parse the deferred data and return a Coin. + * @param {Coins} coins + * @param {Number} index + * @returns {Coin} + */ + DeferredCoin.prototype.toCoin = function toCoin(coins, index) { var p = new BufferReader(this.raw); var prefix, script, value; @@ -358,6 +368,12 @@ DeferredCoin.prototype.toCoin = function toCoin(coins, index) { }); }; +/** + * Slice off the part of the buffer + * relevant to this particular coin. + * @returns {Buffer} + */ + DeferredCoin.prototype.toRaw = function toRaw() { return this.raw.slice(this.offset, this.offset + this.size); }; diff --git a/lib/bcoin/coinview.js b/lib/bcoin/coinview.js index 4ac42b0c..13ed1133 100644 --- a/lib/bcoin/coinview.js +++ b/lib/bcoin/coinview.js @@ -25,8 +25,8 @@ function CoinView(coins) { } /** - * Add a coin to the collection. - * @param {Coins|TX} tx/coins + * Add coins to the collection. + * @param {Coins} coins */ CoinView.prototype.add = function add(coins) { @@ -35,7 +35,7 @@ CoinView.prototype.add = function add(coins) { /** * Add a coin to the collection. - * @param {Coins|TX} tx/coins + * @param {Coin} coin */ CoinView.prototype.addCoin = function addCoin(coin) { @@ -46,12 +46,12 @@ CoinView.prototype.addCoin = function addCoin(coin) { }; /** - * Remove a collection from the view. - * @param {Coins|TX} tx/coins + * Add a tx to the collection. + * @param {TX} tx */ -CoinView.prototype.remove = function remove(coins) { - delete this.coins[coins.hash]; +CoinView.prototype.addTX = function addTX(tx) { + this.add(bcoin.coins.fromTX(tx)); }; /** diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 1c40613f..62bfc0a7 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1989,15 +1989,6 @@ TX.fromExtended = function fromExtended(data, saveCoins, enc) { return new TX(TX.parseExtended(data, saveCoins, enc)); }; -/** - * Convert transaction outputs to a {Coins} object. - * @returns {Coins} - */ - -TX.prototype.toCoins = function toCoins() { - return bcoin.coins.fromTX(this); -}; - /** * Test whether an object is a TX. * @param {Object} obj