rpc: fix issue with querying txids by block heights

This commit is contained in:
Braydon Fuller 2016-04-12 20:33:18 -04:00 committed by Braydon Fuller
parent 96d8307b55
commit 94ea69a4d5
5 changed files with 48 additions and 26 deletions

View File

@ -85,14 +85,15 @@ class AddressIndexTest(BitcoinTestFramework):
assert_equal(txidsb[2], txidb2) assert_equal(txidsb[2], txidb2)
# Check that limiting by height works # 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({ height_txids = self.nodes[1].getaddresstxids({
"addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br"], "addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br"],
"start": 111, "start": 105,
"end": 111 "end": 110
}) })
assert_equal(len(height_txids), 1) assert_equal(len(height_txids), 2)
assert_equal(height_txids[0], txidb2) assert_equal(height_txids[0], txidb0)
assert_equal(height_txids[1], txidb1)
# Check that multiple addresses works # Check that multiple addresses works
multitxids = self.nodes[1].getaddresstxids({"addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"]}) multitxids = self.nodes[1].getaddresstxids({"addresses": ["2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"]})

View File

@ -561,23 +561,14 @@ struct CAddressIndexKey {
struct CAddressIndexIteratorKey { struct CAddressIndexIteratorKey {
unsigned int type; unsigned int type;
uint160 hashBytes; uint160 hashBytes;
bool includeHeight;
int blockHeight;
size_t GetSerializeSize(int nType, int nVersion) const { size_t GetSerializeSize(int nType, int nVersion) const {
if (includeHeight) { return 21;
return 25;
} else {
return 21;
}
} }
template<typename Stream> template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const { void Serialize(Stream& s, int nType, int nVersion) const {
ser_writedata8(s, type); ser_writedata8(s, type);
hashBytes.Serialize(s, nType, nVersion); hashBytes.Serialize(s, nType, nVersion);
if (includeHeight) {
ser_writedata32be(s, blockHeight);
}
} }
template<typename Stream> template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersion) { void Unserialize(Stream& s, int nType, int nVersion) {
@ -588,14 +579,6 @@ struct CAddressIndexIteratorKey {
CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) { CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) {
type = addressType; type = addressType;
hashBytes = addressHash; hashBytes = addressHash;
includeHeight = false;
}
CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash, int height) {
type = addressType;
hashBytes = addressHash;
blockHeight = height;
includeHeight = true;
} }
CAddressIndexIteratorKey() { CAddressIndexIteratorKey() {
@ -605,7 +588,44 @@ struct CAddressIndexIteratorKey {
void SetNull() { void SetNull() {
type = 0; type = 0;
hashBytes.SetNull(); 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;
} }
}; };

View File

@ -675,7 +675,7 @@ UniValue getaddresstxids(const UniValue& params, bool fHelp)
UniValue endValue = find_value(params[0].get_obj(), "end"); UniValue endValue = find_value(params[0].get_obj(), "end");
if (startValue.isNum() && endValue.isNum()) { if (startValue.isNum() && endValue.isNum()) {
start = startValue.get_int(); start = startValue.get_int();
end = startValue.get_int(); end = endValue.get_int();
} }
} }

View File

@ -242,7 +242,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type,
boost::scoped_ptr<CDBIterator> pcursor(NewIterator()); boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
if (start > 0 && end > 0) { 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 { } else {
pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash))); pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash)));
} }

View File

@ -21,6 +21,7 @@ struct CAddressUnspentKey;
struct CAddressUnspentValue; struct CAddressUnspentValue;
struct CAddressIndexKey; struct CAddressIndexKey;
struct CAddressIndexIteratorKey; struct CAddressIndexIteratorKey;
struct CAddressIndexIteratorHeightKey;
struct CTimestampIndexKey; struct CTimestampIndexKey;
struct CTimestampIndexIteratorKey; struct CTimestampIndexIteratorKey;
struct CSpentIndexKey; struct CSpentIndexKey;