Fabcoin Core  0.16.2
P2P Digital Currency
undo.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_UNDO_H
7 #define FABCOIN_UNDO_H
8 
9 #include <compressor.h>
10 #include <consensus/consensus.h>
11 #include <primitives/transaction.h>
12 #include <serialize.h>
13 
22 {
23  const Coin* txout;
24 
25 public:
26  template<typename Stream>
27  void Serialize(Stream &s) const {
28  ::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1 : 0)));
29  if (txout->nHeight > 0) {
30  // Required to maintain compatibility with older undo format.
31  ::Serialize(s, (unsigned char)0);
32  }
33  ::Serialize(s, CTxOutCompressor(REF(txout->out)));
34  }
35 
36  TxInUndoSerializer(const Coin* coin) : txout(coin) {}
37 };
38 
40 {
42 
43 public:
44  template<typename Stream>
45  void Unserialize(Stream &s) {
46  unsigned int nCode = 0;
47  ::Unserialize(s, VARINT(nCode));
48  txout->nHeight = nCode / 2;
49  txout->fCoinBase = nCode & 1;
50  if (txout->nHeight > 0) {
51  // Old versions stored the version number for the last spend of
52  // a transaction's outputs. Non-final spends were indicated with
53  // height = 0.
54  int nVersionDummy;
55  ::Unserialize(s, VARINT(nVersionDummy));
56  }
58  }
59 
60  TxInUndoDeserializer(Coin* coin) : txout(coin) {}
61 };
62 
63 static const size_t MIN_TRANSACTION_INPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxIn(), SER_NETWORK, PROTOCOL_VERSION);
64 static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT;
65 
67 class CTxUndo
68 {
69 public:
70  // undo information for all txins
71  std::vector<Coin> vprevout;
72 
73  template <typename Stream>
74  void Serialize(Stream& s) const {
75  // TODO: avoid reimplementing vector serializer
76  uint64_t count = vprevout.size();
77  ::Serialize(s, COMPACTSIZE(REF(count)));
78  for (const auto& prevout : vprevout) {
79  ::Serialize(s, REF(TxInUndoSerializer(&prevout)));
80  }
81  }
82 
83  template <typename Stream>
84  void Unserialize(Stream& s) {
85  // TODO: avoid reimplementing vector deserializer
86  uint64_t count = 0;
87  ::Unserialize(s, COMPACTSIZE(count));
88  if (count > MAX_INPUTS_PER_BLOCK) {
89  throw std::ios_base::failure("Too many input undo records");
90  }
91  vprevout.resize(count);
92  for (auto& prevout : vprevout) {
93  ::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
94  }
95  }
96 };
97 
100 {
101 public:
102  std::vector<CTxUndo> vtxundo; // for all but the coinbase
103 
105 
106  template <typename Stream, typename Operation>
107  inline void SerializationOp(Stream& s, Operation ser_action) {
108  READWRITE(vtxundo);
109  }
110 };
111 
112 #endif // FABCOIN_UNDO_H
#define VARINT(obj)
Definition: serialize.h:389
std::vector< Coin > vprevout
Definition: undo.h:71
const Coin * txout
Definition: undo.h:23
#define READWRITE(obj)
Definition: serialize.h:179
A UTXO entry.
Definition: coins.h:29
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:93
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:989
#define COMPACTSIZE(obj)
Definition: serialize.h:390
size_t count
Definition: ExecStats.cpp:37
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:84
void Unserialize(Stream &s)
Definition: undo.h:45
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:21
An input of a transaction.
Definition: transaction.h:61
void SerializationOp(Stream &s, Operation ser_action)
Definition: undo.h:107
TxInUndoSerializer(const Coin *coin)
Definition: undo.h:36
void Serialize(Stream &s) const
Definition: undo.h:74
Coin * txout
Definition: undo.h:41
Undo information for a CBlock.
Definition: undo.h:99
TxInUndoDeserializer(Coin *coin)
Definition: undo.h:60
Undo information for a CTransaction.
Definition: undo.h:67
void Serialize(Stream &s) const
Definition: undo.h:27
ADD_SERIALIZE_METHODS
Definition: undo.h:104
void Unserialize(Stream &s, char &a)
Definition: serialize.h:210
std::vector< CTxUndo > vtxundo
Definition: undo.h:102
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:49