Skip to content

Commit 7be9a9a

Browse files
committed
Merge #12683: Fix more constness violations in serialization code
172f5fa Support deserializing into temporaries (Pieter Wuille) 2761bca Merge READWRITEMANY into READWRITE (Pieter Wuille) Pull request description: This is another fragment of improvements from #10785. The current serialization code does not support serializing/deserializing from/to temporaries (like `s >> CFlatData(script)`). As a result, there are many invocations of the `REF` macro which in addition to changing the reference type also changes the constness. This is unnecessary in C++11 as we can use rvalue references now instead. The first commit is an extra simplification we can make that removes the duplication of code between `READWRITE` and `READWRITEMANY` (and related functions). Tree-SHA512: babfa9cb268cc3bc39917e4f0a90e4651c33d85032161e16547a07f3b257b7ca7940e0cbfd69f09439d26fafbb1a6cf6359101043407e2c7aeececf7f20b6eed
2 parents 2bac3e4 + 172f5fa commit 7be9a9a

File tree

10 files changed

+30
-56
lines changed

10 files changed

+30
-56
lines changed

src/blockencodings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ class BlockTransactions {
9090
while (txn.size() < txn_size) {
9191
txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
9292
for (; i < txn.size(); i++)
93-
READWRITE(REF(TransactionCompressor(txn[i])));
93+
READWRITE(TransactionCompressor(txn[i]));
9494
}
9595
} else {
9696
for (size_t i = 0; i < txn.size(); i++)
97-
READWRITE(REF(TransactionCompressor(txn[i])));
97+
READWRITE(TransactionCompressor(txn[i]));
9898
}
9999
}
100100
};
@@ -115,7 +115,7 @@ struct PrefilledTransaction {
115115
if (idx > std::numeric_limits<uint16_t>::max())
116116
throw std::ios_base::failure("index overflowed 16-bits");
117117
index = idx;
118-
READWRITE(REF(TransactionCompressor(tx)));
118+
READWRITE(TransactionCompressor(tx));
119119
}
120120
};
121121

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Coin
6969
::Unserialize(s, VARINT(code));
7070
nHeight = code >> 1;
7171
fCoinBase = code & 1;
72-
::Unserialize(s, REF(CTxOutCompressor(out)));
72+
::Unserialize(s, CTxOutCompressor(out));
7373
}
7474

7575
bool IsSpent() const {

src/compressor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CScriptCompressor
7373
s >> VARINT(nSize);
7474
if (nSize < nSpecialScripts) {
7575
std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
76-
s >> REF(CFlatData(vch));
76+
s >> CFlatData(vch);
7777
Decompress(nSize, vch);
7878
return;
7979
}
@@ -84,7 +84,7 @@ class CScriptCompressor
8484
s.ignore(nSize);
8585
} else {
8686
script.resize(nSize);
87-
s >> REF(CFlatData(script));
87+
s >> CFlatData(script);
8888
}
8989
}
9090
};

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class CHashVerifier : public CHashWriter
173173
}
174174

175175
template<typename T>
176-
CHashVerifier<Source>& operator>>(T& obj)
176+
CHashVerifier<Source>& operator>>(T&& obj)
177177
{
178178
// Unserialize from this stream
179179
::Unserialize(*this, obj);

src/script/bitcoinconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TxInputStream
4040
}
4141

4242
template<typename T>
43-
TxInputStream& operator>>(T& obj)
43+
TxInputStream& operator>>(T&& obj)
4444
{
4545
::Unserialize(*this, obj);
4646
return *this;

src/serialize.h

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ enum
148148
SER_GETHASH = (1 << 2),
149149
};
150150

151-
#define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
152-
#define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
151+
#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
153152

