changeset 29658:0757de524b85

rpc: fix issue with querying txids by block heights
author Braydon Fuller <courier@braydon.com>
date Tue, 12 Apr 2016 20:33:18 -0400
parents 190090231c39
children 8cf6c8639b4e
files qa/rpc-tests/addressindex.py src/main.h src/rpcmisc.cpp src/txdb.cpp src/txdb.h
diffstat 5 files changed, 48 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/qa/rpc-tests/addressindex.py	Tue Apr 12 16:04:10 2016 -0400
+++ b/qa/rpc-tests/addressindex.py	Tue Apr 12 20:33:18 2016 -0400
@@ -85,14 +85,15 @@
         assert_equal(txidsb[2], txidb2)
 
         # Check that limiting by height works
-        chain_height = self.nodes[1].getblockcount()
+        print "Testing querying txids by range of block heights.."
         height_txids = self.nodes[1].getaddresstxids({
             "addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br"],
-            "start": 111,
-            "end": 111
+            "start": 105,
+            "end": 110
         })
-        assert_equal(len(height_txids), 1)
-        assert_equal(height_txids[0], txidb2)
+        assert_equal(len(height_txids), 2)
+        assert_equal(height_txids[0], txidb0)
+        assert_equal(height_txids[1], txidb1)
 
         # Check that multiple addresses works
         multitxids = self.nodes[1].getaddresstxids({"addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"]})
--- a/src/main.h	Tue Apr 12 16:04:10 2016 -0400
+++ b/src/main.h	Tue Apr 12 20:33:18 2016 -0400
@@ -561,23 +561,14 @@
 struct CAddressIndexIteratorKey {
     unsigned int type;
     uint160 hashBytes;
-    bool includeHeight;
-    int blockHeight;
 
     size_t GetSerializeSize(int nType, int nVersion) const {
-        if (includeHeight) {
-            return 25;
-        } else {
-            return 21;
-        }
+        return 21;
     }
     template<typename Stream>
     void Serialize(Stream& s, int nType, int nVersion) const {
         ser_writedata8(s, type);
         hashBytes.Serialize(s, nType, nVersion);
-        if (includeHeight) {
-            ser_writedata32be(s, blockHeight);
-        }
     }
     template<typename Stream>
     void Unserialize(Stream& s, int nType, int nVersion) {
@@ -588,14 +579,6 @@
     CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) {
         type = addressType;
         hashBytes = addressHash;
-        includeHeight = false;
-    }
-
-    CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash, int height) {
-        type = addressType;
-        hashBytes = addressHash;
-        blockHeight = height;
-        includeHeight = true;
     }
 
     CAddressIndexIteratorKey() {
@@ -605,7 +588,44 @@
     void SetNull() {
         type = 0;
         hashBytes.SetNull();
-        includeHeight = false;
+    }
+};
+
+struct CAddressIndexIteratorHeightKey {
+    unsigned int type;
+    uint160 hashBytes;
+    int blockHeight;
+
+    size_t GetSerializeSize(int nType, int nVersion) const {
+        return 25;
+    }
+    template<typename Stream>
+    void Serialize(Stream& s, int nType, int nVersion) const {
+        ser_writedata8(s, type);
+        hashBytes.Serialize(s, nType, nVersion);
+        ser_writedata32be(s, blockHeight);
+    }
+    template<typename Stream>
+    void Unserialize(Stream& s, int nType, int nVersion) {
+        type = ser_readdata8(s);
+        hashBytes.Unserialize(s, nType, nVersion);
+        blockHeight = ser_readdata32be(s);
+    }
+
+    CAddressIndexIteratorHeightKey(unsigned int addressType, uint160 addressHash, int height) {
+        type = addressType;
+        hashBytes = addressHash;
+        blockHeight = height;
+    }
+
+    CAddressIndexIteratorHeightKey() {
+        SetNull();
+    }
+
+    void SetNull() {
+        type = 0;
+        hashBytes.SetNull();
+        blockHeight = 0;
     }
 };
 
--- a/src/rpcmisc.cpp	Tue Apr 12 16:04:10 2016 -0400
+++ b/src/rpcmisc.cpp	Tue Apr 12 20:33:18 2016 -0400
@@ -675,7 +675,7 @@
         UniValue endValue = find_value(params[0].get_obj(), "end");
         if (startValue.isNum() && endValue.isNum()) {
             start = startValue.get_int();
-            end = startValue.get_int();
+            end = endValue.get_int();
         }
     }
 
--- a/src/txdb.cpp	Tue Apr 12 16:04:10 2016 -0400
+++ b/src/txdb.cpp	Tue Apr 12 20:33:18 2016 -0400
@@ -242,7 +242,7 @@
     boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
 
     if (start > 0 && end > 0) {
-        pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash, start)));
+        pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorHeightKey(type, addressHash, start)));
     } else {
         pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash)));
     }
--- a/src/txdb.h	Tue Apr 12 16:04:10 2016 -0400
+++ b/src/txdb.h	Tue Apr 12 20:33:18 2016 -0400
@@ -21,6 +21,7 @@
 struct CAddressUnspentValue;
 struct CAddressIndexKey;
 struct CAddressIndexIteratorKey;
+struct CAddressIndexIteratorHeightKey;
 struct CTimestampIndexKey;
 struct CTimestampIndexIteratorKey;
 struct CSpentIndexKey;