main: sort address index utxos by height

This commit is contained in:
Braydon Fuller 2016-03-30 16:42:37 -04:00 committed by Simon
parent 110adc6aa7
commit e85d165115
4 changed files with 31 additions and 6 deletions

View File

@ -192,6 +192,21 @@ class AddressIndexTest(BitcoinTestFramework):
assert_equal(len(utxos2), 1)
assert_equal(utxos2[0]["satoshis"], 5000000000)
# Check sorting of utxos
self.nodes[2].generate(150)
txidsort1 = self.nodes[2].sendtoaddress(address2, 50)
self.nodes[2].generate(1)
txidsort2 = self.nodes[2].sendtoaddress(address2, 50)
self.nodes[2].generate(1)
self.sync_all()
utxos3 = self.nodes[1].getaddressutxos({"addresses": [address2]})
assert_equal(len(utxos3), 3)
assert_equal(utxos3[0]["height"], 114)
assert_equal(utxos3[1]["height"], 264)
assert_equal(utxos3[2]["height"], 265)
print "Passed\n"

View File

@ -2357,7 +2357,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
// restore unspent index
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey)));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight)));
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
@ -2367,7 +2367,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
// restore unspent index
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey)));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight)));
} else {
continue;
@ -2676,7 +2676,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue));
// record unspent output
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey)));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
@ -2685,7 +2685,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue));
// record unspent output
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey)));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));
} else {
continue;

View File

@ -377,6 +377,7 @@ struct CAddressUnspentKey {
struct CAddressUnspentValue {
CAmount satoshis;
CScript script;
int blockHeight;
ADD_SERIALIZE_METHODS;
@ -384,11 +385,13 @@ struct CAddressUnspentValue {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(satoshis);
READWRITE(script);
READWRITE(blockHeight);
}
CAddressUnspentValue(CAmount sats, CScript scriptPubKey) {
CAddressUnspentValue(CAmount sats, CScript scriptPubKey, int height) {
satoshis = sats;
script = scriptPubKey;
blockHeight = height;
}
CAddressUnspentValue() {
@ -398,6 +401,7 @@ struct CAddressUnspentValue {
void SetNull() {
satoshis = -1;
script.clear();
blockHeight = 0;
}
bool IsNull() const {

View File

@ -542,8 +542,11 @@ bool getAddressesFromParams(const UniValue& params, std::vector<std::pair<uint16
}
return true;
}
bool heightSort(std::pair<CAddressUnspentKey, CAddressUnspentValue> a,
std::pair<CAddressUnspentKey, CAddressUnspentValue> b) {
return a.second.blockHeight < b.second.blockHeight;
}
UniValue getaddressutxos(const UniValue& params, bool fHelp)
@ -579,6 +582,8 @@ UniValue getaddressutxos(const UniValue& params, bool fHelp)
}
}
std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort);
UniValue result(UniValue::VARR);
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) {
@ -589,6 +594,7 @@ UniValue getaddressutxos(const UniValue& params, bool fHelp)
output.push_back(Pair("outputIndex", it->first.index));
output.push_back(Pair("script", HexStr(it->second.script.begin(), it->second.script.end())));
output.push_back(Pair("satoshis", it->second.satoshis));
output.push_back(Pair("height", it->second.blockHeight));
result.push_back(output);
}