Skip to content

Commit 8d55154

Browse files
committed
Merge bitcoin/bitcoin#34602: test: addrman: successive failures in the last week for IsTerrible
6202acd test: addrman: successive failures in the last week for IsTerrible (brunoerg) f611d3b refactor: addrman: move consts to .h (brunoerg) Pull request description: This PR adds test coverage for the case that an address is considered terrible if we had N successive failures in the last week. It kills the following mutant (https://corecheck.dev/mutation/src/addrman.cpp#L88): ```diff diff --git a/src/addrman.cpp b/src/addrman.cpp index e3981e6..f8045491c1 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -65,7 +65,7 @@ bool AddrInfo::IsTerrible(NodeSeconds now) const } if (now - m_last_success > ADDRMAN_MIN_FAIL && nAttempts >= ADDRMAN_MAX_FAILURES) { // N successive failures in the last week - return true; + return false; } return false; ``` ACKs for top commit: frankomosh: re-ACK 6202acd naiyoma: tACK 6202acd danielabrozzoni: tACK 6202acd sedited: ACK 6202acd Tree-SHA512: b4736ef91b75ba4c060dc18a2b796aee94d0d8be5ca58b9b873156248cd0dc6910595595e0f56d75bffff4f94c035319ac8428f7d79f07fe685bdba27a188829
2 parents bac8046 + 6202acd commit 8d55154

3 files changed

Lines changed: 57 additions & 20 deletions

File tree

src/addrman.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@
2424
#include <cmath>
2525
#include <optional>
2626

27-
/** Over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread */
28-
static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP{8};
29-
/** Over how many buckets entries with new addresses originating from a single group are spread */
30-
static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
31-
/** Maximum number of times an address can occur in the new table */
32-
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
33-
/** How old addresses can maximally be */
34-
static constexpr auto ADDRMAN_HORIZON{30 * 24h};
35-
/** After how many failed attempts we give up on a new node */
36-
static constexpr int32_t ADDRMAN_RETRIES{3};
37-
/** How many successive failures are allowed ... */
38-
static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
39-
/** ... in at least this duration */
40-
static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
41-
/** How recent a successful connection should be before we allow an address to be evicted from tried */
42-
static constexpr auto ADDRMAN_REPLACEMENT{4h};
43-
/** The maximum number of tried addr collisions to store */
44-
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
45-
/** The maximum time we'll spend trying to resolve a tried table collision */
46-
static constexpr auto ADDRMAN_TEST_WINDOW{40min};
4727

4828
int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
4929
{

src/addrman.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
#include <utility>
2020
#include <vector>
2121

22+
/** Over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread */
23+
static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP{8};
24+
/** Over how many buckets entries with new addresses originating from a single group are spread */
25+
static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
26+
/** Maximum number of times an address can occur in the new table */
27+
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
28+
/** How old addresses can maximally be */
29+
static constexpr auto ADDRMAN_HORIZON{30 * 24h};
30+
/** After how many failed attempts we give up on a new node */
31+
static constexpr int32_t ADDRMAN_RETRIES{3};
32+
/** How many successive failures are allowed ... */
33+
static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
34+
/** ... in at least this duration */
35+
static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
36+
/** How recent a successful connection should be before we allow an address to be evicted from tried */
37+
static constexpr auto ADDRMAN_REPLACEMENT{4h};
38+
/** The maximum number of tried addr collisions to store */
39+
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
40+
/** The maximum time we'll spend trying to resolve a tried table collision */
41+
static constexpr auto ADDRMAN_TEST_WINDOW{40min};
42+
2243
class InvalidAddrManVersionError : public std::ios_base::failure
2344
{
2445
public:

src/test/addrman_tests.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,42 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
9393
BOOST_CHECK(addrman->Size() >= 1);
9494
}
9595

96+
97+
BOOST_AUTO_TEST_CASE(addrman_terrible_many_failures)
98+
{
99+
auto now = Now<NodeSeconds>();
100+
SetMockTime(now - (ADDRMAN_MIN_FAIL + 24h));
101+
102+
auto addrman{std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node))};
103+
104+
CNetAddr source{ResolveIP("250.1.2.1")};
105+
CAddress addr{CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE)};
106+
addr.nTime = Now<NodeSeconds>();
107+
108+
BOOST_CHECK(addrman->Add({addr}, source));
109+
BOOST_CHECK(addrman->Good(addr));
110+
111+
SetMockTime(now);
112+
113+
CAddress addr_helper{CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE)};
114+
addr_helper.nTime = Now<NodeSeconds>();
115+
BOOST_CHECK(addrman->Add({addr_helper}, source));
116+
BOOST_CHECK(addrman->Good(addr_helper));
117+
118+
for (int i = 0; i < ADDRMAN_MAX_FAILURES; ++i) {
119+
// Use a time > 60s ago so IsTerrible doesn't bail out at the "tried in the last minute" check
120+
addrman->Attempt(addr, /*fCountFailure=*/true, Now<NodeSeconds>() - 61s);
121+
}
122+
123+
std::vector<CAddress> filtered{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
124+
BOOST_CHECK_EQUAL(filtered.size(), 1U);
125+
BOOST_CHECK_EQUAL(filtered[0].ToStringAddrPort(), "251.252.2.3:8333");
126+
127+
std::vector<CAddress> unfiltered{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt, /*filtered=*/false)};
128+
BOOST_CHECK_EQUAL(unfiltered.size(), 2U);
129+
}
130+
131+
96132
BOOST_AUTO_TEST_CASE(addrman_penalty_self_announcement)
97133
{
98134
SetMockTime(Now<NodeSeconds>());

0 commit comments

Comments
 (0)