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 1867992 : void Ser(Stream &s, const Coin& txout) { 28 5560938 : ::Serialize(s, VARINT(txout.nHeight * 4 + (txout.fCoinBase ? 2u : 0u) + (txout.fCoinStake ? 1u : 0u))); 29 1867992 : if (txout.nHeight > 0) { 30 : // Required to maintain compatibility with older undo format. 31 1867992 : ::Serialize(s, (unsigned char)0); 32 : } 33 1867992 : ::Serialize(s, Using<TxOutCompression>(txout.out)); 34 1867992 : } 35 : 36 : template<typename Stream> 37 259622 : void Unser(Stream &s, Coin& txout) { 38 259622 : unsigned int nCode = 0; 39 259622 : ::Unserialize(s, VARINT(nCode)); 40 259622 : txout.nHeight = nCode >> 2; 41 259622 : txout.fCoinBase = nCode & 2; 42 259622 : txout.fCoinStake = nCode & 1; 43 259622 : 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 259622 : ::Unserialize(s, VARINT(nVersionDummy)); 49 : } 50 259622 : ::Unserialize(s, Using<TxOutCompression>(txout.out)); 51 259622 : } 52 : }; 53 : 54 : /** Undo information for a CTransaction */ 55 5476731 : class CTxUndo 56 : { 57 : public: 58 : // undo information for all txins 59 : std::vector<Coin> vprevout; 60 : 61 1165690 : SERIALIZE_METHODS(CTxUndo, obj) { READWRITE(Using<VectorFormatter<TxInUndoFormatter>>(obj.vprevout)); } 62 : }; 63 : 64 : /** Undo information for a CBlock */ 65 118060 : class CBlockUndo 66 : { 67 : public: 68 : std::vector<CTxUndo> vtxundo; // for all but the coinbase 69 : 70 166846 : SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); } 71 : }; 72 : 73 : #endif // PIVX_UNDO_H