From 6ed53602ac7c565273b5722de167cb2569a0e381 Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 22 Dec 2023 10:32:32 -0300 Subject: net: peer manager, dynamically adjust desirable services flag Introduces functionality to detect when limited peers connections are desirable or not. Ensuring that the new connections desirable services flags stay relevant throughout the software's lifecycle. (Unlike the previous approach, where once the validation IBD flag was set, the desirable services flags remained constant forever). This will let us recover from stalling scenarios where the node had successfully synced, but subsequently dropped connections and remained inactive for a duration longer than the limited peers threshold (the timeframe within which limited peers can provide blocks). Then, upon reconnection to the network, the node may end up only establishing connections with limited peers, leading to an inability to synchronize the chain. This also fixes a possible limited peers threshold violation during IBD, when the user configures `-maxtipage` further than the BIP159's limits. This rule violation could lead to sync delays and, in the worst-case scenario, trigger the same post-IBD stalling scenario (mentioned above) but during IBD. --- src/net_processing.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/net_processing.cpp') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 3e09b54336..84a7f4c4d5 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -133,6 +133,8 @@ static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8; static const int MAX_NUM_UNCONNECTING_HEADERS_MSGS = 10; /** Minimum blocks required to signal NODE_NETWORK_LIMITED */ static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288; +/** Window, in blocks, for connecting to NODE_NETWORK_LIMITED peers */ +static const unsigned int NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS = 144; /** Average delay between local address broadcasts */ static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h}; /** Average delay between peer address broadcasts */ @@ -1678,8 +1680,11 @@ bool PeerManagerImpl::HasAllDesirableServiceFlags(ServiceFlags services) const ServiceFlags PeerManagerImpl::GetDesirableServiceFlags(ServiceFlags services) const { - if (services & NODE_NETWORK_LIMITED && GetServicesFlagsIBDCache()) { - return ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS); + if (services & NODE_NETWORK_LIMITED) { + // Limited peers are desirable when we are close to the tip. + if (ApproximateBestBlockDepth() < NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS) { + return ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS); + } } return ServiceFlags(NODE_NETWORK | NODE_WITNESS); } -- cgit v1.2.3