Skip to content

Commit 448bdff

Browse files
author
MarcoFalke
committed
Merge #18317: Serialization improvements step 6 (all except wallet/gui)
f9ee0f3 Add comments to CustomUintFormatter (Pieter Wuille) 4eb5643 Convert everything except wallet/qt to new serialization (Pieter Wuille) 2b1f85e Convert blockencodings_tests to new serialization (Pieter Wuille) 73747af Convert merkleblock to new serialization (Pieter Wuille) d06fedd Add SER_READ and SER_WRITE for read/write-dependent statements (Russell Yanofsky) 6f9a1e5 Extend CustomUintFormatter to support enums (Russell Yanofsky) 769ee5f Merge BigEndian functionality into CustomUintFormatter (Pieter Wuille) Pull request description: The next step of changes from #10785. This: * Adds support for enum serialization to `CustomUintFormatter`, used in `CAddress` for service flags. * Merges `BigEndian` into `CustomUintFormatter`, used in `CNetAddr` for port numbers. * Converts everything (except wallet and gui) to use the new serialization framework. ACKs for top commit: MarcoFalke: re-ACK f9ee0f3, only change is new documentation commit for CustomUintFormatter 📂 ryanofsky: Code review ACK f9ee0f3. Just new commit adding comment since last review jonatack: Code review re-ACK f9ee0f3 only change since last review is an additional commit adding Doxygen documentation for `CustomUintFormatter`. Tree-SHA512: e7a0a36afae592d5a4ff8c81ae04d858ac409388e361f2bc197d9a78abca45134218497ab2dfd6d031e0cce0ca586cf857077b7c6ce17fccf67e2d367c1b6cd4
2 parents e20e964 + f9ee0f3 commit 448bdff

22 files changed

+156
-302
lines changed

src/bench/prevector.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
struct nontrivial_t {
2121
int x;
2222
nontrivial_t() :x(-1) {}
23-
ADD_SERIALIZE_METHODS
24-
template <typename Stream, typename Operation>
25-
inline void SerializationOp(Stream& s, Operation ser_action) {READWRITE(x);}
23+
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
2624
};
2725
static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
2826
"expected nontrivial_t to not be trivially constructible");

src/blockencodings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ class CBlockHeaderAndShortTxIDs {
9292

9393
friend class PartiallyDownloadedBlock;
9494

95-
static const int SHORTTXIDS_LENGTH = 6;
9695
protected:
9796
std::vector<uint64_t> shorttxids;
9897
std::vector<PrefilledTransaction> prefilledtxn;
9998

10099
public:
100+
static constexpr int SHORTTXIDS_LENGTH = 6;
101+
101102
CBlockHeader header;
102103

103104
// Dummy for deserialization

src/bloom.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,7 @@ class CBloomFilter
6464
CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn);
6565
CBloomFilter() : nHashFuncs(0), nTweak(0), nFlags(0) {}
6666

67-
ADD_SERIALIZE_METHODS;
68-
69-
template <typename Stream, typename Operation>
70-
inline void SerializationOp(Stream& s, Operation ser_action) {
71-
READWRITE(vData);
72-
READWRITE(nHashFuncs);
73-
READWRITE(nTweak);
74-
READWRITE(nFlags);
75-
}
67+
SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }
7668

7769
void insert(const std::vector<unsigned char>& vKey);
7870
void insert(const COutPoint& outpoint);

src/flatfile.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ struct FlatFilePos
1616
int nFile;
1717
unsigned int nPos;
1818

19-
ADD_SERIALIZE_METHODS;
20-
21-
template <typename Stream, typename Operation>
22-
inline void SerializationOp(Stream& s, Operation ser_action) {
23-
READWRITE(VARINT_MODE(nFile, VarIntMode::NONNEGATIVE_SIGNED));
24-
READWRITE(VARINT(nPos));
25-
}
19+
SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
2620

2721
FlatFilePos() : nFile(-1), nPos(0) {}
2822

