diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index 4d5b984a..03ae89aa 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -53,7 +53,7 @@ exports.show = function(req, res, next) { } else { return res.jsonp(a.getObj()); } - }, req.query.noTxList); + }, {noTxList: req.query.noTxList}); } }; diff --git a/app/models/Address.js b/app/models/Address.js index 1e5e8b31..75f975c0 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -92,6 +92,94 @@ Address.prototype.getObj = function() { }; }; +Address.prototype._addTxItem = function(txItem, txList) { + var add=0, addSpend=0; + var v = txItem.value_sat; + var seen = this.seen; + + // Founding tx + if ( !seen[txItem.txid] ) { + seen[txItem.txid]=1; + add=1; + + if (txList) + txList.push({txid: txItem.txid, ts: txItem.ts}); + } + + // Spent tx + if (txItem.spentTxId && !seen[txItem.spentTxId] ) { + if (txList) { + txList.push({txid: txItem.spentTxId, ts: txItem.spentTs}); + } + seen[txItem.spentTxId]=1; + addSpend=1; + } + if (txItem.isConfirmed) { + this.txApperances += add; + this.totalReceivedSat += v; + if (! txItem.spentTxId ) { + //unspent + this.balanceSat += v; + } + else if(!txItem.spentIsConfirmed) { + // unspent + this.balanceSat += v; + this.unconfirmedBalanceSat -= v; + this.unconfirmedTxApperances += addSpend; + } + else { + // spent + this.totalSentSat += v; + this.txApperances += addSpend; + } + } + else { + this.unconfirmedBalanceSat += v; + this.unconfirmedTxApperances += add; + } +}; + +Address.prototype._setTxs = function(txs) { + + // sort input and outputs togheter + txs.sort( + function compare(a,b) { + if (a.ts < b.ts) return 1; + if (a.ts > b.ts) return -1; + return 0; + }); + + this.transactions = txs.map(function(i) { return i.txid; } ); +}; + +Address.prototype.update = function(next, opts) { + var self = this; + if (!self.addrStr) return next(); + opts = opts || {}; + + var txList = opts.noTxList ? null : []; + var tDb = TransactionDb; + var bDb = BlockDb; + tDb.fromAddr(self.addrStr, function(err,txOut){ + if (err) return next(err); + + bDb.fillConfirmations(txOut, function(err) { + if (err) return next(err); + tDb.cacheConfirmations(txOut, function(err) { + if (err) return next(err); + + txOut.forEach(function(txItem){ + self._addTxItem(txItem, txList); + }); + + if (txList) + self._setTxs(txList); + return next(); + }); + }); + }); +}; + Address.prototype.getUtxo = function(next) { var self = this; var tDb = TransactionDb; @@ -124,94 +212,5 @@ Address.prototype.getUtxo = function(next) { }); }; - -Address.prototype._addTxItem = function(txItem, notxlist) { - var add=0, addSpend=0; - var v = txItem.value_sat; - var seen = this.seen; - var txs = []; - - if ( !seen[txItem.txid] ) { - if (!notxlist) { - txs.push({txid: txItem.txid, ts: txItem.ts}); - } - seen[txItem.txid]=1; - add=1; - } - - if (txItem.spentTxId && !seen[txItem.spentTxId] ) { - if (!notxlist) { - txs.push({txid: txItem.spentTxId, ts: txItem.spentTs}); - } - seen[txItem.spentTxId]=1; - addSpend=1; - } - if (txItem.isConfirmed) { - this.txApperances += add; - this.totalReceivedSat += v; - if (! txItem.spentTxId ) { - //unspent - this.balanceSat += v; - } - else if(!txItem.spentIsConfirmed) { - // unspent - this.balanceSat += v; - this.unconfirmedBalanceSat -= v; - this.unconfirmedTxApperances += addSpend; - } - else { - // spent - this.totalSentSat += v; - this.txApperances += addSpend; - } - } - else { - this.unconfirmedBalanceSat += v; - this.unconfirmedTxApperances += add; - } - - return txs; -}; - -Address.prototype._setTxs = function(txs) { - - // sort input and outputs togheter - txs.sort( - function compare(a,b) { - if (a.ts < b.ts) return 1; - if (a.ts > b.ts) return -1; - return 0; - }); - - this.transactions = txs.map(function(i) { return i.txid; } ); -}; - -Address.prototype.update = function(next, notxlist) { - var self = this; - if (!self.addrStr) return next(); - - var txs = []; - var tDb = TransactionDb; - var bDb = BlockDb; - tDb.fromAddr(self.addrStr, function(err,txOut){ - if (err) return next(err); - - bDb.fillConfirmations(txOut, function(err) { - if (err) return next(err); - tDb.cacheConfirmations(txOut, function(err) { - if (err) return next(err); - - txOut.forEach(function(txItem){ - txs=txs.concat(self._addTxItem(txItem, notxlist)); - }); - - if (!notxlist) - self._setTxs(txs); - return next(); - }); - }); - }); -}; - module.exports = require('soop')(Address); diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 80562705..a172c0e8 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -366,8 +366,8 @@ BlockDb.prototype.fillConfirmations = function(txouts, cb) { var self = this; this.getTip(function(err, hash, height){ var txs = txouts.filter(function(x){ - return !x.spentIsConfirmedCached // not 100%cached - && !(x.isConfirmedCached && !x.spentTxId); // and not 50%cached but not spent + return !x.spentIsConfirmedCached // not 100%cached + && !(x.isConfirmedCached && !x.spentTxId); // and not partial cached but not spent }); //console.log('[BlockDb.js.373:txs:]',txs.length, txs.slice(0,5)); //TODO diff --git a/test/integration/addr.js b/test/integration/addr.js index 8b527357..d18cf2fe 100644 --- a/test/integration/addr.js +++ b/test/integration/addr.js @@ -68,7 +68,7 @@ describe('Address balances', function() { if (v.totalSent) assert.equal(v.totalSent, a.totalSent, 'send: ' + a.totalSent); if (v.balance) assert.equal(v.balance, a.balance, 'balance: ' + a.balance); done(); - },1); + },{noTxList:1}); }); } }); diff --git a/test/integration/addrCache.js b/test/integration/addrCache.js index 556775e2..2e15d6bb 100644 --- a/test/integration/addrCache.js +++ b/test/integration/addrCache.js @@ -91,7 +91,7 @@ describe('Address cache ', function() { a.totalReceived.should.equal(1376000, 'totalReceived'); a.txApperances.should.equal(8003, 'txApperances'); return done(); - },1); + },{noTxList:1}); }); });