Skip to content

Commit b94ad40

Browse files
committed
refactor: impl GetEntries(), adapt to support multiple addresses
GetEntries does nothing except wrap the one singular entry we have into a set. Right now this functionally changes nothing but this lets us change code to adapt to the possibility that a MnNetInfo-like object could store more than one value at some point down the line.
1 parent 1ad4edc commit b94ad40

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

src/evo/netinfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
5656
return NetInfoStatus::BadInput;
5757
}
5858

59+
CServiceList MnNetInfo::GetEntries() const
60+
{
61+
CServiceList ret;
62+
if (!IsEmpty()) {
63+
ret.push_back(m_addr);
64+
}
65+
// If MnNetInfo is empty, we probably don't expect any entries to show up, so
66+
// we return a blank set instead.
67+
return ret;
68+
}
69+
5970
std::string MnNetInfo::ToString() const
6071
{
6172
// Extra padding to account for padding done by the calling function.

src/evo/netinfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ constexpr std::string_view NISToString(const NetInfoStatus code) {
2929
assert(false);
3030
}
3131

32+
using CServiceList = std::vector<std::reference_wrapper<const CService>>;
33+
3234
class MnNetInfo
3335
{
3436
private:
@@ -50,6 +52,7 @@ class MnNetInfo
5052
}
5153

5254
NetInfoStatus AddEntry(const std::string& service);
55+
CServiceList GetEntries() const;
5356

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

src/rpc/masternode.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,34 +565,47 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
565565
}
566566

567567
if (strMode == "addr") {
568-
std::string strAddress = dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort();
568+
std::string strAddress;
569+
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
570+
strAddress += entry.ToStringAddrPort() + " ";
571+
}
572+
strAddress.pop_back(); // Remove trailing space
569573
if (!strFilter.empty() && strAddress.find(strFilter) == std::string::npos &&
570574
strOutpoint.find(strFilter) == std::string::npos) return;
571575
obj.pushKV(strOutpoint, strAddress);
572576
} else if (strMode == "full") {
573-
std::string strFull = strprintf("%s %s %s %s %s %s",
577+
std::string strFull = strprintf("%s %s %s %s %s ",
574578
PadString(dmnToStatus(dmn), 18),
575579
dmn.pdmnState->nPoSePenalty,
576580
payeeStr,
577581
PadString(ToString(dmnToLastPaidTime(dmn)), 10),
578-
PadString(ToString(dmn.pdmnState->nLastPaidHeight), 6),
579-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort());
582+
PadString(ToString(dmn.pdmnState->nLastPaidHeight), 6));
583+
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
584+
strFull += entry.ToStringAddrPort() + " ";
585+
}
586+
strFull.pop_back(); // Remove trailing space
580587
if (!strFilter.empty() && strFull.find(strFilter) == std::string::npos &&
581588
strOutpoint.find(strFilter) == std::string::npos) return;
582589
obj.pushKV(strOutpoint, strFull);
583590
} else if (strMode == "info") {
584-
std::string strInfo = strprintf("%s %s %s %s",
591+
std::string strInfo = strprintf("%s %s %s ",
585592
PadString(dmnToStatus(dmn), 18),
586593
dmn.pdmnState->nPoSePenalty,
587-
payeeStr,
588-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort());
594+
payeeStr);
595+
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
596+
strInfo += entry.ToStringAddrPort() + " ";
597+
}
598+
strInfo.pop_back(); // Remove trailing space
589599
if (!strFilter.empty() && strInfo.find(strFilter) == std::string::npos &&
590600
strOutpoint.find(strFilter) == std::string::npos) return;
591601
obj.pushKV(strOutpoint, strInfo);
592602
} else if (strMode == "json" || strMode == "recent" || strMode == "evo") {
593-
std::string strInfo = strprintf("%s %s %s %s %s %s %s %s %s %s %s",
594-
dmn.proTxHash.ToString(),
595-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort(),
603+
std::string strInfo{dmn.proTxHash.ToString() + " "};
604+
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
605+
strInfo += entry.ToStringAddrPort() + " ";
606+
}
607+
strInfo.pop_back(); // Remove trailing space
608+
strInfo += strprintf("%s %s %s %s %s %s %s %s %s",
596609
payeeStr,
597610
dmnToStatus(dmn),
598611
dmn.pdmnState->nPoSePenalty,

src/test/evo_netinfo_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
BOOST_FIXTURE_TEST_SUITE(evo_netinfo_tests, RegTestingSetup)
1515

16+
void ValidateGetEntries(const CServiceList& entries, const size_t expected_size)
17+
{
18+
BOOST_CHECK_EQUAL(entries.size(), expected_size);
19+
}
20+
1621
BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
1722
{
1823
// Validate AddEntry() rules enforcement
@@ -39,6 +44,11 @@ BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
3944
for (const auto& [input, expected_ret] : vals) {
4045
MnNetInfo netInfo;
4146
BOOST_CHECK_EQUAL(netInfo.AddEntry(input), expected_ret);
47+
if (expected_ret != NetInfoStatus::Success) {
48+
BOOST_CHECK(netInfo.GetEntries().empty());
49+
} else {
50+
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
51+
}
4252
}
4353
}
4454

src/txmempool.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,9 @@ 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-
mapProTxAddresses.emplace(proTx.netInfo.GetPrimary(), tx_hash);
694+
for (const CService& entry : proTx.netInfo.GetEntries()) {
695+
mapProTxAddresses.emplace(entry, tx_hash);
696+
}
695697
mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx_hash);
696698
mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx_hash);
697699
if (!proTx.collateralOutpoint.hash.IsNull()) {
@@ -702,7 +704,9 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con
702704
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
703705
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
704706
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
705-
mapProTxAddresses.emplace(proTx.netInfo.GetPrimary(), tx_hash);
707+
for (const CService& entry : proTx.netInfo.GetEntries()) {
708+
mapProTxAddresses.emplace(entry, tx_hash);
709+
}
706710
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
707711
auto proTx = *Assert(GetTxPayload<CProUpRegTx>(tx));
708712
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
@@ -791,15 +795,19 @@ void CTxMemPool::removeUncheckedProTx(const CTransaction& tx)
791795
if (!proTx.collateralOutpoint.IsNull()) {
792796
eraseProTxRef(tx_hash, proTx.collateralOutpoint.hash);
793797
}
794-
mapProTxAddresses.erase(proTx.netInfo.GetPrimary());
798+
for (const CService& entry : proTx.netInfo.GetEntries()) {
799+
mapProTxAddresses.erase(entry);
800+
}
795801
mapProTxPubKeyIDs.erase(proTx.keyIDOwner);
796802
mapProTxBlsPubKeyHashes.erase(proTx.pubKeyOperator.GetHash());
797803
mapProTxCollaterals.erase(proTx.collateralOutpoint);
798804
mapProTxCollaterals.erase(COutPoint(tx_hash, proTx.collateralOutpoint.n));
799805
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
800806
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
801807
eraseProTxRef(proTx.proTxHash, tx_hash);
802-
mapProTxAddresses.erase(proTx.netInfo.GetPrimary());
808+
for (const CService& entry : proTx.netInfo.GetEntries()) {
809+
mapProTxAddresses.erase(entry);
810+
}
803811
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
804812
auto proTx = *Assert(GetTxPayload<CProUpRegTx>(tx));
805813
eraseProTxRef(proTx.proTxHash, tx_hash);
@@ -1026,10 +1034,12 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10261034
}
10271035
auto& proTx = *opt_proTx;
10281036

