addrman: Fix AddrMan::Add() return semantics and logging #23380

pull jnewbery wants to merge 4 commits into bitcoin:master from jnewbery:2021-10-addrman-add-logging changing 6 files +87 −74
  1. jnewbery commented at 12:03 PM on October 28, 2021: member

    Previously, Add() would return true if the function created a new AddressInfo object, even if that object could not be successfully entered into the new table and was deleted. That would happen if the new table position was already taken and the existing entry could not be removed.

    Instead, return true if the new AddressInfo object is successfully entered into the new table. This fixes a bug in the "Added %i addresses" log, which would not always accurately log how many addresses had been added.

  2. [addrman] Add doxygen comment to AddrMan::Add()
    Does not document the return value since we're going to fix the
    semantics in a future commit.
    e58598e833
  3. [addrman] Rename Add_() to AddSingle() 2658eb6d68
  4. jnewbery commented at 12:04 PM on October 28, 2021: member

    @mzumsande - I think this fixes the logging bug that you encountered.

  5. mzumsande commented at 12:38 PM on October 28, 2021: member

    Concept ACK I encountered this in the form of the RPC addpeeraddress returning true although nothing was added - which is fixed by this PR as well.

  6. jnewbery force-pushed on Oct 28, 2021
  7. [addrman] Add Add_() inner function, fix Add() return semantics
    Previously, Add() would return true if the function created a new
    AddressInfo object, even if that object could not be successfully
    entered into the new table and was deleted. That would happen if the new
    table position was already taken and the existing entry could not be
    removed.
    
    Instead, return true if the new AddressInfo object is successfully
    entered into the new table. This fixes a bug in the "Added %i addresses"
    log, which would not always accurately log how many addresses had been
    added.
    
    p2p_addrv2_relay.py and p2p_addr_relay.py need to be updated since they
    were incorrectly asserting on the buggy log (assuming that addresses are
    added to addrman, when there could in fact be new table position
    collisions that prevent some of those address records from being added).
    2095df7b7b
  8. [MOVEONLY] reorder functions in addrman_impl.h and addrman.cpp
    Keep the internal {Function}_() functions grouped together.
    
    Review with `git diff --color-moved=dimmed-zebra`
    61ec0539b2
  9. jnewbery force-pushed on Oct 28, 2021
  10. DrahtBot added the label P2P on Oct 28, 2021
  11. shaavan approved
  12. shaavan commented at 1:27 PM on October 28, 2021: contributor

    ACK 61ec0539b26a902a41a2602187a71f9dba3c6935

    This PR primarily deals with the bug, where Addrman::Add() returns true, even when the AddrInfo object has not been successfully added to the table. This PR also focuses on other minor changes.

    (A short description of this PR, in chronological order of the commits. This might help fellow reviewers to understand this PR better.)

    Description:

    • Expanded the documentation for the Addrman::Add() function making it more clear.
    • In AddrManImpl class Add_() has been renamed to AddSingle() and a new definition of Add_() is introduced which can tackle addition of multiple instances AddressInfo object.
    • AddSingle() function has been appropriately modified to return bool 0 if the AdrrInfo object has not been successfully entered into the table.
    • AddrmanImpl::Add() has been appropriately modified to inculcate the changes. One doubt though: was there any specific reason for using auto ret = instead of bool ret = ?
    • Finally, reordering of AddrManImpl::Good_() has been done to group internal functions together.

    This PR is able to correct the described bug successfully, and I agree with the changes made in this PR.

    A Sidenote:

    (Note: This is not specifically part of this PR. And can be taken as a brainstorming activity)

    In the AddrmanImpl class, what do you think about making the internal {Function}_() functions protected or private, as a non-class member need not call this function directly, and they should call their respective {Function}()?

    <p>

  13. jnewbery commented at 1:46 PM on October 28, 2021: member

    Thank you for the very thorough review @shaavan!

    (Note: This is not specifically part of this PR. And can be taken as a brainstorming activity)

    In the AddrmanImpl class, what do you think about making the internal {Function}_() functions protected or private, as a non-class member need not call this function directly, and they should call their respective {Function}()?

    You're exactly correct that they don't need to be accessed outside the class. In fact, they already are private. There's a private specifier a few lines above:

    https://github.com/bitcoin/bitcoin/blob/ab25ef8c7f767258d5fe44f53b35ad8bd51ed5cd/src/addrman_impl.h#L144

  14. shaavan commented at 1:56 PM on October 28, 2021: contributor

    There's a private specifier a few lines above:

    Oops, I missed that! 😅. Thanks for informing me!

  15. jnewbery commented at 2:07 PM on October 28, 2021: member

    AddrmanImpl::Add() has been appropriately modified to inculcate the changes. One doubt though: was there any specific reason for using auto ret = instead of bool ret = ? @shaavan - The idea was that all of the public functions could have a standard structure of:

    ReturnType AddrManImpl::Function(args...)
    {
        LOCK(cs);
        Check();
        const auto ret = Function_(args...);
        Check();
        return ret;
    }
    

    which could then be replaced by a variadic template to reduce the boilerplate/repetation. I guess that would still work if const auto ret = Function_(args...); was replaced by const ReturnType ret = Function_(args...); though.

    In any case, I don't think it matters too much either way.

  16. shaavan commented at 2:11 PM on October 28, 2021: contributor

    In any case, I don't think it matters too much either way.

    Makes sense. Both auto and RetType would have the same functionality. But the auto keyword allows maintaining a standard structure, so it makes sense to use auto here.

  17. naumenkogs commented at 7:47 AM on October 29, 2021: member

    ACK 61ec0539b26a902a41a2602187a71f9dba3c6935

    The bug is fixed and the refactoring makes sense.

  18. mzumsande commented at 8:21 PM on October 30, 2021: member

    ACK 61ec0539b26a902a41a2602187a71f9dba3c6935

    Reviewed code and verified that addpeeraddress now actually returns whether the address was successfully added.

  19. fanquake merged this on Nov 1, 2021
  20. fanquake closed this on Nov 1, 2021

  21. jnewbery deleted the branch on Nov 1, 2021
  22. sidhujag referenced this in commit 6d6af11505 on Nov 1, 2021
  23. jonatack referenced this in commit cb8959af71 on Nov 3, 2021
  24. jonatack commented at 7:56 PM on November 3, 2021: member

    Concept ACK. Good idea. I'm seeing addrman unit test failures that appear to begin with 2095df7b7bfcb; maybe there is an interaction with other changes on master that wasn't apparent in this branch, but I am only beginning to catch up with the changes of the past few weeks. Edit: it looks like the errors had a different cause.

  25. DrahtBot locked this on Nov 4, 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-14 21:13 UTC

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