Skip to content

Commit 2687b43

Browse files
committed
refactor: move address lookup to MnNetInfo::AddEntry()
1 parent f8adfab commit 2687b43

File tree

8 files changed

+77
-24
lines changed

8 files changed

+77
-24
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ BITCOIN_TESTS =\
113113
test/evo_deterministicmns_tests.cpp \
114114
test/evo_islock_tests.cpp \
115115
test/evo_mnhf_tests.cpp \
116+
test/evo_netinfo_tests.cpp \
116117
test/evo_simplifiedmns_tests.cpp \
117118
test/evo_trivialvalidation.cpp \
118119
test/evo_utils_tests.cpp \

src/evo/netinfo.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44

55
#include <evo/netinfo.h>
66

7-
NetInfoStatus MnNetInfo::AddEntry(const CService& service)
7+
#include <chainparams.h>
8+
#include <netbase.h>
9+
#include <span.h>
10+
11+
NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
812
{
9-
m_addr = service;
10-
return NetInfoStatus::Success;
13+
if (auto service = Lookup(input, /*portDefault=*/Params().GetDefaultPort(), /*fAllowLookup=*/false);
14+
service.has_value()) {
15+
m_addr = service.value();
16+
ASSERT_IF_DEBUG(m_addr != CService());
17+
return NetInfoStatus::Success;
18+
}
19+
return NetInfoStatus::BadInput;
1120
}

src/evo/netinfo.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@
1111
class CService;
1212

1313
enum NetInfoStatus : uint8_t {
14+
BadInput,
1415
Success
1516
};
1617

18+
constexpr std::string_view NISToString(const NetInfoStatus code)
19+
{
20+
switch (code) {
21+
case NetInfoStatus::BadInput:
22+
return "invalid address";
23+
case NetInfoStatus::Success:
24+
return "success";
25+
} // no default case, so the compiler can warn about missing cases
26+
assert(false);
27+
}
28+
1729
class MnNetInfo
1830
{
1931
private:
@@ -31,7 +43,7 @@ class MnNetInfo
3143
READWRITE(obj.m_addr);
3244
}
3345

34-
NetInfoStatus AddEntry(const CService& service);
46+
NetInfoStatus AddEntry(const std::string& service);
3547

3648
const CService& GetPrimary() const { return m_addr; }
3749
bool IsEmpty() const { return *this == MnNetInfo(); }

src/rpc/evo.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,8 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
683683
}
684684

685685
if (!request.params[paramIdx].get_str().empty()) {
686-
if (auto addr = Lookup(request.params[paramIdx].get_str(), Params().GetDefaultPort(), false); addr.has_value()) {
687-
CHECK_NONFATAL(ptx.netInfo.AddEntry(addr.value()) == NetInfoStatus::Success);
688-
} else {
689-
throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
686+
if (auto entryRet = ptx.netInfo.AddEntry(request.params[paramIdx].get_str()); entryRet != NetInfoStatus::Success) {
687+
throw std::runtime_error(strprintf("%s (%s)", NISToString(entryRet), request.params[paramIdx].get_str()));
690688
}
691689
}
692690

@@ -978,10 +976,8 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
978976

979977
ptx.nVersion = dmn->pdmnState->nVersion;
980978

981-
if (auto addr = Lookup(request.params[1].get_str().c_str(), Params().GetDefaultPort(), false); addr.has_value()) {
982-
CHECK_NONFATAL(ptx.netInfo.AddEntry(addr.value()) == NetInfoStatus::Success);
983-
} else {
984-
throw std::runtime_error(strprintf("invalid network address %s", request.params[1].get_str()));
979+
if (auto entryRet = ptx.netInfo.AddEntry(request.params[1].get_str()); entryRet != NetInfoStatus::Success) {
980+
throw std::runtime_error(strprintf("%s (%s)", NISToString(entryRet), request.params[1].get_str()));
985981
}
986982

987983
CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[2].get_str(), "operatorKey");

src/test/block_reward_reallocation_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static CMutableTransaction CreateProRegTx(const CChain& active_chain, const CTxM
117117
CProRegTx proTx;
118118
proTx.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme);
119119
proTx.collateralOutpoint.n = 0;
120-
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", port)), NetInfoStatus::Success);
120+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(strprintf("1.1.1.1:%d", port)), NetInfoStatus::Success);
121121
proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID();
122122
proTx.pubKeyOperator.Set(operatorKeyRet.GetPublicKey(), bls::bls_legacy_scheme.load());
123123
proTx.keyIDVoting = ownerKeyRet.GetPubKey().GetID();

