Skip to content

Commit

Permalink
Merge bitcoin#18638: net: Use mockable time for ping/pong, add tests
Browse files Browse the repository at this point in the history
fa33654 net: Use mockable time for ping/pong, add tests (MarcoFalke)
faab4aa util: Add count_microseconds helper (MarcoFalke)

Pull request description:

  Switch `CNode::m_ping_start` and `CNetMessage::m_time` to mockable time, so that tests can be added.

  Mockable time is also type-safe, since it uses `std::chrono`

ACKs for top commit:
  jonatack:
    Code review re-ACK fa33654 re-read code, verified rebase per `git range-diff 4b5c919 fa94d6f fa33654`, previous tested ACKs still valid
  troygiorshev:
    ACK fa33654

Tree-SHA512: 7d632bd6019ce7c882029e71b667a61517e783af82755a85dd979ef09380934e172dec8b8f91d57b200a30a6e096aeaf01f19fee7f3aed0e0e871c72eb44d70e
  • Loading branch information
MarcoFalke authored and knst committed Jul 8, 2024
1 parent 6e5d3f1 commit 264e7f9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,7 @@ void CNode::CopyStats(CNodeStats& stats)
bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
{
complete = false;
// TODO: use mocktime here after bitcoin#19499 is backported
const auto time = std::chrono::microseconds(GetTimeMicros());
const auto time = GetTime<std::chrono::microseconds>();
LOCK(cs_vRecv);
m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
nRecvBytes += msg_bytes.size();
Expand Down
8 changes: 2 additions & 6 deletions test/functional/p2p_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def run_test(self):
self.mock_forward(ping_delay)
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))
# TODO this check doesn't work due to partial 18638
# re-enable it after #19499 is done
# self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)

self.log.info('Check that minping is decreased after a fast roundtrip')
# mock time PING_INTERVAL ahead to trigger node into sending a ping
Expand All @@ -104,9 +102,7 @@ def run_test(self):
self.mock_forward(ping_delay)
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))
# TODO this check doesn't work due to partial 18638
# re-enable it after #19499 is done
# self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)

self.log.info('Check that peer is disconnected after ping timeout')
assert 'ping' not in no_pong_node.last_message
Expand Down

0 comments on commit 264e7f9

Please sign in to comment.