2014-10-18 19:53:37 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-10-18 19:53:37 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_UNDO_H
|
|
|
|
#define BITCOIN_UNDO_H
|
2014-10-18 19:53:37 +02:00
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
#include <coins.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <compressor.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <serialize.h>
|
2021-06-26 12:37:06 +02:00
|
|
|
#include <version.h>
|
2014-10-18 19:53:37 +02:00
|
|
|
|
2017-09-06 20:25:03 +02:00
|
|
|
/** Formatter for undo information for a CTxIn
|
2014-10-18 19:53:37 +02:00
|
|
|
*
|
2017-06-02 00:47:58 +02:00
|
|
|
* Contains the prevout's CTxOut being spent, and its metadata as well
|
|
|
|
* (coinbase or not, height). The serialization contains a dummy value of
|
2018-03-21 16:16:28 +01:00
|
|
|
* zero. This is compatible with older versions which expect to see
|
2017-06-02 00:47:58 +02:00
|
|
|
* the transaction version there.
|
2014-10-18 19:53:37 +02:00
|
|
|
*/
|
2017-09-06 20:25:03 +02:00
|
|
|
struct TxInUndoFormatter
|
2014-10-18 19:53:37 +02:00
|
|
|
{
|
|
|
|
template<typename Stream>
|
2017-09-06 20:25:03 +02:00
|
|
|
void Ser(Stream &s, const Coin& txout) {
|
2020-03-30 15:52:12 +02:00
|
|
|
::Serialize(s, VARINT(txout.nHeight * uint32_t{2} + txout.fCoinBase ));
|
2017-09-06 20:25:03 +02:00
|
|
|
if (txout.nHeight > 0) {
|
2017-06-02 00:47:58 +02:00
|
|
|
// Required to maintain compatibility with older undo format.
|
2016-11-09 12:32:57 +01:00
|
|
|
::Serialize(s, (unsigned char)0);
|
2017-06-02 00:47:58 +02:00
|
|
|
}
|
2021-05-27 16:59:09 +02:00
|
|
|
::Serialize(s, Using<TxOutCompression>(txout.out));
|
2014-10-18 19:53:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2017-09-06 20:25:03 +02:00
|
|
|
void Unser(Stream &s, Coin& txout) {
|
2020-03-30 15:52:12 +02:00
|
|
|
uint32_t nCode = 0;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, VARINT(nCode));
|
2020-03-30 15:52:12 +02:00
|
|
|
txout.nHeight = nCode >> 1;
|
2017-09-06 20:25:03 +02:00
|
|
|
txout.fCoinBase = nCode & 1;
|
|
|
|
if (txout.nHeight > 0) {
|
2017-06-02 00:47:58 +02:00
|
|
|
// Old versions stored the version number for the last spend of
|
|
|
|
// a transaction's outputs. Non-final spends were indicated with
|
|
|
|
// height = 0.
|
2020-12-17 13:20:31 +01:00
|
|
|
unsigned int nVersionDummy;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, VARINT(nVersionDummy));
|
2017-06-02 00:47:58 +02:00
|
|
|
}
|
2021-05-27 16:59:09 +02:00
|
|
|
::Unserialize(s, Using<TxOutCompression>(txout.out));
|
2014-10-18 19:53:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Undo information for a CTransaction */
|
|
|
|
class CTxUndo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// undo information for all txins
|
2017-06-02 00:47:58 +02:00
|
|
|
std::vector<Coin> vprevout;
|
2014-10-18 19:53:37 +02:00
|
|
|
|
2017-09-06 20:25:03 +02:00
|
|
|
SERIALIZE_METHODS(CTxUndo, obj) { READWRITE(Using<VectorFormatter<TxInUndoFormatter>>(obj.vprevout)); }
|
2014-10-18 19:53:37 +02:00
|
|
|
};
|
|
|
|
|
2014-10-27 14:42:49 +01:00
|
|
|
/** Undo information for a CBlock */
|
|
|
|
class CBlockUndo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<CTxUndo> vtxundo; // for all but the coinbase
|
|
|
|
|
2017-09-06 20:25:03 +02:00
|
|
|
SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); }
|
2014-10-27 14:42:49 +01:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_UNDO_H
|