[addrman] Move serialization code to cpp #22740

pull amitiuttarwar wants to merge 5 commits into bitcoin:master from amitiuttarwar:2021-08-move-addrman-serialize changing 2 files +311 −308
  1. amitiuttarwar commented at 5:10 AM on August 19, 2021: contributor

    Moving the serialization code from the header to the cpp helps clarify interfaces vs internals, as well as speed up the compilation of the whole program with a smaller header file.

  2. fanquake added the label P2P on Aug 19, 2021
  3. jonatack commented at 1:36 PM on August 20, 2021: member

    Concept ACK. Ready for rebase now that #22697 is merged.

  4. MarcoFalke commented at 1:45 PM on August 20, 2021: member

    Is there a reason to not move every implementation to the cpp file?

  5. amitiuttarwar force-pushed on Aug 20, 2021
  6. amitiuttarwar commented at 7:48 PM on August 20, 2021: contributor

    @MarcoFalke

    Is there a reason to not move every implementation to the cpp file?

    hmm, are you referring to the definitions of the wrapper functions? Most of what's left in the header are the simple public functions that acquire lock, call check & invoke the private functions.

  7. amitiuttarwar marked this as ready for review on Aug 20, 2021
  8. DrahtBot commented at 2:15 AM on August 21, 2021: 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:

    • #22734 (addrman: Avoid crash on corrupt data, Force Check after deserialize by MarcoFalke)
    • #22094 (p2p: fix ubsan addrman errors, make nTime truncation conversion explicit by jonatack)

    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.

  9. in src/test/addrman_tests.cpp:115 in 1e4744cd8b outdated
     110 | @@ -111,8 +111,8 @@ class CAddrManTest : public CAddrMan
     111 |      {
     112 |          LOCK(cs);
     113 |          int nId = mapAddr[addr];
     114 | -        for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
     115 | -            for (int entry = 0; entry < ADDRMAN_BUCKET_SIZE; ++entry) {
     116 | +        for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
     117 | +            for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; ++entry) {
    


    jnewbery commented at 3:58 PM on August 25, 2021:

    I don't think these need to change any more, now that you're defining ADDRMAN_NEW_BUCKET_COUNT and ADDRMAN_BUCKET_SIZE as ints.


    amitiuttarwar commented at 3:30 AM on August 26, 2021:

    fixed, thanks!

  10. jnewbery commented at 4:09 PM on August 25, 2021: member

    Concept ACK. A couple of suggestions:

    1. Add the following reviewer hint to commits [addrman] Move CAddrMan::Serialize to cpp file, [addrman] Move CAddrMan::Unserialize to cpp file and [move-only] Extract constants from addrman .h to .cpp:

    git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space

    1. The format specifier for printing the signed ints here is incorrect:
    --- a/src/addrman.cpp
    +++ b/src/addrman.cpp
    @@ -258,14 +258,14 @@ void CAddrMan::Unserialize(Stream& s_)
     
         if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
             throw std::ios_base::failure(
    -                strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %u]",
    +                strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %d]",
                         nNew,
                         ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
         }
     
         if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
             throw std::ios_base::failure(
    -                strprintf("Corrupt CAddrMan serialization: nTried=%d, should be in [0, %u]",
    +                strprintf("Corrupt CAddrMan serialization: nTried=%d, should be in [0, %d]",
                         nTried,
                         ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
         }
    

    I haven't checked format specifiers in other places. This may help in fixing them up: https://en.cppreference.com/w/cpp/io/c/fprintf.

  11. MarcoFalke commented at 4:27 PM on August 25, 2021: member

    I haven't checked format specifiers in other places

    We use tinyformat, so when in doubt you can (at no risk) just always use %s.

  12. amitiuttarwar force-pushed on Aug 26, 2021
  13. amitiuttarwar commented at 3:41 AM on August 26, 2021: contributor

    thanks for the review @jnewbery. took all your suggestions & updated the commit messages with reviewer hints. @MarcoFalke thanks for the tip about tinyformat! I had not realized the specifier interchangeability that provides. I did update the inconsistency that John pointed out, but seems like getting these specifiers right is generally unnecessary in our codebase!? 🤯

  14. in src/addrman.cpp:1036 in 954785dd1d outdated
    1032 | @@ -733,3 +1033,4 @@ std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
    1033 |      }
    1034 |      return bits;
    1035 |  }
    1036 | +
    


    jnewbery commented at 1:15 PM on August 26, 2021:

    Please remove extra newline


    amitiuttarwar commented at 6:56 PM on August 26, 2021:

    fixed

  15. in src/addrman.cpp:261 in 954785dd1d outdated
     256 | +        nUBuckets ^= (1 << 30);
     257 | +    }
     258 | +
     259 | +    if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
     260 | +        throw std::ios_base::failure(
     261 | +                strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %d]",
    


    jnewbery commented at 1:17 PM on August 26, 2021:

    These changes to the format specifiers should be in [addrman] Change addrman #define constants to be constexprs (where you declare the types of the constants) rather than [move-only] Extract constants from addrman .h to .cpp (which should be move-only)


    amitiuttarwar commented at 6:57 PM on August 26, 2021:

    blergh, my bad. fixed now.

  16. jnewbery commented at 1:17 PM on August 26, 2021: member

    Code review ACK 954785dd1d37968aba618743baf25760b8105a4c

  17. [addrman] Move CAddrMan::Serialize to cpp file
    Reviewer hint: use `git diff --color-moved=dimmed-zebra
    --color-moved-ws=ignore-all-space`
    
    Co-authored-by: Amiti Uttarwar <amiti@uttarwar.org>
    1622543cf4
  18. [addrman] Move CAddrMan::Unserialize to cpp file
    Reviewer hint: use `git diff --color-moved=dimmed-zebra
    --color-moved-ws=ignore-all-space`
    
    Co-authored-by: Amiti Uttarwar <amiti@uttarwar.org>
    a65053f1d4
  19. [addrman] Change addrman #define constants to be constexprs
    Co-authored-by: John Newbery <john@johnnewbery.com>
    7dc443a62d
  20. [move-only] Extract constants from addrman .h to .cpp
    Reviewer hint: use `git diff --color-moved=dimmed-zebra
    --color-moved-ws=ignore-all-space`
    
    Co-authored-by: John Newbery <john@johnnewbery.com>
    af9638a0fb
  21. [refactor] [addrman] Update constant comments
    Co-authored-by: Amiti Uttarwar <amiti@uttarwar.org>
    85b15ddc8f
  22. amitiuttarwar force-pushed on Aug 26, 2021
  23. amitiuttarwar commented at 6:58 PM on August 26, 2021: contributor

    thanks for the review @jnewbery, I fixed those small issues.

  24. jnewbery commented at 10:05 AM on August 27, 2021: member

    Code review ACK 85b15ddc8ff499fe21d8ab35ece3994f8878b3de

  25. 0xB10C commented at 12:12 PM on August 30, 2021: member

    Code review ACK 85b15ddc8

    Thanks for the reviewer hints. I found https://stackoverflow.com/questions/42388077/constexpr-vs-macros/42388687 useful to learn about #define vs constexpr.

  26. amitiuttarwar commented at 4:46 PM on August 30, 2021: contributor

    thanks @0xB10C, for the review and the useful link :)

  27. in src/addrman.cpp:22 in 85b15ddc8f
      22 | -//! over how many buckets entries with new addresses originating from a single group are spread
      23 | +/** Over how many buckets entries with new addresses originating from a single group are spread */
      24 |  static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
      25 | -
      26 | -//! in how many buckets for entries with new addresses a single address may occur
      27 | +/** Maximum number of times an address can be added to the new table */
    


    mzumsande commented at 9:02 PM on August 31, 2021:

    nit: I think the old comment was more precise in referring to state rather than process ("added") - an address could be added more often over time, in the not untypical case where earlier instances were replaced due to new table collisions.


    amitiuttarwar commented at 9:42 PM on August 31, 2021:

    ah, I see what you're saying. maybe this would be better?

    /** Maximum number of times an address can occur in the new table */
    

    I'm not planning to address right now, but will keep it in mind for future changes :)

  28. mzumsande commented at 9:32 PM on August 31, 2021: member

    Code Review ACK 85b15ddc8ff499fe21d8ab35ece3994f8878b3de (+ performed some light testing)

  29. fanquake merged this on Sep 1, 2021
  30. fanquake closed this on Sep 1, 2021

  31. sidhujag referenced this in commit ab79dcf8f2 on Sep 1, 2021
  32. MarcoFalke commented at 9:40 AM on September 2, 2021: member

    When moving code from the h to the cpp, it could make sense to also move the includes:

    diff --git a/src/addrman.cpp b/src/addrman.cpp
    index 5d9aeec1b1..9e8534e70e 100644
    --- a/src/addrman.cpp
    +++ b/src/addrman.cpp
    @@ -5,10 +5,12 @@
     
     #include <addrman.h>
     
    +#include <clientversion.h>
     #include <hash.h>
     #include <logging.h>
     #include <netaddress.h>
     #include <serialize.h>
    +#include <streams.h>
     
     #include <cmath>
     #include <optional>
    diff --git a/src/addrman.h b/src/addrman.h
    index 13b53fdc78..561589d413 100644
    --- a/src/addrman.h
    +++ b/src/addrman.h
    @@ -6,23 +6,16 @@
     #ifndef BITCOIN_ADDRMAN_H
     #define BITCOIN_ADDRMAN_H
     
    -#include <clientversion.h>
    -#include <config/bitcoin-config.h>
     #include <fs.h>
    -#include <hash.h>
    +#include <logging.h>
     #include <netaddress.h>
     #include <protocol.h>
    -#include <random.h>
    -#include <streams.h>
     #include <sync.h>
     #include <timedata.h>
    -#include <tinyformat.h>
    -#include <util/system.h>
     
    -#include <iostream>
    +#include <cstdint>
     #include <optional>
     #include <set>
    -#include <stdint.h>
     #include <unordered_map>
     #include <vector>
     
    diff --git a/src/net.cpp b/src/net.cpp
    index 9b1e17c587..ddb6a2d34f 100644
    --- a/src/net.cpp
    +++ b/src/net.cpp
    @@ -24,6 +24,7 @@
     #include <scheduler.h>
     #include <util/sock.h>
     #include <util/strencodings.h>
    +#include <util/system.h>
     #include <util/thread.h>
     #include <util/trace.h>
     #include <util/translation.h>
    diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp
    index 30c29eb356..adbf8028ea 100644
    --- a/src/qt/walletframe.cpp
    +++ b/src/qt/walletframe.cpp
    @@ -11,6 +11,7 @@
     #include <qt/psbtoperationsdialog.h>
     #include <qt/walletmodel.h>
     #include <qt/walletview.h>
    +#include <util/system.h>
     
     #include <cassert>
     
    diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
    index cd5dc2370f..f78001619e 100644
    --- a/src/test/addrman_tests.cpp
    +++ b/src/test/addrman_tests.cpp
    @@ -5,13 +5,14 @@
     #include <addrdb.h>
     #include <addrman.h>
     #include <chainparams.h>
    +#include <clientversion.h>
    +#include <hash.h>
    +#include <netbase.h>
    +#include <random.h>
     #include <test/data/asmap.raw.h>
     #include <test/util/setup_common.h>
     #include <util/asmap.h>
     #include <util/string.h>
    -#include <hash.h>
    -#include <netbase.h>
    -#include <random.h>
     
     #include <boost/test/unit_test.hpp>
     
    diff --git a/src/test/fuzz/versionbits.cpp b/src/test/fuzz/versionbits.cpp
    index 9186821836..73a7d24971 100644
    --- a/src/test/fuzz/versionbits.cpp
    +++ b/src/test/fuzz/versionbits.cpp
    @@ -6,6 +6,7 @@
     #include <chainparams.h>
     #include <consensus/params.h>
     #include <primitives/block.h>
    +#include <util/system.h>
     #include <versionbits.h>
     
     #include <test/fuzz/FuzzedDataProvider.h>
    
  33. amitiuttarwar commented at 2:31 PM on September 2, 2021: contributor

    @MarcoFalke good point. I'm planning to open a pull along these lines in the next few days, so will include this patch there. thanks :)

  34. jonatack commented at 3:31 PM on September 2, 2021: member

    Post-merge ACK

  35. DrahtBot locked this on Sep 2, 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-20 18:14 UTC

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