BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 
23 {
24  const Coin* txout;
25 
26 public:
27  template<typename Stream>
28  void Serialize(Stream &s) const {
29  ::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1u : 0u)));
30  if (txout->nHeight > 0) {
31  // Required to maintain compatibility with older undo format.
32  ::Serialize(s, (unsigned char)0);
33  }
35  }
36 
37  explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
38 };
39 
41 {
43 
44 public:
45  template<typename Stream>
46  void Unserialize(Stream &s) {
47  unsigned int nCode = 0;
48  ::Unserialize(s, VARINT(nCode));
49  txout->nHeight = nCode / 2;
50  txout->fCoinBase = nCode & 1;
51  if (txout->nHeight > 0) {
52  // Old versions stored the version number for the last spend of
53  // a transaction's outputs. Non-final spends were indicated with
54  // height = 0.
55  unsigned int nVersionDummy;
56  ::Unserialize(s, VARINT(nVersionDummy));
57  }
59  }
60 
61  explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
62 };
63 
64 static const size_t MIN_TRANSACTION_INPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxIn(), PROTOCOL_VERSION);
65 static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT;
66 
68 class CTxUndo
69 {
70 public:
71  // undo information for all txins
72  std::vector<Coin> vprevout;
73 
74  template <typename Stream>
75  void Serialize(Stream& s) const {
76  // TODO: avoid reimplementing vector serializer
77  uint64_t count = vprevout.size();
78  ::Serialize(s, COMPACTSIZE(REF(count)));
79  for (const auto& prevout : vprevout) {
80  ::Serialize(s, TxInUndoSerializer(&prevout));
81  }
82  }
83 
84  template <typename Stream>
85  void Unserialize(Stream& s) {
86  // TODO: avoid reimplementing vector deserializer
87  uint64_t count = 0;
88  ::Unserialize(s, COMPACTSIZE(count));
89  if (count > MAX_INPUTS_PER_BLOCK) {
90  throw std::ios_base::failure("Too many input undo records");
91  }
92  vprevout.resize(count);
93  for (auto& prevout : vprevout) {
94  ::Unserialize(s, TxInUndoDeserializer(&prevout));
95  }
96  }
97 };
98 
101 {
102 public:
103  std::vector<CTxUndo> vtxundo; // for all but the coinbase
104 
106 
107  template <typename Stream, typename Operation>
108  inline void SerializationOp(Stream& s, Operation ser_action) {
110  }
111 };
112 
113 #endif // BITCOIN_UNDO_H
std::vector< Coin > vprevout
Definition: undo.h:72
const Coin * txout
Definition: undo.h:24
A UTXO entry.
Definition: coins.h:29
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:86
#define COMPACTSIZE(obj)
Definition: serialize.h:412
CTxOut out
unspent transaction output
Definition: coins.h:33
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:36
void Unserialize(Stream &s)
Definition: undo.h:85
void Unserialize(Stream &s)
Definition: undo.h:46
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:981
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:39
Undo information for a CTxIn.
Definition: undo.h:22
An input of a transaction.
Definition: transaction.h:61
void SerializationOp(Stream &s, Operation ser_action)
Definition: undo.h:108
TxInUndoSerializer(const Coin *coin)
Definition: undo.h:37
void Serialize(Stream &s) const
Definition: undo.h:28
Coin * txout
Definition: undo.h:42
Undo information for a CBlock.
Definition: undo.h:100
TxInUndoDeserializer(Coin *coin)
Definition: undo.h:61
Undo information for a CTransaction.
Definition: undo.h:68
void Serialize(Stream &s) const
Definition: undo.h:75
ADD_SERIALIZE_METHODS
Definition: undo.h:105
#define READWRITE(...)
Definition: serialize.h:173
std::vector< CTxUndo > vtxundo
Definition: undo.h:103
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:411