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

View File

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

View File

@ -64,10 +64,12 @@ CoinView.prototype.addTX = function addTX(tx) {
*/
CoinView.prototype.get = function get(hash, index) {
if (!this.coins[hash])
var coins = this.coins[hash];
if (!coins)
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) {
if (!this.coins[hash])
var coins = this.coins[hash];
if (!coins)
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) {
if (!this.coins[hash])
var coins = this.coins[hash];
if (!coins)
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) {
var res = true;
var i, input, prevout;
for (i = 0; i < tx.inputs.length; i++) {
@ -113,10 +118,10 @@ CoinView.prototype.fillCoins = function fillCoins(tx) {
prevout = input.prevout;
input.coin = this.spend(prevout.hash, prevout.index);
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) {
if (!options)
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}
*/
HD.fromAny = function fromAny(options, network) {
HD.from = function from(options, network) {
var xkey;
assert(options, 'Options required.');

View File

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

View File

@ -161,7 +161,7 @@ Output.prototype.inspect = function inspect() {
Output.prototype.toJSON = function toJSON() {
return {
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())
return 0;
size = this.toRaw(new BufferWriter()).written;
size += 148;
size = this.getSize() + 148;
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.
* @param {Rate?} rate
@ -205,10 +208,9 @@ Output.prototype.isDust = function isDust(rate) {
*/
Output.prototype.fromJSON = function fromJSON(json) {
return Output.fromOptions({
value: utils.satoshi(json.value),
script: bcoin.script.fromRaw(json.script, 'hex')
});
this.value = utils.satoshi(json.value);
this.script = bcoin.script.fromJSON(json.script);
return this;
};
/**

View File

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

View File

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

View File

@ -276,6 +276,14 @@ Witness.prototype.toRaw = function toRaw(writer) {
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.
* @param {Number|String|Buffer|BN} data
@ -1169,6 +1177,14 @@ Script.prototype.toRaw = function toRaw(writer) {
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.
* @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.flag = data.flag;
this.inputs = [];
this.outputs = [];
this.locktime = data.locktime;
this.ts = data.ts || 0;
this.block = data.block || null;
this.index = data.index != null ? data.index : -1;
this.ps = this.ts === 0 ? (data.ps != null ? data.ps : utils.now()) : 0;
this.height = data.height != null ? data.height : -1;
this.mutable = false;
this._hash = null;
this._whash = null;
this._raw = data._raw || null;
this._size = data._size || 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++)
this.inputs.push(new bcoin.input(data.inputs[i]));
@ -1974,8 +1962,6 @@ TX.prototype.fromRaw = function fromRaw(data) {
inCount = p.readVarint();
this.inputs = [];
for (i = 0; i < inCount; i++) {
input = bcoin.input.fromRaw(p);
input.witness = new bcoin.witness();
@ -1984,8 +1970,6 @@ TX.prototype.fromRaw = function fromRaw(data) {
outCount = p.readVarint();
this.outputs = [];
for (i = 0; i < outCount; i++)
this.outputs.push(bcoin.output.fromRaw(p));
@ -2022,15 +2006,11 @@ TX.prototype.fromWitness = function fromWitness(data) {
inCount = p.readVarint();
this.inputs = [];
for (i = 0; i < inCount; i++)
this.inputs.push(bcoin.input.fromRaw(p));
outCount = p.readVarint();
this.outputs = [];
for (i = 0; i < outCount; i++)
this.outputs.push(bcoin.output.fromRaw(p));

View File

@ -61,7 +61,7 @@ function Wallet(options) {
master = bcoin.hd.fromMnemonic(null, this.network);
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))
master = MasterKey.fromKey(master);