Skip to content

Commit 3c94b00

Browse files
committed
Convert undo.h to new serialization framework
1 parent 3cd8ab9 commit 3c94b00

File tree

1 file changed

+13
-56
lines changed

1 file changed

+13
-56
lines changed

src/undo.h

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,50 @@
1313
#include <serialize.h>
1414
#include <version.h>
1515

16-
/** Undo information for a CTxIn
16+
/** Formatter for undo information for a CTxIn
1717
*
1818
* Contains the prevout's CTxOut being spent, and its metadata as well
1919
* (coinbase or not, height). The serialization contains a dummy value of
2020
* zero. This is compatible with older versions which expect to see
2121
* the transaction version there.
2222
*/
23-
class TxInUndoSerializer
23+
struct TxInUndoFormatter
2424
{
25-
const Coin* txout;
26-
27-
public:
2825
template<typename Stream>
29-
void Serialize(Stream &s) const {
30-
::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1u : 0u)));
31-
if (txout->nHeight > 0) {
26+
void Ser(Stream &s, const Coin& txout) {
27+
::Serialize(s, VARINT(txout.nHeight * 2 + (txout.fCoinBase ? 1u : 0u)));
28+
if (txout.nHeight > 0) {
3229
// Required to maintain compatibility with older undo format.
3330
::Serialize(s, (unsigned char)0);
3431
}
35-
::Serialize(s, Using<TxOutCompression>(REF(txout->out)));
32+
::Serialize(s, Using<TxOutCompression>(txout.out));
3633
}
3734

38-
explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
39-
};
40-
41-
class TxInUndoDeserializer
42-
{
43-
Coin* txout;
44-
45-
public:
4635
template<typename Stream>
47-
void Unserialize(Stream &s) {
36+
void Unser(Stream &s, Coin& txout) {
4837
unsigned int nCode = 0;
4938
::Unserialize(s, VARINT(nCode));
50-
txout->nHeight = nCode / 2;
51-
txout->fCoinBase = nCode & 1;
52-
if (txout->nHeight > 0) {
39+
txout.nHeight = nCode / 2;
40+
txout.fCoinBase = nCode & 1;
41+
if (txout.nHeight > 0) {
5342
// Old versions stored the version number for the last spend of
5443
// a transaction's outputs. Non-final spends were indicated with
5544
// height = 0.
5645
unsigned int nVersionDummy;
5746
::Unserialize(s, VARINT(nVersionDummy));
5847
}
59-
::Unserialize(s, Using<TxOutCompression>(REF(txout->out)));
48+
::Unserialize(s, Using<TxOutCompression>(txout.out));
6049
}
61-
62-
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
6350
};
6451

65-
static const size_t MIN_TRANSACTION_INPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxIn(), PROTOCOL_VERSION);
66-
static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT;
67-
6852
/** Undo information for a CTransaction */
6953
class CTxUndo
7054
{
7155
public:
7256
// undo information for all txins
7357
std::vector<Coin> vprevout;
7458

75-
template <typename Stream>
76-
void Serialize(Stream& s) const {
77-
// TODO: avoid reimplementing vector serializer
78-
uint64_t count = vprevout.size();
79-
::Serialize(s, COMPACTSIZE(REF(count)));
80-
for (const auto& prevout : vprevout) {
81-
::Serialize(s, TxInUndoSerializer(&prevout));
82-
}
83-
}
84-
85-
template <typename Stream>
86-
void Unserialize(Stream& s) {
87-
// TODO: avoid reimplementing vector deserializer
88-
uint64_t count = 0;
89-
::Unserialize(s, COMPACTSIZE(count));
90-
if (count > MAX_INPUTS_PER_BLOCK) {
91-
throw std::ios_base::failure("Too many input undo records");
92-
}
93-
vprevout.resize(count);
94-
for (auto& prevout : vprevout) {
95-
::Unserialize(s, TxInUndoDeserializer(&prevout));
96-
}
97-
}
59+
SERIALIZE_METHODS(CTxUndo, obj) { READWRITE(Using<VectorFormatter<TxInUndoFormatter>>(obj.vprevout)); }
9860
};
9961

10062
/** Undo information for a CBlock */
@@ -103,12 +65,7 @@ class CBlockUndo
10365
public:
10466
std::vector<CTxUndo> vtxundo; // for all but the coinbase
10567

106-
ADD_SERIALIZE_METHODS;
107-
108-
template <typename Stream, typename Operation>
109-
inline void SerializationOp(Stream& s, Operation ser_action) {
110-
READWRITE(vtxundo);
111-
}
68+
SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); }
11269
};
11370

11471
#endif // BITCOIN_UNDO_H

0 commit comments

Comments
 (0)