Bitcoin Core  0.19.99
P2P Digital Currency
undo.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_UNDO_H
7 #define BITCOIN_UNDO_H
8 
9 #include <coins.h>
10 #include <compressor.h>
11 #include <consensus/consensus.h>
12 #include <primitives/transaction.h>
13 #include <serialize.h>
14 #include <version.h>
15 
24 {
25  const Coin* txout;
26 
27 public:
28  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) {
32  // Required to maintain compatibility with older undo format.
33  ::Serialize(s, (unsigned char)0);
34  }
35  ::Serialize(s, CTxOutCompressor(REF(txout->out)));
36  }
37 
38  explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
39 };
40 
42 {
44 
45 public:
46  template<typename Stream>
47  void Unserialize(Stream &s) {
48  unsigned int nCode = 0;
49  ::Unserialize(s, VARINT(nCode));
50  txout->nHeight = nCode / 2;
51  txout->fCoinBase = nCode & 1;
52  if (txout->nHeight > 0) {
53  // Old versions stored the version number for the last spend of
54  // a transaction's outputs. Non-final spends were indicated with
55  // height = 0.
56  unsigned int nVersionDummy;
57  ::Unserialize(s, VARINT(nVersionDummy));
58  }
60  }
61 
62  explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
63 };
64 
67 
69 class CTxUndo
70 {
71 public:
72  // undo information for all txins
73  std::vector<Coin> vprevout;
74 
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  }
98 };
99 
102 {
103 public:
104  std::vector<CTxUndo> vtxundo; // for all but the coinbase
105 
107 
108  template <typename Stream, typename Operation>
109  inline void SerializationOp(Stream& s, Operation ser_action) {
110  READWRITE(vtxundo);
111  }
112 };
113 
114 #endif // BITCOIN_UNDO_H
std::vector< Coin > vprevout
Definition: undo.h:73
const Coin * txout
Definition: undo.h:25
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
A UTXO entry.
Definition: coins.h:30
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:86
#define COMPACTSIZE(obj)
Definition: serialize.h:423
CTxOut out
unspent transaction output
Definition: coins.h:34
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:37
static const size_t MIN_TRANSACTION_INPUT_WEIGHT
Definition: undo.h:65
void Unserialize(Stream &s)
Definition: undo.h:86
void Unserialize(Stream &s)
Definition: undo.h:47
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1005
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:40
Undo information for a CTxIn.
Definition: undo.h:23
An input of a transaction.
Definition: transaction.h:63
void SerializationOp(Stream &s, Operation ser_action)
Definition: undo.h:109
TxInUndoSerializer(const Coin *coin)
Definition: undo.h:38
static const size_t MAX_INPUTS_PER_BLOCK
Definition: undo.h:66
void Serialize(Stream &s) const
Definition: undo.h:29
Coin * txout
Definition: undo.h:43
Undo information for a CBlock.
Definition: undo.h:101
TxInUndoDeserializer(Coin *coin)
Definition: undo.h:62
Undo information for a CTransaction.
Definition: undo.h:69
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
void Serialize(Stream &s) const
Definition: undo.h:76
ADD_SERIALIZE_METHODS
Definition: undo.h:106
static int count
Definition: tests.c:45
void Unserialize(Stream &s, char &a)
Definition: serialize.h:222
#define READWRITE(...)
Definition: serialize.h:184
std::vector< CTxUndo > vtxundo
Definition: undo.h:104
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers...
Definition: serialize.h:48
#define VARINT(obj,...)
Definition: serialize.h:422