You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

undo.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 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. #ifndef BITCOIN_UNDO_H
  6. #define BITCOIN_UNDO_H
  7. #include "compressor.h"
  8. #include "consensus/consensus.h"
  9. #include "primitives/transaction.h"
  10. #include "serialize.h"
  11. /** Undo information for a CTxIn
  12. *
  13. * Contains the prevout's CTxOut being spent, and its metadata as well
  14. * (coinbase or not, height). The serialization contains a dummy value of
  15. * zero. This is be compatible with older versions which expect to see
  16. * the transaction version there.
  17. */
  18. class TxInUndoSerializer
  19. {
  20. const Coin* txout;
  21. public:
  22. template<typename Stream>
  23. void Serialize(Stream &s) const {
  24. ::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1 : 0)));
  25. if (txout->nHeight > 0) {
  26. // Required to maintain compatibility with older undo format.
  27. ::Serialize(s, (unsigned char)0);
  28. }
  29. ::Serialize(s, CTxOutCompressor(REF(txout->out)));
  30. }
  31. TxInUndoSerializer(const Coin* coin) : txout(coin) {}
  32. };
  33. class TxInUndoDeserializer
  34. {
  35. Coin* txout;
  36. public:
  37. template<typename Stream>
  38. void Unserialize(Stream &s) {
  39. unsigned int nCode = 0;
  40. ::Unserialize(s, VARINT(nCode));
  41. txout->nHeight = nCode / 2;
  42. txout->fCoinBase = nCode & 1;
  43. if (txout->nHeight > 0) {
  44. // Old versions stored the version number for the last spend of
  45. // a transaction's outputs. Non-final spends were indicated with
  46. // height = 0.
  47. int nVersionDummy;
  48. ::Unserialize(s, VARINT(nVersionDummy));
  49. }
  50. ::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))));
  51. }
  52. TxInUndoDeserializer(Coin* coin) : txout(coin) {}
  53. };
  54. static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_BASE_SIZE / ::GetSerializeSize(CTxIn(), SER_NETWORK, PROTOCOL_VERSION);
  55. /** Undo information for a CTransaction */
  56. class CTxUndo
  57. {
  58. public:
  59. // undo information for all txins
  60. std::vector<Coin> vprevout;
  61. template <typename Stream>
  62. void Serialize(Stream& s) const {
  63. // TODO: avoid reimplementing vector serializer
  64. uint64_t count = vprevout.size();
  65. ::Serialize(s, COMPACTSIZE(REF(count)));
  66. for (const auto& prevout : vprevout) {
  67. ::Serialize(s, REF(TxInUndoSerializer(&prevout)));
  68. }
  69. }
  70. template <typename Stream>
  71. void Unserialize(Stream& s) {
  72. // TODO: avoid reimplementing vector deserializer
  73. uint64_t count = 0;
  74. ::Unserialize(s, COMPACTSIZE(count));
  75. if (count > MAX_INPUTS_PER_BLOCK) {
  76. throw std::ios_base::failure("Too many input undo records");
  77. }
  78. vprevout.resize(count);
  79. for (auto& prevout : vprevout) {
  80. ::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
  81. }
  82. }
  83. };
  84. /** Undo information for a CBlock */
  85. class CBlockUndo
  86. {
  87. public:
  88. std::vector<CTxUndo> vtxundo; // for all but the coinbase
  89. ADD_SERIALIZE_METHODS;
  90. template <typename Stream, typename Operation>
  91. inline void SerializationOp(Stream& s, Operation ser_action) {
  92. READWRITE(vtxundo);
  93. }
  94. };
  95. #endif // BITCOIN_UNDO_H