From ff9d484876ae2cc9403a0cc079b72bbf28404783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Mon, 9 Dec 2024 13:30:04 +0100 Subject: [PATCH] coins,refactor: Unify Coin Serialization styles --- src/coins.h | 4 ++-- src/kernel/coinstats.cpp | 2 +- src/undo.h | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/coins.h b/src/coins.h index 0899e4c7360..a4069b43e6e 100644 --- a/src/coins.h +++ b/src/coins.h @@ -26,7 +26,7 @@ * A UTXO entry. * * Serialized format: - * - VARINT((coinbase ? 1 : 0) | (height << 1)) + * - VARINT((height << 1) | (coinbase ? 1 : 0)) * - the non-spent CTxOut (via TxOutCompression) */ class Coin @@ -61,7 +61,7 @@ public: template void Serialize(Stream &s) const { assert(!IsSpent()); - uint32_t code = nHeight * uint32_t{2} + fCoinBase; + uint32_t code{static_cast(nHeight << 1) | fCoinBase}; ::Serialize(s, VARINT(code)); ::Serialize(s, Using(out)); } diff --git a/src/kernel/coinstats.cpp b/src/kernel/coinstats.cpp index 81c496ab342..03ae6b100a3 100644 --- a/src/kernel/coinstats.cpp +++ b/src/kernel/coinstats.cpp @@ -51,7 +51,7 @@ template static void TxOutSer(T& ss, const COutPoint& outpoint, const Coin& coin) { ss << outpoint; - ss << static_cast((coin.nHeight << 1) + coin.fCoinBase); + ss << (static_cast(coin.nHeight << 1) | coin.fCoinBase); ss << coin.out; } diff --git a/src/undo.h b/src/undo.h index 1fb9ac06880..f66ea29410d 100644 --- a/src/undo.h +++ b/src/undo.h @@ -23,7 +23,8 @@ struct TxInUndoFormatter { template void Ser(Stream &s, const Coin& txout) { - ::Serialize(s, VARINT(txout.nHeight * uint32_t{2} + txout.fCoinBase )); + uint32_t nCode{static_cast(txout.nHeight << 1) | txout.fCoinBase}; + ::Serialize(s, VARINT(nCode)); if (txout.nHeight > 0) { // Required to maintain compatibility with older undo format. ::Serialize(s, (unsigned char)0);