[net] Consolidate logic around calling CAddrMan::Connected() #20291

pull jnewbery wants to merge 2 commits into bitcoin:master from jnewbery:2020-11-consolidate-addrman-connect changing 2 files +16 −11
  1. jnewbery commented at 9:20 AM on November 3, 2020: member

    Currently, the logic around whether we called CAddrMan::Connected() for a peer is spread between verack processing (where we discard inbound peers) and FinalizeNode (where we discard misbehaving and block-relay-only peers). Consolidate that logic to a single place.

    Also remove the CNode.fCurrentlyConnected bool, which is now redundant. We can rely on CNode.fSuccessfullyConnected, since the two bools were only ever flipped to true in the same place.

  2. [net] Consolidate logic around calling CAddrMan::Connected()
    Currently, the logic around whether we called CAddrMan::Connected() for
    a peer is spread between verack processing (where we discard inbound
    peers) and FinalizeNode (where we discard misbehaving and
    block-relay-only peers). Consolidate that logic to a single place.
    
    Also remove the CNode.fCurrentlyConnected bool, which is now
    redundant. We can rely on CNode.fSuccessfullyConnected, since the two
    bools were only ever flipped to true in the same place.
    eefe194718
  3. fanquake added the label P2P on Nov 3, 2020
  4. jnewbery commented at 9:22 AM on November 3, 2020: member

    I originally suggested this as a follow up to #20187: #20187 (review)

    cc @mzumsande @amitiuttarwar @sdaftuar

  5. MarcoFalke added the label Refactoring on Nov 3, 2020
  6. DrahtBot commented at 11:07 AM on November 3, 2020: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #20228 (addrman: Make addrman a top-level component 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.

  7. mzumsande commented at 3:31 PM on November 3, 2020: member

    Concept ACK.

    How about adding a commit to rename CAddrMan::Connected (which is only ever called during disconnection) and correct the documentation, as suggested in #20187 (review) and #20187#pullrequestreview-521189824 ?

  8. [addrman] Fix Connected() comment 0bfce9dc46
  9. jnewbery commented at 11:44 AM on November 4, 2020: member

    How about adding a commit to rename CAddrMan::Connected (which is only ever called during disconnection) and correct the documentation

    I haven't changed the name, but I've updated the documentation, so at least it should be obvious to readers what the function is doing. I think we need to reconsider our whole approach to nTime/nLastSuccess, and potentially remove nTime for addr messages altogether (see https://github.com/bitcoin-core/bitcoin-devwiki/wiki/P2P-IRC-meetings#20-oct-2020). For now, I think updating the function comment is enough.

  10. practicalswift commented at 5:03 PM on November 4, 2020: contributor

    Concept ACK

  11. in src/addrman.h:689 in 0bfce9dc46
     685 | @@ -676,7 +686,7 @@ friend class CAddrManTest;
     686 |          return vAddr;
     687 |      }
     688 |  
     689 | -    //! Mark an entry as currently-connected-to.
     690 | +    //! Outer function for Connected_()
    


    amitiuttarwar commented at 7:26 PM on November 4, 2020:

    this comment seems unnecessary- addrman uses this convention of external Function() & internal Function_() in several places.

    (but ofc not a big deal if it stays)


    jnewbery commented at 10:30 AM on November 6, 2020:

    I like all functions to have doxygen comments, even if they're short and seem unnecessary. I prefer this to say "outer function" than just be a duplicate of the comment for the inner function, which is what it was before.

  12. in src/addrman.h:262 in 0bfce9dc46
     257 | @@ -258,8 +258,18 @@ friend class CAddrManTest;
     258 |      //! Select several addresses at once.
     259 |      void GetAddr_(std::vector<CAddress> &vAddr, size_t max_addresses, size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs);
     260 |  
     261 | -    //! Mark an entry as currently-connected-to.
     262 | -    void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
     263 | +    /** We have successfully connected to this peer. Calling this function
     264 | +     *  updates the CAddress's nTime, which is used in our IsTerrible()
    


    amitiuttarwar commented at 2:01 AM on November 5, 2020:

    might be worth adding a word or two that indicates these are our primary uses of nTime, but not the only ones. the comment got me curious so I dove in and looked around, but its not the easiest to sift through a grep for nTime 😅

         *  updates the CAddress's nTime, which is primarily used in our IsTerrible()
    

    incase curious, some other times we read from nTime are when processing an ADDR message & deciding whether to relay & for debug printing in ConnectNode


    jnewbery commented at 10:34 AM on November 6, 2020:

    some other times we read from nTime are when processing an ADDR message & deciding whether to relay

    Ah. This is using the nTime from a received addr, so not quite the same as the nTime stored in a CAddress in addrman.

    for debug printing in ConnectNode

    Yes, this is true. The nTime is logged here.

    I think I'm right in saying that the only functional uses of nTime in a CAddress in addrman are in IsTerrible() and when relaying the CAddress to a peer in response to a getaddr.


    amitiuttarwar commented at 10:23 PM on November 6, 2020:

    I think I'm right in saying that the only functional uses of nTime in a CAddress in addrman are in IsTerrible() and when relaying the CAddress to a peer in response to a getaddr.

    yup, I agree. thats why my suggestion was just to clarify that difference (esp since grep doesn't immediately make this clear). not a big deal though, maybe to consider if you touch up the code for another reason.

    Ah. This is using the nTime from a received addr, so not quite the same as the nTime stored in a CAddress in addrman.

    hm well, looks like we read the addr into a CAddress object and then decide whether or not to add it to our addrman? ofc the functionality of Connected_ doesn't have the ability to impact the value at that time, but its still the same CAddress.nTime field thats being read?

  13. amitiuttarwar commented at 2:05 AM on November 5, 2020: contributor

    code review ACK 0bfce9dc46. nice tidy, and bonus that we get to remove an unnecessary call to cs_main

    one slightly tangential question- when looking for other touch points of nTime, I came across this comment: https://github.com/bitcoin/bitcoin/blob/c772f4cb04aa2edc04d849039b07c5a454d3f666/src/addrman.h#L206-L208 but from reading the function definition, I don't understand how. can it really update the existing nTime of a CAddress? looks like both the header comment and function were added in https://github.com/bitcoin/bitcoin/commit/5fee401fe14aa6459428a26a82f764db70a6a0b9, so seems likely I'm misunderstanding the code.

  14. jnewbery commented at 10:29 AM on November 6, 2020: member

    one slightly tangential question- when looking for other touch points of nTime, I came across this comment:

    // find an entry, creating it if necessary. // nTime and nServices of found node is updated, if necessary. CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);

    but from reading the function definition, I don't understand how. can it really update the existing nTime of a CAddress? looks like both the header comment and function were added in 5fee401, so seems likely I'm misunderstanding the code.

    No, you're not misunderstanding. That comment is just plain wrong. It doesn't find the entry, it always creates it. And it doesn't update the nTime and nServices. I think it's probably vestigial from when the code was being written. Perhaps logic was moved around (what it's describing is closer to what the Add_() function does), but it's inaccurate now.

  15. mjdietzx approved
  16. mjdietzx commented at 9:15 PM on November 6, 2020: contributor

    ack 0bfce9dc46234b196a8b3679c21d6f8455962495

  17. mzumsande commented at 10:07 PM on November 10, 2020: member

    Code review ACK 0bfce9dc46234b196a8b3679c21d6f8455962495

  18. fanquake requested review from sdaftuar on Nov 11, 2020
  19. in src/net_processing.cpp:857 in 0bfce9dc46
     851 | @@ -855,8 +852,9 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
     852 |      if (state->fSyncStarted)
     853 |          nSyncStarted--;
     854 |  
     855 | -    if (misbehavior == 0 && state->fCurrentlyConnected && !node.IsBlockOnlyConn()) {
     856 | -        // Note: we avoid changing visible addrman state for block-relay-only peers
     857 | +    if (node.fSuccessfullyConnected && misbehavior == 0 &&
     858 | +        !node.IsBlockOnlyConn() && !node.IsInboundConn()) {
     859 | +        // Only change visible addrman state for outbound, full-relay peers
    


    sdaftuar commented at 6:02 PM on November 11, 2020:

    Perhaps it's worth explicitly documenting that we do not call Connected() for FEELER connections? (This behavior emerges from feelers not having the fSuccessfullyConnected bool set, and I think it's desirable to maintain this behavior because otherwise long-lived block-relay-only connections might stick out as the ones which haven't been updated in a while.)


    jnewbery commented at 10:26 AM on November 12, 2020:

    Good suggestion. I'm touching this line again in #20228, which will need to be rebased once this is merged. Rather than invalidate the ACKs here for a comment update, I'll make the change there.


    jnewbery commented at 5:52 PM on November 19, 2020:

    Done in #20228.

  20. MarcoFalke merged this on Nov 19, 2020
  21. MarcoFalke closed this on Nov 19, 2020

  22. jnewbery deleted the branch on Nov 19, 2020
  23. sidhujag referenced this in commit fedac62135 on Nov 19, 2020
  24. deadalnix referenced this in commit 4237e75eea on Dec 21, 2021
  25. deadalnix referenced this in commit 16dac8c61a on Dec 21, 2021
  26. DrahtBot locked this on Feb 15, 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: 2026-04-22 06:14 UTC

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