Skip to content

Commit 8a083bc

Browse files
committed
Merge bitcoin/bitcoin#23218: p2p: Use mocktime for ping timeout
fadf118 p2p: Use mocktime for ping timeout (MarcoFalke) Pull request description: It is slightly confusing to use mocktime for some times, but not others. Start fixing that by making the ping timeout use mocktime. The only downside would be that tests that use mocktime disconnect peers after this patch. However, I don't think this is an issue, as the inactivity check is already disabled for all functional tests after commit 6d76b57. Only one unit test needed the inactivity check disabled as part of this patch. A nice side effect of this patch is that the `p2p_ping` functional test now runs 4 seconds faster. ACKs for top commit: laanwj: Code review ACK fadf118 Tree-SHA512: e9e7b21040a89d9d574b3038f85a67e6336de6cd6e41aa286769cd03cada6e75a94ec01700e052e56d822ef85d7813cc06bf7e67b81543eff8917a16cdccf942
2 parents f41aa81 + fadf118 commit 8a083bc

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,9 +1295,8 @@ void CConnman::NotifyNumConnectionsChanged()
12951295
}
12961296
}
12971297

1298-
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now_in) const
1298+
bool CConnman::ShouldRunInactivityChecks(const CNode& node, int64_t now) const
12991299
{
1300-
const int64_t now = now_in ? now_in.value() : GetTimeSeconds();
13011300
return node.nTimeConnected + m_peer_connect_timeout < now;
13021301
}
13031302

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ class CConnman
942942
std::chrono::microseconds PoissonNextSendInbound(std::chrono::microseconds now, std::chrono::seconds average_interval);
943943

944944
/** Return true if we should disconnect the peer for failing an inactivity check. */
945-
bool ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now=std::nullopt) const;
945+
bool ShouldRunInactivityChecks(const CNode& node, int64_t secs_now) const;
946946

947947
private:
948948
struct ListenSocket {

src/net_processing.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4312,8 +4312,11 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
43124312

43134313
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
43144314
{
4315-
if (m_connman.ShouldRunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
4315+
if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now).count()) &&
4316+
peer.m_ping_nonce_sent &&
43164317
now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
4318+
// The ping timeout is using mocktime. To disable the check during
4319+
// testing, increase -peertimeout.
43174320
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
43184321
node_to.fDisconnect = true;
43194322
return;

src/test/denialofservice_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
5252
{
5353
const CChainParams& chainparams = Params();
5454
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
55+
// Disable inactivity checks for this test to avoid interference
56+
static_cast<ConnmanTestMsg*>(connman.get())->SetPeerConnectTimeout(99999);
5557
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
5658
*m_node.chainman, *m_node.mempool, false);
5759

src/test/util/net.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
struct ConnmanTestMsg : public CConnman {
1919
using CConnman::CConnman;
20+
21+
void SetPeerConnectTimeout(int64_t timeout)
22+
{
23+
m_peer_connect_timeout = timeout;
24+
}
25+
2026
void AddTestNode(CNode& node)
2127
{
2228
LOCK(cs_vNodes);

test/functional/p2p_ping.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ def on_ping(self, message):
3030
pass
3131

3232

33+
TIMEOUT_INTERVAL = 20 * 60
34+
35+
3336
class PingPongTest(BitcoinTestFramework):
3437
def set_test_params(self):
3538
self.setup_clean_chain = True
3639
self.num_nodes = 1
37-
self.extra_args = [['-peertimeout=3']]
40+
# Set the peer connection timeout low. It does not matter for this
41+
# test, as long as it is less than TIMEOUT_INTERVAL.
42+
self.extra_args = [['-peertimeout=1']]
3843

3944
def check_peer_info(self, *, pingtime, minping, pingwait):
4045
stats = self.nodes[0].getpeerinfo()[0]
@@ -110,8 +115,11 @@ def run_test(self):
110115
self.nodes[0].ping()
111116
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
112117
with self.nodes[0].assert_debug_log(['ping timeout: 1201.000000s']):
113-
self.mock_forward(20 * 60 + 1)
114-
time.sleep(4) # peertimeout + 1
118+
self.mock_forward(TIMEOUT_INTERVAL // 2)
119+
# Check that sending a ping does not prevent the disconnect
120+
no_pong_node.sync_with_ping()
121+
self.mock_forward(TIMEOUT_INTERVAL // 2 + 1)
122+
no_pong_node.wait_for_disconnect()
115123

116124

117125
if __name__ == '__main__':

0 commit comments

Comments
 (0)