LCOV - code coverage report
Current view: top level - src/primitives - transaction_identifier.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 25 25
Test Date: 2026-01-10 04:21:25 Functions: 100.0 % 3 3
Branches: 30.8 % 1420 438

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/license/mit.
       4                 :             : 
       5                 :             : #ifndef BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
       6                 :             : #define BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
       7                 :             : 
       8                 :             : #include <attributes.h>
       9                 :             : #include <uint256.h>
      10                 :             : #include <util/types.h>
      11                 :             : 
      12                 :             : #include <compare>
      13                 :             : #include <concepts>
      14                 :             : #include <tuple>
      15                 :             : #include <variant>
      16                 :             : 
      17                 :             : /** transaction_identifier represents the two canonical transaction identifier
      18                 :             :  * types (txid, wtxid).*/
      19                 :             : template <bool has_witness>
      20                 :             : class transaction_identifier
      21                 :             : {
      22                 :             :     uint256 m_wrapped;
      23                 :             : 
      24                 :             :     // Note: Use FromUint256 externally instead.
      25                 :    31968441 :     transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
      26                 :             : 
      27                 :  2935197220 :     constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
      28                 :             :     template <typename Other>
      29                 :             :     constexpr int Compare(const Other& other) const
      30                 :             :     {
      31                 :             :         static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
      32                 :             :         return 0;
      33                 :             :     }
      34                 :             : 
      35                 :             : public:
      36   [ +  +  +  -  :     1009346 :     transaction_identifier() : m_wrapped{} {}
           +  - ][ #  # ]
      37                 :             :     consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {}
      38                 :             : 
      39                 :             :     template <typename Other>
      40   [ -  +  -  +  :   503351478 :     bool operator==(const Other& other) const { return Compare(other) == 0; }
          +  +  +  +  -  
          -  +  -  +  +  
          -  -  -  +  +  
             +  +  +  -  
           - ][ +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          +  -  -  -  -  
                   +  + ]
           [ +  +  +  + ]
           [ +  -  +  +  
          +  +  +  +  +  
          +  -  +  +  -  
          +  +  -  +  +  
          +  +  +  +  +  
           -  - ][ +  +  
          +  +  +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  -  
          +  -  +  -  +  
             -  +  +  -  
           - ][ +  +  -  
          +  +  +  -  +  
           +  + ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      41                 :             :     template <typename Other>
      42   [ +  +  +  + ]:  1644018665 :     bool operator<(const Other& other) const { return Compare(other) < 0; }
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  +  
          +  +  -  -  -  
           - ][ -  -  -  
          -  -  -  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
           -  - ][ +  -  
          -  +  +  -  +  
          -  +  -  -  -  
          -  -  +  +  +  
          -  -  +  +  -  
          +  -  +  -  -  
          -  -  -  -  +  
          +  -  +  -  +  
          -  -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
          -  -  -  -  -  
          +  +  +  +  -  
          -  -  -  +  -  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  +  
          -  -  +  +  +  
          +  -  -  +  -  
          -  -  +  -  +  
          -  +  +  -  -  
           -  - ][ -  -  
          -  -  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  -  -  
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  +  
          +  +  +  +  +  
          +  +  +  -  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  -  +  +  
          +  +  +  +  +  
          +  -  -  -  -  
             -  -  +  + ]
           [ +  +  +  +  
           +  + ][ +  +  
          +  +  +  -  +  
          +  +  +  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  +  +  +  
          +  +  +  +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  +  -  -  -  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  +  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  +  -  -  +  
          -  -  +  +  -  
          -  -  -  -  +  
           - ][ +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  -  +  +  +  
          +  +  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          +  -  -  -  -  
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
      43                 :             : 
      44   [ +  -  #  #  :   972849029 :     const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ -  -  -  
          -  -  -  -  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  -  
             -  -  -  +  
                      - ]
      45   [ -  -  -  - ]:    16533296 :     static transaction_identifier FromUint256(const uint256& id) { return {id}; }
         [ +  + ][ +  -  
             +  -  +  - ]
      46                 :             : 
      47                 :             :     /** Wrapped `uint256` methods. */
      48         [ +  + ]:    50238860 :     constexpr bool IsNull() const { return m_wrapped.IsNull(); }
      49                 :      228350 :     constexpr void SetNull() { m_wrapped.SetNull(); }
      50                 :          48 :     static std::optional<transaction_identifier> FromHex(std::string_view hex)
      51                 :             :     {
      52         [ -  + ]:          48 :         auto u{uint256::FromHex(hex)};
      53         [ -  + ]:          48 :         if (!u) return std::nullopt;
      54                 :          48 :         return FromUint256(*u);
      55                 :             :     }
      56 [ +  - ][ +  -  :      192505 :     std::string GetHex() const { return m_wrapped.GetHex(); }
          +  -  -  -  +  
             -  +  -  -  
           - ][ -  -  -  
                -  +  - ]
           [ -  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
             #  #  #  #  
                      # ]
      57 [ +  - ][ +  -  :     4571561 :     std::string ToString() const { return m_wrapped.ToString(); }
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
          -  -  -  +  -  
          +  -  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
             -  -  -  -  
           - ][ +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
      58                 :             :     static constexpr auto size() { return decltype(m_wrapped)::size(); }
      59                 :             :     constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
      60                 :      590208 :     constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
      61                 :        2959 :     constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
      62                 : 12252610541 :     template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
      63                 :    15027637 :     template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
      64                 :             : };
      65                 :             : 
      66                 :             : /** Txid commits to all transaction fields except the witness. */
      67                 :             : using Txid = transaction_identifier<false>;
      68                 :             : /** Wtxid commits to all transaction fields including the witness. */
      69                 :             : using Wtxid = transaction_identifier<true>;
      70                 :             : 
      71                 :             : template <typename T>
      72                 :             : concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>;
      73                 :             : 
      74                 :             : class GenTxid : public std::variant<Txid, Wtxid>
      75                 :             : {
      76                 :             : public:
      77   [ +  -  +  - ]:     3032749 :     using variant::variant;
      78                 :             : 
      79   [ -  -  -  -  :      574357 :     bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); }
           +  + ][ -  +  
             #  #  #  # ]
      80                 :             : 
      81   [ +  +  -  +  :     5243418 :     const uint256& ToUint256() const LIFETIMEBOUND
          +  -  +  +  -  
           +  +  - ][ +  
          +  -  #  #  #  
          #  #  #  #  #  
                      # ]
      82                 :             :     {
      83   [ +  +  -  +  :   121580228 :         return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);
          +  -  +  +  -  
          +  +  -  +  +  
          -  +  +  -  +  
          +  -  +  +  -  
          +  +  -  +  +  
          -  +  +  -  +  
          +  -  +  +  -  
          +  +  -  +  +  
          -  +  +  +  +  
          -  +  +  -  +  
          +  +  +  -  +  
          +  +  +  -  +  
          +  -  +  +  +  
          +  -  +  +  -  
          +  +  +  +  -  
          +  +  -  +  +  
          +  +  -  +  +  
          -  +  +  +  +  
          -  +  +  -  +  
          +  +  +  -  +  
          -  +  +  -  +  
           +  - ][ +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  +  
          -  -  -  -  -  
             -  +  +  - ]
           [ +  +  -  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      84                 :             :     }
      85                 :             : 
      86                 :       62316 :     friend auto operator<=>(const GenTxid& a, const GenTxid& b)
      87                 :             :     {
      88                 :             :         // Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away.
      89   [ +  +  -  +  :      186948 :         return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256());
                   +  - ]
      90                 :             :     }
      91                 :             : };
      92                 :             : 
      93                 :             : #endif // BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
        

Generated by: LCOV version 2.0-1