Bitcoin-Core

比特幣核心中的 SERIALIZE_METHODS 宏是什麼?

  • July 5, 2022
* Implement the Serialize and Unserialize methods by delegating to a single templated
* static method that takes the to-be-(de)serialized object as a parameter. This approach
* has the advantage that the constness of the object becomes a template parameter, and
* thus allows a single implementation that sees the object as const for serializing
* and non-const for deserializing, without casts.
*/
#define SERIALIZE_METHODS(cls, obj)                                                 \
   template<typename Stream>                                                       \
   void Serialize(Stream& s) const                                                 \
   {                                                                               \
       static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
       Ser(s, *this);                                                              \
   }                                                                               \
   template<typename Stream>                                                       \
   void Unserialize(Stream& s)                                                     \
   {                                                                               \
       static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
       Unser(s, *this);                                                            \
   }                                                                               \
   FORMATTER_METHODS(cls, obj)

這個宏在比特幣核心中經常使用。例如CBlockFileInfo

class CBlockFileInfo
{
public:
   unsigned int nBlocks;      //!< number of blocks stored in file
   unsigned int nSize;        //!< number of used bytes of block file
   unsigned int nUndoSize;    //!< number of used bytes in the undo file
   unsigned int nHeightFirst; //!< lowest height of block in file
   unsigned int nHeightLast;  //!< highest height of block in file
   uint64_t nTimeFirst;       //!< earliest time of block in file
   uint64_t nTimeLast;        //!< latest time of block in file

   SERIALIZE_METHODS(CBlockFileInfo, obj)
   {
       READWRITE(VARINT(obj.nBlocks));
       READWRITE(VARINT(obj.nSize));
       READWRITE(VARINT(obj.nUndoSize));
       READWRITE(VARINT(obj.nHeightFirst));
       READWRITE(VARINT(obj.nHeightLast));
       READWRITE(VARINT(obj.nTimeFirst));
       READWRITE(VARINT(obj.nTimeLast));
   }

有人可以用簡單的語言解釋這個宏實際上做了什麼嗎?我試圖通過閱讀上面的評論及其實現來弄清楚它的作用,但沒有理解任何有用的東西。

它們是 Bitcoin Core 的自定義序列化框架的一部分。

序列化是將對象轉換為字節數組的過程,目的是將它們儲存在磁碟上、通過網路傳輸它們或計算它們的雜湊值。

反序列化是相反的過程,其中對像是從字節數組中重建的。

它是使用許多類、輔助函式和宏來實現的,在serialize.h.

具體來說,您引用的程式碼CBlockFileInfo僅意味著:

  • 要序列化CBlockFileInfo對象obj,請執行以下操作:

    • obj.nBlocks以 VARINT 格式序列化
    • obj.nSize以 VARINT 格式序列化
    • obj.nTimeLast以 VARINT 格式序列化
  • 要反序列化,請執行相同操作(但使用反序列化而不是序列化)。

這種方法可以在類中對如何序列化/反序列化進行單一定義,而無需多次複製程式碼。

引用自:https://bitcoin.stackexchange.com/questions/114352