diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 2b93dfcb..a462d8f0 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -1677,11 +1677,11 @@ ChainDB.prototype.connectBlock = co(function* connectBlock(block, view) { for (i = 0; i < view.length; i++) { coins = view[i]; - raw = coins.toRaw(); - if (!raw) { + if (coins.isFullySpent()) { this.del(layout.c(coins.hash)); this.coinCache.unpush(coins.hash); } else { + raw = coins.toRaw(); this.put(layout.c(coins.hash), raw); this.coinCache.push(coins.hash, raw); } @@ -1773,11 +1773,11 @@ ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) { for (i = 0; i < view.length; i++) { coins = view[i]; - raw = coins.toRaw(); - if (!raw) { + if (coins.isFullySpent()) { this.del(layout.c(coins.hash)); this.coinCache.unpush(coins.hash); } else { + raw = coins.toRaw(); this.put(layout.c(coins.hash), raw); this.coinCache.push(coins.hash, raw); } diff --git a/lib/blockchain/coins-old.js b/lib/blockchain/coins-old.js index 45907b6c..b54d4cf2 100644 --- a/lib/blockchain/coins-old.js +++ b/lib/blockchain/coins-old.js @@ -183,6 +183,15 @@ Coins.prototype.size = function size() { return index + 1; }; +/** + * Test whether the coins are fully spent. + * @returns {Boolean} + */ + +Coins.prototype.isFullySpent = function isFullySpent() { + return this.size() === 0; +}; + /* * Coins serialization: * version: varint diff --git a/lib/blockchain/coins.js b/lib/blockchain/coins.js index d21c3ca4..c8508bec 100644 --- a/lib/blockchain/coins.js +++ b/lib/blockchain/coins.js @@ -175,6 +175,15 @@ Coins.prototype.cleanup = function cleanup() { this.outputs.length = len; }; +/** + * Test whether the coins are fully spent. + * @returns {Boolean} + */ + +Coins.prototype.isFullySpent = function isFullySpent() { + return this.outputs.length === 0; +}; + /* * Coins serialization: * version: varint @@ -215,9 +224,8 @@ Coins.prototype.toRaw = function toRaw() { var nonzero = 0; var i, j, code, ch, output; - // Return nothing if we're fully spent. - if (len === 0) - return; + // Throw if we're fully spent. + assert(len !== 0, 'Cannot serialize fully-spent coins.'); // Calculate number of unspents and spent field size. // size = number of bytes required for the bit field. @@ -537,7 +545,7 @@ CoinEntry.prototype.toWriter = function toWriter(bw) { if (this.output) { compress.output(this.output, bw); - return; + return bw; } assert(this.raw); @@ -549,6 +557,8 @@ CoinEntry.prototype.toWriter = function toWriter(bw) { raw = this.raw.slice(this.offset, this.offset + this.size); bw.writeBytes(raw); + + return bw; }; /** diff --git a/lib/blockchain/compress.js b/lib/blockchain/compress.js index b3b8983f..74252d75 100644 --- a/lib/blockchain/compress.js +++ b/lib/blockchain/compress.js @@ -104,7 +104,7 @@ function decompressScript(script, br) { // This violates consensus rules. // We don't need to read it. script.fromNulldata(new Buffer(0)); - p.seek(size); + br.seek(size); } else { data = br.readBytes(size); script.fromRaw(data);