Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2013 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 BITCOIN_UNDO_H
7 : #define BITCOIN_UNDO_H
8 :
9 : #include "compressor.h"
10 : #include "primitives/transaction.h"
11 : #include "serialize.h"
12 :
13 : /** Undo information for a CTxIn
14 : *
15 : * Contains the prevout's CTxOut being spent, and if this was the
16 : * last output of the affected transaction, its metadata as well
17 : * (coinbase or not, height, transaction version)
18 : */
19 43639 : class CTxInUndo
20 : {
21 : public:
22 : CTxOut txout; // the txout data before being spent
23 : bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
24 : unsigned int nHeight; // if the outpoint was the last unspent: its height
25 : int nVersion; // if the outpoint was the last unspent: its version
26 :
27 8120 : CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
28 7311 : CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
29 :
30 : unsigned int GetSerializeSize(int nType, int nVersion) const {
31 : return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
32 : (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
33 : ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
34 : }
35 :
36 : template<typename Stream>
37 5068 : void Serialize(Stream &s, int nType, int nVersion) const {
38 10136 : ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
39 5068 : if (nHeight > 0)
40 3004 : ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
41 10136 : ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
42 5068 : }
43 :
44 : template<typename Stream>
45 656 : void Unserialize(Stream &s, int nType, int nVersion) {
46 656 : unsigned int nCode = 0;
47 656 : ::Unserialize(s, VARINT(nCode), nType, nVersion);
48 656 : nHeight = nCode / 2;
49 656 : fCoinBase = nCode & 1;
50 656 : if (nHeight > 0)
51 380 : ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
52 1968 : ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
53 656 : }
54 : };
55 :
56 : /** Undo information for a CTransaction */
57 165842 : class CTxUndo
58 : {
59 : public:
60 : // undo information for all txins
61 : std::vector<CTxInUndo> vprevout;
62 :
63 3532 : ADD_SERIALIZE_METHODS;
64 :
65 : template <typename Stream, typename Operation>
66 0 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
67 3532 : READWRITE(vprevout);
68 0 : }
69 : };
70 :
71 : /** Undo information for a CBlock */
72 46968 : class CBlockUndo
73 : {
74 : public:
75 : std::vector<CTxUndo> vtxundo; // for all but the coinbase
76 :
77 77024 : ADD_SERIALIZE_METHODS;
78 :
79 : template <typename Stream, typename Operation>
80 0 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
81 66160 : READWRITE(vtxundo);
82 0 : }
83 : };
84 :
85 : #endif // BITCOIN_UNDO_H
|