refactor tx instantiation.

This commit is contained in:
Christopher Jeffrey 2016-02-06 17:12:16 -08:00
parent 769b50c999
commit 7bb20437bc
3 changed files with 26 additions and 44 deletions

View File

@ -7,6 +7,7 @@
var bcoin = require('../bcoin');
var bn = require('bn.js');
var utils = bcoin.utils;
var assert = utils.assert;
var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network;
@ -67,13 +68,9 @@ function Block(data, subtype) {
}
if (this.subtype === 'block') {
this.txs = this.txs.map(function(tx) {
tx.network = self.network;
tx.relayedBy = self.relayedBy;
tx = bcoin.tx(tx);
tx.block = self.hash('hex');
tx.ts = tx.ts || self.ts;
return tx;
this.txs = this.txs.map(function(data) {
assert(!(data instanceof bcoin.tx));
return bcoin.tx(data, self);
});
}
@ -136,6 +133,7 @@ Block.prototype.getSize = function getSize() {
Block.prototype.size = Block.prototype.getSize;
Block.prototype.hasTX = function hasTX(hash) {
assert(this.subtype === 'merkleblock');
return this.tx.indexOf(hash) !== -1;
};
@ -328,7 +326,7 @@ Block.prototype.verifyContext = function verifyContext() {
}
// Ensure the miner's target is equal to what we expect
if (this.bits !== this.chain.target(prev, this)) {
if (this.bits !== this.chain.getTarget(prev, this)) {
utils.debug('Block is using wrong target: %s', this.rhash);
return false;
}
@ -659,8 +657,8 @@ Block.prototype.toJSON = function toJSON() {
Block.fromJSON = function fromJSON(json) {
var raw, parser, data, block;
utils.assert.equal(json.v, 1);
utils.assert.equal(json.type, 'block');
assert.equal(json.v, 1);
assert.equal(json.type, 'block');
raw = utils.toArray(json.block, 'hex');

View File

@ -57,8 +57,15 @@ function TX(data, block) {
}, this);
}
if (block && block.subtype === 'merkleblock') {
if (!data.ts && block && block.hasTX(this.hash('hex'))) {
if (block && !data.ts) {
this.network = true;
this.relayedBy = block.relayedBy;
if (block.subtype === 'merkleblock') {
if (block.hasTX(this.hash('hex'))) {
this.ts = block.ts;
this.block = block.hash('hex');
}
} else {
this.ts = block.ts;
this.block = block.hash('hex');
}

View File

@ -794,13 +794,11 @@ Wallet.prototype.setChangeDepth = function setChangeDepth(depth) {
return true;
};
Wallet.prototype.getKeyHash =
Wallet.prototype.getKeyhash = function getKeyhash() {
Wallet.prototype.getKeyHash = function getKeyHash() {
return this.currentAddress.getKeyHash();
};
Wallet.prototype.getKeyAddress =
Wallet.prototype.getKeyaddress = function getKeyaddress() {
Wallet.prototype.getKeyAddress = function getKeyAddress() {
return this.currentAddress.getKeyAddress();
};
@ -813,39 +811,18 @@ Wallet.prototype.getAddress = function getAddress() {
};
Wallet.prototype.ownInput = function ownInput(tx, index) {
if (tx instanceof bcoin.input) {
var input = tx;
var scriptAddress = input.getScriptAddress();
if (this._addressTable[scriptAddress] != null)
return true;
var addresses = input.getAddresses();
var address;
for (var i = 0; i < addresses.length; i++) {
address = addresses[i];
if (this._addressTable[address] != null)
return true;
}
return false;
}
if (tx instanceof bcoin.input)
return tx.test(this._addressTable) ? [tx] : false;
this.fillPrevout(tx);
return tx.testInputs(this._addressTable, index, true);
};
Wallet.prototype.ownOutput = function ownOutput(tx, index) {
if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) {
var output = tx;
var scriptAddress = output.getScriptAddress();
if (this._addressTable[scriptAddress] != null)
return true;
var addresses = output.getAddresses();
var address;
for (var i = 0; i < addresses.length; i++) {
address = addresses[i];
if (this._addressTable[address] != null)
return true;
}
return false;
}
if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin))
return tx.test(this._addressTable) ? [tx] : false;
return tx.testOutputs(this._addressTable, index, true);
};