From 7959a190850db293fa3faf6251e270738581a764 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 16 Mar 2016 14:00:50 -0400 Subject: [PATCH] main: serialize height in BE for address index key fixes a sorting issue when iterating over keys --- src/main.h | 49 +++++++++++++++++++++++++++++++++---------------- src/serialize.h | 11 +++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main.h b/src/main.h index c2196fcbd7..1379152533 100644 --- a/src/main.h +++ b/src/main.h @@ -299,16 +299,27 @@ struct CAddressIndexKey { uint256 txhash; size_t outindex; - ADD_SERIALIZE_METHODS; - - template - 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 + 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 + 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, @@ -340,12 +351,18 @@ struct CAddressIndexIteratorKey { unsigned int type; uint160 hashBytes; - ADD_SERIALIZE_METHODS; - - template - 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 + void Serialize(Stream& s, int nType, int nVersion) const { + ser_writedata8(s, type); + hashBytes.Serialize(s, nType, nVersion); + } + template + void Unserialize(Stream& s, int nType, int nVersion) { + type = ser_readdata8(s); + hashBytes.Unserialize(s, nType, nVersion); } CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) { diff --git a/src/serialize.h b/src/serialize.h index 5c2db9d332..41a51d2374 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -91,6 +91,11 @@ template inline void ser_writedata32(Stream &s, uint32_t obj) obj = htole32(obj); s.write((char*)&obj, 4); } +template inline void ser_writedata32be(Stream &s, uint32_t obj) +{ + obj = htobe32(obj); + s.write((char*)&obj, 4); +} template inline void ser_writedata64(Stream &s, uint64_t obj) { obj = htole64(obj); @@ -114,6 +119,12 @@ template inline uint32_t ser_readdata32(Stream &s) s.read((char*)&obj, 4); return le32toh(obj); } +template inline uint32_t ser_readdata32be(Stream &s) +{ + uint32_t obj; + s.read((char*)&obj, 4); + return be32toh(obj); +} template inline uint64_t ser_readdata64(Stream &s) { uint64_t obj;