src/index/blockfilterindex.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@ struct DBVal {
3939
uint256 header;
4040
FlatFilePos pos;
4141

42-
ADD_SERIALIZE_METHODS;
43-
44-
template <typename Stream, typename Operation>
45-
inline void SerializationOp(Stream& s, Operation ser_action) {
46-
READWRITE(hash);
47-
READWRITE(header);
48-
READWRITE(pos);
49-
}
42+
SERIALIZE_METHODS(DBVal, obj) { READWRITE(obj.hash, obj.header, obj.pos); }
5043
};
5144

5245
struct DBHeightKey {
@@ -78,17 +71,14 @@ struct DBHashKey {
7871

7972
explicit DBHashKey(const uint256& hash_in) : hash(hash_in) {}
8073

81-
ADD_SERIALIZE_METHODS;
82-
83-
template <typename Stream, typename Operation>
84-
inline void SerializationOp(Stream& s, Operation ser_action) {
74+
SERIALIZE_METHODS(DBHashKey, obj) {
8575
char prefix = DB_BLOCK_HASH;
8676
READWRITE(prefix);
8777
if (prefix != DB_BLOCK_HASH) {
8878
throw std::ios_base::failure("Invalid format for block filter index DB hash key");
8979
}
9080

91-
READWRITE(hash);
81+
READWRITE(obj.hash);
9282
}
9383
};
9484

src/index/txindex.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ struct CDiskTxPos : public FlatFilePos
2121
{
2222
unsigned int nTxOffset; // after header
2323

24-
ADD_SERIALIZE_METHODS;
25-
26-
template <typename Stream, typename Operation>
27-
inline void SerializationOp(Stream& s, Operation ser_action) {
28-
READWRITEAS(FlatFilePos, *this);
29-
READWRITE(VARINT(nTxOffset));
24+
SERIALIZE_METHODS(CDiskTxPos, obj)
25+
{
26+
READWRITEAS(FlatFilePos, obj);
27+
READWRITE(VARINT(obj.nTxOffset));
3028
}
3129

3230
CDiskTxPos(const FlatFilePos &blockIn, unsigned int nTxOffsetIn) : FlatFilePos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {

src/merkleblock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
#include <consensus/consensus.h>
1010

1111

12+
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits)
13+
{
14+
std::vector<unsigned char> ret((bits.size() + 7) / 8);
15+
for (unsigned int p = 0; p < bits.size(); p++) {
16+
ret[p / 8] |= bits[p] << (p % 8);
17+
}
18+
return ret;
19+
}
20+
21+
std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
22+
{
23+
std::vector<bool> ret(bytes.size() * 8);
24+
for (unsigned int p = 0; p < ret.size(); p++) {
25+
ret[p] = (bytes[p / 8] & (1 << (p % 8))) != 0;
26+
}
27+
return ret;
28+
}
29+
1230
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
1331
{
1432
header = block.GetBlockHeader();

src/merkleblock.h

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
#include <vector>
1515

16+
// Helper functions for serialization.
17+
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits);
18+
std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes);
19+
1620
/** Data structure that represents a partial merkle tree.
1721
*
1822
* It represents a subset of the txid's of a known block, in a way that
@@ -81,27 +85,14 @@ class CPartialMerkleTree
8185

8286
public:
8387

84-
/** serialization implementation */
85-
ADD_SERIALIZE_METHODS;
86-
87-
template <typename Stream, typename Operation>
88-
inline void SerializationOp(Stream& s, Operation ser_action) {
89-
READWRITE(nTransactions);
90-
READWRITE(vHash);
91-
std::vector<unsigned char> vBytes;
92-
if (ser_action.ForRead()) {
93-
READWRITE(vBytes);
94-
CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this));
95-
us.vBits.resize(vBytes.size() * 8);
96-
for (unsigned int p = 0; p < us.vBits.size(); p++)
97-
us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
98-
us.fBad = false;
99-
} else {
100-
vBytes.resize((vBits.size()+7)/8);
101-
for (unsigned int p = 0; p < vBits.size(); p++)
102-
vBytes[p / 8] |= vBits[p] << (p % 8);
103-
READWRITE(vBytes);
104-
}
88+
SERIALIZE_METHODS(CPartialMerkleTree, obj)
89+
{
90+
READWRITE(obj.nTransactions, obj.vHash);
91+
std::vector<unsigned char> bytes;
92+
SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
93+
READWRITE(bytes);
94+
SER_READ(obj, obj.vBits = BytesToBits(bytes));
95+
SER_READ(obj, obj.fBad = false);
10596
}
10697

10798
/** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
@@ -157,13 +148,7 @@ class CMerkleBlock
157148

158149
CMerkleBlock() {}
159150

160-
ADD_SERIALIZE_METHODS;
161-
162-
template <typename Stream, typename Operation>
163-
inline void SerializationOp(Stream& s, Operation ser_action) {
164-
READWRITE(header);
165-
READWRITE(txn);
166-
}
151+
SERIALIZE_METHODS(CMerkleBlock, obj) { READWRITE(obj.header, obj.txn); }
167152

168153
private:
169154
// Combined constructor to consolidate code

src/netaddress.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ class CNetAddr
9999
friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
100100
friend bool operator<(const CNetAddr& a, const CNetAddr& b);
101101

102-
ADD_SERIALIZE_METHODS;
103-
104-
template <typename Stream, typename Operation>
105-
inline void SerializationOp(Stream& s, Operation ser_action) {
106-
READWRITE(ip);
107-
}
102+
SERIALIZE_METHODS(CNetAddr, obj) { READWRITE(obj.ip); }
108103

109104
friend class CSubNet;
110105
};
@@ -136,14 +131,7 @@ class CSubNet
136131
friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
137132
friend bool operator<(const CSubNet& a, const CSubNet& b);
138133

139-
ADD_SERIALIZE_METHODS;
140-
141-
template <typename Stream, typename Operation>
142-
inline void SerializationOp(Stream& s, Operation ser_action) {
143-
READWRITE(network);
144-
READWRITE(netmask);
145-
READWRITE(valid);
146-
}
134+
SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
147135
};
148136

149137
/** A combination of a network address (CNetAddr) and a (TCP) port */
@@ -171,13 +159,7 @@ class CService : public CNetAddr
171159
CService(const struct in6_addr& ipv6Addr, unsigned short port);
172160
explicit CService(const struct sockaddr_in6& addr);
173161

174-
ADD_SERIALIZE_METHODS;
175-
176-
template <typename Stream, typename Operation>
177-
inline void SerializationOp(Stream& s, Operation ser_action) {
178-
READWRITE(ip);
179-
READWRITE(WrapBigEndian(port));
180-
}
162+
SERIALIZE_METHODS(CService, obj) { READWRITE(obj.ip, Using<BigEndianFormatter<2>>(obj.port)); }
181163
};
182164

183165
bool SanityCheckASMap(const std::vector<bool>& asmap);

src/node/utxo_snapshot.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@ class SnapshotMetadata
3535
m_coins_count(coins_count),
3636
m_nchaintx(nchaintx) { }
3737

38-
ADD_SERIALIZE_METHODS;
39-
40-
template <typename Stream, typename Operation>
41-
inline void SerializationOp(Stream& s, Operation ser_action)
42-
{
43-
READWRITE(m_base_blockhash);
44-
READWRITE(m_coins_count);
45-
READWRITE(m_nchaintx);
46-
}
47-
38+
SERIALIZE_METHODS(SnapshotMetadata, obj) { READWRITE(obj.m_base_blockhash, obj.m_coins_count, obj.m_nchaintx); }
4839
};
4940

5041
#endif // BITCOIN_NODE_UTXO_SNAPSHOT_H

0 commit comments

Comments
 (0)