154153
/**
155154
* Implement three methods for serializable objects. These are actually wrappers over
@@ -351,10 +350,10 @@ I ReadVarInt(Stream& is)
351350
}
352351
}
353352

354-
#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
355-
#define VARINT(obj) REF(WrapVarInt(REF(obj)))
356-
#define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
357-
#define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
353+
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
354+
#define VARINT(obj) WrapVarInt(REF(obj))
355+
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
356+
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
358357

359358
/**
360359
* Wrapper for serializing arrays and POD.
@@ -539,7 +538,7 @@ inline void Serialize(Stream& os, const T& a)
539538
}
540539

541540
template<typename Stream, typename T>
542-
inline void Unserialize(Stream& is, T& a)
541+
inline void Unserialize(Stream& is, T&& a)
543542
{
544543
a.Unserialize(is);
545544
}
@@ -825,19 +824,6 @@ struct CSerActionUnserialize
825824
constexpr bool ForRead() const { return true; }
826825
};
827826

828-
template<typename Stream, typename T>
829-
inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
830-
{
831-
::Serialize(s, obj);
832-
}
833-
834-
template<typename Stream, typename T>
835-
inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
836-
{
837-
::Unserialize(s, obj);
838-
}
839-
840-
841827

842828

843829

@@ -897,45 +883,33 @@ void SerializeMany(Stream& s)
897883
{
898884
}
899885

900-
template<typename Stream, typename Arg>
901-
void SerializeMany(Stream& s, Arg&& arg)
902-
{
903-
::Serialize(s, std::forward<Arg>(arg));
904-
}
905-
906886
template<typename Stream, typename Arg, typename... Args>
907-
void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
887+
void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
908888
{
909-
::Serialize(s, std::forward<Arg>(arg));
910-
::SerializeMany(s, std::forward<Args>(args)...);
889+
::Serialize(s, arg);
890+
::SerializeMany(s, args...);
911891
}
912892

913893
template<typename Stream>
914894
inline void UnserializeMany(Stream& s)
915895
{
916896
}
917897

918-
template<typename Stream, typename Arg>
919-
inline void UnserializeMany(Stream& s, Arg& arg)
920-
{
921-
::Unserialize(s, arg);
922-
}
923-
924898
template<typename Stream, typename Arg, typename... Args>
925-
inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
899+
inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
926900
{
927901
::Unserialize(s, arg);
928902
::UnserializeMany(s, args...);
929903
}
930904

931905
template<typename Stream, typename... Args>
932-
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
906+
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
933907
{
934-
::SerializeMany(s, std::forward<Args>(args)...);
908+
::SerializeMany(s, args...);
935909
}
936910

937911
template<typename Stream, typename... Args>
938-
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
912+
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
939913
{
940914
::UnserializeMany(s, args...);
941915
}

src/streams.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OverrideStream
4242
}
4343

4444
template<typename T>
45-
OverrideStream<Stream>& operator>>(T& obj)
45+
OverrideStream<Stream>& operator>>(T&& obj)
4646
{
4747
// Unserialize from this stream
4848
::Unserialize(*this, obj);
@@ -399,7 +399,7 @@ class CDataStream
399399
}
400400

401401
template<typename T>
402-
CDataStream& operator>>(T& obj)
402+
CDataStream& operator>>(T&& obj)
403403
{
404404
// Unserialize from this stream
405405
::Unserialize(*this, obj);
@@ -543,7 +543,7 @@ class CAutoFile
543543
}
544544

545545
template<typename T>
546-
CAutoFile& operator>>(T& obj)
546+
CAutoFile& operator>>(T&& obj)
547547
{
548548
// Unserialize from this stream
549549
if (!file)
@@ -686,7 +686,7 @@ class CBufferedFile
686686
}
687687

688688
template<typename T>
689-
CBufferedFile& operator>>(T& obj) {
689+
CBufferedFile& operator>>(T&& obj) {
690690
// Unserialize from this stream
691691
::Unserialize(*this, obj);
692692
return (*this);

src/test/serialize_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
5353

5454
template <typename Stream, typename Operation>
5555
inline void SerializationOp(Stream& s, Operation ser_action) {
56-
READWRITEMANY(intval, boolval, stringval, FLATDATA(charstrval), txval);
56+
READWRITE(intval, boolval, stringval, FLATDATA(charstrval), txval);
5757
}
5858
};
5959

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class CCoins
348348
vout.assign(vAvail.size(), CTxOut());
349349
for (unsigned int i = 0; i < vAvail.size(); i++) {
350350
if (vAvail[i])
351-
::Unserialize(s, REF(CTxOutCompressor(vout[i])));
351+
::Unserialize(s, CTxOutCompressor(vout[i]));
352352
}
353353
// coinbase height
354354
::Unserialize(s, VARINT(nHeight));

src/undo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TxInUndoDeserializer
5454
int nVersionDummy;
5555
::Unserialize(s, VARINT(nVersionDummy));
5656
}
57-
::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))));
57+
::Unserialize(s, CTxOutCompressor(REF(txout->out)));
5858
}
5959

6060
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
@@ -76,7 +76,7 @@ class CTxUndo
7676
uint64_t count = vprevout.size();
7777
::Serialize(s, COMPACTSIZE(REF(count)));
7878
for (const auto& prevout : vprevout) {
79-
::Serialize(s, REF(TxInUndoSerializer(&prevout)));
79+
::Serialize(s, TxInUndoSerializer(&prevout));
8080
}
8181
}
8282

@@ -90,7 +90,7 @@ class CTxUndo
9090
}
9191
vprevout.resize(count);
9292
for (auto& prevout : vprevout) {
93-
::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
93+
::Unserialize(s, TxInUndoDeserializer(&prevout));
9494
}
9595
}
9696
};

0 commit comments

Comments
 (0)