Follow-ups to 19107 #20364

pull troygiorshev wants to merge 3 commits into bitcoin:master from troygiorshev:2020-11-19107-follow-ups changing 4 files +34 −37
  1. troygiorshev commented at 9:46 pm on November 10, 2020: contributor

    This PR contains a few follow-ups to #19107.

    Most notable is a change to GetMessage, where it no longer returns an optional. Ultimately this was done in a heavy-handed way that didn’t improve readability and would probably cause more problems than it would save.

  2. DrahtBot added the label P2P on Nov 10, 2020
  3. DrahtBot commented at 2:57 am on November 11, 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:

    • #20864 (net: Move SocketSendData lock annotation to header by MarcoFalke)

    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.

  4. troygiorshev commented at 4:23 pm on November 11, 2020: contributor
    CI fails with “Timed out!” when running the fuzz tests, at exactly 2h of running time. Looks to be unrelated to this PR.
  5. MarcoFalke commented at 4:32 pm on November 11, 2020: member
    You can fix that by rebasing on current master
  6. in src/net.cpp:758 in eb21969fe3 outdated
    750@@ -751,16 +751,16 @@ const uint256& V1TransportDeserializer::GetMessageHash() const
    751     return data_hash;
    752 }
    753 
    754-Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, uint32_t& out_err_raw_size)
    755+CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, bool& reject_message)
    756 {
    757     // decompose a single CNetMessage from the TransportDeserializer
    


    ryanofsky commented at 9:29 pm on November 11, 2020:

    In commit “Remove Optional from GetMessage” (eb21969fe3fcd845d4060d890f7c46d703238661)

    Might be good to initialize reject_message = false here just to be clear this is an output parameter, not an in/out parameter


    troygiorshev commented at 5:47 am on November 17, 2020:

    I hesitated away from this just in case the caller wanted to reject the message for some other reason before calling GetMessage. (So reject_message would already be true)

    However, now writing that out into words, I think that’s an unlikely scenario, and I agree that setting it at the beginning makes things clearer.

    Thanks, made the change!

  7. ryanofsky approved
  8. ryanofsky commented at 9:34 pm on November 11, 2020: member
    Code review ACK ec4fd8a7fd6efb60938adb21f6764ce67e12be50. Thanks for followups #19107 (review) and #19107 (review)!
  9. in src/net.cpp:779 in ec4fd8a7fd outdated
    781     } else if (!hdr.IsCommandValid()) {
    782-        LogPrint(BCLog::NET, "HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n",
    783-                 hdr.GetCommand(), msg->m_message_size, m_node_id);
    784-        out_err_raw_size = msg->m_raw_message_size;
    785-        msg = nullopt;
    786+        LogPrint(BCLog::NET, "HEADER ERROR - INVALID COMMAND (%s, %u bytes), peer=%d\n",
    


    MarcoFalke commented at 9:18 am on November 12, 2020:
    0        LogPrint(BCLog::NET, "HEADER ERROR - INVALID MSGTYPE (%s, %u bytes), peer=%d\n",
    

    style-nit: When changing the log message, might as well change command->msg type, which is preferred in new code.

  10. troygiorshev force-pushed on Nov 17, 2020
  11. troygiorshev commented at 5:40 am on November 17, 2020: contributor

    git range-diff master ec4fd8a a10135e

    • Trivial rebase to master, to fix the CI error
  12. Remove Optional from GetMessage
    GetMesage no longer returns an optional.
    
    Additionally, we now access mapRecvBytesPerMsgCmd with `.at` not
    `.find`.
    
    This is shorter and more clear.
    This throws an error if COMMAND_OTHER doesn't exist, which should never
    happen.  `.find` instead just accessed the last element, which could
    make debugging more difficult.
    
    For this part, I think this is a definite improvement, but I'm not sure
    it's the best solution.  Open to suggestions.
    29c54e3026
  13. Replace GetID() with id in TransportDeserializer constructor b9f66231d8
  14. Make error log messages more descriptive 78fb468c7a
  15. troygiorshev force-pushed on Nov 17, 2020
  16. troygiorshev commented at 5:53 am on November 17, 2020: contributor

    git range-diff master a10135e 78fb468

    • Initialized out parameter reject_message
    • Renamed COMMAND to MSGTYPE in error logs
  17. in src/net.cpp:2983 in b9f66231d8 outdated
    2979@@ -2980,7 +2980,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
    2980         LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
    2981     }
    2982 
    2983-    m_deserializer = MakeUnique<V1TransportDeserializer>(V1TransportDeserializer(Params(), GetId(), SER_NETWORK, INIT_PROTO_VERSION));
    2984+    m_deserializer = MakeUnique<V1TransportDeserializer>(V1TransportDeserializer(Params(), id, SER_NETWORK, INIT_PROTO_VERSION));
    


    ajtowns commented at 8:39 am on November 19, 2020:
    GetId() is an inline method that returns id already; this change seems pretty pointless?

    jnewbery commented at 11:14 am on February 4, 2021:

    We should remove GetId() entirely. id is a const member so should just be made public.

    https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-get

    We certainly shouldn’t be using trivial getters inside CNode methods. They can access member variables directly.

  18. in src/net.cpp:675 in 29c54e3026 outdated
    675             //store received bytes per message command
    676             //to prevent a memory DOS, only allow valid commands
    677-            mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(result->m_command);
    678+            mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.m_command);
    679             if (i == mapRecvBytesPerMsgCmd.end())
    680                 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
    


    ajtowns commented at 8:49 am on November 19, 2020:
    Should change this .find() to .at() as well?

    jnewbery commented at 11:31 am on February 4, 2021:

    std::map::at returns a reference to the value, not an iterator on the map, which is what is required here for the i->second call below. Alternatively:

     0diff --git a/src/net.cpp b/src/net.cpp
     1index 9c2a79b809..e6a3d69810 100644
     2--- a/src/net.cpp
     3+++ b/src/net.cpp
     4@@ -670,11 +670,11 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
     5 
     6             //store received bytes per message command
     7             //to prevent a memory DOS, only allow valid commands
     8-            mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.m_command);
     9-            if (i == mapRecvBytesPerMsgCmd.end())
    10-                i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
    11-            assert(i != mapRecvBytesPerMsgCmd.end());
    12-            i->second += msg.m_raw_message_size;
    13+            if (auto i = mapRecvBytesPerMsgCmd.find(msg.m_command); i != mapRecvBytesPerMsgCmd.end()) {
    14+                i->second += msg.m_raw_message_size;
    15+            } else {
    16+                mapRecvBytesPerMsgCmd.at(NET_MESSAGE_COMMAND_OTHER) += msg.m_raw_message_size;
    17+            }
    
  19. ajtowns commented at 9:05 am on November 19, 2020: member

    PR title/description should summarise what was actually being done in this PR – I think it’s “[refactor] net.cpp: various minor cleanups” or similar?

    I think you could split out “.find() to .at()” and “drop redundant out_err_raw_size” as separate commits; they seem pretty independent. I’m not sure having “this function might throw, but we’re sure it never will” is better than “this function might deref end() but we’re sure it never will” is better. Maybe an Assert() would be better, or maybe using &map[NET_MESSAGE_COMMAND_OTHER] (which creates it if missing) would be better.

    I’m not seeing what the benefit of dropping the optional is though, it just changes:

    0    Optional<CNetMessage> x = GetMessage(time);
    1    if (!x) { ...; return; }
    2    do_stuff(*x);
    

    to

    0    bool reject_message;
    1    CNetMessage x = GetMessage(time, reject_message);
    2    if (reject_message) { ...; return; }
    3    do_stuff(x);
    

    which doesn’t seem like an improvement to me?

  20. ryanofsky commented at 12:09 pm on November 19, 2020: member

    I’m not seeing what the benefit of dropping the optional is though, it just changes:

    The benefit of dropping optional is that the calling code can reliably use msg.m_raw_message_size, instead of sometimes using m_raw_message_size and sometimes having to look in out_err_raw_size depending on the code path. It is easy to imagine a future change changing the logic a little and causing the wrong size to be read from the wrong location. Getting rid of optional also would have reduced the size of #19107 diff, which was the original reason for the suggestion, but that ship already sailed.

    I’m not sure having “this function might throw, but we’re sure it never will” is better than “this function might deref end() but we’re sure it never will” is better.

    I won’t die on this hill but I do think an error is better than undefined behavior

  21. ryanofsky approved
  22. ryanofsky commented at 12:11 pm on November 19, 2020: member
    Code review ACK 78fb468c7a1e5942cb1dc16864a4bbf8c85381ba. Only changes since last review are resetting output parameter and tweaking log messages
  23. DrahtBot added the label Needs rebase on Jan 7, 2021
  24. DrahtBot commented at 3:06 pm on January 7, 2021: member

    🐙 This pull request conflicts with the target branch and needs rebase.

    Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a “draft”.

  25. jnewbery commented at 11:32 am on February 4, 2021: member

    Code review ACK 78fb468c7a1e5942cb1dc16864a4bbf8c85381ba

    Needs rebase

  26. jnewbery commented at 11:33 am on February 4, 2021: member
    Suggest you remove “For this part, I think this is a definite improvement, but I’m not sure it’s the best solution. Open to suggestions.” from the first commit log. The commit messages should be used to explain the code changes, and github should be used for review/meta-commentary.
  27. fanquake added the label Up for grabs on Aug 18, 2021
  28. fanquake commented at 12:09 pm on August 18, 2021: member
    Closing this as up for grabs.
  29. fanquake closed this on Aug 18, 2021

  30. fanquake removed the label Up for grabs on Aug 18, 2021
  31. fanquake removed the label Needs rebase on Aug 18, 2021
  32. MarcoFalke referenced this in commit 9e3f7dcaa2 on Nov 2, 2021
  33. DrahtBot locked this on Aug 18, 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-03 19:12 UTC

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