0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

Convert undo.h to new serialization framework

This commit is contained in:
Pieter Wuille 2020-01-29 11:21:36 -08:00
parent 3cd8ab9d11
commit 3c94b0039d

View file

@ -13,58 +13,42 @@
#include <serialize.h> #include <serialize.h>
#include <version.h> #include <version.h>
/** Undo information for a CTxIn /** Formatter for undo information for a CTxIn
* *
* Contains the prevout's CTxOut being spent, and its metadata as well * Contains the prevout's CTxOut being spent, and its metadata as well
* (coinbase or not, height). The serialization contains a dummy value of * (coinbase or not, height). The serialization contains a dummy value of
* zero. This is compatible with older versions which expect to see * zero. This is compatible with older versions which expect to see
* the transaction version there. * the transaction version there.
*/ */
class TxInUndoSerializer struct TxInUndoFormatter
{ {
const Coin* txout;
public:
template<typename Stream> template<typename Stream>
void Serialize(Stream &s) const { void Ser(Stream &s, const Coin& txout) {
::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1u : 0u))); ::Serialize(s, VARINT(txout.nHeight * 2 + (txout.fCoinBase ? 1u : 0u)));
if (txout->nHeight > 0) { if (txout.nHeight > 0) {
// Required to maintain compatibility with older undo format. // Required to maintain compatibility with older undo format.
::Serialize(s, (unsigned char)0); ::Serialize(s, (unsigned char)0);
} }
::Serialize(s, Using<TxOutCompression>(REF(txout->out))); ::Serialize(s, Using<TxOutCompression>(txout.out));
} }
explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
};
class TxInUndoDeserializer
{
Coin* txout;
public:
template<typename Stream> template<typename Stream>
void Unserialize(Stream &s) { void Unser(Stream &s, Coin& txout) {
unsigned int nCode = 0; unsigned int nCode = 0;
::Unserialize(s, VARINT(nCode)); ::Unserialize(s, VARINT(nCode));
txout->nHeight = nCode / 2; txout.nHeight = nCode / 2;
txout->fCoinBase = nCode & 1; txout.fCoinBase = nCode & 1;
if (txout->nHeight > 0) { if (txout.nHeight > 0) {
// Old versions stored the version number for the last spend of // Old versions stored the version number for the last spend of
// a transaction's outputs. Non-final spends were indicated with // a transaction's outputs. Non-final spends were indicated with
// height = 0. // height = 0.
unsigned int nVersionDummy; unsigned int nVersionDummy;
::Unserialize(s, VARINT(nVersionDummy)); ::Unserialize(s, VARINT(nVersionDummy));
} }
::Unserialize(s, Using<TxOutCompression>(REF(txout->out))); ::Unserialize(s, Using<TxOutCompression>(txout.out));
} }
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
}; };
static const size_t MIN_TRANSACTION_INPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxIn(), PROTOCOL_VERSION);
static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_INPUT_WEIGHT;
/** Undo information for a CTransaction */ /** Undo information for a CTransaction */
class CTxUndo class CTxUndo
{ {
@ -72,29 +56,7 @@ public:
// undo information for all txins // undo information for all txins
std::vector<Coin> vprevout; std::vector<Coin> vprevout;
template <typename Stream> SERIALIZE_METHODS(CTxUndo, obj) { READWRITE(Using<VectorFormatter<TxInUndoFormatter>>(obj.vprevout)); }
void Serialize(Stream& s) const {
// TODO: avoid reimplementing vector serializer
uint64_t count = vprevout.size();
::Serialize(s, COMPACTSIZE(REF(count)));
for (const auto& prevout : vprevout) {
::Serialize(s, TxInUndoSerializer(&prevout));
}
}
template <typename Stream>
void Unserialize(Stream& s) {
// TODO: avoid reimplementing vector deserializer
uint64_t count = 0;
::Unserialize(s, COMPACTSIZE(count));
if (count > MAX_INPUTS_PER_BLOCK) {
throw std::ios_base::failure("Too many input undo records");
}
vprevout.resize(count);
for (auto& prevout : vprevout) {
::Unserialize(s, TxInUndoDeserializer(&prevout));
}
}
}; };
/** Undo information for a CBlock */ /** Undo information for a CBlock */
@ -103,12 +65,7 @@ class CBlockUndo
public: public:
std::vector<CTxUndo> vtxundo; // for all but the coinbase std::vector<CTxUndo> vtxundo; // for all but the coinbase
ADD_SERIALIZE_METHODS; SERIALIZE_METHODS(CBlockUndo, obj) { READWRITE(obj.vtxundo); }
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(vtxundo);
}
}; };
#endif // BITCOIN_UNDO_H #endif // BITCOIN_UNDO_H