main: serialize height in BE for address index key

fixes a sorting issue when iterating over keys
This commit is contained in:
Braydon Fuller 2016-03-16 14:00:50 -04:00 committed by Braydon Fuller
parent 5b5f3f7d00
commit 7959a19085
2 changed files with 44 additions and 16 deletions

View File

@ -299,16 +299,27 @@ struct CAddressIndexKey {
uint256 txhash; uint256 txhash;
size_t outindex; size_t outindex;
ADD_SERIALIZE_METHODS; size_t GetSerializeSize(int nType, int nVersion) const {
return 65;
template <typename Stream, typename Operation> }
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { template<typename Stream>
READWRITE(type); void Serialize(Stream& s, int nType, int nVersion) const {
READWRITE(hashBytes); ser_writedata8(s, type);
READWRITE(blockHeight); hashBytes.Serialize(s, nType, nVersion);
READWRITE(txindex); // Heights are stored big-endian for key sorting in LevelDB
READWRITE(txhash); ser_writedata32be(s, blockHeight);
READWRITE(outindex); ser_writedata32be(s, txindex);
txhash.Serialize(s, nType, nVersion);
ser_writedata32(s, outindex);
}
template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersion) {
type = ser_readdata8(s);
hashBytes.Unserialize(s, nType, nVersion);
blockHeight = ser_readdata32be(s);
txindex = ser_readdata32be(s);
txhash.Unserialize(s, nType, nVersion);
outindex = ser_readdata32(s);
} }
CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex, CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex,
@ -340,12 +351,18 @@ struct CAddressIndexIteratorKey {
unsigned int type; unsigned int type;
uint160 hashBytes; uint160 hashBytes;
ADD_SERIALIZE_METHODS; size_t GetSerializeSize(int nType, int nVersion) const {
return 21;
template <typename Stream, typename Operation> }
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { template<typename Stream>
READWRITE(type); void Serialize(Stream& s, int nType, int nVersion) const {
READWRITE(hashBytes); ser_writedata8(s, type);
hashBytes.Serialize(s, nType, nVersion);
}
template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersion) {
type = ser_readdata8(s);
hashBytes.Unserialize(s, nType, nVersion);
} }
CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) { CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) {

View File

@ -91,6 +91,11 @@ template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
obj = htole32(obj); obj = htole32(obj);
s.write((char*)&obj, 4); s.write((char*)&obj, 4);
} }
template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
{
obj = htobe32(obj);
s.write((char*)&obj, 4);
}
template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj) template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
{ {
obj = htole64(obj); obj = htole64(obj);
@ -114,6 +119,12 @@ template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
s.read((char*)&obj, 4); s.read((char*)&obj, 4);
return le32toh(obj); return le32toh(obj);
} }
template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
{
uint32_t obj;
s.read((char*)&obj, 4);
return be32toh(obj);
}
template<typename Stream> inline uint64_t ser_readdata64(Stream &s) template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
{ {
uint64_t obj; uint64_t obj;