Address History: Combine addresses so that history is txid unique.

This commit is contained in:
Braydon Fuller 2015-09-14 21:03:06 -04:00
parent da1513edb2
commit 5fbd8991ed
3 changed files with 61 additions and 48 deletions

View File

@ -280,14 +280,14 @@ describe('Node Functionality', function() {
}
results.length.should.equal(1);
var info = results[0];
info.address.should.equal(address);
should.exist(info.addresses[address]);
info.addresses[address].outputIndexes.length.should.equal(1);
info.addresses[address].outputIndexes[0].should.be.within(0, 1);
info.addresses[address].inputIndexes.should.deep.equal([]);
info.satoshis.should.equal(10 * 1e8);
info.confirmations.should.equal(3);
info.timestamp.should.be.a('number');
info.fees.should.be.within(190, 193);
info.outputIndexes.length.should.equal(1);
info.outputIndexes[0].should.be.within(0, 1);
info.inputIndexes.should.deep.equal([]);
info.tx.should.be.an.instanceof(Transaction);
done();
});
@ -423,18 +423,17 @@ describe('Node Functionality', function() {
if (err) {
throw err;
}
history.length.should.equal(5);
history.length.should.equal(4);
history[0].height.should.equal(157);
history[0].confirmations.should.equal(1);
history[1].height.should.equal(157);
history[2].height.should.equal(156);
history[2].address.should.equal(address4);
history[3].height.should.equal(155);
history[3].address.should.equal(address3);
history[4].height.should.equal(154);
history[4].address.should.equal(address2);
history[4].satoshis.should.equal(99990000);
history[4].confirmations.should.equal(4);
history[1].height.should.equal(156);
should.exist(history[1].addresses[address4]);
history[2].height.should.equal(155);
should.exist(history[2].addresses[address3]);
history[3].height.should.equal(154);
should.exist(history[3].addresses[address2]);
history[3].satoshis.should.equal(99990000);
history[3].confirmations.should.equal(4);
done();
});
});
@ -455,12 +454,11 @@ describe('Node Functionality', function() {
if (err) {
throw err;
}
history.length.should.equal(3);
history.length.should.equal(2);
history[0].height.should.equal(157);
history[0].confirmations.should.equal(1);
history[1].height.should.equal(157);
history[2].height.should.equal(156);
history[2].address.should.equal(address4);
history[1].height.should.equal(156);
should.exist(history[1].addresses[address4]);
done();
});
});
@ -507,9 +505,8 @@ describe('Node Functionality', function() {
history.length.should.equal(3);
history[0].height.should.equal(157);
history[0].confirmations.should.equal(1);
history[1].height.should.equal(157);
history[2].height.should.equal(156);
history[2].address.should.equal(address4);
history[1].height.should.equal(156);
should.exist(history[1].addresses[address4]);
done();
});
});
@ -525,16 +522,16 @@ describe('Node Functionality', function() {
}
history.length.should.equal(6);
history[0].height.should.equal(157);
history[0].inputIndexes.should.deep.equal([0, 1]);
history[0].outputIndexes.should.deep.equal([2]);
history[0].addresses[address].inputIndexes.should.deep.equal([0, 1]);
history[0].addresses[address].outputIndexes.should.deep.equal([2]);
history[0].confirmations.should.equal(1);
history[1].height.should.equal(156);
history[2].height.should.equal(155);
history[3].height.should.equal(154);
history[4].height.should.equal(153);
history[4].satoshis.should.equal(-10000);
history[4].outputIndexes.should.deep.equal([0, 1, 2, 3, 4]);
history[4].inputIndexes.should.deep.equal([0]);
history[4].addresses[address].outputIndexes.should.deep.equal([0, 1, 2, 3, 4]);
history[4].addresses[address].inputIndexes.should.deep.equal([0]);
history[5].height.should.equal(150);
history[5].satoshis.should.equal(10 * 1e8);
done();
@ -624,8 +621,8 @@ describe('Node Functionality', function() {
history.length.should.equal(1);
history[0].height.should.equal(153);
history[0].satoshis.should.equal(-10000);
history[0].outputIndexes.should.deep.equal([0, 1, 2, 3, 4]);
history[0].inputIndexes.should.deep.equal([0]);
history[0].addresses[address].outputIndexes.should.deep.equal([0, 1, 2, 3, 4]);
history[0].addresses[address].inputIndexes.should.deep.equal([0]);
done();
});
});

View File

