Bitcoin ABC 0.32.6
P2P Digital Currency
undo.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin 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 BITCOIN_UNDO_H
8#define BITCOIN_UNDO_H
9
10#include <coins.h>
11#include <compressor.h>
12#include <consensus/consensus.h>
13#include <disconnectresult.h>
14#include <serialize.h>
15
16class CBlock;
17class CBlockIndex;
18class CCoinsViewCache;
20
30 template <typename Stream> void Ser(Stream &s, const Coin &txout) {
32 s, VARINT(txout.GetHeight() * 2 + (txout.IsCoinBase() ? 1 : 0)));
33 if (txout.GetHeight() > 0) {
34 // Required to maintain compatibility with older undo format.
35 ::Serialize(s, uint8_t(0));
36 }
37 ::Serialize(s, Using<TxOutCompression>(txout.GetTxOut()));
38 }
39
40 template <typename Stream> void Unser(Stream &s, Coin &txout) {
41 uint32_t nCode = 0;
42 ::Unserialize(s, VARINT(nCode));
43 uint32_t nHeight = nCode / 2;
44 bool fCoinBase = nCode & 1;
45 if (nHeight > 0) {
46 // Old versions stored the version number for the last spend of a
47 // transaction's outputs. Non-final spends were indicated with
48 // height = 0.
49 unsigned int nVersionDummy = 0;
50 ::Unserialize(s, VARINT(nVersionDummy));
51 }
52
53 CTxOut out;
54 ::Unserialize(s, Using<TxOutCompression>(out));
55
56 txout = Coin(std::move(out), nHeight, fCoinBase);
57 }
58};
59
61class CTxUndo {
62public:
63 // Undo information for all txins
64 std::vector<Coin> vprevout;
65
68 }
69};
70
73public:
74 // For all but the coinbase
75 std::vector<CTxUndo> vtxundo;
76
77 SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); }
78};
79
88 const COutPoint &out);
89
94DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block,
95 const CBlockIndex *pindex,
96 CCoinsViewCache &coins);
97
98#endif // BITCOIN_UNDO_H
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
Undo information for a CBlock.
Definition: undo.h:72
std::vector< CTxUndo > vtxundo
Definition: undo.h:75
SERIALIZE_METHODS(CBlockUndo, obj)
Definition: undo.h:77
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
An output of a transaction.
Definition: transaction.h:128
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:61
std::vector< Coin > vprevout
Definition: undo.h:64
SERIALIZE_METHODS(CTxUndo, obj)
Definition: undo.h:66
A UTXO entry.
Definition: coins.h:29
uint32_t GetHeight() const
Definition: coins.h:46
bool IsCoinBase() const
Definition: coins.h:47
CTxOut & GetTxOut()
Definition: coins.h:50
DisconnectResult
unsigned int nHeight
void Serialize(Stream &, V)=delete
#define VARINT(obj)
Definition: serialize.h:635
void Unserialize(Stream &, V)=delete
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:630
#define READWRITE(...)
Definition: serialize.h:176
Formatter for undo information for a CTxIn.
Definition: undo.h:29
void Unser(Stream &s, Coin &txout)
Definition: undo.h:40
void Ser(Stream &s, const Coin &txout)
Definition: undo.h:30
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:774
DisconnectResult UndoCoinSpend(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &coins)
Undo a block from the block and the undoblock data.