Skip to content

Commit e1783cb

Browse files
committed
refactor: remove direct access to MnNetInfo::addr
1 parent e868aff commit e1783cb

File tree

9 files changed

+36
-15
lines changed

9 files changed

+36
-15
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ libbitcoin_common_a_SOURCES = \
777777
core_write.cpp \
778778
deploymentinfo.cpp \
779779
evo/core_write.cpp \
780+
evo/netinfo.cpp \
780781
governance/common.cpp \
781782
init/common.cpp \
782783
key.cpp \

src/evo/deterministicmns.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
470470
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate collateralOutpoint=%s", __func__,
471471
dmn->proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort())));
472472
}
473-
if (dmn->pdmnState->netInfo.GetPrimary() != CService() && !AddUniqueProperty(*dmn, dmn->pdmnState->netInfo)) {
473+
if (!dmn->pdmnState->netInfo.IsEmpty() && !AddUniqueProperty(*dmn, dmn->pdmnState->netInfo)) {
474474
mnUniquePropertyMap = mnUniquePropertyMapSaved;
475475
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate address=%s", __func__,
476476
dmn->proTxHash.ToString(),
@@ -573,7 +573,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
573573
throw(std::runtime_error(strprintf("%s: Can't delete a masternode %s with a collateralOutpoint=%s", __func__,
574574
proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort())));
575575
}
576-
if (dmn->pdmnState->netInfo.GetPrimary() != CService() && !DeleteUniqueProperty(*dmn, dmn->pdmnState->netInfo)) {
576+
if (!dmn->pdmnState->netInfo.IsEmpty() && !DeleteUniqueProperty(*dmn, dmn->pdmnState->netInfo)) {
577577
mnUniquePropertyMap = mnUniquePropertyMapSaved;
578578
throw(std::runtime_error(strprintf("%s: Can't delete a masternode %s with a address=%s", __func__,
579579
proTxHash.ToString(), dmn->pdmnState->netInfo.GetPrimary().ToStringAddrPort())));
@@ -802,7 +802,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
802802

803803
auto dmnState = std::make_shared<CDeterministicMNState>(proTx);
804804
dmnState->nRegisteredHeight = nHeight;
805-
if (proTx.netInfo.GetPrimary() == CService()) {
805+
if (proTx.netInfo.IsEmpty()) {
806806
// start in banned pdmnState as we need to wait for a ProUpServTx
807807
dmnState->BanIfNotBanned(nHeight);
808808
}
@@ -1303,7 +1303,7 @@ bool CheckProRegTx(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl:
13031303

13041304
// It's allowed to set addr to 0, which will put the MN into PoSe-banned state and require a ProUpServTx to be issues later
13051305
// If any of both is set, it must be valid however
1306-
if (opt_ptx->netInfo.GetPrimary() != CService() && !CheckService(*opt_ptx, state)) {
1306+
if (!opt_ptx->netInfo.IsEmpty() && !CheckService(*opt_ptx, state)) {
13071307
// pass the state returned by the function above
13081308
return false;
13091309
}

src/evo/dmnstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class CDeterministicMNState
112112
{
113113
nVersion = ProTxVersion::LegacyBLS;
114114
pubKeyOperator = CBLSLazyPublicKey();
115-
netInfo.m_addr = CService();
115+
netInfo.Clear();
116116
scriptOperatorPayout = CScript();
117117
nRevocationReason = CProUpRevTx::REASON_NOT_SPECIFIED;
118118
platformNodeID = uint160();

src/evo/netinfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 <evo/netinfo.h>
6+
7+
NetInfoStatus MnNetInfo::AddEntry(const CService& service)
8+
{
9+
m_addr = service;
10+
return NetInfoStatus::Success;
11+
}

src/evo/netinfo.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010

1111
class CService;
1212

13+
enum class NetInfoStatus : uint8_t {
14+
Success
15+
};
16+
1317
class MnNetInfo
1418
{
15-
public:
19+
private:
1620
CService m_addr{};
1721

1822
public:
@@ -27,7 +31,12 @@ class MnNetInfo
2731
READWRITE(obj.m_addr);
2832
}
2933

34+
NetInfoStatus AddEntry(const CService& service);
35+
3036
const CService& GetPrimary() const { return m_addr; }
37+
bool IsEmpty() const { return *this == MnNetInfo(); }
38+
39+
void Clear() { m_addr = CService(); }
3140
};
3241

3342
#endif // BITCOIN_EVO_NETINFO_H

src/rpc/evo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
684684

685685
if (!request.params[paramIdx].get_str().empty()) {
686686
if (auto addr = Lookup(request.params[paramIdx].get_str(), Params().GetDefaultPort(), false); addr.has_value()) {
687-
ptx.netInfo.m_addr = addr.value();
687+
CHECK_NONFATAL(ptx.netInfo.AddEntry(addr.value()) == NetInfoStatus::Success);
688688
} else {
689689
throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
690690
}
@@ -979,7 +979,7 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
979979
ptx.nVersion = dmn->pdmnState->nVersion;
980980

981981
if (auto addr = Lookup(request.params[1].get_str().c_str(), Params().GetDefaultPort(), false); addr.has_value()) {
982-
ptx.netInfo.m_addr = addr.value();
982+
CHECK_NONFATAL(ptx.netInfo.AddEntry(addr.value()) == NetInfoStatus::Success);
983983
} else {
984984
throw std::runtime_error(strprintf("invalid network address %s", request.params[1].get_str()));
985985
}

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-
proTx.netInfo.m_addr = LookupNumeric("1.1.1.1", port);
120+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", 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-
proTx.netInfo.m_addr = LookupNumeric("1.1.1.1", port);
109+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", 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-
proTx.netInfo.m_addr = LookupNumeric("1.1.1.1", port);
131+
BOOST_CHECK_EQUAL(proTx.netInfo.AddEntry(LookupNumeric("1.1.1.1", 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-
payload.netInfo.m_addr = LookupNumeric("1.1.1.1", 1);
637+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("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-
payload.netInfo.m_addr = LookupNumeric("1.1.1.1", 2);
711+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("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-
payload.netInfo.m_addr = LookupNumeric("1.1.1.1", 1);
779+
BOOST_CHECK_EQUAL(payload.netInfo.AddEntry(LookupNumeric("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_simplifiedmns_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(simplifiedmns_merkleroots)
2424

2525
std::string ip = strprintf("%d.%d.%d.%d", 0, 0, 0, i);
2626
if (auto service = Lookup(ip, i, false); service.has_value()) {
27-
smle.netInfo.m_addr = service.value();
27+
BOOST_CHECK_EQUAL(smle.netInfo.AddEntry(service.value()), NetInfoStatus::Success);
2828
} else {
2929
BOOST_REQUIRE(false);
3030
}

0 commit comments

Comments
 (0)