@ -133,25 +133,35 @@ AddressHistory.prototype.combineTransactionInfo = function() {
var l = this.transactionInfo.length;
for(var i = 0; i < l; i++) {
var item = this.transactionInfo[i];
var mapKey = item.address + item.txid;
var mapKey = item.txid;
if (combinedArrayMap[mapKey] >= 0) {
var combined = this.combinedArray[combinedArrayMap[mapKey]];
if (!combined.addresses[item.address]) {
combined.addresses[item.address] = {
outputIndexes: [],
inputIndexes: []
};
}
if (item.outputIndex >= 0) {
combined.satoshis += item.satoshis;
combined.outputIndexes.push(item.outputIndex);
combined.addresses[item.address].outputIndexes.push(item.outputIndex);
} else if (item.inputIndex >= 0) {
combined.inputIndexes.push(item.inputIndex);
combined.addresses[item.address].inputIndexes.push(item.inputIndex);
}
} else {
item.outputIndexes = [];
item.inputIndexes = [];
item.addresses = {};
item.addresses[item.address] = {
outputIndexes: [],
inputIndexes: []
};
if (item.outputIndex >= 0) {
item.outputIndexes.push(item.outputIndex);
delete item.outputIndex;
item.addresses[item.address].outputIndexes.push(item.outputIndex);
} else if (item.inputIndex >= 0) {
item.inputIndexes.push(item.inputIndex);
delete item.inputIndex;
item.addresses[item.address].inputIndexes.push(item.inputIndex);
}
delete item.outputIndex;
delete item.inputIndex;
delete item.address;
this.combinedArray.push(item);
combinedArrayMap[mapKey] = this.combinedArray.length - 1;
}
@ -206,15 +216,13 @@ AddressHistory.prototype.getDetailedInfo = function(txInfo, next) {
}
self.detailedArray.push({
address: txInfo.address,
addresses: txInfo.addresses,
satoshis: self.getSatoshisDetail(transaction, txInfo),
height: transaction.__height,
confirmations: self.getConfirmationsDetail(transaction),
timestamp: transaction.__timestamp,
// TODO bitcore should return null instead of throwing error on coinbase
fees: !transaction.isCoinbase() ? transaction.getFee() : null,
outputIndexes: txInfo.outputIndexes,
inputIndexes: txInfo.inputIndexes,
tx: transaction
});
@ -235,11 +243,14 @@ AddressHistory.prototype.getConfirmationsDetail = function(transaction) {
AddressHistory.prototype.getSatoshisDetail = function(transaction, txInfo) {
var satoshis = txInfo.satoshis || 0;
if (txInfo.inputIndexes.length >= 0) {
for(var j = 0; j < txInfo.inputIndexes.length; j++) {
satoshis -= transaction.inputs[txInfo.inputIndexes[j]].output.satoshis;
for(var address in txInfo.addresses) {
if (txInfo.addresses[address].inputIndexes.length >= 0) {
for(var j = 0; j < txInfo.addresses[address].inputIndexes.length; j++) {
satoshis -= transaction.inputs[txInfo.addresses[address].inputIndexes[j]].output.satoshis;
}
}
}
return satoshis;
};

View File

@ -639,26 +639,29 @@ describe('Address Service History', function() {
addresses: []
});
var transactionInfo = {
addresses: {},
txid: txid,
timestamp: 1407292005,
outputIndexes: [1],
inputIndexes: [],
satoshis: 48020000,
address: txAddress
};
transactionInfo.addresses[txAddress] = {};
transactionInfo.addresses[txAddress].outputIndexes = [1];
transactionInfo.addresses[txAddress].inputIndexes = [];
history.getDetailedInfo(transactionInfo, function(err) {
if (err) {
throw err;
}
var info = history.detailedArray[0];
info.address.should.equal(txAddress);
info.addresses[txAddress].should.deep.equal({
outputIndexes: [1],
inputIndexes: []
});
info.satoshis.should.equal(48020000);
info.height.should.equal(314159);
info.confirmations.should.equal(1);
info.timestamp.should.equal(1407292005);
info.fees.should.equal(20000);
info.outputIndexes.should.deep.equal([1]);
info.inputIndexes.should.deep.equal([]);
info.tx.should.equal(transaction);
});
});
@ -701,8 +704,10 @@ describe('Address Service History', function() {
]
};
var txInfo = {
inputIndexes: [0]
addresses: {}
};
txInfo.addresses[address] = {};
txInfo.addresses[address].inputIndexes = [0];
history.getSatoshisDetail(transaction, txInfo).should.equal(-10000);
});
});