Skip to content

Commit 4d878eb

Browse files
committed
evo: allow swapping MnNetInfo with a differing underlying impl
1 parent 4db135b commit 4d878eb

24 files changed

+197
-86
lines changed

src/coinjoin/client.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, CChainState& active_cha
186186
if (!m_mn_sync.IsBlockchainSynced()) return;
187187

188188
if (!mixingMasternode) return;
189-
if (mixingMasternode->pdmnState->netInfo.GetPrimary() != peer.addr) return;
189+
if (mixingMasternode->pdmnState->netInfo->GetPrimary() != peer.addr) return;
190190

191191
if (msg_type == NetMsgType::DSSTATUSUPDATE) {
192192
CCoinJoinStatusUpdate psssup;
@@ -1106,7 +1106,7 @@ bool CCoinJoinClientSession::JoinExistingQueue(CAmount nBalanceNeedsAnonymized,
11061106

11071107
m_clientman.AddUsedMasternode(dsq.masternodeOutpoint);
11081108

1109-
if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->netInfo.GetPrimary())) {
1109+
if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->netInfo->GetPrimary())) {
11101110
WalletCJLogPrint(m_wallet, /* Continued */
11111111
"CCoinJoinClientSession::JoinExistingQueue -- skipping connection, masternode=%s\n", dmn->proTxHash.ToString());
11121112
continue;
@@ -1178,7 +1178,7 @@ bool CCoinJoinClientSession::StartNewQueue(CAmount nBalanceNeedsAnonymized, CCon
11781178
continue;
11791179
}
11801180

1181-
if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->netInfo.GetPrimary())) {
1181+
if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->netInfo->GetPrimary())) {
11821182
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::StartNewQueue -- skipping connection, masternode=%s\n",
11831183
dmn->proTxHash.ToString());
11841184
nTries++;
@@ -1218,7 +1218,7 @@ bool CCoinJoinClientSession::ProcessPendingDsaRequest(CConnman& connman)
12181218

