Skip to content

Commit

Permalink
main: serialize height in BE for address index key
Browse files Browse the repository at this point in the history
fixes a sorting issue when iterating over keys
  • Loading branch information
braydonf committed Mar 16, 2016
1 parent 5b0f931 commit 513c563
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,27 @@ struct CAddressIndexKey {
uint256 txhash;
size_t outindex;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(type);
READWRITE(hashBytes);
READWRITE(blockHeight);
READWRITE(txindex);
READWRITE(txhash);
READWRITE(outindex);
size_t GetSerializeSize(int nType, int nVersion) const {
return 65;
}
template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const {
ser_writedata8(s, type);
hashBytes.Serialize(s, nType, nVersion);
// Heights are stored big-endian for key sorting in LevelDB
ser_writedata32be(s, blockHeight);
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,
Expand Down Expand Up @@ -331,12 +342,18 @@ struct CAddressIndexIteratorKey {
unsigned int type;
uint160 hashBytes;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(type);
READWRITE(hashBytes);
size_t GetSerializeSize(int nType, int nVersion) const {
return 21;
}
template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const {
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) {
Expand Down
11 changes: 11 additions & 0 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
obj = htole32(obj);
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)
{
obj = htole64(obj);
Expand All @@ -114,6 +119,12 @@ template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
s.read((char*)&obj, 4);
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)
{
uint64_t obj;
Expand Down

0 comments on commit 513c563

Please sign in to comment.