1029-
if (mapProTxAddresses.count(proTx.netInfo.GetPrimary())) {
1030-
uint256 conflictHash = mapProTxAddresses[proTx.netInfo.GetPrimary()];
1031-
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
1032-
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
1037+
for (const CService& entry : proTx.netInfo.GetEntries()) {
1038+
if (mapProTxAddresses.count(entry)) {
1039+
uint256 conflictHash = mapProTxAddresses[entry];
1040+
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
1041+
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
1042+
}
10331043
}
10341044
}
10351045
removeProTxPubKeyConflicts(tx, proTx.keyIDOwner);
@@ -1046,10 +1056,12 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10461056
return;
10471057
}
10481058

1049-
if (mapProTxAddresses.count(opt_proTx->netInfo.GetPrimary())) {
1050-
uint256 conflictHash = mapProTxAddresses[opt_proTx->netInfo.GetPrimary()];
1051-
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
1052-
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
1059+
for (const CService& entry : opt_proTx->netInfo.GetEntries()) {
1060+
if (mapProTxAddresses.count(entry)) {
1061+
uint256 conflictHash = mapProTxAddresses[entry];
1062+
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
1063+
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
1064+
}
10531065
}
10541066
}
10551067
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
@@ -1382,8 +1394,14 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
13821394
return true; // i.e. can't decode payload == conflict
13831395
}
13841396
auto& proTx = *opt_proTx;
1385-
if (mapProTxAddresses.count(proTx.netInfo.GetPrimary()) || mapProTxPubKeyIDs.count(proTx.keyIDOwner) || mapProTxBlsPubKeyHashes.count(proTx.pubKeyOperator.GetHash()))
1397+
for (const CService& entry : proTx.netInfo.GetEntries()) {
1398+
if (mapProTxAddresses.count(entry)) {
1399+
return true;
1400+
}
1401+
}
1402+
if (mapProTxPubKeyIDs.count(proTx.keyIDOwner) || mapProTxBlsPubKeyHashes.count(proTx.pubKeyOperator.GetHash())) {
13861403
return true;
1404+
}
13871405
if (!proTx.collateralOutpoint.hash.IsNull()) {
13881406
if (mapProTxCollaterals.count(proTx.collateralOutpoint)) {
13891407
// there is another ProRegTx that refers to the same collateral
@@ -1401,8 +1419,12 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
14011419
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
14021420
return true; // i.e. can't decode payload == conflict
14031421
}
1404-
auto it = mapProTxAddresses.find(opt_proTx->netInfo.GetPrimary());
1405-
return it != mapProTxAddresses.end() && it->second != opt_proTx->proTxHash;
1422+
for (const CService& entry : opt_proTx->netInfo.GetEntries()) {
1423+
auto it = mapProTxAddresses.find(entry);
1424+
if (it != mapProTxAddresses.end() && it->second != opt_proTx->proTxHash) {
1425+
return true;
1426+
}
1427+
}
14061428
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
14071429
const auto opt_proTx = GetTxPayload<CProUpRegTx>(tx);
14081430
if (!opt_proTx) {

0 commit comments

Comments
 (0)