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

View File

@ -57,8 +57,15 @@ function TX(data, block) {
}, this); }, this);
} }
if (block && block.subtype === 'merkleblock') { if (block && !data.ts) {
if (!data.ts && block && block.hasTX(this.hash('hex'))) { 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.ts = block.ts;
this.block = block.hash('hex'); this.block = block.hash('hex');
} }

View File

@ -794,13 +794,11 @@ Wallet.prototype.setChangeDepth = function setChangeDepth(depth) {
return true; return true;
}; };
Wallet.prototype.getKeyHash = Wallet.prototype.getKeyHash = function getKeyHash() {
Wallet.prototype.getKeyhash = function getKeyhash() {
return this.currentAddress.getKeyHash(); return this.currentAddress.getKeyHash();
}; };
Wallet.prototype.getKeyAddress = Wallet.prototype.getKeyAddress = function getKeyAddress() {
Wallet.prototype.getKeyaddress = function getKeyaddress() {
return this.currentAddress.getKeyAddress(); return this.currentAddress.getKeyAddress();
}; };
@ -813,39 +811,18 @@ Wallet.prototype.getAddress = function getAddress() {
}; };
Wallet.prototype.ownInput = function ownInput(tx, index) { Wallet.prototype.ownInput = function ownInput(tx, index) {
if (tx instanceof bcoin.input) { if (tx instanceof bcoin.input)
var input = tx; return tx.test(this._addressTable) ? [tx] : false;
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;
}
this.fillPrevout(tx); this.fillPrevout(tx);
return tx.testInputs(this._addressTable, index, true); return tx.testInputs(this._addressTable, index, true);
}; };
Wallet.prototype.ownOutput = function ownOutput(tx, index) { Wallet.prototype.ownOutput = function ownOutput(tx, index) {
if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) { if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin))
var output = tx; return tx.test(this._addressTable) ? [tx] : false;
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;
}
return tx.testOutputs(this._addressTable, index, true); return tx.testOutputs(this._addressTable, index, true);
}; };