From 58a859267cb2baf48daef8c363488f6c9cca6afc Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 7 Feb 2016 07:18:19 -0800 Subject: [PATCH] more tx-pool consistency. --- lib/bcoin/address.js | 33 ++++++++++++- lib/bcoin/tx-pool.js | 114 +++++++++++++++++++++++-------------------- lib/bcoin/wallet.js | 85 +++++++++++++++++--------------- 3 files changed, 140 insertions(+), 92 deletions(-) diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index be8e897e..ae122235 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -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, diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 91ac5ebb..69ca7f2a 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -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; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index bff268b0..fad140d5 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -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(); });