Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2013 The Bitcoin developers 3 : // Copyright (c) 2016-2021 The PIVX 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 PIVX_UNDO_H 8 : #define PIVX_UNDO_H 9 : 10 : #include "chain.h" 11 : #include "compressor.h" 12 : #include "consensus/consensus.h" 13 : #include "primitives/transaction.h" 14 : #include "serialize.h" 15 : #include "version.h" 16 : 17 : /** Formatter for undo information for a CTxIn 18 : * 19 : * Contains the prevout's CTxOut being spent, and its metadata as well 20 : * (coinbase/coinstake or not, height). The serialization contains a 21 : * dummy value of zero. This is be compatible with older versions which 22 : * expect to see the transaction version there. 23 : */ 24 : struct TxInUndoFormatter 25 : { 26 : template<typename Stream> 27 1355368 : void Ser(Stream &s, const Coin& txout) { 28 4023768 : ::Serialize(s, VARINT(txout.nHeight * 4 + (txout.fCoinBase ? 2u : 0u) + (txout.fCoinStake ? 1u : 0u))); 29 1355368 : if (txout.nHeight > 0) { 30 : // Required to maintain compatibility with older undo format. 31 1355368 : ::Serialize(s, (unsigned char)0); 32 : } 33 1355368 : ::Serialize(s, Using<TxOutCompression>(txout.out)); 34 1355368 : } 35 : 36 : template<typename Stream> 37 224390 : void Unser(Stream &s, Coin& txout) { 38 224390 : unsigned int nCode = 0; 39 224390 : ::Unserialize(s, VARINT(nCode)); 40 224390 : txout.nHeight = nCode >> 2; 41 224390 : txout.fCoinBase = nCode & 2; 42 224390 : txout.fCoinStake = nCode & 1; 43 224390 : 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 : unsigned int nVersionDummy; 48 224390 : ::Unserialize(s, VARINT(nVersionDummy)); 49 : } 50 224390 : ::Unserialize(s, Using<TxOutCompression>(txout.out)); 51 224390 : } 52 : }; 53 : 54 : /** Undo information for a CTransaction */ 55 4530595 : class CTxUndo 56 : { 57 : public: 58 : // undo information for all txins 59 : std::vector<Coin> vprevout; 60 : 61 891378 : SERIALIZE_METHODS(CTxUndo, obj) { READWRITE(Using<VectorFormatter<TxInUndoFormatter>>(obj.vprevout)); } 62 : }; 63 : 64 : /** Undo information for a CBlock */ 65 116930 : class CBlockUndo 66 : { 67 : public: 68 : std::vector<CTxUndo> vtxundo; // for all but the coinbase 69 : 70 165516 : SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); } 71 : }; 72 : 73 : #endif // PIVX_UNDO_H