12191219
CService mn_addr;
12201220
if (auto dmn = m_dmnman.GetListAtChainTip().GetMN(pendingDsaRequest.GetProTxHash())) {
1221-
mn_addr = Assert(dmn->pdmnState)->netInfo.GetPrimary();
1221+
mn_addr = Assert(dmn->pdmnState)->netInfo->GetPrimary();
12221222
} else {
12231223
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- cannot find address to connect, masternode=%s\n", __func__,
12241224
pendingDsaRequest.GetProTxHash().ToString());
@@ -1820,7 +1820,7 @@ void CCoinJoinClientSession::RelayIn(const CCoinJoinEntry& entry, CConnman& conn
18201820
{
18211821
if (!mixingMasternode) return;
18221822

1823-
connman.ForNode(mixingMasternode->pdmnState->netInfo.GetPrimary(), [&entry, &connman, this](CNode* pnode) {
1823+
connman.ForNode(mixingMasternode->pdmnState->netInfo->GetPrimary(), [&entry, &connman, this](CNode* pnode) {
18241824
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::RelayIn -- found master, relaying message to %s\n",
18251825
pnode->addr.ToStringAddrPort());
18261826
CNetMsgMaker msgMaker(pnode->GetCommonVersion());
@@ -1876,7 +1876,7 @@ void CCoinJoinClientSession::GetJsonInfo(UniValue& obj) const
18761876
assert(mixingMasternode->pdmnState);
18771877
obj.pushKV("protxhash", mixingMasternode->proTxHash.ToString());
18781878
obj.pushKV("outpoint", mixingMasternode->collateralOutpoint.ToStringShort());
1879-
obj.pushKV("service", mixingMasternode->pdmnState->netInfo.GetPrimary().ToStringAddrPort());
1879+
obj.pushKV("service", mixingMasternode->pdmnState->netInfo->GetPrimary().ToStringAddrPort());
18801880
}
18811881
obj.pushKV("denomination", ValueFromAmount(CoinJoin::DenominationToAmount(nSessionDenom)));
18821882
obj.pushKV("state", GetStateString());

src/evo/core_write.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
ret.pushKV("type", ToUnderlying(nType));
6868
ret.pushKV("collateralHash", collateralOutpoint.hash.ToString());
6969
ret.pushKV("collateralIndex", (int)collateralOutpoint.n);
70-
ret.pushKV("service", netInfo.GetPrimary().ToStringAddrPort());
70+
ret.pushKV("service", netInfo->GetPrimary().ToStringAddrPort());
7171
ret.pushKV("ownerAddress", EncodeDestination(PKHash(keyIDOwner)));
7272
ret.pushKV("votingAddress", EncodeDestination(PKHash(keyIDVoting)));
7373
if (CTxDestination dest; ExtractDestination(scriptPayout, dest)) {
@@ -114,7 +114,7 @@
114114
ret.pushKV("version", nVersion);
115115
ret.pushKV("type", ToUnderlying(nType));
116116
ret.pushKV("proTxHash", proTxHash.ToString());
117-
ret.pushKV("service", netInfo.GetPrimary().ToStringAddrPort());
117+
ret.pushKV("service", netInfo->GetPrimary().ToStringAddrPort());
118118
if (CTxDestination dest; ExtractDestination(scriptOperatorPayout, dest)) {
119119
ret.pushKV("operatorPayoutAddress", EncodeDestination(dest));
120120
}

src/evo/deterministicmns.cpp

Lines changed: 14 additions & 13 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-
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo.GetEntries()) {
473+
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo->GetEntries()) {
474474
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
475475
const CService& service{service_opt.value()};
476476
if (!AddUniqueProperty(*dmn, service)) {
@@ -524,7 +524,7 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
524524
// We track each individual entry in netInfo as opposed to netInfo itself (preventing us from
525525
// using UpdateUniqueProperty()), so we need to successfully purge all old entries and insert
526526
// new entries to successfully update.
527-
for (const NetInfoEntry& old_entry : oldState->netInfo.GetEntries()) {
527+
for (const NetInfoEntry& old_entry : oldState->netInfo->GetEntries()) {
528528
if (const auto& service_opt{old_entry.GetAddrPort()}; service_opt.has_value()) {
529529
const CService& service{service_opt.value()};
530530
if (!DeleteUniqueProperty(*dmn, service)) {
@@ -534,7 +534,7 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
534534
return strprintf("invalid address");
535535
}
536536
}
537-
for (const NetInfoEntry& new_entry : pdmnState->netInfo.GetEntries()) {
537+
for (const NetInfoEntry& new_entry : pdmnState->netInfo->GetEntries()) {
538538
if (const auto& service_opt{new_entry.GetAddrPort()}; service_opt.has_value()) {
539539
const CService& service{service_opt.value()};
540540
if (!AddUniqueProperty(*dmn, service)) {
@@ -607,7 +607,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
607607
throw(std::runtime_error(strprintf("%s: Can't delete a masternode %s with a collateralOutpoint=%s", __func__,
608608
proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort())));
609609
}
610-
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo.GetEntries()) {
610+
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo->GetEntries()) {
611611
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
612612
const CService& service{service_opt.value()};
613613
if (!DeleteUniqueProperty(*dmn, service)) {
@@ -833,7 +833,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
833833
}
834834
}
835835

836-
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
836+
for (const NetInfoEntry& entry : proTx.netInfo->GetEntries()) {
837837
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
838838
const CService& service{service_opt.value()};
839839
if (newList.HasUniqueProperty(service)) {
@@ -851,7 +851,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
851851

852852
auto dmnState = std::make_shared<CDeterministicMNState>(proTx);
853853
dmnState->nRegisteredHeight = nHeight;
854-
if (proTx.netInfo.IsEmpty()) {
854+
if (proTx.netInfo->IsEmpty()) {
855855
// start in banned pdmnState as we need to wait for a ProUpServTx
856856
dmnState->BanIfNotBanned(nHeight);
857857
}
@@ -869,7 +869,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
869869
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-payload");
870870
}
871871

872-
for (const NetInfoEntry& entry : opt_proTx->netInfo.GetEntries()) {
872+
for (const NetInfoEntry& entry : opt_proTx->netInfo->GetEntries()) {
873873
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
874874
const CService& service{service_opt.value()};
875875
if (newList.HasUniqueProperty(service) &&
@@ -934,6 +934,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
934934
newState->BanIfNotBanned(nHeight);
935935
// we update pubKeyOperator here, make sure state version matches
936936
newState->nVersion = opt_proTx->nVersion;
937+
newState->netInfo = MakeNetInfo(*newState);
937938
newState->pubKeyOperator = opt_proTx->pubKeyOperator;
938939
}
939940
newState->keyIDVoting = opt_proTx->keyIDVoting;
@@ -1236,7 +1237,7 @@ void CDeterministicMNManager::CleanupCache(int nHeight)
12361237
template <typename ProTx>
12371238
static bool CheckService(const ProTx& proTx, TxValidationState& state)
12381239
{
1239-
switch (proTx.netInfo.Validate()) {
1240+
switch (proTx.netInfo->Validate()) {
12401241
case NetInfoStatus::BadAddress:
12411242
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-addr");
12421243
case NetInfoStatus::BadPort:
@@ -1288,8 +1289,8 @@ static bool CheckPlatformFields(const ProTx& proTx, TxValidationState& state)
12881289
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-platform-http-port");
12891290
}
12901291

1291-
if (proTx.platformP2PPort == proTx.platformHTTPPort || proTx.platformP2PPort == proTx.netInfo.GetPrimary().GetPort() ||
1292-
proTx.platformHTTPPort == proTx.netInfo.GetPrimary().GetPort()) {
1292+
if (proTx.platformP2PPort == proTx.platformHTTPPort || proTx.platformP2PPort == proTx.netInfo->GetPrimary().GetPort() ||
1293+
proTx.platformHTTPPort == proTx.netInfo->GetPrimary().GetPort()) {
12931294
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-platform-dup-ports");
12941295
}
12951296

@@ -1354,7 +1355,7 @@ bool CheckProRegTx(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl:
13541355

13551356
// 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
13561357
// If any of both is set, it must be valid however
1357-
if (!opt_ptx->netInfo.IsEmpty() && !CheckService(*opt_ptx, state)) {
1358+
if (!opt_ptx->netInfo->IsEmpty() && !CheckService(*opt_ptx, state)) {
13581359
// pass the state returned by the function above
13591360
return false;
13601361
}
@@ -1414,7 +1415,7 @@ bool CheckProRegTx(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl:
14141415
auto mnList = dmnman.GetListForBlock(pindexPrev);
14151416

14161417
// only allow reusing of addresses when it's for the same collateral (which replaces the old MN)
1417-
for (const NetInfoEntry& entry : opt_ptx->netInfo.GetEntries()) {
1418+
for (const NetInfoEntry& entry : opt_ptx->netInfo->GetEntries()) {
14181419
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
14191420
const CService& service{service_opt.value()};
14201421
if (mnList.HasUniqueProperty(service) &&
@@ -1492,7 +1493,7 @@ bool CheckProUpServTx(CDeterministicMNManager& dmnman, const CTransaction& tx, g
14921493
}
14931494

14941495
// don't allow updating to addresses already used by other MNs
1495-
for (const NetInfoEntry& entry : opt_ptx->netInfo.GetEntries()) {
1496+
for (const NetInfoEntry& entry : opt_ptx->netInfo->GetEntries()) {
14961497
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
14971498
const CService& service{service_opt.value()};
14981499
if (mnList.HasUniqueProperty(service) && mnList.GetUniquePropertyMN(service)->proTxHash != opt_ptx->proTxHash) {

src/evo/deterministicmns.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ class CDeterministicMNList
399399
DMNL_NO_TEMPLATE(CBLSPublicKey);
400400
DMNL_NO_TEMPLATE(MnNetInfo);
401401
DMNL_NO_TEMPLATE(NetInfoEntry);
402+
DMNL_NO_TEMPLATE(NetInfoInterface);
403+
DMNL_NO_TEMPLATE(std::shared_ptr<NetInfoInterface>);
402404
#undef DMNL_NO_TEMPLATE
403405
return ::SerializeHash(v);
404406
}

src/evo/dmnstate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ std::string CDeterministicMNState::ToString() const
3232
" %s",
3333
nVersion, nRegisteredHeight, nLastPaidHeight, nPoSePenalty, nPoSeRevivedHeight, nPoSeBanHeight,
3434
nRevocationReason, EncodeDestination(PKHash(keyIDOwner)), pubKeyOperator.ToString(),
35-
EncodeDestination(PKHash(keyIDVoting)), payoutAddress, operatorPayoutAddress, netInfo.ToString());
35+
EncodeDestination(PKHash(keyIDVoting)), payoutAddress, operatorPayoutAddress, netInfo->ToString());
3636
}
3737

3838
UniValue CDeterministicMNState::ToJson(MnType nType) const
3939
{
4040
UniValue obj(UniValue::VOBJ);
4141
obj.pushKV("version", nVersion);
42-
obj.pushKV("service", netInfo.GetPrimary().ToStringAddrPort());
42+
obj.pushKV("service", netInfo->GetPrimary().ToStringAddrPort());
4343
obj.pushKV("registeredHeight", nRegisteredHeight);
4444
obj.pushKV("lastPaidHeight", nLastPaidHeight);
4545
obj.pushKV("consecutivePayments", nConsecutivePayments);
@@ -73,7 +73,7 @@ UniValue CDeterministicMNStateDiff::ToJson(MnType nType) const
7373
obj.pushKV("version", state.nVersion);
7474
}
7575
if (fields & Field_netInfo) {
76-
obj.pushKV("service", state.netInfo.GetPrimary().ToStringAddrPort());
76+
obj.pushKV("service", state.netInfo->GetPrimary().ToStringAddrPort());
7777
}
7878
if (fields & Field_nRegisteredHeight) {
7979
obj.pushKV("registeredHeight", state.nRegisteredHeight);

src/evo/dmnstate.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CDeterministicMNState
5454
CKeyID keyIDOwner;
5555
CBLSLazyPublicKey pubKeyOperator;
5656
CKeyID keyIDVoting;
57-
MnNetInfo netInfo;
57+
std::shared_ptr<NetInfoInterface> netInfo{nullptr};
5858
CScript scriptPayout;
5959
CScript scriptOperatorPayout;
6060

@@ -100,7 +100,7 @@ class CDeterministicMNState
100100
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), obj.nVersion == ProTxVersion::LegacyBLS));
101101
READWRITE(
102102
obj.keyIDVoting,
103-
obj.netInfo,
103+
NetInfoSerWrapper(const_cast<std::shared_ptr<NetInfoInterface>&>(obj.netInfo)),
104104
obj.scriptPayout,
105105
obj.scriptOperatorPayout,
106106
obj.platformNodeID,
@@ -112,7 +112,7 @@ class CDeterministicMNState
112112
{
113113
nVersion = ProTxVersion::LegacyBLS;
114114
pubKeyOperator = CBLSLazyPublicKey();
115-
netInfo.Clear();
115+
netInfo = MakeNetInfo(*this);
116116
scriptOperatorPayout = CScript();
117117
nRevocationReason = CProUpRevTx::REASON_NOT_SPECIFIED;
118118
platformNodeID = uint160();
@@ -246,6 +246,10 @@ class CDeterministicMNStateDiff
246246
SER_READ(obj, read_pubkey = true);
247247
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.state.pubKeyOperator), obj.state.nVersion == ProTxVersion::LegacyBLS));
248248
}
249+
} else if constexpr (BaseType::mask == Field_netInfo) {
250+
if (obj.fields & member.mask) {
251+
READWRITE(NetInfoSerWrapper(const_cast<std::shared_ptr<NetInfoInterface>&>(obj.state.netInfo)));
252+
}
249253
} else {
250254
if (obj.fields & member.mask) {
251255
READWRITE(member.get(obj.state));

src/evo/netinfo.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@ std::string NetInfoEntry::ToStringAddrPort() const
122122
m_data);
123123
}
124124

125+
bool NetInfoInterface::IsEqual(const std::shared_ptr<NetInfoInterface>& lhs, const std::shared_ptr<NetInfoInterface>& rhs)
126+
{
127+
if (!lhs || !rhs) {
128+
// We don't compare uninitialized objects
129+
return false;
130+
}
131+
132+
if (lhs == rhs) {
133+
// Points to the same object
134+
return true;
135+
}
136+
137+
if (const auto lhs_ptr{std::dynamic_pointer_cast<MnNetInfo>(lhs)}) {
138+
if (const auto rhs_ptr{std::dynamic_pointer_cast<MnNetInfo>(lhs)}) {
139+
// Successful downcasting of both lhs and rhs, can now do deep comparison
140+
return *lhs_ptr == *rhs_ptr;
141+
}
142+
}
143+
144+
// Downcasting failed, lhs and rhs are differing types
145+
return false;
146+
}
147+
125148
NetInfoStatus MnNetInfo::ValidateService(const CService& service)
126149
{
127150
if (!service.IsValid()) {

0 commit comments

Comments
 (0)