From ca0ee59d6efd0581c8af888d5afbcfc75e811a59 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 12 Apr 2016 20:33:18 -0400 Subject: [PATCH] rpc: fix issue with querying txids by block heights --- qa/rpc-tests/addressindex.py | 11 +++---- src/main.h | 58 ++++++++++++++++++++++++------------ src/rpc/misc.cpp | 2 +- src/txdb.cpp | 2 +- src/txdb.h | 1 + 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index 3ef5bcf67..e45efdbda 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -85,14 +85,15 @@ class AddressIndexTest(BitcoinTestFramework): 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"]}) diff --git a/src/main.h b/src/main.h index 71acc01a3..3403142b3 100644 --- a/src/main.h +++ b/src/main.h @@ -539,23 +539,14 @@ struct CAddressIndexKey { 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 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 void Unserialize(Stream& s, int nType, int nVersion) { @@ -566,14 +557,6 @@ struct CAddressIndexIteratorKey { 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() { @@ -583,7 +566,44 @@ struct CAddressIndexIteratorKey { 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 + void Serialize(Stream& s, int nType, int nVersion) const { + ser_writedata8(s, type); + hashBytes.Serialize(s, nType, nVersion); + ser_writedata32be(s, blockHeight); + } + template + 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; } }; diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index b76adb26f..b31b9a568 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -789,7 +789,7 @@ UniValue getaddresstxids(const UniValue& params, bool fHelp) 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(); } } diff --git a/src/txdb.cpp b/src/txdb.cpp index 7d01c9484..4843735aa 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -372,7 +372,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, boost::scoped_ptr 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))); } diff --git a/src/txdb.h b/src/txdb.h index 3e9d6a078..2f4a9695e 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -21,6 +21,7 @@ struct CAddressUnspentKey; struct CAddressUnspentValue; struct CAddressIndexKey; struct CAddressIndexIteratorKey; +struct CAddressIndexIteratorHeightKey; struct CTimestampIndexKey; struct CTimestampIndexIteratorKey; struct CSpentIndexKey;