changeset 29648:05a47a3a8daa

main: sort address index utxos by height
author Braydon Fuller <courier@braydon.com>
date Wed, 30 Mar 2016 16:42:37 -0400
parents 2aa7cdf6ef8e
children 139d221a297c
files qa/rpc-tests/addressindex.py src/main.cpp src/main.h src/rpcmisc.cpp
diffstat 4 files changed, 31 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/qa/rpc-tests/addressindex.py	Wed Mar 30 15:12:19 2016 -0400
+++ b/qa/rpc-tests/addressindex.py	Wed Mar 30 16:42:37 2016 -0400
@@ -192,6 +192,21 @@
         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"
 
 
--- a/src/main.cpp	Wed Mar 30 15:12:19 2016 -0400
+++ b/src/main.cpp	Wed Mar 30 16:42:37 2016 -0400
@@ -2132,7 +2132,7 @@
                         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()) {
@@ -2142,7 +2142,7 @@
                         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;
@@ -2523,7 +2523,7 @@
                     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);
@@ -2532,7 +2532,7 @@
                     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;
--- a/src/main.h	Wed Mar 30 15:12:19 2016 -0400
+++ b/src/main.h	Wed Mar 30 16:42:37 2016 -0400
@@ -399,6 +399,7 @@
 struct CAddressUnspentValue {
     CAmount satoshis;
     CScript script;
+    int blockHeight;
 
     ADD_SERIALIZE_METHODS;
 
@@ -406,11 +407,13 @@
     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
         READWRITE(satoshis);
         READWRITE(*(CScriptBase*)(&script));
+        READWRITE(blockHeight);
     }
 
-    CAddressUnspentValue(CAmount sats, CScript scriptPubKey) {
+    CAddressUnspentValue(CAmount sats, CScript scriptPubKey, int height) {
         satoshis = sats;
         script = scriptPubKey;
+        blockHeight = height;
     }
 
     CAddressUnspentValue() {
@@ -420,6 +423,7 @@
     void SetNull() {
         satoshis = -1;
         script.clear();
+        blockHeight = 0;
     }
 
     bool IsNull() const {
--- a/src/rpcmisc.cpp	Wed Mar 30 15:12:19 2016 -0400
+++ b/src/rpcmisc.cpp	Wed Mar 30 16:42:37 2016 -0400
@@ -431,8 +431,11 @@
     }
 
     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)
@@ -468,6 +471,8 @@
         }
     }
 
+    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++) {
@@ -478,6 +483,7 @@
         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);
     }