diff --git a/lib/blockchain/coins.js b/lib/blockchain/coins.js index 7352bdfc..9828c3db 100644 --- a/lib/blockchain/coins.js +++ b/lib/blockchain/coins.js @@ -136,12 +136,12 @@ Coins.prototype.get = function get(index) { var coin; if (index >= this.outputs.length) - return null; + return; coin = this.outputs[index]; if (!coin) - return null; + return; return coin.toCoin(this, index); }; @@ -215,27 +215,23 @@ Coins.prototype.size = function size() { Coins.prototype.toRaw = function toRaw() { var p = new BufferWriter(); - var height = this.height; var length = this.size(); - var i, output, bits, start, len, bit, oct; + var len = Math.ceil(length / 8); + var i, output, bits, start, bit, oct, data; // Return nothing if we're fully spent. if (length === 0) - return null; + return; - // Varint version: hopefully some smartass - // miner doesn't start mining `-1` versions. + // Varint version: hopefully we + // never run into `-1` versions. p.writeVarint(this.version); - // Unfortunately, we don't have a compact - // way to store unconfirmed height. - if (height === -1) - height = 0x7fffffff; - // Create the `bits` value: // (height | coinbase-flag). - bits = height << 1; + bits = this.height << 1; + // Append the coinbase bit. if (this.coinbase) bits |= 1; @@ -255,7 +251,6 @@ Coins.prototype.toRaw = function toRaw() { // Fill the spent field with zeroes to avoid // allocating a buffer. We mark the spents // after rendering the final buffer. - len = Math.ceil(length / 8); p.writeVarint(len); start = p.written; p.fill(0, len); @@ -272,7 +267,7 @@ Coins.prototype.toRaw = function toRaw() { // Render the buffer with all // zeroes in the spent field. - p = p.render(); + data = p.render(); // Mark the spents in the spent field. // This is essentially a NOP for new coins. @@ -284,10 +279,12 @@ Coins.prototype.toRaw = function toRaw() { bit = i % 8; oct = (i - bit) / 8; - p[start + oct] |= 1 << (7 - bit); + oct += start; + + data[oct] |= 1 << (7 - bit); } - return p; + return data; }; /** @@ -300,8 +297,7 @@ Coins.prototype.toRaw = function toRaw() { Coins.prototype.fromRaw = function fromRaw(data, hash, index) { var p = new BufferReader(data); var pos = 0; - var start, len, bit, oct; - var bits, coin, spent; + var bits, len, start, bit, oct, spent, coin; this.version = p.readVarint(); @@ -311,9 +307,6 @@ Coins.prototype.fromRaw = function fromRaw(data, hash, index) { this.hash = hash; this.coinbase = (bits & 1) !== 0; - if (this.height === 0x7fffffff) - this.height = -1; - // Mark the start of the spent field and // seek past it to avoid reading a buffer. len = p.readVarint(); @@ -321,10 +314,13 @@ Coins.prototype.fromRaw = function fromRaw(data, hash, index) { p.seek(len); while (p.left()) { - // Read a single bit out of the spent field. bit = pos % 8; oct = (pos - bit) / 8; - spent = (data[start + oct] >>> (7 - bit)) & 1; + oct += start; + + // Read a single bit out of the spent field. + spent = data[oct] >>> (7 - bit); + spent &= 1; // Already spent. if (spent) { @@ -356,8 +352,7 @@ Coins.parseCoin = function parseCoin(data, hash, index) { var p = new BufferReader(data); var coin = new Coin(); var pos = 0; - var start, len, bit, oct; - var spent, bits; + var bits, len, start, bit, oct, spent; coin.version = p.readVarint(); @@ -369,9 +364,6 @@ Coins.parseCoin = function parseCoin(data, hash, index) { coin.hash = hash; coin.coinbase = (bits & 1) !== 0; - if (coin.height === 0x7fffffff) - coin.height = -1; - // Mark the start of the spent field and // seek past it to avoid reading a buffer. len = p.readVarint(); @@ -379,15 +371,18 @@ Coins.parseCoin = function parseCoin(data, hash, index) { p.seek(len); while (p.left()) { - // Read a single bit out of the spent field. bit = pos % 8; oct = (pos - bit) / 8; - spent = (data[start + oct] >>> (7 - bit)) & 1; + oct += start; + + // Read a single bit out of the spent field. + spent = data[oct] >>> (7 - bit); + spent &= 1; // We found our coin. if (pos === index) { if (spent) - return null; + return; decompress.script(p, coin.script); coin.value = p.readVarint(); return coin; @@ -403,8 +398,6 @@ Coins.parseCoin = function parseCoin(data, hash, index) { skipCoin(p); pos++; } - - return null; }; /**