Merge pull request #339 from matiu/feat/firstSeenTs

add firstSeenTs for sorting
This commit is contained in:
Matias Alejo Garcia 2015-07-14 20:36:45 -03:00
commit bdb35b3b5f
3 changed files with 20 additions and 11 deletions

View File

@ -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();
}, {

View File

@ -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;
@ -115,7 +116,8 @@ Address.prototype._addTxItem = function(txItem, txList, includeInfo) {
addTx({
txid: txItem.txid,
ts: txItem.ts
ts: txItem.ts,
firstSeenTs: txItem.firstSeenTs,
});
}
@ -182,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,
@ -193,7 +197,7 @@ Address.prototype.update = function(next, opts) {
confirmations: x.isConfirmedCached ? (config.safeConfirmations) : x.confirmations,
confirmationsFromCache: !!x.isConfirmedCached,
};
});
}), 'scriptPubKey');;
return next();
});
} else {
@ -202,7 +206,6 @@ Address.prototype.update = function(next, opts) {
});
if (txList)
self.transactions = txList;
return next();
}
});

View File

@ -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)