Merge bitcoin/bitcoin#34303: test: addrman: test self-announcement time penalty handling

e770392084aa52e5568cf001da4d537fda1d71b3 test: addrman: test self-announcement time penalty handling (Bruno Garcia)

Pull request description:

  This PR adds a test case for addrman that verifies that addresses announcing themselves (addr == source) are exempt from time penalties, while addresses announced by others receive the expected penalty.

  It fixes the following mutant (https://corecheck.dev/mutation/src/addrman.cpp#L561):
  ```diff
  diff --git a/src/addrman.cpp b/src/addrman.cpp
  index 206b54118e..c6a045fd8d 100644
  --- a/src/addrman.cpp
  +++ b/src/addrman.cpp
  @@ -558,7 +558,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
       AddrInfo* pinfo = Find(addr, &nId);

       // Do not set a penalty for a source's self-announcement
  -    if (addr == source) {
  +    if (addr != source) {
           time_penalty = 0s;
       }
  ```

ACKs for top commit:
  maflcko:
    review ACK e770392084aa52e5568cf001da4d537fda1d71b3 🐤
  achow101:
    ACK e770392084aa52e5568cf001da4d537fda1d71b3
  fjahr:
    Code review ACK e770392084aa52e5568cf001da4d537fda1d71b3
  naiyoma:
    tACK e770392084aa52e5568cf001da4d537fda1d71b3

Tree-SHA512: ec029d1e1e979f91840af944984cad530a1ce9a0eceb123230817f0ef3b9ad47253eebc4c953d350de2d904b59496fcd4757123c8bd63cf0e09c3581da48fff8
This commit is contained in:
Ava Chow 2026-01-28 14:37:29 -08:00
commit a6cdc3ec9b
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41

View File

@ -17,6 +17,7 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <cstdint>
#include <optional> #include <optional>
#include <string> #include <string>
@ -106,6 +107,45 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_CHECK(addrman->Size() >= 1); 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) BOOST_AUTO_TEST_CASE(addrman_ports)
{ {
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node)); auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));