From 90ea2e24d11d71a4d50f2c31106528c5dc324359 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 9 May 2014 14:03:00 +0400 Subject: [PATCH] wallet: .pending() --- lib/bcoin/tx-pool.js | 35 +++++++++++++++++++++++++---------- lib/bcoin/wallet.js | 4 ++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 65499d29..1bcd76da 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -49,8 +49,14 @@ TXPool.prototype.add = function add(tx, noWrite) { var hash = tx.hash('hex'); // Do not add TX two times - if (this._all[hash]) + if (this._all[hash]) { + // Transaction was confirmed, update it in storage + if (tx.ts !== 0 && this._all[hash].ts === 0) { + this._all[hash].ts = tx.ts; + this._storeTX(hash, tx); + } return false; + } this._all[hash] = tx; var own = this._wallet.own(tx); @@ -103,25 +109,34 @@ TXPool.prototype.add = function add(tx, noWrite) { if (updated) this.emit('update', this._lastTs); - if (!noWrite && this._storage) { - var self = this; - this._storage.put(this._prefix + hash, tx.toJSON(), function(err) { - if (err) - self.emit('error', err); - }); - } + if (!noWrite && this._storage) + this._storeTX(hash, tx); return true; }; +TXPool.prototype._storeTX = function _storeTX(hash, tx) { + var self = this; + this._storage.put(this._prefix + hash, tx.toJSON(), function(err) { + if (err) + self.emit('error', err); + }); +}; + TXPool.prototype.unspent = function unspent() { return Object.keys(this._unspent).map(function(key) { return this._unspent[key]; - }, this).filter(function(item) { - return this._wallet.own(item.tx, item.index); }, this); }; +TXPool.prototype.pending = function pending() { + return Object.keys(this._all).map(function(key) { + return this._all[key]; + }, this).filter(function(tx) { + return tx.ts === 0; + }); +}; + TXPool.prototype.balance = function balance() { var acc = new bn(0); var unspent = this.unspent(); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index efc4830a..5c1d389d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -199,6 +199,10 @@ Wallet.prototype.unspent = function unspent() { return this.tx.unspent(); }; +Wallet.prototype.pending = function pending() { + return this.tx.pending(); +}; + Wallet.prototype.balance = function balance() { return this.tx.balance(); };