net: reduce CAddress usage to CService or CNetAddr #31854

pull vasild wants to merge 1 commits into bitcoin:master from vasild:reduce_CAddress changing 3 files +23 −24
  1. vasild commented at 10:50 AM on February 13, 2025: contributor

    Using CAddress when only CService or CNetAddr is needed is excessive and confusing. Fix those occurrences to use the class they need:

    • CConnman::CalculateKeyedNetGroup() needs CNetAddr, not CAddress, thus change its argument.

    • Both callers of CConnman::CreateNodeFromAcceptedSocket() create a dummy CAddress from CService, so use CService instead.

    • GetBindAddress() only needs to return CService.

    • CNode::addrBind only needs to be CService.

  2. DrahtBot commented at 10:50 AM on February 13, 2025: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31854.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK hodlinator, Sjors, laanwj, achow101

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

  3. DrahtBot added the label P2P on Feb 13, 2025
  4. net: reduce CAddress usage to CService or CNetAddr
    * `CConnman::CalculateKeyedNetGroup()` needs `CNetAddr`, not `CAddress`,
      thus change its argument.
    
    * Both callers of `CConnman::CreateNodeFromAcceptedSocket()` create a
      dummy `CAddress` from `CService`, so use `CService` instead.
    
    * `GetBindAddress()` only needs to return `CService`.
    
    * `CNode::addrBind` only needs to be `CService`.
    cd4bfaee10
  5. in src/net.cpp:383 in 74f42a5920 outdated
     380 | @@ -381,9 +381,9 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
     381 |  }
     382 |  
     383 |  /** Get the bind address for a socket as CAddress */
    


    fanquake commented at 11:23 AM on February 13, 2025:

    Still documented as returning a CAddress?


    vasild commented at 11:40 AM on February 13, 2025:

    Should be "CService", fixed, thanks!

  6. vasild force-pushed on Feb 13, 2025
  7. vasild commented at 11:39 AM on February 13, 2025: contributor

    74f42a5920...cd4bfaee10: address #31854 (review)

  8. laanwj commented at 12:57 PM on February 13, 2025: member

    Concept ACK

  9. hodlinator approved
  10. hodlinator commented at 2:47 PM on February 13, 2025: contributor

    ACK cd4bfaee103591f212b88afd17969c16c1056eb6

    Concept

    Defers "faking" of CAddress by initializing CAddress::nServices = NODE_NONE when all we actually have is a CService (network address+port), to all the way down to the construction of CNode:

    https://github.com/bitcoin/bitcoin/blob/cd4bfaee103591f212b88afd17969c16c1056eb6/src/net.cpp#L1831-L1833

    Type safety sanity check

    graph LR;
        CService-->CNetAddr;
        CAddress-->CService;
    

    CAddress, which inherits from CService, has non-explicit constructors taking either 0 or 2 arguments, so implicit conversion CService -> CAddress is not an issue (verified to give compiler error). CAddress -> CService is allowed due to implicitly generated CService copy-constructor, but should be acceptable. Similarly, CService -> CNetAddr works, but the inverse does not (also verified).

  11. DrahtBot requested review from laanwj on Feb 13, 2025
  12. Sjors commented at 10:06 AM on February 14, 2025: member

    ACK cd4bfaee10

  13. laanwj approved
  14. laanwj commented at 11:08 AM on February 14, 2025: member

    Code review ACK cd4bfaee103591f212b88afd17969c16c1056eb6

  15. achow101 commented at 6:49 PM on February 14, 2025: member

    ACK cd4bfaee103591f212b88afd17969c16c1056eb6

  16. achow101 merged this on Feb 14, 2025
  17. achow101 closed this on Feb 14, 2025

  18. jonatack commented at 7:00 PM on February 14, 2025: member

    Post-merge ACK cd4bfaee103591f212b88afd17969c16c1056eb6. Good idea.

  19. vasild deleted the branch on Feb 17, 2025

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-16 09:13 UTC

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