LCOV - code coverage report
Current view: top level - src/rpc - evo_util.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 25 25 100.0 %
Date: 2026-06-25 07:23:43 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2025 The Dash Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_RPC_EVO_UTIL_H
       6             : #define BITCOIN_RPC_EVO_UTIL_H
       7             : 
       8             : #include <netaddress.h>
       9             : #include <util/check.h>
      10             : 
      11             : #include <evo/dmn_types.h>
      12             : #include <evo/netinfo.h>
      13             : 
      14             : #include <cstdint>
      15             : #include <type_traits>
      16             : 
      17             : #include <univalue.h>
      18             : 
      19             : class CSimplifiedMNListEntry;
      20             : 
      21             : /** Process setting (legacy) Core network information field based on ProTx version */
      22             : template <typename ProTx>
      23             : void ProcessNetInfoCore(ProTx& ptx, const UniValue& input, const bool optional);
      24             : 
      25             : /** Process setting (legacy) Platform network information fields based on ProTx version */
      26             : template <typename ProTx>
      27             : void ProcessNetInfoPlatform(ProTx& ptx, const UniValue& input_p2p, const UniValue& input_http, const bool optional);
      28             : 
      29             : /** Reads network info reporting and appends data from legacy fields if applicable */
      30             : template <typename Obj>
      31       14841 : UniValue GetNetInfoWithLegacyFields(const Obj& obj, const MnType& type)
      32             : {
      33       14841 :     UniValue ret{CHECK_NONFATAL(obj.netInfo)->ToJson()};
      34             : 
      35             :     // Nothing to do here if not EvoNode, netInfo is empty or netInfo is capable of natively
      36             :     // storing Platform fields
      37       14841 :     if (type != MnType::Evo || obj.netInfo->IsEmpty() || obj.netInfo->CanStorePlatform()) return ret;
      38             : 
      39         672 :     CNetAddr addr{obj.netInfo->GetPrimary()};
      40        1344 :     ret.pushKV(PurposeToString(NetInfoPurpose::PLATFORM_HTTPS).data(),
      41         672 :                ArrFromService(CService(addr, obj.platformHTTPPort)));
      42             : 
      43             :     if constexpr (!std::is_same_v<std::decay_t<Obj>, CSimplifiedMNListEntry>) {
      44             :         // CSimplifiedMNListEntry doesn't store platformP2PPort
      45        1296 :         ret.pushKV(PurposeToString(NetInfoPurpose::PLATFORM_P2P).data(),
      46         648 :                    ArrFromService(CService(addr, obj.platformP2PPort)));
      47             :     }
      48             : 
      49         672 :     return ret;
      50       14841 : }
      51             : 
      52             : /** Returns platform port based on purpose and network info version */
      53             : template <bool is_p2p, typename Obj>
      54          62 : int32_t GetPlatformPort(const Obj& obj)
      55             : {
      56             :     // Currently, there is nothing that prevents PLATFORM_{HTTPS,P2P} to be registered to
      57             :     // an addr *different* from the primary addr for CORE_P2P. This breaks the assumptions
      58             :     // under which platform{HTTP,P2P}Port operate under (i.e. all three are hosted with the
      59             :     // same addr).
      60             :     //
      61             :     // TODO: Introduce restrictions that enforce this assumption *until* we remove legacy
      62             :     //       fields for good.
      63             :     //
      64          62 :     bool is_legacy{!(CHECK_NONFATAL(obj.netInfo)->CanStorePlatform())};
      65             :     if constexpr (is_p2p) {
      66             :         static_assert(!std::is_same_v<std::decay_t<Obj>, CSimplifiedMNListEntry>,
      67             :                       "CSimplifiedMNListEntry doesn't have platformP2PPort");
      68          30 :         if (is_legacy) {
      69           8 :             return obj.platformP2PPort;
      70             :         }
      71          22 :         if (obj.netInfo->IsEmpty()) {
      72           6 :             return -1; // Blank entry, nothing to report
      73             :         }
      74          16 :         CHECK_NONFATAL(obj.netInfo->HasEntries(NetInfoPurpose::PLATFORM_P2P));
      75          16 :         return obj.netInfo->GetEntries(NetInfoPurpose::PLATFORM_P2P)[0].GetPort();
      76             :     } else {
      77          32 :         if (is_legacy) {
      78          10 :             return obj.platformHTTPPort;
      79             :         }
      80          22 :         if (obj.netInfo->IsEmpty()) {
      81           6 :             return -1; // Blank entry, nothing to report
      82             :         }
      83          16 :         CHECK_NONFATAL(obj.netInfo->HasEntries(NetInfoPurpose::PLATFORM_HTTPS));
      84          16 :         return obj.netInfo->GetEntries(NetInfoPurpose::PLATFORM_HTTPS)[0].GetPort();
      85             :     }
      86          62 : }
      87             : 
      88             : #endif // BITCOIN_RPC_EVO_UTIL_H

Generated by: LCOV version 1.16