Raven Core  3.0.0
P2P Digital Currency
undo.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_UNDO_H
8 #define RAVEN_UNDO_H
9 
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 ? 1 : 0)));
30  if (txout->nHeight > 0) {
31  // Required to maintain compatibility with older undo format.
32  ::Serialize(s, (unsigned char)0);
33  }
34  ::Serialize(s, CTxOutCompressor(REF(txout->out)));
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  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(), SER_NETWORK, PROTOCOL_VERSION);
66 // Deprecated for RIP2 implementation
67 //static const size_t MAX_INPUTS_PER_BLOCK = /*fAssetsIsActive ? MAX_BLOCK_WEIGHT_RIP2 / MIN_TRANSACTION_INPUT_WEIGHT :*/ MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT;
68 
72 class CTxUndo
73 {
74 public:
75  // undo information for all txins
76  std::vector<Coin> vprevout;
77 
78  template <typename Stream>
79  void Serialize(Stream& s) const {
80  // TODO: avoid reimplementing vector serializer
81  uint64_t count = vprevout.size();
82  ::Serialize(s, COMPACTSIZE(REF(count)));
83  for (const auto& prevout : vprevout) {
84  ::Serialize(s, REF(TxInUndoSerializer(&prevout)));
85  }
86  }
87 
88  template <typename Stream>
89  void Unserialize(Stream& s) {
90  // TODO: avoid reimplementing vector deserializer
91  uint64_t count = 0;
92  ::Unserialize(s, COMPACTSIZE(count));
93  if (fAssetsIsActive) {
94  if (count > MAX_BLOCK_WEIGHT_RIP2 / MIN_TRANSACTION_INPUT_WEIGHT) {
95  throw std::ios_base::failure("Too many input undo records");
96  }
97  } else {
98  if (count > MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT) {
99  throw std::ios_base::failure("Too many input undo records");
100  }
101  }
102  vprevout.resize(count);
103  for (auto& prevout : vprevout) {
104  ::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
105  }
106  }
107 };
108 
111 {
112 public:
113  std::vector<CTxUndo> vtxundo; // for all but the coinbase
114 
116 
117  template <typename Stream, typename Operation>
118  inline void SerializationOp(Stream& s, Operation ser_action) {
119  READWRITE(vtxundo);
120  }
121 };
122 
123 #endif // RAVEN_UNDO_H
#define VARINT(obj)
Definition: serialize.h:367
std::vector< Coin > vprevout
Definition: undo.h:76
const Coin * txout
Definition: undo.h:24
#define READWRITE(obj)
Definition: serialize.h:163
A UTXO entry.
Definition: coins.h:32
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:94
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
#define COMPACTSIZE(obj)
Definition: serialize.h:368
CTxOut out
unspent transaction output
Definition: coins.h:36
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:39
void Unserialize(Stream &s)
Definition: undo.h:89
void Unserialize(Stream &s)
Definition: undo.h:46
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:42
Undo information for a CTxIn.
Definition: undo.h:22
An input of a transaction.
Definition: transaction.h:67
void SerializationOp(Stream &s, Operation ser_action)
Definition: undo.h:118
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:110
TxInUndoDeserializer(Coin *coin)
Definition: undo.h:61
RVN START.
Definition: undo.h:72
void Serialize(Stream &s) const
Definition: undo.h:79
ADD_SERIALIZE_METHODS
Definition: undo.h:115
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
std::vector< CTxUndo > vtxundo
Definition: undo.h:113
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
Definition: serialize.h:48