p2p: Make timeout mockable and type safe, speed up test #19499

pull MarcoFalke wants to merge 2 commits into bitcoin:master from MarcoFalke:2007-p2pMockTimeout changing 12 files +66 −58
  1. MarcoFalke commented at 2:25 pm on July 12, 2020: member

    Use type-safe time for better code readability/maintainability and mockable time for better testability. This speeds up the p2p_timeout test.

    This is also a bugfix for intermittent test issues like: https://cirrus-ci.com/task/4769904156999680?command=ci#L2836

    Fixes #20654

  2. MarcoFalke added the label P2P on Jul 12, 2020
  3. MarcoFalke added the label Refactoring on Jul 12, 2020
  4. MarcoFalke force-pushed on Jul 12, 2020
  5. DrahtBot commented at 5:13 pm on July 12, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #22778 (net processing: Reduce resource usage for inbound block-relay-only connections by jnewbery)
    • #21160 (net/net processing: Move tx inventory into net_processing by jnewbery)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  6. practicalswift commented at 11:07 pm on July 13, 2020: contributor
    Concept ACK: mockable is good
  7. luke-jr commented at 10:21 pm on July 29, 2020: member
    Doesn’t this defeat the purpose of the test?
  8. DrahtBot added the label Needs rebase on Aug 24, 2020
  9. jnewbery commented at 1:08 pm on December 10, 2020: member
    Concept ACK. There’s a lot of refactor/tidyup in this PR. I wonder if that can be split out to make this more focused on the functional changes.
  10. MarcoFalke force-pushed on Sep 29, 2021
  11. DrahtBot removed the label Needs rebase on Sep 29, 2021
  12. DrahtBot added the label Needs rebase on Oct 21, 2021
  13. MarcoFalke force-pushed on Oct 22, 2021
  14. MarcoFalke force-pushed on Oct 22, 2021
  15. MarcoFalke force-pushed on Oct 22, 2021
  16. MarcoFalke commented at 9:02 am on October 22, 2021: member

    Concept ACK. There’s a lot of refactor/tidyup in this PR. I wonder if that can be split out to make this more focused on the functional changes.

    Good idea. Split up a scripted-diff as the first commit.

  17. MarcoFalke commented at 9:03 am on October 22, 2021: member

    Doesn’t this defeat the purpose of the test?

    This is a bugfix for the test. Currently it intermittently fails, see OP.

  18. DrahtBot removed the label Needs rebase on Oct 22, 2021
  19. MarcoFalke force-pushed on Nov 2, 2021
  20. MarcoFalke added this to the milestone 23.0 on Nov 17, 2021
  21. MarcoFalke removed this from the milestone 23.0 on Nov 17, 2021
  22. scripted-diff: Rename m_last_send and m_last_recv
    -BEGIN VERIFY SCRIPT-
    
    ren() { sed -i "s/\<$1\>/$2/g" $(git grep -l "$1" ./src) ; }
    
    ren nLastSend m_last_send
    ren nLastRecv m_last_recv
    
    -END VERIFY SCRIPT-
    fa6d5a238d
  23. MarcoFalke force-pushed on Nov 17, 2021
  24. laanwj added this to the "Blockers" column in a project

  25. in src/net.cpp:1335 in faf36165fd outdated
    1337 
    1338     if (!ShouldRunInactivityChecks(node, now)) return false;
    1339 
    1340-    if (node.m_last_recv == 0 || node.m_last_send == 0) {
    1341-        LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d peer=%d\n", m_peer_connect_timeout, node.m_last_recv != 0, node.m_last_send != 0, node.GetId());
    1342+    if (last_recv.count() == 0 || last_send.count() == 0) {
    


    mzumsande commented at 10:25 pm on December 3, 2021:
    Why sometimes .count() and sometimes count_seconds()?

    MarcoFalke commented at 9:50 am on December 6, 2021:
    last_recv is a bit like std::optional. I use .count(), when .has_value() would be used and I use count_seconds(), when .value() would be used.
  26. in src/net_processing.cpp:4317 in faf36165fd outdated
    4311@@ -4312,9 +4312,10 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
    4312 
    4313 void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
    4314 {
    4315-    if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now).count()) &&
    4316+    if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now)) &&
    4317         peer.m_ping_nonce_sent &&
    4318-        now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
    4319+        now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL})
    


    mzumsande commented at 11:29 pm on December 3, 2021:
    0        now > peer.m_ping_start.load() + TIMEOUT_INTERVAL)
    

    TIMEOUT_INTERVAL is of type std::chrono::minutes now.


    MarcoFalke commented at 9:51 am on December 6, 2021:
    Nice, fixed.
  27. in src/net.cpp:1327 in faf36165fd outdated
    1327 bool CConnman::InactivityCheck(const CNode& node) const
    1328 {
    1329-    // Use non-mockable system time (otherwise these timers will pop when we
    1330-    // use setmocktime in the tests).
    1331-    int64_t now = GetTimeSeconds();
    1332+    // Test that see disconnects after using mocktime can start with a large
    


    mzumsande commented at 11:56 pm on December 3, 2021:
    nit: Tests

    MarcoFalke commented at 9:51 am on December 6, 2021:
    thx, fixed
  28. mzumsande commented at 0:15 am on December 4, 2021: member

    Concept ACK

    Reviewed the non-GUI-related parts so far and played with p2p_timeouts.py - looks good to me, just some nits.

  29. p2p: Make timeout mockable and type safe, speed up test fadc0c80ae
  30. MarcoFalke force-pushed on Dec 6, 2021
  31. in src/net_processing.cpp:4318 in fadc0c80ae
    4311@@ -4312,9 +4312,10 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
    4312 
    4313 void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
    4314 {
    4315-    if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now).count()) &&
    4316+    if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now)) &&
    4317         peer.m_ping_nonce_sent &&
    4318-        now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
    4319+        now > peer.m_ping_start.load() + TIMEOUT_INTERVAL)
    4320+    {
    


    laanwj commented at 5:20 pm on December 9, 2021:
    More consistent to keep the { on the same line?

    MarcoFalke commented at 5:47 pm on December 9, 2021:

    I think (and other seem to agree, see #21735) that multiline if conditions are hard to read because there is no break between condition and body, so changed it here.

    Happy to revert, though.


    laanwj commented at 6:19 pm on December 9, 2021:
    I don’t particularly want to get into discussion about code style, it just seemed inconsistent to me (as all other if cases have the { on the same line), if you have a good reason for it it’s fine with me.
  32. laanwj commented at 6:21 pm on December 9, 2021: member
    Code review ACK fadc0c80ae4e526fb2b503f7cc02f6122aaf1de5
  33. naumenkogs commented at 8:49 am on December 10, 2021: member
    ACK fadc0c80ae4e526fb2b503f7cc02f6122aaf1de5
  34. MarcoFalke merged this on Dec 10, 2021
  35. MarcoFalke closed this on Dec 10, 2021

  36. MarcoFalke deleted the branch on Dec 10, 2021
  37. sidhujag referenced this in commit f7136df5b2 on Dec 10, 2021
  38. fanquake removed this from the "Blockers" column in a project

  39. RandyMcMillan referenced this in commit 3f2e8f8b86 on Dec 23, 2021
  40. Fabcien referenced this in commit 5070d1e559 on Feb 2, 2022
  41. DrahtBot locked this on Dec 10, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-07-01 13:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me