more serialization work.

This commit is contained in:
Christopher Jeffrey 2016-06-17 02:26:48 -07:00
parent 16404a03ba
commit 4b5697a615
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
11 changed files with 88 additions and 90 deletions

View File

@ -42,11 +42,9 @@ function Block(data) {
bcoin.abstractblock.call(this, data); bcoin.abstractblock.call(this, data);
this.txs = []; this.txs = null;
this._cbHeight = null; this._cbHeight = null;
this._commitmentHash = null; this._commitmentHash = null;
this._raw = null; this._raw = null;
this._size = null; this._size = null;
this._witnessSize = null; this._witnessSize = null;
@ -85,7 +83,13 @@ Block.fromOptions = function fromOptions(data) {
*/ */
Block.prototype.render = function render(writer) { Block.prototype.render = function render(writer) {
return this.getRaw(writer); var raw = this.getRaw();
if (writer) {
writer.writeBytes(raw);
writer._witnessSize = raw._witnessSize;
return writer;
}
return raw;
}; };
/** /**
@ -94,8 +98,16 @@ Block.prototype.render = function render(writer) {
*/ */
Block.prototype.renderNormal = function renderNormal(writer) { Block.prototype.renderNormal = function renderNormal(writer) {
if (!this.hasWitness()) var raw;
return this.getRaw(writer); if (!this.hasWitness()) {
raw = this.getRaw();
if (writer) {
writer.writeBytes(raw);
writer._witnessSize = raw._witnessSize;
return writer;
}
return raw;
}
return this.frameNormal(writer); return this.frameNormal(writer);
}; };
@ -105,8 +117,16 @@ Block.prototype.renderNormal = function renderNormal(writer) {
*/ */
Block.prototype.renderWitness = function renderWitness(writer) { Block.prototype.renderWitness = function renderWitness(writer) {
if (this.hasWitness()) var raw;
return this.getRaw(writer); if (this.hasWitness()) {
raw = this.getRaw();
if (writer) {
writer.writeBytes(raw);
writer._witnessSize = raw._witnessSize;
return writer;
}
return raw;
}
return this.frameWitness(writer); return this.frameWitness(writer);
}; };
@ -116,25 +136,16 @@ Block.prototype.renderWitness = function renderWitness(writer) {
* @returns {Buffer} * @returns {Buffer}
*/ */
Block.prototype.getRaw = function getRaw(writer) { Block.prototype.getRaw = function getRaw() {
var raw; var raw;
if (this._raw) { if (this._raw) {
assert(this._size > 0); assert(this._size > 0);
assert(this._witnessSize >= 0); assert(this._witnessSize >= 0);
if (writer) {
writer.writeBytes(this._raw);
writer._witnessSize = this._raw._witnessSize;
return writer;
}
return this._raw; return this._raw;
} }
raw = this.frameWitness(); raw = this.frameWitness();
// if (this.hasWitness())
// raw = this.frameWitness();
// else
// raw = this.frameNormal();
if (!this.mutable) { if (!this.mutable) {
this._size = raw.length; this._size = raw.length;
@ -142,12 +153,6 @@ Block.prototype.getRaw = function getRaw(writer) {
this._raw = raw; this._raw = raw;
} }
if (writer) {
writer.writeBytes(raw);
writer._witnessSize = raw._witnessSize;
return writer;
}
return raw; return raw;
}; };
@ -679,13 +684,8 @@ Block.fromJSON = function fromJSON(json) {
* @returns {Buffer|String} * @returns {Buffer|String}
*/ */
Block.prototype.toRaw = function toRaw(enc) { Block.prototype.toRaw = function toRaw(writer) {
var data = this.render(); return this.render(writer);
if (enc === 'hex')
data = data.toString('hex');
return data;
}; };
/** /**

View File

@ -45,7 +45,10 @@ Coins.prototype.fromOptions = function fromOptions(options) {
this.hash = options.hash || null; this.hash = options.hash || null;
this.height = options.height != null ? options.height : -1; this.height = options.height != null ? options.height : -1;
this.coinbase = options.coinbase || false; this.coinbase = options.coinbase || false;
this.outputs = options.outputs || [];
if (options.outputs)
this.outputs = options.outputs;
return this; return this;
}; };
@ -210,7 +213,6 @@ Coins.prototype.fromRaw = function fromRaw(data, hash, index) {
this.height = height >>> 1; this.height = height >>> 1;
this.hash = hash; this.hash = hash;
this.coinbase = (height & 1) !== 0; this.coinbase = (height & 1) !== 0;
this.outputs = [];
if (this.height === 0x7fffffff) if (this.height === 0x7fffffff)
this.height = -1; this.height = -1;

View File

@ -64,10 +64,12 @@ CoinView.prototype.addTX = function addTX(tx) {
*/ */
CoinView.prototype.get = function get(hash, index) { CoinView.prototype.get = function get(hash, index) {
if (!this.coins[hash]) var coins = this.coins[hash];
if (!coins)
return; return;
return this.coins[hash].get(index); return coins.get(index);
}; };
/** /**
@ -78,10 +80,12 @@ CoinView.prototype.get = function get(hash, index) {
*/ */
CoinView.prototype.has = function has(hash, index) { CoinView.prototype.has = function has(hash, index) {
if (!this.coins[hash]) var coins = this.coins[hash];
if (!coins)
return false; return false;
return this.coins[hash].has(index); return coins.has(index);
}; };
/** /**
@ -92,10 +96,12 @@ CoinView.prototype.has = function has(hash, index) {
*/ */
CoinView.prototype.spend = function spend(hash, index) { CoinView.prototype.spend = function spend(hash, index) {
if (!this.coins[hash]) var coins = this.coins[hash];
if (!coins)
return; return;
return this.coins[hash].spend(index); return coins.spend(index);
}; };
/** /**
@ -105,7 +111,6 @@ CoinView.prototype.spend = function spend(hash, index) {
*/ */
CoinView.prototype.fillCoins = function fillCoins(tx) { CoinView.prototype.fillCoins = function fillCoins(tx) {
var res = true;
var i, input, prevout; var i, input, prevout;
for (i = 0; i < tx.inputs.length; i++) { for (i = 0; i < tx.inputs.length; i++) {
@ -113,10 +118,10 @@ CoinView.prototype.fillCoins = function fillCoins(tx) {
prevout = input.prevout; prevout = input.prevout;
input.coin = this.spend(prevout.hash, prevout.index); input.coin = this.spend(prevout.hash, prevout.index);
if (!input.coin) if (!input.coin)
res = false; return false;
} }
return res; return true;
}; };
/** /**

View File

@ -250,7 +250,7 @@ Mnemonic.isMnemonic = function isMnemonic(obj) {
function HD(options, network) { function HD(options, network) {
if (!options) if (!options)
return HD.fromMnemonic(null, network); return HD.fromMnemonic(null, network);
return HD.fromAny(options, network); return HD.from(options, network);
} }
/** /**
@ -332,7 +332,7 @@ HD.fromRaw = function fromRaw(data) {
* @returns {HDPrivateKey|HDPublicKey} * @returns {HDPrivateKey|HDPublicKey}
*/ */
HD.fromAny = function fromAny(options, network) { HD.from = function from(options, network) {
var xkey; var xkey;
assert(options, 'Options required.'); assert(options, 'Options required.');

View File

@ -64,7 +64,7 @@ Outpoint.prototype.fromJSON = function fromJSON(json) {
}; };
Outpoint.fromJSON = function fromJSON(json) { Outpoint.fromJSON = function fromJSON(json) {
return Outpoint().fromJSON(json); return new Outpoint().fromJSON(json);
}; };
Outpoint.prototype.fromTX = function fromTX(tx, i) { Outpoint.prototype.fromTX = function fromTX(tx, i) {
@ -74,7 +74,7 @@ Outpoint.prototype.fromTX = function fromTX(tx, i) {
}; };
Outpoint.fromTX = function fromTX(tx, i) { Outpoint.fromTX = function fromTX(tx, i) {
return Outpoint().fromTX(tx, i); return new Outpoint().fromTX(tx, i);
}; };
Outpoint.prototype.toJSON = function toJSON() { Outpoint.prototype.toJSON = function toJSON() {
@ -84,6 +84,10 @@ Outpoint.prototype.toJSON = function toJSON() {
}; };
}; };
Outpoint.prototype.inspect = function inspect() {
return '<Outpoint: ' + this.hash + '/' + this.index + '>';
};
/** /**
* Represents a transaction input. * Represents a transaction input.
* @exports Input * @exports Input
@ -373,8 +377,8 @@ Input.prototype.toJSON = function toJSON() {
return { return {
prevout: this.prevout.toJSON(), prevout: this.prevout.toJSON(),
coin: this.coin ? this.coin.toJSON() : null, coin: this.coin ? this.coin.toJSON() : null,
script: this.script.toRaw().toString('hex'), script: this.script.toJSON(),
witness: this.witness.toRaw().toString('hex'), witness: this.witness.toJSON(),
sequence: this.sequence sequence: this.sequence
}; };
}; };
@ -382,8 +386,8 @@ Input.prototype.toJSON = function toJSON() {
Input.prototype.fromJSON = function fromJSON(json) { Input.prototype.fromJSON = function fromJSON(json) {
this.prevout = Outpoint.fromJSON(json.prevout); this.prevout = Outpoint.fromJSON(json.prevout);
this.coin = json.coin ? bcoin.coin.fromJSON(json.coin) : null; this.coin = json.coin ? bcoin.coin.fromJSON(json.coin) : null;
this.script = bcoin.script.fromRaw(json.script, 'hex'); this.script = bcoin.script.fromJSON(json.script);
this.witness = bcoin.witness.fromRaw(json.witness, 'hex'); this.witness = bcoin.witness.fromJSON(json.witness);
this.sequence = json.sequence; this.sequence = json.sequence;
return this; return this;
}; };

View File

@ -161,7 +161,7 @@ Output.prototype.inspect = function inspect() {
Output.prototype.toJSON = function toJSON() { Output.prototype.toJSON = function toJSON() {
return { return {
value: utils.btc(this.value), value: utils.btc(this.value),
script: this.script.toRaw().toString('hex') script: this.script.toJSON()
}; };
}; };
@ -181,12 +181,15 @@ Output.prototype.getDustThreshold = function getDustThreshold(rate) {
if (this.script.isUnspendable()) if (this.script.isUnspendable())
return 0; return 0;
size = this.toRaw(new BufferWriter()).written; size = this.getSize() + 148;
size += 148;
return 3 * bcoin.tx.getMinFee(size, rate); return 3 * bcoin.tx.getMinFee(size, rate);
}; };
Output.prototype.getSize = function getSize() {
return this.toRaw(bcoin.writer()).written;
};
/** /**
* Test whether the output should be considered dust. * Test whether the output should be considered dust.
* @param {Rate?} rate * @param {Rate?} rate
@ -205,10 +208,9 @@ Output.prototype.isDust = function isDust(rate) {
*/ */
Output.prototype.fromJSON = function fromJSON(json) { Output.prototype.fromJSON = function fromJSON(json) {
return Output.fromOptions({ this.value = utils.satoshi(json.value);
value: utils.satoshi(json.value), this.script = bcoin.script.fromJSON(json.script);
script: bcoin.script.fromRaw(json.script, 'hex') return this;
});
}; };
/** /**

View File

@ -719,11 +719,9 @@ Peer.prototype._onPacket = function onPacket(packet) {
case 'filterclear': case 'filterclear':
return this._handleFilterClear(payload); return this._handleFilterClear(payload);
case 'block': case 'block':
// payload = new bcoin.memblock(payload);
this.fire(cmd, payload); this.fire(cmd, payload);
break; break;
case 'merkleblock': case 'merkleblock':
// payload = new bcoin.merkleblock(payload);
payload.verifyPartial(); payload.verifyPartial();
this.lastBlock = payload; this.lastBlock = payload;
this.waiting = payload.matches.length; this.waiting = payload.matches.length;
@ -731,7 +729,6 @@ Peer.prototype._onPacket = function onPacket(packet) {
this._flushMerkle(); this._flushMerkle();
break; break;
case 'tx': case 'tx':
// payload = new bcoin.tx(payload);
if (this.lastBlock) { if (this.lastBlock) {
if (this.lastBlock.hasTX(payload)) { if (this.lastBlock.hasTX(payload)) {
this.lastBlock.addTX(payload); this.lastBlock.addTX(payload);
@ -812,9 +809,6 @@ Peer.prototype._handleFilterClear = function _handleFilterClear(payload) {
}; };
Peer.prototype._handleUTXOs = function _handleUTXOs(payload) { Peer.prototype._handleUTXOs = function _handleUTXOs(payload) {
payload.coins = payload.coins(function(coin) {
return new bcoin.coin(coin);
});
bcoin.debug('Received %d utxos (%s).', bcoin.debug('Received %d utxos (%s).',
payload.coins.length, this.hostname); payload.coins.length, this.hostname);
this.fire('utxos', payload); this.fire('utxos', payload);
@ -1574,11 +1568,6 @@ Peer.prototype._handleHeaders = function _handleHeaders(headers) {
this.setMisbehavior(100); this.setMisbehavior(100);
return; return;
} }
headers = headers.map(function(header) {
return new bcoin.headers(header);
});
this.fire('headers', headers); this.fire('headers', headers);
}; };

View File

@ -427,7 +427,7 @@ Parser.parseUTXOs = function parseUTXOs(p) {
coin = bcoin.output.fromRaw(p); coin = bcoin.output.fromRaw(p);
coin.version = version; coin.version = version;
coin.height = height; coin.height = height;
coins.push(coin); coins.push(new bcoin.coin(coin));
} }
return { return {

View File

@ -276,6 +276,14 @@ Witness.prototype.toRaw = function toRaw(writer) {
return p; return p;
}; };
Witness.prototype.toJSON = function toJSON() {
return this.toRaw().toString('hex');
};
Witness.fromJSON = function fromJSON(json) {
return Witness.fromRaw(json, 'hex');
};
/** /**
* Unshift an item onto the witness vector. * Unshift an item onto the witness vector.
* @param {Number|String|Buffer|BN} data * @param {Number|String|Buffer|BN} data
@ -1169,6 +1177,14 @@ Script.prototype.toRaw = function toRaw(writer) {
return this.raw; return this.raw;
}; };
Script.prototype.toJSON = function toJSON() {
return this.toRaw().toString('hex');
};
Script.fromJSON = function fromJSON(json) {
return Script.fromRaw(json, 'hex');
};
/** /**
* Get the script's "subscript" starting at a separator. * Get the script's "subscript" starting at a separator.
* @param {Number?} lastSep - The last separator to sign/verify beyond. * @param {Number?} lastSep - The last separator to sign/verify beyond.

View File

@ -93,29 +93,17 @@ TX.prototype.fromOptions = function fromOptions(data) {
this.version = data.version; this.version = data.version;
this.flag = data.flag; this.flag = data.flag;
this.inputs = [];
this.outputs = [];
this.locktime = data.locktime; this.locktime = data.locktime;
this.ts = data.ts || 0; this.ts = data.ts || 0;
this.block = data.block || null; this.block = data.block || null;
this.index = data.index != null ? data.index : -1; this.index = data.index != null ? data.index : -1;
this.ps = this.ts === 0 ? (data.ps != null ? data.ps : utils.now()) : 0; this.ps = this.ts === 0 ? (data.ps != null ? data.ps : utils.now()) : 0;
this.height = data.height != null ? data.height : -1; this.height = data.height != null ? data.height : -1;
this.mutable = false;
this._hash = null;
this._whash = null;
this._raw = data._raw || null; this._raw = data._raw || null;
this._size = data._size || null; this._size = data._size || null;
this._witnessSize = data._witnessSize != null ? data._witnessSize : null; this._witnessSize = data._witnessSize != null ? data._witnessSize : null;
this._outputValue = null;
this._inputValue = null;
this._hashPrevouts = null;
this._hashSequence = null;
this._hashOutputs = null;
for (i = 0; i < data.inputs.length; i++) for (i = 0; i < data.inputs.length; i++)
this.inputs.push(new bcoin.input(data.inputs[i])); this.inputs.push(new bcoin.input(data.inputs[i]));
@ -1974,8 +1962,6 @@ TX.prototype.fromRaw = function fromRaw(data) {
inCount = p.readVarint(); inCount = p.readVarint();
this.inputs = [];
for (i = 0; i < inCount; i++) { for (i = 0; i < inCount; i++) {
input = bcoin.input.fromRaw(p); input = bcoin.input.fromRaw(p);
input.witness = new bcoin.witness(); input.witness = new bcoin.witness();
@ -1984,8 +1970,6 @@ TX.prototype.fromRaw = function fromRaw(data) {
outCount = p.readVarint(); outCount = p.readVarint();
this.outputs = [];
for (i = 0; i < outCount; i++) for (i = 0; i < outCount; i++)
this.outputs.push(bcoin.output.fromRaw(p)); this.outputs.push(bcoin.output.fromRaw(p));
@ -2022,15 +2006,11 @@ TX.prototype.fromWitness = function fromWitness(data) {
inCount = p.readVarint(); inCount = p.readVarint();
this.inputs = [];
for (i = 0; i < inCount; i++) for (i = 0; i < inCount; i++)
this.inputs.push(bcoin.input.fromRaw(p)); this.inputs.push(bcoin.input.fromRaw(p));
outCount = p.readVarint(); outCount = p.readVarint();
this.outputs = [];
for (i = 0; i < outCount; i++) for (i = 0; i < outCount; i++)
this.outputs.push(bcoin.output.fromRaw(p)); this.outputs.push(bcoin.output.fromRaw(p));

View File

@ -61,7 +61,7 @@ function Wallet(options) {
master = bcoin.hd.fromMnemonic(null, this.network); master = bcoin.hd.fromMnemonic(null, this.network);
if (!bcoin.hd.isHD(master) && !MasterKey.isMasterKey(master)) if (!bcoin.hd.isHD(master) && !MasterKey.isMasterKey(master))
master = bcoin.hd.fromAny(master, this.network); master = bcoin.hd.from(master, this.network);
if (!MasterKey.isMasterKey(master)) if (!MasterKey.isMasterKey(master))
master = MasterKey.fromKey(master); master = MasterKey.fromKey(master);