chain: pass height around.

This commit is contained in:
Christopher Jeffrey 2016-12-06 17:13:04 -08:00
parent 136a48b4b4
commit d4b8afa747
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 40 additions and 38 deletions

View File

@ -651,7 +651,7 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) {
// Skip everything if we're // Skip everything if we're
// using checkpoints. // using checkpoints.
if (historical) { if (historical) {
view.addTX(tx); view.addTX(tx, height);
continue; continue;
} }
@ -689,7 +689,7 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) {
} }
// Add new coins. // Add new coins.
view.addTX(tx); view.addTX(tx, height);
} }
if (historical) if (historical)

View File

@ -1301,7 +1301,7 @@ ChainDB.prototype._save = co(function* save(entry, block, view) {
if (!view) { if (!view) {
// Save block data. // Save block data.
yield this.saveBlock(block); yield this.saveBlock(entry, block);
return; return;
} }
@ -1313,7 +1313,7 @@ ChainDB.prototype._save = co(function* save(entry, block, view) {
this.cacheHeight.push(entry.height, entry); this.cacheHeight.push(entry.height, entry);
// Connect block and save data. // Connect block and save data.
yield this.saveBlock(block, view); yield this.saveBlock(entry, block, view);
// Commit new chain state. // Commit new chain state.
this.put(layout.R, this.pending.commit(hash)); this.put(layout.R, this.pending.commit(hash));
@ -1364,7 +1364,7 @@ ChainDB.prototype._reconnect = co(function* reconnect(entry, block, view) {
this.saveUpdates(); this.saveUpdates();
// Connect inputs. // Connect inputs.
yield this.connectBlock(block, view); yield this.connectBlock(entry, block, view);
// Update chain state. // Update chain state.
this.put(layout.R, this.pending.commit(hash)); this.put(layout.R, this.pending.commit(hash));
@ -1419,7 +1419,7 @@ ChainDB.prototype._disconnect = co(function* disconnect(entry) {
throw new Error('Block not found.'); throw new Error('Block not found.');
// Disconnect inputs. // Disconnect inputs.
yield this.disconnectBlock(block); yield this.disconnectBlock(entry, block);
// Revert chain state to previous tip. // Revert chain state to previous tip.
this.put(layout.R, this.pending.commit(entry.prevBlock)); this.put(layout.R, this.pending.commit(entry.prevBlock));
@ -1503,7 +1503,7 @@ ChainDB.prototype.reset = co(function* reset(block) {
// Disconnect and remove block data. // Disconnect and remove block data.
try { try {
yield this.removeBlock(tip.hash); yield this.removeBlock(tip);
} catch (e) { } catch (e) {
this.drop(); this.drop();
throw e; throw e;
@ -1593,7 +1593,7 @@ ChainDB.prototype._removeChain = co(function* removeChain(hash) {
* @returns {Promise} - Returns {@link Block}. * @returns {Promise} - Returns {@link Block}.
*/ */
ChainDB.prototype.saveBlock = co(function* saveBlock(block, view) { ChainDB.prototype.saveBlock = co(function* saveBlock(entry, block, view) {
if (this.options.spv) if (this.options.spv)
return; return;
@ -1604,30 +1604,30 @@ ChainDB.prototype.saveBlock = co(function* saveBlock(block, view) {
if (!view) if (!view)
return; return;
yield this.connectBlock(block, view); yield this.connectBlock(entry, block, view);
}); });
/** /**
* Remove a block (not an entry) to the database. * Remove a block (not an entry) to the database.
* Disconnect inputs. * Disconnect inputs.
* @param {Block|Hash} block - {@link Block} or hash. * @param {ChainEntry} entry
* @returns {Promise} - Returns {@link Block}. * @returns {Promise} - Returns {@link Block}.
*/ */
ChainDB.prototype.removeBlock = co(function* removeBlock(hash) { ChainDB.prototype.removeBlock = co(function* removeBlock(entry) {
var block; var block;
if (this.options.spv) if (this.options.spv)
return; return;
block = yield this.getBlock(hash); block = yield this.getBlock(entry.hash);
if (!block) if (!block)
throw new Error('Block not found.'); throw new Error('Block not found.');
this.del(layout.b(block.hash())); this.del(layout.b(block.hash()));
return yield this.disconnectBlock(block); return yield this.disconnectBlock(entry, block);
}); });
/** /**
@ -1660,7 +1660,7 @@ ChainDB.prototype.saveView = function saveView(view) {
* @returns {Promise} - Returns {@link Block}. * @returns {Promise} - Returns {@link Block}.
*/ */
ChainDB.prototype.connectBlock = co(function* connectBlock(block, view) { ChainDB.prototype.connectBlock = co(function* connectBlock(entry, block, view) {
var i, j, tx, input, output; var i, j, tx, input, output;
if (this.options.spv) if (this.options.spv)
@ -1705,7 +1705,7 @@ ChainDB.prototype.connectBlock = co(function* connectBlock(block, view) {
this.put(layout.u(block.hash()), view.undo.commit()); this.put(layout.u(block.hash()), view.undo.commit());
// Prune height-288 if pruning is enabled. // Prune height-288 if pruning is enabled.
yield this.pruneBlock(block); yield this.pruneBlock(entry);
}); });
/** /**
@ -1714,7 +1714,7 @@ ChainDB.prototype.connectBlock = co(function* connectBlock(block, view) {
* @returns {Promise} - Returns {@link Block}. * @returns {Promise} - Returns {@link Block}.
*/ */
ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) { ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(entry, block) {
var i, j, view, undo, tx, input, output; var i, j, view, undo, tx, input, output;
if (this.options.spv) if (this.options.spv)
@ -1729,18 +1729,6 @@ ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) {
for (i = block.txs.length - 1; i >= 0; i--) { for (i = block.txs.length - 1; i >= 0; i--) {
tx = block.txs[i]; tx = block.txs[i];
for (j = tx.outputs.length - 1; j >= 0; j--) {
output = tx.outputs[j];
if (output.script.isUnspendable())
continue;
this.pending.spend(output);
}
// Remove any created coins.
view.removeTX(tx);
if (i > 0) { if (i > 0) {
yield view.ensureInputs(this, tx); yield view.ensureInputs(this, tx);
@ -1751,6 +1739,18 @@ ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) {
} }
} }
// Remove any created coins.
view.removeTX(tx, entry.height);
for (j = tx.outputs.length - 1; j >= 0; j--) {
output = tx.outputs[j];
if (output.script.isUnspendable())
continue;
this.pending.spend(output);
}
// Remove from transaction index. // Remove from transaction index.
this.unindexTX(tx); this.unindexTX(tx);
} }
@ -1773,7 +1773,7 @@ ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) {
* @returns {Promise} * @returns {Promise}
*/ */
ChainDB.prototype.pruneBlock = co(function* pruneBlock(block) { ChainDB.prototype.pruneBlock = co(function* pruneBlock(entry) {
var height, hash; var height, hash;
if (this.options.spv) if (this.options.spv)
@ -1782,7 +1782,7 @@ ChainDB.prototype.pruneBlock = co(function* pruneBlock(block) {
if (!this.options.prune) if (!this.options.prune)
return; return;
height = block.height - this.network.block.keepBlocks; height = entry.height - this.network.block.keepBlocks;
if (height <= this.network.block.pruneAfterHeight) if (height <= this.network.block.pruneAfterHeight)
return; return;

View File

@ -428,12 +428,14 @@ Coins.fromRaw = function fromRaw(data, hash) {
* @param {TX} tx * @param {TX} tx
*/ */
Coins.prototype.fromTX = function fromTX(tx) { Coins.prototype.fromTX = function fromTX(tx, height) {
var i, output; var i, output;
assert(typeof height === 'number');
this.version = tx.version; this.version = tx.version;
this.hash = tx.hash('hex'); this.hash = tx.hash('hex');
this.height = tx.height; this.height = height;
this.coinbase = tx.isCoinbase(); this.coinbase = tx.isCoinbase();
for (i = 0; i < tx.outputs.length; i++) { for (i = 0; i < tx.outputs.length; i++) {
@ -458,8 +460,8 @@ Coins.prototype.fromTX = function fromTX(tx) {
* @returns {Coins} * @returns {Coins}
*/ */
Coins.fromTX = function fromTX(tx) { Coins.fromTX = function fromTX(tx, height) {
return new Coins().fromTX(tx); return new Coins().fromTX(tx, height);
}; };
/** /**

View File

@ -41,8 +41,8 @@ CoinView.prototype.addCoins = function addCoins(coins) {
* @param {TX} tx * @param {TX} tx
*/ */
CoinView.prototype.addTX = function addTX(tx) { CoinView.prototype.addTX = function addTX(tx, height) {
var coins = Coins.fromTX(tx); var coins = Coins.fromTX(tx, height);
return this.addCoins(coins); return this.addCoins(coins);
}; };
@ -51,8 +51,8 @@ CoinView.prototype.addTX = function addTX(tx) {
* @param {TX} tx * @param {TX} tx
*/ */
CoinView.prototype.removeTX = function removeTX(tx) { CoinView.prototype.removeTX = function removeTX(tx, height) {
var coins = Coins.fromTX(tx); var coins = Coins.fromTX(tx, height);
coins.outputs.length = 0; coins.outputs.length = 0;
return this.addCoins(coins); return this.addCoins(coins);
}; };