src/test/evo_deterministicmns_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static CMutableTransaction CreateProRegTx(const CChain& active_chain, const CTxM
106106
CProRegTx proTx;
107107
proTx.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme);
108108
proTx.collateralOutpoint.n = 0;
109-
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", port)), NetInfoStatus::Success);
109+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(strprintf("1.1.1.1:%d", port)), NetInfoStatus::Success);
110110
proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID();
111111
proTx.pubKeyOperator.Set(operatorKeyRet.GetPublicKey(), bls::bls_legacy_scheme.load());
112112
proTx.keyIDVoting = ownerKeyRet.GetPubKey().GetID();
@@ -128,7 +128,7 @@ static CMutableTransaction CreateProUpServTx(const CChain& active_chain, const C
128128
CProUpServTx proTx;
129129
proTx.nVersion = CProUpServTx::GetMaxVersion(!bls::bls_legacy_scheme);
130130
proTx.proTxHash = proTxHash;
131-
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", port)), NetInfoStatus::Success);
131+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(strprintf("1.1.1.1:%d", port)), NetInfoStatus::Success);
132132
proTx.scriptOperatorPayout = scriptOperatorPayout;
133133

134134
CMutableTransaction tx;
@@ -634,7 +634,7 @@ void FuncTestMempoolReorg(TestChainSetup& setup)
634634

635635
CProRegTx payload;
636636
payload.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme);
637-
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("1.1.1.1", 1)), NetInfoStatus::Success);
637+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry("1.1.1.1:1"), NetInfoStatus::Success);
638638
payload.keyIDOwner = ownerKey.GetPubKey().GetID();
639639
payload.pubKeyOperator.Set(operatorKey.GetPublicKey(), bls::bls_legacy_scheme.load());
640640
payload.keyIDVoting = ownerKey.GetPubKey().GetID();
@@ -708,7 +708,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
708708
auto scriptPayout = GetScriptForDestination(PKHash(payoutKey.GetPubKey()));
709709

710710
CProRegTx payload;
711-
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("1.1.1.1", 2)), NetInfoStatus::Success);
711+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry("1.1.1.1:2"), NetInfoStatus::Success);
712712
payload.keyIDOwner = ownerKey.GetPubKey().GetID();
713713
payload.pubKeyOperator.Set(operatorKey.GetPublicKey(), bls::bls_legacy_scheme.load());
714714
payload.keyIDVoting = ownerKey.GetPubKey().GetID();
@@ -776,7 +776,7 @@ void FuncVerifyDB(TestChainSetup& setup)
776776

777777
CProRegTx payload;
778778
payload.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme);
779-
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("1.1.1.1", 1)), NetInfoStatus::Success);
779+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry("1.1.1.1:1"), NetInfoStatus::Success);
780780
payload.keyIDOwner = ownerKey.GetPubKey().GetID();
781781
payload.pubKeyOperator.Set(operatorKey.GetPublicKey(), bls::bls_legacy_scheme.load());
782782
payload.keyIDVoting = ownerKey.GetPubKey().GetID();

src/test/evo_netinfo_tests.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#include <test/util/setup_common.h>
6+
7+
#include <chainparams.h>
8+
#include <evo/netinfo.h>
9+
#include <netbase.h>
10+
#include <streams.h>
11+
12+
#include <boost/test/unit_test.hpp>
13+
14+
const std::vector<std::pair</*input=*/std::string, /*expected_ret=*/NetInfoStatus>> vals{
15+
// Address and port specified
16+
{"1.1.1.1:8888", NetInfoStatus::Success},
17+
// Address specified, port should default to default P2P core
18+
{"1.1.1.1", NetInfoStatus::Success},
19+
// Port greater than uint16_t max
20+
{"1.1.1.1:99999", NetInfoStatus::BadInput},
21+
// Domains are not allowed
22+
{"example.com:8888", NetInfoStatus::BadInput},
23+
// Incorrect IPv4 address
24+
{"1.1.1.256:8888", NetInfoStatus::BadInput},
25+
// Missing address
26+
{":8888", NetInfoStatus::BadInput},
27+
};
28+
29+
BOOST_FIXTURE_TEST_SUITE(evo_netinfo_tests, RegTestingSetup)
30+
31+
BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
32+
{
33+
// Validate AddEntry() rules enforcement
34+
for (const auto& [input, expected_ret] : vals) {
35+
MnNetInfo netInfo;
36+
BOOST_CHECK_EQUAL(netInfo.AddEntry(input), expected_ret);
37+
}
38+
}
39+
40+
BOOST_AUTO_TEST_SUITE_END()

src/test/evo_simplifiedmns_tests.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ BOOST_AUTO_TEST_CASE(simplifiedmns_merkleroots)
2222
smle.proRegTxHash.SetHex(strprintf("%064x", i));
2323
smle.confirmedHash.SetHex(strprintf("%064x", i));
2424

25-
std::string ip = strprintf("%d.%d.%d.%d", 0, 0, 0, i);
26-
if (auto service = Lookup(ip, i, false); service.has_value()) {
27-
BOOST_CHECK_EQUAL(smle.netInfo.AddEntry(service.value()), NetInfoStatus::Success);
28-
} else {
29-
BOOST_REQUIRE(false);
30-
}
25+
BOOST_CHECK_EQUAL(smle.netInfo.AddEntry(strprintf("%d.%d.%d.%d:%d", 0, 0, 0, i, i)), NetInfoStatus::Success);
3126

3227
std::vector<unsigned char> vecBytes{static_cast<unsigned char>(i)};
3328
vecBytes.resize(CBLSSecretKey::SerSize);

0 commit comments

Comments
 (0)