optimize tx.

This commit is contained in:
Christopher Jeffrey 2016-02-24 17:42:47 -08:00
parent 08ccb4c6ee
commit 15a3f4b28b
2 changed files with 26 additions and 51 deletions

View File

@ -62,10 +62,6 @@ function MTX(options) {
utils.inherits(MTX, bcoin.tx); utils.inherits(MTX, bcoin.tx);
MTX.prototype._init = function _init() {
;
};
MTX.prototype.clone = function clone() { MTX.prototype.clone = function clone() {
var tx = new MTX(this); var tx = new MTX(this);

View File

@ -40,24 +40,17 @@ function TX(data, block) {
this._chain = data.chain; this._chain = data.chain;
if (data.inputs) { // assert(data.inputs.length !== 0);
assert(this.inputs.length === 0); // assert(data.outputs.length !== 0);
data.inputs.forEach(function(input) {
this.addInput(input);
}, this);
}
if (data.outputs) { data.inputs.forEach(function(input) {
assert(this.outputs.length === 0); this.inputs.push(new bcoin.input(input));
data.outputs.forEach(function(output) { }, this);
this.addOutput(output);
}, this);
}
this._init(block); data.outputs.forEach(function(output) {
} this.outputs.push(new bcoin.output(output));
}, this);
TX.prototype._init = function _init(block) {
if (block && this.ts === 0) { if (block && this.ts === 0) {
if (block.type === 'merkleblock') { if (block.type === 'merkleblock') {
if (block.hasTX(this.hash('hex'))) if (block.hasTX(this.hash('hex')))
@ -72,7 +65,7 @@ TX.prototype._init = function _init(block) {
if (!this._size) if (!this._size)
this._size = this._raw.length; this._size = this._raw.length;
}; }
TX.prototype.setBlock = function setBlock(block) { TX.prototype.setBlock = function setBlock(block) {
this.relayedBy = block.relayedBy; this.relayedBy = block.relayedBy;
@ -84,15 +77,21 @@ TX.prototype.setBlock = function setBlock(block) {
TX.prototype.clone = function clone() { TX.prototype.clone = function clone() {
var tx = new TX(this); var tx = new TX(this);
tx.inputs = tx.inputs.map(function(input) { // tx.inputs = tx.inputs.map(function(input) {
input.script = input.script.slice(); // var _raw = input.script._raw;
return input; // input.script = input.script.slice();
}); // if (_raw)
// utils.hidden(input.script, '_raw', _raw);
// return input;
// });
tx.outputs = tx.outputs.map(function(output) { // tx.outputs = tx.outputs.map(function(output) {
output.script = output.script.slice(); // var _raw = output.script._raw;
return output; // output.script = output.script.slice();
}); // if (_raw)
// utils.hidden(output.script, '_raw', _raw);
// return output;
// });
delete tx._raw; delete tx._raw;
delete tx._size; delete tx._size;
@ -101,16 +100,10 @@ TX.prototype.clone = function clone() {
}; };
TX.prototype.hash = function hash(enc) { TX.prototype.hash = function hash(enc) {
var hash; if (!this._hash)
this._hash = utils.dsha256(this.render());
if (this._hash) return enc === 'hex' ? utils.toHex(this._hash) : this._hash;
return enc === 'hex' ? utils.toHex(this._hash) : this._hash;
hash = utils.dsha256(this._raw);
this._hash = hash;
return enc === 'hex' ? utils.toHex(hash) : hash;
}; };
TX.prototype.render = function render() { TX.prototype.render = function render() {
@ -123,14 +116,6 @@ TX.prototype.getSize = function getSize() {
return this._size || this.render().length; return this._size || this.render().length;
}; };
TX.prototype.addInput = function addInput(input) {
assert(input.prevout);
input = bcoin.input(input);
this.inputs.push(input);
};
TX.prototype._inputIndex = function _inputIndex(hash, index) { TX.prototype._inputIndex = function _inputIndex(hash, index) {
var i, ex; var i, ex;
@ -143,12 +128,6 @@ TX.prototype._inputIndex = function _inputIndex(hash, index) {
return -1; return -1;
}; };
TX.prototype.addOutput = function addOutput(output) {
output = bcoin.output(output);
this.outputs.push(output);
};
TX.prototype.getSubscript = function getSubscript(index) { TX.prototype.getSubscript = function getSubscript(index) {
var script; var script;