more tx-pool consistency.

This commit is contained in:
Christopher Jeffrey 2016-02-07 07:18:19 -08:00
parent 7fbfc1e248
commit 58a859267c
3 changed files with 140 additions and 92 deletions

View File

@ -85,8 +85,28 @@ Address.prototype.__defineGetter__('balance', function() {
return this.getBalance();
});
Address.prototype.getAll = function getAll() {
return this._wallet.getAll(this);
};
Address.prototype.getUnspent = function getUnspent() {
return this._wallet.getUnspent(this);
};
Address.prototype.getPending = function getPending() {
return this._wallet.getPending(this);
};
Address.prototype.getSent = function getSent() {
return this._wallet.getSent(this);
};
Address.prototype.getReceived = function getReceived() {
return this._wallet.getReceived(this);
};
Address.prototype.getBalance = function getBalance() {
return this._wallet.tx.getAddressBalance(this.getAddress());
return this._wallet.getBalance(this);
};
Address.prototype.setRedeem = function setRedeem(redeem) {
@ -524,6 +544,17 @@ Address.prototype.__defineGetter__('address', function() {
return this.getAddress();
});
Address.prototype.toAddress = function toAddress() {
return {
address: this.getAddress(),
hash160: utils.toHex(this.getHash()),
received: this.getReceived(),
sent: this.getSent(),
balance: this.getBalance(),
txs: this.getAll()
};
};
Address.prototype.toJSON = function toJSON(encrypt) {
return {
v: 1,

View File

@ -217,21 +217,6 @@ TXPool.prototype.getCoin = function getCoin(hash, index) {
return this._unspent[hash + '/' + index];
};
TXPool.prototype.getOutput = function getOutput(hash, index) {
var id = hash + '/' + index;
var tx;
if (this._unspent[id])
return this._unspent[id];
tx = this._all[hash];
if (!tx)
return;
return bcoin.coin(tx, index);
};
TXPool.prototype._storeTX = function _storeTX(hash, tx, noWrite) {
var self = this;
@ -265,18 +250,6 @@ TXPool.prototype._removeTX = function _removeTX(tx, noWrite) {
});
};
TXPool.prototype.getAll = function getAll(address) {
if (!address)
address = this._wallet;
return Object.keys(this._all).map(function(key) {
return this._all[key];
}, this).filter(function(tx) {
return address.ownOutput(tx)
|| address.ownInput(tx);
});
};
TXPool.prototype._addOutput = function _addOutput(tx, i, remove) {
var output, address;
@ -364,33 +337,76 @@ TXPool.prototype._removeInput = function _removeInput(tx, i) {
return this._addInput(tx, i, true);
};
TXPool.prototype.getAddressBalance = function getAddressBalance(address) {
if (this._addresses[address])
return this._addresses[address].balance.clone();
return new bn(0);
};
TXPool.prototype.getUnspent = function getUnspent(address) {
if (!address)
address = this._wallet;
return Object.keys(this._unspent).map(function(key) {
return this._unspent[key];
}, this).filter(function(item) {
return address.ownOutput(item);
});
};
TXPool.prototype.getPending = function getPending() {
TXPool.prototype.getAll = function getAll(address) {
return Object.keys(this._all).map(function(key) {
return this._all[key];
}, this).filter(function(tx) {
if (address) {
if (!address.ownOutput(tx) && !address.ownInput(tx))
return false;
}
return true;
});
};
TXPool.prototype.getUnspent = function getUnspent(address) {
return Object.keys(this._unspent).map(function(key) {
return this._unspent[key];
}, this).filter(function(item) {
if (address) {
if (!address.ownOutput(item))
return false;
}
return true;
});
};
TXPool.prototype.getPending = function getPending(address) {
return Object.keys(this._all).map(function(key) {
return this._all[key];
}, this).filter(function(tx) {
if (address) {
if (!address.ownInput(tx) && !address.ownOutput(tx))
return false;
}
return tx.ts === 0;
});
};
TXPool.prototype.getSent = function getSent(address) {
if (address) {
if (typeof address !== 'string')
address = address.getAddress();
if (this._addresses[address])
return this._addresses[address].sent.clone();
return new bn(0);
}
return this._sent.clone();
};
TXPool.prototype.getReceived = function getReceived(address) {
if (address) {
if (typeof address !== 'string')
address = address.getAddress();
if (this._addresses[address])
return this._addresses[address].received.clone();
return new bn(0);
}
return this._sent.clone();
};
TXPool.prototype.getBalance = function getBalance(address) {
if (address) {
if (typeof address !== 'string')
address = address.getAddress();
if (this._addresses[address])
return this._addresses[address].balance.clone();
return new bn(0);
}
return this._balance.clone();
};
TXPool.prototype.getBalanceUnspent = function getBalanceUnspent(address) {
var acc = new bn(0);
var unspent = this.getUnspent(address);
if (unspent.length === 0)
@ -401,12 +417,6 @@ TXPool.prototype.getBalance = function getBalance(address) {
}, acc);
};
TXPool.prototype.getBalance = function getBalance(address) {
if (address)
return this.getAddressBalance(address);
return this._balance.clone();
};
// Legacy
TXPool.prototype.all = TXPool.prototype.getAll;
TXPool.prototype.unspent = TXPool.prototype.getUnspent;

View File

@ -937,20 +937,58 @@ Wallet.prototype.addTX = function addTX(tx, block) {
return this.tx.add(tx);
};
Wallet.prototype.getAll = function getAll() {
return this.tx.getAll();
Wallet.prototype.getAll = function getAll(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return [];
}
return this.tx.getAll(address);
};
Wallet.prototype.getUnspent = function getUnspent() {
return this.tx.getUnspent();
Wallet.prototype.getUnspent = function getUnspent(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return [];
}
return this.tx.getUnspent(address);
};
Wallet.prototype.getPending = function getPending() {
return this.tx.getPending();
Wallet.prototype.getPending = function getPending(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return [];
}
return this.tx.getPending(address);
};
Wallet.prototype.getBalance = function getBalance() {
return this.tx.getBalance();
Wallet.prototype.getSent = function getSent(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return new bn(0);
}
return this.tx.getSent(address);
};
Wallet.prototype.getReceived = function getReceived(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return new bn(0);
}
return this.tx.getReceived(address);
};
Wallet.prototype.getBalance = function getBalance(address) {
if (typeof address === 'string') {
address = this.findAddress(address);
if (!address)
return new bn(0);
}
return this.tx.getBalance(address);
};
// Legacy
@ -959,37 +997,6 @@ Wallet.prototype.unspent = Wallet.prototype.getUnspent;
Wallet.prototype.pending = Wallet.prototype.getPending;
Wallet.prototype.balance = Wallet.prototype.getBalance;
Wallet.prototype.toAddress = function toAddress() {
var self = this;
var received = new bn(0);
var sent = new bn(0);
var txs = Object.keys(this.tx._all).reduce(function(out, hash) {
out.push(self.tx._all[hash]);
return out;
}, []);
txs.forEach(function(tx) {
tx.inputs.forEach(function(input, i) {
if (self.ownInput(tx, i))
sent.iadd(input.value);
});
tx.outputs.forEach(function(output, i) {
if (self.ownOutput(tx, i))
received.iadd(output.value);
});
});
return {
address: this.getAddress(),
hash: utils.toHex(this.getHash()),
received: received,
sent: sent,
balance: this.getBalance(),
txs: txs
};
};
Wallet.prototype.__defineGetter__('script', function() {
return this.getScript();
});