From 90d5e57e7aebb85ee8fd668b06b9f879178cad3d Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jul 2015 19:36:49 -0300 Subject: [PATCH 1/2] add firstSeenTs for sorting --- app/controllers/addresses.js | 12 ++++++++---- app/models/Address.js | 4 ++-- lib/TransactionDb.js | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index 78c514b..3bc451e 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -143,9 +143,8 @@ exports.multitxs = function(req, res, next) { if (to > nbTxs) to = nbTxs; txs.sort(function(a, b) { - return (b.ts || b.ts) - (a.ts || a.ts); + return (b.firstSeenTs || b.ts) - (a.firstSeenTs || a.ts); }); - txs = txs.slice(from, to); var txIndex = {}; @@ -153,13 +152,17 @@ exports.multitxs = function(req, res, next) { txIndex[tx.txid] = tx; }); - async.eachLimit(txs, RPC_CONCURRENCY, function(tx, callback) { - tDb.fromIdWithInfo(tx.txid, function(err, tx) { + async.eachLimit(txs, RPC_CONCURRENCY, function(tx2, callback) { + tDb.fromIdWithInfo(tx2.txid, function(err, tx) { if (err) { console.log(err); return common.handleErrors(err, res); } if (tx && tx.info) { + + if (tx2.firstSeenTs) + tx.info.firstSeenTs = tx2.firstSeenTs; + txIndex[tx.txid].info = tx.info; } else nbTxs--; @@ -192,6 +195,7 @@ exports.multitxs = function(req, res, next) { async.eachLimit(as, RPC_CONCURRENCY, function(a, callback) { a.update(function(err) { if (err) callback(err); + txs.push(a.transactions); callback(); }, { diff --git a/app/models/Address.js b/app/models/Address.js index cbdd322..f34c3e1 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -115,7 +115,8 @@ Address.prototype._addTxItem = function(txItem, txList, includeInfo) { addTx({ txid: txItem.txid, - ts: txItem.ts + ts: txItem.ts, + firstSeenTs: txItem.firstSeenTs, }); } @@ -202,7 +203,6 @@ Address.prototype.update = function(next, opts) { }); if (txList) self.transactions = txList; - return next(); } }); diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index a3e9580..aa791b8 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -197,7 +197,6 @@ TransactionDb.prototype._fillOutpoints = function(txInfo, cb) { return c_in(); // error not scalated } - txInfo.firstSeenTs = ret.ts; i.addr = ret.addr; i.valueSat = ret.valueSat; i.value = ret.valueSat / util.COIN; @@ -467,8 +466,11 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) { var k = data.key.split('-'); var index = k[3] + k[4]; if (!unique[index]) { - unique[index] = 1; - ret.push(self._parseAddrData(k, data, opts.ignoreCache)); + unique[index] = self._parseAddrData(k, data, opts.ignoreCache); + ret.push(unique[index]); + } else { + // add first seen + unique[index].firstSeenTs = END_OF_WORLD_TS - parseInt(k[2]); } }) .on('error', cb) From b452f76250173a410f8a5cad041cd85736a4aef9 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jul 2015 20:33:23 -0300 Subject: [PATCH 2/2] filter out utxo without scriptpubkey --- app/models/Address.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/Address.js b/app/models/Address.js index f34c3e1..780daf7 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -1,6 +1,7 @@ 'use strict'; var imports = require('soop').imports(); +var _ = require('lodash'); var async = require('async'); var bitcore = require('bitcore'); var BitcoreAddress = bitcore.Address; @@ -183,7 +184,9 @@ Address.prototype.update = function(next, opts) { return !x.spentTxId; }); tDb.fillScriptPubKey(txOut, function() { - self.unspent = txOut.map(function(x) { + //_.filter will filterout unspend without scriptPubkey + //(probably from double spends) + self.unspent = _.filter(txOut.map(function(x) { return { address: self.addrStr, txid: x.txid, @@ -194,7 +197,7 @@ Address.prototype.update = function(next, opts) { confirmations: x.isConfirmedCached ? (config.safeConfirmations) : x.confirmations, confirmationsFromCache: !!x.isConfirmedCached, }; - }); + }), 'scriptPubKey');; return next(); }); } else {