2014-10-18 19:53:37 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-01-27 12:05:25 +01:00
|
|
|
// Copyright (c) 2009-2014 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
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <compressor.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <serialize.h>
|
2014-10-18 19:53:37 +02:00
|
|
|
|
|
|
|
/** Undo information for a CTxIn
|
|
|
|
*
|
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
|
|
|
|
* zero. This is be compatible with older versions which expect to see
|
|
|
|
* the transaction version there.
|
2014-10-18 19:53:37 +02:00
|
|
|
*/
|
2017-06-02 00:47:58 +02:00
|
|
|
class TxInUndoSerializer
|
2014-10-18 19:53:37 +02:00
|
|
|
{
|
2017-06-02 00:47:58 +02:00
|
|
|
const Coin* txout;
|
2014-10-18 19:53:37 +02:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
public:
|
2014-10-18 19:53:37 +02:00
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Serialize(Stream &s) const {
|
|
|
|
::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1 : 0)));
|
2017-06-02 00:47:58 +02:00
|
|
|
if (txout->nHeight > 0) {
|
|
|
|
// 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
|
|
|
}
|
2016-11-09 12:32:57 +01:00
|
|
|
::Serialize(s, CTxOutCompressor(REF(txout->out)));
|
2014-10-18 19:53:37 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
|
2017-06-02 00:47:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class TxInUndoDeserializer
|
|
|
|
{
|
|
|
|
Coin* txout;
|
|
|
|
|
|
|
|
public:
|
2014-10-18 19:53:37 +02:00
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Unserialize(Stream &s) {
|
2014-10-18 19:53:37 +02:00
|
|
|
unsigned int nCode = 0;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, VARINT(nCode));
|
2017-06-02 00:47:58 +02:00
|
|
|
txout->nHeight = nCode / 2;
|
|
|
|
txout->fCoinBase = nCode & 1;
|
|
|
|
if (txout->nHeight > 0) {
|
|
|
|
// Old versions stored the version number for the last spend of
|
|
|
|
// a transaction's outputs. Non-final spends were indicated with
|
|
|
|
// height = 0.
|
|
|
|
int nVersionDummy;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, VARINT(nVersionDummy));
|
2017-06-02 00:47:58 +02:00
|
|
|
}
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))));
|
2014-10-18 19:53:37 +02:00
|
|
|
}
|
2017-06-02 00:47:58 +02:00
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
|
2014-10-18 19:53:37 +02:00
|
|
|
};
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
static const size_t MAX_INPUTS_PER_BLOCK = MaxBlockSize(true) / ::GetSerializeSize(CTxIn(), SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
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-06-02 00:47:58 +02:00
|
|
|
template <typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Serialize(Stream& s) const {
|
2017-06-02 00:47:58 +02:00
|
|
|
// TODO: avoid reimplementing vector serializer
|
|
|
|
uint64_t count = vprevout.size();
|
2016-11-09 12:32:57 +01:00
|
|
|
::Serialize(s, COMPACTSIZE(REF(count)));
|
2017-06-02 00:47:58 +02:00
|
|
|
for (const auto& prevout : vprevout) {
|
2016-11-09 12:32:57 +01:00
|
|
|
::Serialize(s, REF(TxInUndoSerializer(&prevout)));
|
2017-06-02 00:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-18 19:53:37 +02:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
template <typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Unserialize(Stream& s) {
|
2017-06-02 00:47:58 +02:00
|
|
|
// TODO: avoid reimplementing vector deserializer
|
|
|
|
uint64_t count = 0;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, COMPACTSIZE(count));
|
2017-06-02 00:47:58 +02:00
|
|
|
if (count > MAX_INPUTS_PER_BLOCK) {
|
|
|
|
throw std::ios_base::failure("Too many input undo records");
|
|
|
|
}
|
|
|
|
vprevout.resize(count);
|
|
|
|
for (auto& prevout : vprevout) {
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
|
2017-06-02 00:47:58 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-11-09 12:32:57 +01:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2014-10-27 14:42:49 +01:00
|
|
|
READWRITE(vtxundo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_UNDO_H
|