refactor addr update opts

This commit is contained in:
Matias Alejo Garcia 2014-05-28 09:31:59 -03:00
parent 172c2589d7
commit b1261bbf3b
5 changed files with 93 additions and 94 deletions

View File

@ -53,7 +53,7 @@ exports.show = function(req, res, next) {
} else {
return res.jsonp(a.getObj());
}
}, req.query.noTxList);
}, {noTxList: req.query.noTxList});
}
};

View File

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

View File

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

View File

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

View File

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