Skip to content

Commit a03c3dd

Browse files
committed
evo: prohibit overwriting entry in MnNetInfo
1 parent 0d56a1b commit a03c3dd

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/evo/deterministicmns.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,9 @@ static bool CheckService(const ProTx& proTx, TxValidationState& state)
12431243
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-terrible");
12441244
case NetInfoStatus::Success:
12451245
return true;
1246+
// Shouldn't be possible during self-checks
1247+
case NetInfoStatus::MaxLimit:
1248+
assert(false);
12461249
} // no default case, so the compiler can warn about missing cases
12471250
assert(false);
12481251
}

src/evo/netinfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ NetInfoStatus MnNetInfo::ValidateService(const CService& service)
145145

146146
NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
147147
{
148+
if (!IsEmpty()) {
149+
return NetInfoStatus::MaxLimit;
150+
}
148151
if (auto service = Lookup(input, /*portDefault=*/Params().GetDefaultPort(), /*fAllowLookup=*/false);
149152
service.has_value()) {
150153
const auto ret = ValidateService(service.value());

src/evo/netinfo.h

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

1515
enum NetInfoStatus : uint8_t {
16+
// Adding entries
17+
MaxLimit,
18+
19+
// Validation
1620
BadInput,
1721
BadPort,
1822
Malformed,
@@ -22,6 +26,8 @@ enum NetInfoStatus : uint8_t {
2226
constexpr std::string_view NISToString(const NetInfoStatus code)
2327
{
2428
switch (code) {
29+
case NetInfoStatus::MaxLimit:
30+
return "too many entries";
2531
case NetInfoStatus::BadInput:
2632
return "invalid address";
2733
case NetInfoStatus::BadPort:

src/test/evo_netinfo_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
5757
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
5858
}
5959
}
60+
61+
{
62+
// MnNetInfo only stores one value, overwriting prohibited
63+
MnNetInfo netInfo;
64+
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.1:8888"), NetInfoStatus::Success);
65+
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.2:8888"), NetInfoStatus::MaxLimit);
66+
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
67+
}
6068
}
6169

6270
bool CheckIfSerSame(const CService& lhs, const MnNetInfo& rhs)
@@ -81,6 +89,7 @@ BOOST_AUTO_TEST_CASE(cservice_compatible)
8189

8290
// Valid IPv4 address, default P2P port implied
8391
service = LookupNumeric("1.1.1.1", Params().GetDefaultPort());
92+
netInfo.Clear();
8493
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.1"), NetInfoStatus::Success);
8594
BOOST_CHECK(CheckIfSerSame(service, netInfo));
8695

0 commit comments

Comments
 (0)