test: addrman: test self-announcement time penalty handling
What changed, and why it matters
This commit only adds a new test to Bitcoin Core. It verifies that when a Bitcoin node announces its own network address, it does not receive a time penalty, but addresses announced by other nodes do receive a time penalty. There is no code change that alters behavior or fixes a bug.
No action required. This is a test-only commit and does not change production code or address a security vulnerability.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds a unit test in src/test/addrman_tests.cpp named addrman_penalty_self_announcement. It checks AddrMan::Add behavior when the address and source are identical (self-announcement) versus different. For self-announcement, the stored nTime should equal the original base_time. For non-self-announcement, the stored nTime should be reduced by the supplied time_penalty (3600 seconds). The commit also adds an include for
Changed components
src/test/addrman_tests.cppInspect captured patch +40 / −0
diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
index 4debdc16..49a9e202 100644
--- a/src/test/addrman_tests.cpp
+++ b/src/test/addrman_tests.cpp
@@ -17,6 +17,7 @@
#include <boost/test/unit_test.hpp>
+#include <cstdint>
#include <optional>
#include <string>
@@ -106,6 +107,45 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_CHECK(addrman->Size() >= 1);
}
+BOOST_AUTO_TEST_CASE(addrman_penalty_self_announcement)
+{
+ SetMockTime(Now<NodeSeconds>());
+ auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
+
+ const auto base_time{Now<NodeSeconds>() - 10000s};
+ CService addr1 = ResolveService("250.1.1.1", 8333);
+ CNetAddr source1 = ResolveIP("250.1.1.1"); // Same as addr1 - self announcement
+
+ CAddress caddr1(addr1, NODE_NONE);
+ caddr1.nTime = base_time;
+
+ const auto time_penalty{3600s};
+
+ BOOST_CHECK(addrman->Add({caddr1}, source1, time_penalty));
+
+ auto addr_pos1{addrman->FindAddressEntry(caddr1)};
+ BOOST_REQUIRE(addr_pos1.has_value());
+
+ std::vector<CAddress> addresses{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
+ BOOST_REQUIRE_EQUAL(addresses.size(), 1U);
+
+ BOOST_CHECK(addresses[0].nTime == base_time);
+
+ CService addr2{ResolveService("250.1.1.2", 8333)};
+ CNetAddr source2{ResolveIP("250.1.1.3")}; // Different from addr2 - not self announcement
+
+ CAddress caddr2(addr2, NODE_NONE);
+ caddr2.nTime = base_time;
+
+ BOOST_CHECK(addrman->Add({caddr2}, source2, time_penalty));
+
+ addresses = addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt);
+ BOOST_REQUIRE_EQUAL(addresses.size(), 2U);
+
+ CAddress retrieved_addr2{addresses[0]};
+ BOOST_CHECK(retrieved_addr2.nTime == base_time - time_penalty);
+}
+
BOOST_AUTO_TEST_CASE(addrman_ports)
{
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.