13
13
#include < serialize.h>
14
14
#include < version.h>
15
15
16
- /* * Undo information for a CTxIn
16
+ /* * Formatter for undo information for a CTxIn
17
17
*
18
18
* Contains the prevout's CTxOut being spent, and its metadata as well
19
19
* (coinbase or not, height). The serialization contains a dummy value of
20
20
* zero. This is compatible with older versions which expect to see
21
21
* the transaction version there.
22
22
*/
23
- class TxInUndoSerializer
23
+ struct TxInUndoFormatter
24
24
{
25
- const Coin* txout;
26
-
27
- public:
28
25
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 ) {
32
29
// Required to maintain compatibility with older undo format.
33
30
::Serialize (s, (unsigned char )0);
34
31
}
35
- ::Serialize (s, Using<TxOutCompression>(REF( txout-> out) ));
32
+ ::Serialize (s, Using<TxOutCompression>(txout. out));
36
33
}
37
34
38
- explicit TxInUndoSerializer (const Coin* coin) : txout(coin) {}
39
- };
40
-
41
- class TxInUndoDeserializer
42
- {
43
- Coin* txout;
44
-
45
- public:
46
35
template <typename Stream>
47
- void Unserialize (Stream &s) {
36
+ void Unser (Stream &s, Coin& txout ) {
48
37
unsigned int nCode = 0 ;
49
38
::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 ) {
53
42
// Old versions stored the version number for the last spend of
54
43
// a transaction's outputs. Non-final spends were indicated with
55
44
// height = 0.
56
45
unsigned int nVersionDummy;
57
46
::Unserialize (s, VARINT(nVersionDummy));
58
47
}
59
- ::Unserialize (s, Using<TxOutCompression>(REF( txout-> out) ));
48
+ ::Unserialize (s, Using<TxOutCompression>(txout. out));
60
49
}
61
-
62
- explicit TxInUndoDeserializer (Coin* coin) : txout(coin) {}
63
50
};
64
51
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
-
68
52
/* * Undo information for a CTransaction */
69
53
class CTxUndo
70
54
{
71
55
public:
72
56
// undo information for all txins
73
57
std::vector<Coin> vprevout;
74
58
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 )); }
98
60
};
99
61
100
62
/* * Undo information for a CBlock */
@@ -103,12 +65,7 @@ class CBlockUndo
103
65
public:
104
66
std::vector<CTxUndo> vtxundo; // for all but the coinbase
105
67
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 ); }
112
69
};
113
70
114
71
#endif // BITCOIN_UNDO_H
0 commit comments