refactor, net: End friendship of CNode, CConnman and ConnmanTestMsg #27257

pull dergoegge wants to merge 8 commits into bitcoin:master from dergoegge:2023-03-cnode-friends changing 4 files +73 −48
  1. dergoegge commented at 5:43 PM on March 14, 2023: member

    We should define clear interfaces between CNode, CConnman and PeerManager. This PR makes a small step in that direction by ending the friendship of CNode, CConnman and ConnmanTestMsg. CNode's message processing queue is made private in the process and its mutex is turned into a non-recursive mutex.

  2. DrahtBot commented at 5:43 PM on March 14, 2023: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK vasild, theStack, brunoerg, jnewbery

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #25572 (refactor: Introduce EvictionManager and use it for the inbound eviction logic by dergoegge)
    • #25268 (refactor: Introduce EvictionManager by dergoegge)
    • #24545 (BIP324: Enable v2 P2P encrypted transport by dhruv)

    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.

  3. dergoegge cross-referenced this on Mar 14, 2023 from issue net, test: Virtualise CConnman and add initial PeerManager unit tests by dergoegge
  4. DrahtBot cross-referenced this on Mar 15, 2023 from issue p2p, rpc: Manual block-relay-only connections with addnode by mzumsande
  5. DrahtBot cross-referenced this on Mar 15, 2023 from issue refactor: Introduce EvictionManager and use it for the inbound eviction logic by dergoegge
  6. DrahtBot cross-referenced this on Mar 15, 2023 from issue refactor: Introduce EvictionManager by dergoegge
  7. DrahtBot cross-referenced this on Mar 16, 2023 from issue p2p: Improve diversification of new connections by stratospher
  8. in src/net.h:428 in 0e769e8ba5 outdated
     419 | +    void MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
     420 | +        EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);
     421 | +
     422 | +    /** Poll the next message from the processing queue of this connection. */
     423 | +    std::optional<std::pair<CNetMessage, bool>> PollMessage(size_t recv_flood_size)
     424 | +        EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);
    


    vasild commented at 1:41 PM on March 16, 2023:

    Would be nice to extend the comment to explain what is the bool in the return value.

  9. in src/net.cpp:2802 in 0e769e8ba5 outdated
    2793 | @@ -2805,6 +2794,37 @@ CNode::CNode(NodeId idIn,
    2794 |      }
    2795 |  }
    2796 |  
    2797 | +void CNode::MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
    2798 | +{
    2799 | +    size_t nSizeAdded = 0;
    


    vasild commented at 2:15 PM on March 16, 2023:

    Add AssertLockNotHeld(m_msg_process_queue_mutex) as per doc/developer-notes.md:

    • Combine annotations in function declarations with run-time asserts in function definitions:
  10. in src/net.cpp:2813 in 0e769e8ba5 outdated
    2806 | +        LOCK(m_msg_process_queue_mutex);
    2807 | +        m_msg_process_queue.splice(m_msg_process_queue.end(), vRecvMsg);
    2808 | +        m_msg_process_queue_size += nSizeAdded;
    2809 | +        fPauseRecv = m_msg_process_queue_size > recv_flood_size;
    2810 | +    }
    2811 | +}
    


    vasild commented at 2:16 PM on March 16, 2023:

    The nested scope is unnecessary now, you can remove { and }.

  11. in src/net.h:427 in 0e769e8ba5 outdated
     422 | +    /** Poll the next message from the processing queue of this connection. */
     423 | +    std::optional<std::pair<CNetMessage, bool>> PollMessage(size_t recv_flood_size)
     424 | +        EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);
     425 | +
     426 | +    /** Account for the total size of a send message in the per msg type connection stats. */
     427 | +    void AccountForSendBytes(const std::string& msg_type, size_t received_bytes)
    


    vasild commented at 2:21 PM on March 16, 2023:

    s/Send/Sent/ s/received_bytes/sent_bytes/

        void AccountForSentBytes(const std::string& msg_type, size_t sent_bytes)
    
  12. vasild commented at 2:26 PM on March 16, 2023: contributor

    Approach ACK 0e769e8ba5c81ff7b5b0a585465091ac3ccead2c

  13. dergoegge force-pushed on Mar 17, 2023
  14. dergoegge commented at 10:24 AM on March 17, 2023: member

    @vasild Thanks for the review! Took all of your suggestions.

  15. vasild approved
  16. vasild commented at 10:32 AM on March 17, 2023: contributor

    ACK d02b99b10ab6a748fd3c8b0d0faca013ada2d6e3

  17. dergoegge force-pushed on Mar 17, 2023
  18. vasild approved
  19. vasild commented at 10:33 AM on March 17, 2023: contributor

    ACK e29ecece9fed29663cc21caff4b00ceb29ecb959

  20. in src/net.cpp:2816 in e29ecece9f outdated
    2811 | +    fPauseRecv = m_msg_process_queue_size > recv_flood_size;
    2812 | +}
    2813 | +
    2814 | +std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage(size_t recv_flood_size)
    2815 | +{
    2816 | +    std::list<CNetMessage> msgs;
    


    brunoerg commented at 8:15 PM on March 17, 2023:

    nit:

    diff --git a/src/net.cpp b/src/net.cpp
    index 1ee783518..e8a25f9e7 100644
    --- a/src/net.cpp
    +++ b/src/net.cpp
    @@ -2813,11 +2813,10 @@ void CNode::MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
     
     std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage(size_t recv_flood_size)
     {
    -    std::list<CNetMessage> msgs;
    -
         LOCK(m_msg_process_queue_mutex);
         if (m_msg_process_queue.empty()) return std::nullopt;
     
    +    std::list<CNetMessage> msgs;
         // Just take one message
         msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
         m_msg_process_queue_size -= msgs.front().m_raw_message_size;
    

    dergoegge commented at 1:43 PM on March 19, 2023:

    Done!

  21. brunoerg approved
  22. brunoerg commented at 8:17 PM on March 17, 2023: contributor

    crACK e29ecece9fed29663cc21caff4b00ceb29ecb959

    nice cleanup!

  23. DrahtBot added the label Needs rebase on Mar 19, 2023
  24. [net] Add connection type getter to CNode ad44aa5c64
  25. [net] Deduplicate marking received message for processing cc5cdf8776
  26. dergoegge force-pushed on Mar 19, 2023
  27. dergoegge commented at 1:43 PM on March 19, 2023: member

    Rebased

  28. DrahtBot removed the label Needs rebase on Mar 19, 2023
  29. in src/net_processing.cpp:4893 in d816bcced0 outdated
    4899 | +        // No message to process
    4900 | +        return false;
    4901 | +    }
    4902 | +
    4903 | +    CNetMessage& msg{poll_result->first};
    4904 | +    fMoreWork = poll_result->second;
    


    theStack commented at 1:01 AM on March 21, 2023:

    in commit d816bcced0c1afb22f97df4650a35301a25cd9de: nit: since fMoreWork is unused above, could reduce it's scope and declare/init it right here:

        bool fMoreWork = poll_result->second;
    

    dergoegge commented at 2:39 PM on March 21, 2023:

    Done!

  30. theStack approved
  31. theStack commented at 1:03 AM on March 21, 2023: contributor

    Code-review ACK 6514352829364ab1ccff3a58faf6ca6ef4aafbbb

  32. DrahtBot requested review from brunoerg on Mar 21, 2023
  33. DrahtBot requested review from vasild on Mar 21, 2023
  34. dergoegge force-pushed on Mar 21, 2023
  35. theStack approved
  36. theStack commented at 3:17 PM on March 21, 2023: contributor

    re-ACK 60c3f4918190900e5f79341abcc0878214656257

  37. brunoerg approved
  38. brunoerg commented at 5:36 PM on March 21, 2023: contributor

    re-ACK 60c3f4918190900e5f79341abcc0878214656257

  39. DrahtBot cross-referenced this on Mar 21, 2023 from issue BIP324: Enable v2 P2P encrypted transport by dhruv
  40. vasild approved
  41. vasild commented at 4:25 AM on March 22, 2023: contributor

    ACK 60c3f4918190900e5f79341abcc0878214656257

  42. [net] Encapsulate CNode message polling 897e342d6e
  43. [net] Make CNode msg process queue members private
    Now that all access to the process queue members is handled by methods
    of `CNode` we can make these members private.
    23d9352654
  44. [net] Make cs_vProcessMsg a non-recursive mutex 6693c499f7
  45. scripted-diff: [net] Rename CNode process queue members
    -BEGIN VERIFY SCRIPT-
    ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); }
    
    ren cs_vProcessMsg    m_msg_process_queue_mutex
    ren vProcessMsg       m_msg_process_queue
    ren nProcessQueueSize m_msg_process_queue_size
    
    -END VERIFY SCRIPT-
    60441a3432
  46. [net] Add CNode helper for send byte accounting 3eac5e7cd1
  47. [net] Remove CNode friends
    Both `CConnman` and `ConnmanTestMsg` no longer access private members of
    `CNode`, we can therefore remove the friend relationship.
    3566aa7d49
  48. dergoegge force-pushed on Mar 22, 2023
  49. in src/net.cpp:2826 in 3566aa7d49
    2821 | +    // Just take one message
    2822 | +    msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
    2823 | +    m_msg_process_queue_size -= msgs.front().m_raw_message_size;
    2824 | +    fPauseRecv = m_msg_process_queue_size > recv_flood_size;
    2825 | +
    2826 | +    return std::make_pair(std::move(msgs.front()), !m_msg_process_queue.empty());
    


    dergoegge commented at 12:23 PM on March 22, 2023:

    I was making a copy here of the CNetMessage, fixed now!

    Easy to review with git range-diff 60c3f4918190900e5f79341abcc0878214656257...3566aa7d495bb783bbd135726238d9f2a9e9f80e.

  50. glozow added the label Refactoring on Mar 23, 2023
  51. vasild approved
  52. vasild commented at 1:25 PM on March 23, 2023: contributor

    ACK 3566aa7d495bb783bbd135726238d9f2a9e9f80e

  53. DrahtBot requested review from brunoerg on Mar 23, 2023
  54. DrahtBot requested review from theStack on Mar 23, 2023
  55. theStack approved
  56. theStack commented at 1:34 PM on March 23, 2023: contributor

    re-ACK 3566aa7d495bb783bbd135726238d9f2a9e9f80e

  57. brunoerg approved
  58. brunoerg commented at 1:36 PM on March 23, 2023: contributor

    re-ACK 3566aa7d495bb783bbd135726238d9f2a9e9f80e

  59. in src/net.h:416 in 3566aa7d49
     409 | @@ -417,6 +410,30 @@ class CNode
     410 |      std::atomic_bool fPauseRecv{false};
     411 |      std::atomic_bool fPauseSend{false};
     412 |  
     413 | +    const ConnectionType& GetConnectionType() const
     414 | +    {
     415 | +        return m_conn_type;
     416 | +    }
    


    jnewbery commented at 2:35 PM on March 23, 2023:

    C++ Core Guidelines suggest avoiding trivial getters: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c131-avoid-trivial-getters-and-setters

    Here, m_conn_type is const, so it can just be made public and read from outside the class.

  60. in src/net.h:419 in 3566aa7d49
     414 | +    {
     415 | +        return m_conn_type;
     416 | +    }
     417 | +
     418 | +    /** Move all messages from the received queue to the processing queue. */
     419 | +    void MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
    


    jnewbery commented at 2:41 PM on March 23, 2023:

    (for follow up) it'd be nice to remove the recv_flood_size parameter here:

    • Add a const size_t m_receive_flood_size member to CNode and set it during construction.
    • Remove this parameter and use m_receive_flood_size for the comparison in this function.
    • Remove the GetReceiveFloodSize() function from CConnman.
  61. in src/test/util/net.cpp:69 in 3566aa7d49
      76 | -            LOCK(node.cs_vProcessMsg);
      77 | -            node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg);
      78 | -            node.nProcessQueueSize += nSizeAdded;
      79 | -            node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize;
      80 | -        }
      81 | +        node.MarkReceivedMsgsForProcessing(nReceiveFloodSize);
    


    jnewbery commented at 2:55 PM on March 23, 2023:

    Style nit: feel free to use a single line for single-line if blocks:

        if (complete) node.MarkReceivedMsgsForProcessing(nReceiveFloodSize);
    

    vasild commented at 9:18 AM on March 24, 2023:

    That is a matter of personal taste (since both are allowed), but I just want to mention here why I, personally, avoid that:

    • It is not gdb friendly - you can't set a breakpoint on the if body (yes, one can set a breakpoint on the function call, but it becomes tedious if that function is called a lot from other places)
    • Creates bigger diff during changes in the future, comapre:
    - if (A) B;
    + if (A) {
    +     B;
    +     C;
    + }
    

    vs

     if (A) {
         B;
    +    C;
     }
    

    The latter is easier to read, especially if A, B and C are complex expressions.


    jnewbery commented at 9:57 AM on March 24, 2023:

    I agree that it's personal taste and the project style guide allows either.

    It is not gdb friendly - you can't set a breakpoint on the if body (yes, one can set a breakpoint on the function call, but it becomes tedious if that function is called a lot from other places)

    Are you familiar with conditional breakpoints? You can set the breakpoint to only stop the program if a boolean condition evaluates to true. Here, I think you could add the breakpoint:

    break "net.cpp":68 if complete

    and it would only stop if the if block is going to be called.


    vasild commented at 12:54 PM on March 24, 2023:

    Alas that does not work if the condition includes function calls.


    jnewbery commented at 1:59 PM on March 24, 2023:

    Alas that does not work if the condition includes function calls.

    Why not? From the GDB docs:

    Break conditions can have side effects, and may even call functions in your program.

  62. jnewbery commented at 4:22 PM on March 23, 2023: contributor

    utACK 3566aa7d495bb783bbd135726238d9f2a9e9f80e

    I didn't verify myself that the new code doesn't result in a copy of CNetMessage, but I trust that you've tested that. A follow up PR could potentially delete the default copy constructor for CNetMessage to ensure that we never copy.

    All of my review comments are nits/style comments and shouldn't stop this from being merged.

  63. fanquake merged this on Mar 23, 2023
  64. fanquake closed this on Mar 23, 2023

  65. dergoegge cross-referenced this on Mar 24, 2023 from issue net: #27257 follow-ups by dergoegge
  66. sidhujag referenced this in commit 707a3ecbc8 on Mar 24, 2023
  67. DrahtBot cross-referenced this on Mar 24, 2023 from issue p2p: Allow whitelisting manual connections by brunoerg
  68. DrahtBot cross-referenced this on Mar 25, 2023 from issue refactor: Continue moving application data from CNode to Peer by dergoegge
  69. fanquake referenced this in commit d254f942a5 on Mar 28, 2023
  70. sidhujag referenced this in commit 1c07f8ff47 on Mar 28, 2023
  71. dergoegge cross-referenced this on Apr 20, 2023 from issue meta: Isolated fuzzing of net processing by dergoegge
  72. satsie cross-referenced this on Apr 26, 2023 from issue rpc: getnetmsgstats revision IV by satsie
  73. satsie cross-referenced this on Apr 27, 2023 from issue rpc: add 'getnetmsgstats', new rpc to view network message statistics by satsie
  74. willcl-ark cross-referenced this on Nov 22, 2023 from issue rpc: add 'getnetmsgstats' RPC by willcl-ark
  75. bitcoin locked this on Mar 23, 2024

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: 2026-05-20 06:53 UTC