WIP
This commit is contained in:
parent
0fccc1c915
commit
2dc2c4106c
@ -47,8 +47,10 @@ var getAddrs = function(req, res, next) {
|
|||||||
var addrStrs = req.param('addrs');
|
var addrStrs = req.param('addrs');
|
||||||
var s = addrStrs.split(',');
|
var s = addrStrs.split(',');
|
||||||
if (s.length === 0) return as;
|
if (s.length === 0) return as;
|
||||||
|
var enableDeadAddresses = s.length > 100;
|
||||||
|
console.log('[addresses.js.50:enableDeadAddresses:]',enableDeadAddresses); //TODO
|
||||||
for (var i = 0; i < s.length; i++) {
|
for (var i = 0; i < s.length; i++) {
|
||||||
var a = new Address(s[i]);
|
var a = new Address(s[i], enableDeadAddresses);
|
||||||
as.push(a);
|
as.push(a);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -244,7 +246,7 @@ exports.multitxs = function(req, res, next) {
|
|||||||
cache[addrStrs] = txs;
|
cache[addrStrs] = txs;
|
||||||
// 5 min. just to purge memory. Cache is overwritten in from=0 requests.
|
// 5 min. just to purge memory. Cache is overwritten in from=0 requests.
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
console.log('Deleting cache');
|
console.log('Deleting cache:', addrStrs.substr(0,20));
|
||||||
delete cache[addrStrs];
|
delete cache[addrStrs];
|
||||||
}, 5 * 60 * 1000);
|
}, 5 * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,8 +13,19 @@ var TransactionDb = imports.TransactionDb || require('../../lib/TransactionDb').
|
|||||||
var BlockDb = imports.BlockDb || require('../../lib/BlockDb').default();
|
var BlockDb = imports.BlockDb || require('../../lib/BlockDb').default();
|
||||||
var config = require('../../config/config');
|
var config = require('../../config/config');
|
||||||
var CONCURRENCY = 5;
|
var CONCURRENCY = 5;
|
||||||
|
var DAYS_TO_DEAD = 2;
|
||||||
|
|
||||||
|
var deadCache = {};
|
||||||
|
|
||||||
|
function Address(addrStr, deadCacheEnable) {
|
||||||
|
|
||||||
|
if (deadCacheEnable && deadCache[addrStr]) {
|
||||||
|
console.log('DEAD CACHE HIT:', addrStr, deadCache[addrStr].cached);
|
||||||
|
return deadCache[addrStr];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.deadCacheEnable = deadCacheEnable;
|
||||||
|
|
||||||
function Address(addrStr) {
|
|
||||||
this.balanceSat = 0;
|
this.balanceSat = 0;
|
||||||
this.totalReceivedSat = 0;
|
this.totalReceivedSat = 0;
|
||||||
this.totalSentSat = 0;
|
this.totalSentSat = 0;
|
||||||
@ -76,6 +87,26 @@ function Address(addrStr) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Address.deleteDeadCache = function(addrStr) {
|
||||||
|
if (deadCache[addrStr]) {
|
||||||
|
console.log('Deleting Dead Address Cache', addrStr);
|
||||||
|
delete deadCache[addrStr];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Address.prototype.setCache = function() {
|
||||||
|
this.cached = true;
|
||||||
|
deadCache[this.addrStr] = this;
|
||||||
|
|
||||||
|
console.log('%%%%%%%% setting DEAD cache for ', this.addrStr, this.unspent.length, this.transactions.length); //TODO
|
||||||
|
console.log('%%%%%%%% cache size:', _.keys(deadCache).length); //TODO
|
||||||
|
|
||||||
|
// TODO expire it...
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Address.prototype.getObj = function() {
|
Address.prototype.getObj = function() {
|
||||||
// Normalize json address
|
// Normalize json address
|
||||||
return {
|
return {
|
||||||
@ -165,8 +196,28 @@ Address.prototype.update = function(next, opts) {
|
|||||||
if (!('ignoreCache' in opts))
|
if (!('ignoreCache' in opts))
|
||||||
opts.ignoreCache = config.ignoreCache;
|
opts.ignoreCache = config.ignoreCache;
|
||||||
|
|
||||||
|
if (opts.onlyUnspent && opts.includeTxInfo)
|
||||||
|
return cb('Bad params');
|
||||||
|
|
||||||
|
if (!opts.ignoreCache && this.cached) {
|
||||||
|
|
||||||
|
console.log('[Address.js.203] CACHED????', this.addrStr, opts.onlyUnspent, opts.includeTxInfo); //TODO
|
||||||
|
|
||||||
|
|
||||||
|
if (opts.onlyUnspent && this.unspent) {
|
||||||
|
console.log('[Address.js.206] YES (unspent)'); //TODO
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.includeTxInfo && this.transactions.length) {
|
||||||
|
console.log('[Address.js.206] YES (TXS)'); //TODO
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// should collect txList from address?
|
// should collect txList from address?
|
||||||
var txList = opts.txLimit === 0 ? null : [];
|
var txList = opts.txLimit === 0 ? null : [];
|
||||||
|
var lastUsage, now = Date.now() / 1000;
|
||||||
|
|
||||||
var tDb = TransactionDb;
|
var tDb = TransactionDb;
|
||||||
var bDb = BlockDb;
|
var bDb = BlockDb;
|
||||||
@ -180,13 +231,15 @@ Address.prototype.update = function(next, opts) {
|
|||||||
// console.log('[Address.js.161:txOut:]',txOut); //TODO
|
// console.log('[Address.js.161:txOut:]',txOut); //TODO
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
if (opts.onlyUnspent) {
|
if (opts.onlyUnspent) {
|
||||||
txOut = txOut.filter(function(x) {
|
|
||||||
|
var unspent = _.filter(txOut,function(x) {
|
||||||
return !x.spentTxId;
|
return !x.spentTxId;
|
||||||
});
|
});
|
||||||
tDb.fillScriptPubKey(txOut, function() {
|
|
||||||
|
tDb.fillScriptPubKey(unspent, function() {
|
||||||
//_.filter will filterout unspend without scriptPubkey
|
//_.filter will filterout unspend without scriptPubkey
|
||||||
//(probably from double spends)
|
//(probably from double spends)
|
||||||
self.unspent = _.filter(txOut.map(function(x) {
|
self.unspent = _.filter(unspent.map(function(x) {
|
||||||
return {
|
return {
|
||||||
address: self.addrStr,
|
address: self.addrStr,
|
||||||
txid: x.txid,
|
txid: x.txid,
|
||||||
@ -198,14 +251,42 @@ Address.prototype.update = function(next, opts) {
|
|||||||
confirmationsFromCache: !!x.isConfirmedCached,
|
confirmationsFromCache: !!x.isConfirmedCached,
|
||||||
};
|
};
|
||||||
}), 'scriptPubKey');;
|
}), 'scriptPubKey');;
|
||||||
|
|
||||||
|
if (self.deadCacheEnable && txOut.length && !self.unspent.length) {
|
||||||
|
// console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$ ',self.addrStr); //TODO
|
||||||
|
// console.log('[Address.js.242] NO UNSPENT:', self.addrStr, txOut.length); //TODO
|
||||||
|
// Asumes that addresses are ordered by Ts;
|
||||||
|
lastUsage = txOut[txOut.length-1].spentTs || now;
|
||||||
|
|
||||||
|
var daysOld = (now-lastUsage) / (3600 * 24);
|
||||||
|
// console.log('[Address.js.253:dayOlds:]',daysOld); //TODO
|
||||||
|
var isOldEnough = daysOld > DAYS_TO_DEAD;
|
||||||
|
|
||||||
|
// console.log('[Address.js.246:isOldEnough:]', isOldEnough, lastUsage, now); //TODO
|
||||||
|
|
||||||
|
if (isOldEnough) {
|
||||||
|
self.setCache();
|
||||||
|
}
|
||||||
|
}
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
console.log('[Address.js.273]'); //TODO
|
||||||
txOut.forEach(function(txItem) {
|
txOut.forEach(function(txItem) {
|
||||||
self._addTxItem(txItem, txList, opts.includeTxInfo);
|
self._addTxItem(txItem, txList, opts.includeTxInfo);
|
||||||
});
|
});
|
||||||
if (txList)
|
if (txList)
|
||||||
self.transactions = txList;
|
self.transactions = txList;
|
||||||
|
|
||||||
|
|
||||||
|
console.log('[Address.js.279]', self.addrStr, self.deadCacheEnable , self.cached); //TODO
|
||||||
|
if (self.deadCacheEnable && self.cached) {
|
||||||
|
|
||||||
|
console.log('[Address.js.281] WASS DEAD ALREADY! CACHING HISTORY'); //TODO
|
||||||
|
self.setCache();
|
||||||
|
}
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
11
lib/Sync.js
11
lib/Sync.js
@ -6,6 +6,8 @@ var config = imports.config || require('../config/config');
|
|||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var networks = bitcore.networks;
|
var networks = bitcore.networks;
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
var _ = require('lodash');
|
||||||
|
var Address = require('../app/models/Address');
|
||||||
|
|
||||||
var logger = require('./logger').logger;
|
var logger = require('./logger').logger;
|
||||||
var d = logger.log;
|
var d = logger.log;
|
||||||
@ -293,7 +295,14 @@ Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
|
|||||||
|
|
||||||
//Store unconfirmed TXs
|
//Store unconfirmed TXs
|
||||||
Sync.prototype.storeTx = function(tx, cb) {
|
Sync.prototype.storeTx = function(tx, cb) {
|
||||||
this.txDb.add(tx, cb);
|
this.txDb.add(tx, function(err, related) {
|
||||||
|
if (related) {
|
||||||
|
_.each(related, function(v,k){
|
||||||
|
Address.deleteDeadCache(k);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return cb(err, related);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user