Distinction between 2 approach of serialization within the Bitcoin Core


This reply, explains what are SERIALIZE_METHODS and DESERIALIZE_METHODS macros within the Bitcoin Core. They forestall duplicate code for implementing the serialization and deserialization logic. However I’ve seen some courses within the Bitcoin Core which do the serialization otherwise. Here is an instance:

/**
 * A UTXO entry.
 *
 * Serialized format:
 * - VARINT((coinbase ? 1 : 0) | (peak << 1))
 * - the non-spent CTxOut (by way of TxOutCompression)
 */
class Coin
{

    ...

    template<typename Stream>
    void Serialize(Stream &s) const {
        assert(!IsSpent());
        uint32_t code = nHeight * uint32_t{2} + fCoinBase;
        ::Serialize(s, VARINT(code));
        ::Serialize(s, Utilizing<TxOutCompression>(out));
    }

    template<typename Stream>
    void Unserialize(Stream &s) {
        uint32_t code = 0;
        ::Unserialize(s, VARINT(code));
        nHeight = code >> 1;
        fCoinBase = code & 1;
        ::Unserialize(s, Utilizing<TxOutCompression>(out));
    }

So, what is the distinction between these two approach of implementing serializing and deserializing strategies?

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here