rpc: sort txids by height for multiple addresses

This commit is contained in:
Braydon Fuller 2016-03-15 16:24:55 -04:00 committed by Simon
parent 73858d5450
commit 3938fe37f3
2 changed files with 22 additions and 3 deletions

View File

@ -48,14 +48,20 @@ class AddressIndexTest(BitcoinTestFramework):
print "Testing p2pkh and p2sh address index..."
txid0 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10)
self.nodes[0].generate(1)
txidb0 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 10)
self.nodes[0].generate(1)
txid1 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 15)
self.nodes[0].generate(1)
txidb1 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 15)
self.nodes[0].generate(1)
txid2 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 20)
self.nodes[0].generate(1)
txidb2 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 20)
self.nodes[0].generate(1)
@ -76,6 +82,12 @@ class AddressIndexTest(BitcoinTestFramework):
# Check that multiple addresses works
multitxids = self.nodes[1].getaddresstxids({"addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"]});
assert_equal(len(multitxids), 6);
assert_equal(multitxids[0], txid0);
assert_equal(multitxids[1], txidb0);
assert_equal(multitxids[2], txid1);
assert_equal(multitxids[3], txidb1);
assert_equal(multitxids[4], txid2);
assert_equal(multitxids[5], txidb2);
# Check that outputs with the same address will only return one txid
print "Testing for txid uniqueness..."

View File

@ -561,17 +561,24 @@ UniValue getaddresstxids(const UniValue& params, bool fHelp)
}
}
// TODO sort by height
std::set<std::string> txids;
std::vector<std::pair<int, std::string> > vtxids;
UniValue result(UniValue::VARR);
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) {
int height = it->first.blockHeight;
std::string txid = it->first.txhash.GetHex();
if (txids.insert(txid).second) {
result.push_back(txid);
vtxids.push_back(std::make_pair(height, txid));
}
}
std::sort(vtxids.begin(), vtxids.end());
UniValue result(UniValue::VARR);
for (std::vector<std::pair<int, std::string> >::const_iterator it=vtxids.begin(); it!=vtxids.end(); it++) {
result.push_back(it->second);
}
return result;
}