Fix: aggregation of duplicate values

Fixed: _aggregateAddressSummaryResult to handle duplicate tx data
This commit is contained in:
sairajzero 2023-01-28 17:18:18 +05:30
parent eb1c6331d8
commit 35c506ac65

View File

@ -377,14 +377,42 @@ AddressService.prototype._aggregateAddressSummaryResult = function (tx, address,
var self = this;
self._setOutputResults(tx, address, result);
self._setInputResults(tx, address, result);
let output = self._getOutputResults(tx, address);
let input = self._getInputResults(tx, address);
//Since tx with multiple (x) input/output occurances of address will invoke this fn x time(s), (as we are not storing txid and hence cannot check for duplications)
//we divide the values by x and aggregate it to result.
//eg. tx with 1 input, 1 output => x=1+1=2.... input_val = 2, output_val = 1.
//the values will be aggregated 2 times, hence, we divide values by x i.e, 2.
//now agg_input_val = 2/2 =1, agg_output_val = 1/2 =0.5
//the fn ll be called x times, hence the total result will be, result=agg*x: input(1*2=2), output(0.5*2=1)
let total_count = input.count + output.count;
let div_input_val = input.value / total_count,
div_output_val = output.value / total_count;
//aggregate the result
txApperances += 1/total_count;
totalReceivedSat += div_output_val;
balanceSat += div_output_val;
totalSentSat += div_input_val;
balanceSat -= div_output_val;
if(!tx.confirmations){
unconfirmedTxApperances += 1/total_count;
unconfirmedBalanceSat += div_output_val;
unconfirmedBalanceSat -= div_input_val;
}
if (!options.noTxList) {
if (!result.transactions) {
result.transactions = [];
}
result.transactions.push(tx.txid());
let txid = tx.txid();
if(!result.transactions.includes(txid)) //push txid only if its not in the array
result.transactions.push(txid);
}
}
@ -411,6 +439,48 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul
return result;
};
AddressService.prototype._getOutputResults = function(tx, address) {
let result = { value: 0, count:0 };
for(var j = 0; j < tx.outputs.length; j++) {
var output = tx.outputs[j];
if (utils.getAddress(output, this._network) !== address) {
continue;
}
result.value += output.value;
result.count++;
}
return result;
};
AddressService.prototype._getInputResults = function(tx, address) {
let result = { value: 0, count:0 };
for(var i = 0; i < tx.inputs.length; i++) {
var input = tx.inputs[i];
if (utils.getAddress(input, this._network) !== address) {
continue;
}
result.value += tx.__inputValues[i];
result.count++;
}
return result;
};
AddressService.prototype.getAddressUnspentOutputs = function(address, options, callback) {
var self = this;
@ -797,13 +867,11 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
txIdTransformStream.on('end', function() {
q.drain = next;
console.debug(q.drain);
q.resume();
});
txIdTransformStream._transform = function(chunk, enc, cb) {
var txInfo = self._encoding.decodeAddressIndexKey(chunk);
self._db.get(chunk, (err, data) => console.debug(data, data.length, data.toString())); //test-check
q.push({ txid: txInfo.txid, height: txInfo.height }, process_chunk);
cb();