refactor.

This commit is contained in:
Christopher Jeffrey 2016-06-12 13:48:02 -07:00
parent 2549ffc4a8
commit c056b0d8ab
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 45 additions and 32 deletions

View File

@ -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();
});

View File

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

View File

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

View File

@ -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));
};
/**

View File

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