Skip to content

Commit bcb8a7d

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 ecc9368 commit bcb8a7d

File tree

5 files changed

+74
-19
lines changed

5 files changed

+74
-19
lines changed

src/evo/netinfo.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
6262
return NetInfoStatus::BadInput;
6363
}
6464

65+
CServiceList MnNetInfo::GetEntries() const
66+
{
67+
CServiceList ret;
68+
if (!IsEmpty()) {
69+
ASSERT_IF_DEBUG(m_addr != CService());
70+
ret.push_back(m_addr);
71+
}
72+
// If MnNetInfo is empty, we probably don't expect any entries to show up, so
73+
// we return a blank set instead.
74+
return ret;
75+
}
76+
6577
std::string MnNetInfo::ToString() const
6678
{
6779
// Extra padding to account for padding done by the calling function.

src/evo/netinfo.h

+3
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

+12-4
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,16 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
565565
payeeStr = EncodeDestination(payeeDest);
566566
}
567567

568+
std::string strAddress{};
569+
if (strMode == "addr" || strMode == "full" || strMode == "info" || strMode == "json" || strMode == "recent" ||
570+
strMode == "evo") {
571+
for (const CService& entry : dmn.pdmnState->netInfo.GetEntries()) {
572+
strAddress += entry.ToStringAddrPort() + " ";
573+
}
574+
if (!strAddress.empty()) strAddress.pop_back(); // Remove trailing space
575+
}
576+
568577
if (strMode == "addr") {
569-
std::string strAddress = dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort();
570578
if (!strFilter.empty() && strAddress.find(strFilter) == std::string::npos &&
571579
strOutpoint.find(strFilter) == std::string::npos) return;
572580
obj.pushKV(strOutpoint, strAddress);
@@ -577,7 +585,7 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
577585
payeeStr,
578586
PadString(ToString(dmnToLastPaidTime(dmn)), 10),
579587
PadString(ToString(dmn.pdmnState->nLastPaidHeight), 6),
580-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort());
588+
strAddress);
581589
if (!strFilter.empty() && strFull.find(strFilter) == std::string::npos &&
582590
strOutpoint.find(strFilter) == std::string::npos) return;
583591
obj.pushKV(strOutpoint, strFull);
@@ -586,14 +594,14 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
586594
PadString(dmnToStatus(dmn), 18),
587595
dmn.pdmnState->nPoSePenalty,
588596
payeeStr,
589-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort());
597+
strAddress);
590598
if (!strFilter.empty() && strInfo.find(strFilter) == std::string::npos &&
591599
strOutpoint.find(strFilter) == std::string::npos) return;
592600
obj.pushKV(strOutpoint, strInfo);
593601
} else if (strMode == "json" || strMode == "recent" || strMode == "evo") {
594602
std::string strInfo = strprintf("%s %s %s %s %d %d %d %s %s %s %s",
595603
dmn.proTxHash.ToString(),
596-
dmn.pdmnState->netInfo.GetPrimary().ToStringAddrPort(),
604+
strAddress,
597605
payeeStr,
598606
dmnToStatus(dmn),
599607
dmn.pdmnState->nPoSePenalty,

src/test/evo_netinfo_tests.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,22 @@ const std::vector<std::pair</*input=*/std::string, /*expected_ret=*/NetInfoStatu
3434

3535
BOOST_FIXTURE_TEST_SUITE(evo_netinfo_tests, RegTestingSetup)
3636

37+
void ValidateGetEntries(const CServiceList& entries, const size_t expected_size)
38+
{
39+
BOOST_CHECK_EQUAL(entries.size(), expected_size);
40+
}
41+
3742
BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
3843
{
3944
// Validate AddEntry() rules enforcement
4045
for (const auto& [input, expected_ret] : vals) {
4146
MnNetInfo netInfo;
4247
BOOST_CHECK_EQUAL(netInfo.AddEntry(input), expected_ret);
48+
if (expected_ret != NetInfoStatus::Success) {
49+
BOOST_CHECK(netInfo.GetEntries().empty());
50+
} else {
51+
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
52+
}
4353
}
4454
}
4555

src/txmempool.cpp

+37-15
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)