Skip to content

Commit 022b76f

Browse files
committed
merge bitcoin#23218: Use mocktime for ping timeout
1 parent 45d9e58 commit 022b76f

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
@@ -1491,9 +1491,8 @@ void CConnman::CalculateNumConnectionsChangedStats()
14911491
statsClient.gauge("peers.torConnections", torNodes, 1.0f);
14921492
}
14931493

1494-
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now_in) const
1494+
bool CConnman::ShouldRunInactivityChecks(const CNode& node, int64_t now) const
14951495
{
1496-
const int64_t now = now_in ? now_in.value() : GetTimeSeconds();
14971496
return node.nTimeConnected + m_peer_connect_timeout < now;
14981497
}
14991498

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ friend class CNode;
12371237
void SetAsmap(std::vector<bool> asmap) { addrman.m_asmap = std::move(asmap); }
12381238

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

12421242
private:
12431243
struct ListenSocket {

src/net_processing.cpp

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

50185018
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
50195019
{
5020-
if (m_connman.ShouldRunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
5020+
if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now).count()) &&
5021+
peer.m_ping_nonce_sent &&
50215022
now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
5023+
// The ping timeout is using mocktime. To disable the check during
5024+
// testing, increase -peertimeout.
50225025
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
50235026
node_to.fDisconnect = true;
50245027
return;

src/test/denialofservice_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
6464
{
6565
const CChainParams& chainparams = Params();
6666
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
67+
// Disable inactivity checks for this test to avoid interference
68+
static_cast<ConnmanTestMsg*>(connman.get())->SetPeerConnectTimeout(99999);
6769
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr, *m_node.scheduler,
6870
*m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman,
6971
::deterministicMNManager, m_node.cj_ctx, m_node.llmq_ctx, false);

src/test/util/net.h

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

1616
struct ConnmanTestMsg : public CConnman {
1717
using CConnman::CConnman;
18+
19+
void SetPeerConnectTimeout(int64_t timeout)
20+
{
21+
m_peer_connect_timeout = timeout;
22+
}
23+
1824
void AddTestNode(CNode& node)
1925
{
2026
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]
@@ -114,8 +119,11 @@ def run_test(self):
114119
self.nodes[0].ping()
115120
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
116121
with self.nodes[0].assert_debug_log(['ping timeout: 1201.000000s']):
117-
self.mock_forward(20 * 60 + 1)
118-
time.sleep(4) # peertimeout + 1
122+
self.mock_forward(TIMEOUT_INTERVAL // 2)
123+
# Check that sending a ping does not prevent the disconnect
124+
no_pong_node.sync_with_ping()
125+
self.mock_forward(TIMEOUT_INTERVAL // 2 + 1)
126+
no_pong_node.wait_for_disconnect()
119127

120128

121129
if __name__ == '__main__':

0 commit comments

Comments
 (0)