Revert "store undo coins in coinviews."

This reverts commit f2465c2caf.
This commit is contained in:
Christopher Jeffrey 2016-06-12 09:11:43 -07:00
parent f2465c2caf
commit 6081998516
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 21 additions and 43 deletions

View File

@ -874,8 +874,7 @@ ChainDB.prototype.removeBlock = function removeBlock(hash, batch, callback) {
ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callback) {
var self = this;
var undo = new bcoin.coinview();
var hasUndo = false;
var undo = new BufferWriter();
var i, j, tx, input, output, key, addresses, address, hash, coins, raw;
if (this.options.spv) {
@ -917,8 +916,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb
batch.del(layout.C(address, input.prevout.hash, input.prevout.index));
}
hasUndo = true;
undo.addCoin(input.coin);
Framer.coin(input.coin, false, undo);
}
for (j = 0; j < tx.outputs.length; j++) {
@ -949,8 +947,8 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb
}
}
if (hasUndo)
batch.put(layout.u(block.hash()), undo.toRaw());
if (undo.written > 0)
batch.put(layout.u(block.hash()), undo.render());
this.emit('add block', block);
@ -1397,7 +1395,16 @@ ChainDB.prototype.getCoinView = function getCoinView(block, callback) {
ChainDB.prototype.getUndoCoins = function getUndoCoins(hash, callback) {
return this.db.fetch(layout.u(hash), function(data) {
return bcoin.coinview.fromRaw(data);
var p = new BufferReader(data);
var coins = [];
var coin;
while (p.left()) {
coin = Parser.parseCoin(p, false);
coins.push(new bcoin.coin(coin));
}
return coins;
}, callback);
};
@ -1409,7 +1416,7 @@ ChainDB.prototype.getUndoCoins = function getUndoCoins(hash, callback) {
ChainDB.prototype.getUndoView = function getUndoView(block, callback) {
var self = this;
var i, j, tx, input;
var i, j, k, tx, input, coin;
return this.getCoinView(block, function(err, view) {
if (err)
@ -1422,7 +1429,7 @@ ChainDB.prototype.getUndoView = function getUndoView(block, callback) {
if (!coins)
return callback(null, view);
for (i = 0; i < block.txs.length; i++) {
for (i = 0, k = 0; i < block.txs.length; i++) {
tx = block.txs[i];
if (tx.isCoinbase())
@ -1430,8 +1437,11 @@ ChainDB.prototype.getUndoView = function getUndoView(block, callback) {
for (j = 0; j < tx.inputs.length; j++) {
input = tx.inputs[j];
input.coin = coins.spend(input.prevout.hash, input.prevout.index);
view.addCoin(input.coin);
coin = coins[k++];
coin.hash = input.prevout.hash;
coin.index = input.prevout.index;
input.coin = coin;
view.addCoin(coin);
}
}

View File

@ -8,8 +8,6 @@
var bcoin = require('./env');
var utils = bcoin.utils;
var assert = utils.assert;
var BufferReader = require('./reader');
var BufferWriter = require('./writer');
/**
* A collections of {@link Coins} objects.
@ -98,36 +96,6 @@ CoinView.prototype.spend = function spend(hash, index) {
return this.coins[hash].spend(index);
};
CoinView.prototype.toRaw = function toRaw() {
var keys = Object.keys(this.coins);
var p = new BufferWriter();
var out = [];
var i, hash;
for (i = 0; i < keys.length; i++) {
hash = keys[i];
p.writeHash(hash);
p.writeVarBytes(this.coins[hash].toRaw());
}
return p.render();
};
CoinView.fromRaw = function fromRaw(data) {
var p = new BufferReader(data, true);
var map = {};
var hash, coins;
while (p.left()) {
hash = p.readHash('hex');
coins = bcoin.coins.fromRaw(p.readVarBytes());
coins.hash = hash;
map[hash] = coins;
}
return new CoinView(map);
};
/**
* Fill transaction(s) with coins.
* @param {TX} tx