Skip to content

Commit 7592def

Browse files
committed
evo: prohibit overwriting entry in MnNetInfo
1 parent dfbc8a1 commit 7592def

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
@@ -1252,6 +1252,9 @@ static bool CheckService(const ProTx& proTx, TxValidationState& state)
12521252
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-bad");
12531253
case NetInfoStatus::Success:
12541254
return true;
1255+
// Shouldn't be possible during self-checks
1256+
case NetInfoStatus::MaxLimit:
1257+
assert(false);
12551258
} // no default case, so the compiler can warn about missing cases
12561259
assert(false);
12571260
}

src/evo/netinfo.cpp

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

149149
NetInfoStatus MnNetInfo::AddEntry(const std::string& input)
150150
{
151+
if (!IsEmpty()) {
152+
return NetInfoStatus::MaxLimit;
153+
}
151154
if (auto service = Lookup(input, /*portDefault=*/Params().GetDefaultPort(), /*fAllowLookup=*/false);
152155
service.has_value()) {
153156
const auto ret = ValidateService(service.value());

src/evo/netinfo.h

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

1616
enum class NetInfoStatus : uint8_t {
17+
// Managing entries
18+
MaxLimit,
19+
20+
// Validation
1721
BadAddress,
1822
BadInput,
1923
BadPort,
@@ -38,6 +42,8 @@ constexpr std::string_view NISToString(const NetInfoStatus code)
3842
return "unroutable address";
3943
case NetInfoStatus::Malformed:
4044
return "malformed";
45+
case NetInfoStatus::MaxLimit:
46+
return "too many entries";
4147
case NetInfoStatus::Success:
4248
return "success";
4349
} // no default case, so the compiler can warn about missing cases

src/test/evo_netinfo_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ BOOST_AUTO_TEST_CASE(mnnetinfo_rules)
5959
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
6060
}
6161
}
62+
63+
{
64+
// MnNetInfo only stores one value, overwriting prohibited
65+
MnNetInfo netInfo;
66+
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.1:9999"), NetInfoStatus::Success);
67+
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.2:9999"), NetInfoStatus::MaxLimit);
68+
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
69+
}
6270
}
6371

6472
bool CheckIfSerSame(const CService& lhs, const MnNetInfo& rhs)
@@ -83,6 +91,7 @@ BOOST_AUTO_TEST_CASE(cservice_compatible)
8391

8492
// Valid IPv4 address, default P2P port implied
8593
service = LookupNumeric("1.1.1.1", Params().GetDefaultPort());
94+
netInfo.Clear();
8695
BOOST_CHECK_EQUAL(netInfo.AddEntry("1.1.1.1"), NetInfoStatus::Success);
8796
BOOST_CHECK(CheckIfSerSame(service, netInfo));
8897

0 commit comments

Comments
 (0)