coins: refactor.

This commit is contained in:
Christopher Jeffrey 2016-11-26 10:03:32 -08:00
parent 20cdb1adbc
commit c523193714
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

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