Skip to content

Commit 4a64950

Browse files
committed
evo: return MnNetInfo::GetEntries() with NetInfoEntry
1 parent fb133cc commit 4a64950

8 files changed

+95
-48
lines changed

src/evo/deterministicmns.cpp

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,17 @@ 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 CService& entry : dmn->pdmnState->netInfo.GetEntries()) {
474-
if (!AddUniqueProperty(*dmn, entry)) {
475-
mnUniquePropertyMap = mnUniquePropertyMapSaved;
476-
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate address=%s", __func__,
477-
dmn->proTxHash.ToString(), entry.ToStringAddrPort())));
473+
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo.GetEntries()) {
474+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
475+
const CService& service{service_opt.value()};
476+
if (!AddUniqueProperty(*dmn, service)) {
477+
mnUniquePropertyMap = mnUniquePropertyMapSaved;
478+
throw std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate address=%s",
479+
__func__, dmn->proTxHash.ToString(), service.ToStringAddrPort()));
480+
}
481+
} else {
482+
throw std::runtime_error(
483+
strprintf("%s: Can't add a masternode %s with invalid address", __func__, dmn->proTxHash.ToString()));
478484
}
479485
}
480486
if (!AddUniqueProperty(*dmn, dmn->pdmnState->keyIDOwner)) {
@@ -518,14 +524,24 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
518524
// We track each individual entry in netInfo as opposed to netInfo itself (preventing us from
519525
// using UpdateUniqueProperty()), so we need to successfully purge all old entries and insert
520526
// new entries to successfully update.
521-
for (const CService& old_entry : oldState->netInfo.GetEntries()) {
522-
if (!DeleteUniqueProperty(*dmn, old_entry)) {
523-
return strprintf("internal error"); // This shouldn't be possible
527+
for (const NetInfoEntry& old_entry : oldState->netInfo.GetEntries()) {
528+
if (const auto& service_opt{old_entry.GetAddrPort()}; service_opt.has_value()) {
529+
const CService& service{service_opt.value()};
530+
if (!DeleteUniqueProperty(*dmn, service)) {
531+
return strprintf("internal error"); // This shouldn't be possible
532+
}
533+
} else {
534+
return strprintf("invalid address");
524535
}
525536
}
526-
for (const CService& new_entry : pdmnState->netInfo.GetEntries()) {
527-
if (!AddUniqueProperty(*dmn, new_entry)) {
528-
return strprintf("duplicate (%s)", new_entry.ToStringAddrPort());
537+
for (const NetInfoEntry& new_entry : pdmnState->netInfo.GetEntries()) {
538+
if (const auto& service_opt{new_entry.GetAddrPort()}; service_opt.has_value()) {
539+
const CService& service{service_opt.value()};
540+
if (!AddUniqueProperty(*dmn, service)) {
541+
return strprintf("duplicate (%s)", service.ToStringAddrPort());
542+
}
543+
} else {
544+
return strprintf("invalid address");
529545
}
530546
}
531547
}
@@ -591,11 +607,17 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
591607
throw(std::runtime_error(strprintf("%s: Can't delete a masternode %s with a collateralOutpoint=%s", __func__,
592608
proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort())));
593609
}
594-
for (const CService& entry : dmn->pdmnState->netInfo.GetEntries()) {
595-
if (!DeleteUniqueProperty(*dmn, entry)) {
596-
mnUniquePropertyMap = mnUniquePropertyMapSaved;
597-
throw(std::runtime_error(strprintf("%s: Can't delete a masternode %s with an address=%s", __func__,
598-
proTxHash.ToString(), entry.ToStringAddrPort())));
610+
for (const NetInfoEntry& entry : dmn->pdmnState->netInfo.GetEntries()) {
611+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
612+
const CService& service{service_opt.value()};
613+
if (!DeleteUniqueProperty(*dmn, service)) {
614+
mnUniquePropertyMap = mnUniquePropertyMapSaved;
615+
throw std::runtime_error(strprintf("%s: Can't delete a masternode %s with an address=%s", __func__,
616+
proTxHash.ToString(), service.ToStringAddrPort()));
617+
}
618+
} else {
619+
throw std::runtime_error(strprintf("%s: Can't delete a masternode %s with invalid address", __func__,
620+
dmn->proTxHash.ToString()));
599621
}
600622
}
601623
if (!DeleteUniqueProperty(*dmn, dmn->pdmnState->keyIDOwner)) {
@@ -811,9 +833,14 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
811833
}
812834
}
813835

814-
for (const CService& entry : proTx.netInfo.GetEntries()) {
815-
if (newList.HasUniqueProperty(entry)) {
816-
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-dup-netinfo-entry");
836+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
837+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
838+
const CService& service{service_opt.value()};
839+
if (newList.HasUniqueProperty(service)) {
840+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-dup-netinfo-entry");
841+
}
842+
} else {
843+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-netinfo-entry");
817844
}
818845
}
819846
if (newList.HasUniqueProperty(proTx.keyIDOwner) || newList.HasUniqueProperty(proTx.pubKeyOperator)) {
@@ -842,10 +869,15 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
842869
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-payload");
843870
}
844871

845-
for (const CService& entry : opt_proTx->netInfo.GetEntries()) {
846-
if (newList.HasUniqueProperty(entry) &&
847-
newList.GetUniquePropertyMN(entry)->proTxHash != opt_proTx->proTxHash) {
848-
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-dup-netinfo-entry");
872+
for (const NetInfoEntry& entry : opt_proTx->netInfo.GetEntries()) {
873+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
874+
const CService& service{service_opt.value()};
875+
if (newList.HasUniqueProperty(service) &&
876+
newList.GetUniquePropertyMN(service)->proTxHash != opt_proTx->proTxHash) {
877+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-dup-netinfo-entry");
878+
}
879+
} else {
880+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-protx-netinfo-entry");
849881
}
850882
}
851883

@@ -1382,10 +1414,15 @@ bool CheckProRegTx(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl:
13821414
auto mnList = dmnman.GetListForBlock(pindexPrev);
13831415

13841416
// only allow reusing of addresses when it's for the same collateral (which replaces the old MN)
1385-
for (const CService& entry : opt_ptx->netInfo.GetEntries()) {
1386-
if (mnList.HasUniqueProperty(entry) &&
1387-
mnList.GetUniquePropertyMN(entry)->collateralOutpoint != collateralOutpoint) {
1388-
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-dup-netinfo-entry");
1417+
for (const NetInfoEntry& entry : opt_ptx->netInfo.GetEntries()) {
1418+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
1419+
const CService& service{service_opt.value()};
1420+
if (mnList.HasUniqueProperty(service) &&
1421+
mnList.GetUniquePropertyMN(service)->collateralOutpoint != collateralOutpoint) {
1422+
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-dup-netinfo-entry");
1423+
}
1424+
} else {
1425+
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-entry");
13891426
}
13901427
}
13911428

@@ -1455,9 +1492,14 @@ bool CheckProUpServTx(CDeterministicMNManager& dmnman, const CTransaction& tx, g
14551492
}
14561493

14571494
// don't allow updating to addresses already used by other MNs
1458-
for (const CService& entry : opt_ptx->netInfo.GetEntries()) {
1459-
if (mnList.HasUniqueProperty(entry) && mnList.GetUniquePropertyMN(entry)->proTxHash != opt_ptx->proTxHash) {
1460-
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-dup-netinfo-entry");
1495+
for (const NetInfoEntry& entry : opt_ptx->netInfo.GetEntries()) {
1496+
if (const auto& service_opt{entry.GetAddrPort()}; service_opt.has_value()) {
1497+
const CService& service{service_opt.value()};
1498+
if (mnList.HasUniqueProperty(service) && mnList.GetUniquePropertyMN(service)->proTxHash != opt_ptx->proTxHash) {
1499+
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-dup-netinfo-entry");
1500+
}
1501+
} else {
1502+
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-entry");
14611503
}
14621504
}
14631505

src/evo/deterministicmns.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ class CDeterministicMNList
398398
static_assert(!std::is_same_v<std::decay_t<T>, name>, "GetUniquePropertyHash cannot be templated against " #name)
399399
DMNL_NO_TEMPLATE(CBLSPublicKey);
400400
DMNL_NO_TEMPLATE(MnNetInfo);
401+
DMNL_NO_TEMPLATE(NetInfoEntry);
401402
#undef DMNL_NO_TEMPLATE
402403
return ::SerializeHash(v);
403404
}

src/evo/netinfo.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
164164
const auto ret = ValidateService(service.value());
165165
if (ret == NetInfoStatus::Success) {
166166
m_addr = NetInfoEntry{service.value()};
167-
ASSERT_IF_DEBUG(GetPrimary() != empty_service);
167+
ASSERT_IF_DEBUG(m_addr.GetAddrPort().has_value());
168168
}
169169
return ret;
170170
}
171171
return NetInfoStatus::BadInput;
172172
}
173173

174-
CServiceList MnNetInfo::GetEntries() const
174+
NetInfoList MnNetInfo::GetEntries() const
175175
{
176-
CServiceList ret;
176+
NetInfoList ret;
177177
if (!IsEmpty()) {
178-
ASSERT_IF_DEBUG(GetPrimary() != empty_service);
179-
ret.push_back(GetPrimary());
178+
ASSERT_IF_DEBUG(m_addr.GetAddrPort().has_value());
179+
ret.push_back(m_addr);
180180
}
181181
// If MnNetInfo is empty, we probably don't expect any entries to show up, so
182182
// we return a blank set instead.

src/evo/netinfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class NetInfoEntry
149149
std::string ToStringAddrPort() const;
150150
};
151151

152-
using CServiceList = std::vector<std::reference_wrapper<const CService>>;
152+
using NetInfoList = std::vector<std::reference_wrapper<const NetInfoEntry>>;
153153

154154
class MnNetInfo
155155
{
@@ -190,7 +190,7 @@ class MnNetInfo
190190
}
191191

192192
NetInfoStatus AddEntry(const std::string& service);
193-
CServiceList GetEntries() const;
193+
NetInfoList GetEntries() const;
194194

195195
const CService& GetPrimary() const;
196196
bool IsEmpty() const { return *this == MnNetInfo(); }

src/rpc/masternode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
568568
std::string strAddress{};
569569
if (strMode == "addr" || strMode == "full" || strMode == "info" || strMode == "json" || strMode == "recent" ||
570570
strMode == "evo") {
571-
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
571+
for (const NetInfoEntry& entry : dmn.pdmnState->netInfo.GetEntries()) {
572572
strAddress += entry.ToStringAddrPort() + " ";
573573
}
574574
if (!strAddress.empty()) strAddress.pop_back(); // Remove trailing space

src/test/evo_netinfo_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ const std::vector<std::pair</*input=*/std::string, /*expected_ret=*/NetInfoStatu
3636
{":9999", NetInfoStatus::BadInput},
3737
};
3838

39-
void ValidateGetEntries(const CServiceList& entries, const size_t expected_size)
39+
void ValidateGetEntries(const NetInfoList& entries, const size_t expected_size)
4040
{
4141
BOOST_CHECK_EQUAL(entries.size(), expected_size);
42+
for (const NetInfoEntry& entry : entries) {
43+
BOOST_CHECK(entry.IsTriviallyValid());
44+
}
4245
}
4346

4447
BOOST_AUTO_TEST_CASE(mnnetinfo_rules)

src/txmempool.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con
691691
if (!proTx.collateralOutpoint.hash.IsNull()) {
692692
mapProTxRefs.emplace(tx_hash, proTx.collateralOutpoint.hash);
693693
}
694-
for (const CService& entry : proTx.netInfo.GetEntries()) {
694+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
695695
mapProTxAddresses.emplace(entry, tx_hash);
696696
}
697697
mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx_hash);
@@ -704,7 +704,7 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con
704704
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
705705
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
706706
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
707-
for (const CService& entry : proTx.netInfo.GetEntries()) {
707+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
708708
mapProTxAddresses.emplace(entry, tx_hash);
709709
}
710710
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
@@ -795,7 +795,7 @@ void CTxMemPool::removeUncheckedProTx(const CTransaction& tx)
795795
if (!proTx.collateralOutpoint.IsNull()) {
796796
eraseProTxRef(tx_hash, proTx.collateralOutpoint.hash);
797797
}
798-
for (const CService& entry : proTx.netInfo.GetEntries()) {
798+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
799799
mapProTxAddresses.erase(entry);
800800
}
801801
mapProTxPubKeyIDs.erase(proTx.keyIDOwner);
@@ -805,7 +805,7 @@ void CTxMemPool::removeUncheckedProTx(const CTransaction& tx)
805805
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
806806
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
807807
eraseProTxRef(proTx.proTxHash, tx_hash);
808-
for (const CService& entry : proTx.netInfo.GetEntries()) {
808+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
809809
mapProTxAddresses.erase(entry);
810810
}
811811
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
@@ -1034,7 +1034,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10341034
}
10351035
auto& proTx = *opt_proTx;
10361036

1037-
for (const CService& entry : proTx.netInfo.GetEntries()) {
1037+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
10381038
if (mapProTxAddresses.count(entry)) {
10391039
uint256 conflictHash = mapProTxAddresses[entry];
10401040
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
@@ -1056,7 +1056,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10561056
return;
10571057
}
10581058

1059-
for (const CService& entry : opt_proTx->netInfo.GetEntries()) {
1059+
for (const NetInfoEntry& entry : opt_proTx->netInfo.GetEntries()) {
10601060
if (mapProTxAddresses.count(entry)) {
10611061
uint256 conflictHash = mapProTxAddresses[entry];
10621062
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
@@ -1394,7 +1394,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
13941394
return true; // i.e. can't decode payload == conflict
13951395
}
13961396
auto& proTx = *opt_proTx;
1397-
for (const CService& entry : proTx.netInfo.GetEntries()) {
1397+
for (const NetInfoEntry& entry : proTx.netInfo.GetEntries()) {
13981398
if (mapProTxAddresses.count(entry)) {
13991399
return true;
14001400
}
@@ -1419,7 +1419,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
14191419
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
14201420
return true; // i.e. can't decode payload == conflict
14211421
}
1422-
for (const CService& entry : opt_proTx->netInfo.GetEntries()) {
1422+
for (const NetInfoEntry& entry : opt_proTx->netInfo.GetEntries()) {
14231423
auto it = mapProTxAddresses.find(entry);
14241424
if (it != mapProTxAddresses.end() && it->second != opt_proTx->proTxHash) {
14251425
return true;

src/txmempool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <addressindex.h>
1818
#include <coins.h>
1919
#include <consensus/amount.h>
20+
#include <evo/netinfo.h>
2021
#include <gsl/pointers.h>
2122
#include <indirectmap.h>
2223
#include <netaddress.h>
@@ -525,7 +526,7 @@ class CTxMemPool
525526
std::map<uint256, std::vector<CSpentIndexKey>> mapSpentInserted;
526527

527528
std::multimap<uint256, uint256> mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx)
528-
std::map<CService, uint256> mapProTxAddresses;
529+
std::map<NetInfoEntry, uint256> mapProTxAddresses;
529530
std::map<CKeyID, uint256> mapProTxPubKeyIDs;
530531
std::map<uint256, uint256> mapProTxBlsPubKeyHashes;
531532
std::map<COutPoint, uint256> mapProTxCollaterals;

0 commit comments

Comments
 (0)