util: simplify the interface of serviceFlagToStr() #19106

pull vasild wants to merge 2 commits into bitcoin:master from vasild:serviceFlagToStr changing 4 files +32 −16
  1. vasild commented at 1:57 pm on May 29, 2020: member

    Don’t take two redundant arguments in serviceFlagToStr().

    Introduce serviceFlagsToStr() which takes a mask (with more than one bit set) and returns a vector of strings.

    As a side effect this fixes an issue introduced in #18165 due to which the GUI could print something like UNKNOWN[1033] & UNKNOWN[1033] & UNKNOWN[2^10] instead of NETWORK & WITNESS.

  2. fanquake added the label Utils/log/libs on May 29, 2020
  3. hebasto commented at 2:00 pm on May 29, 2020: member
    Concept ACK.
  4. in src/protocol.cpp:220 in 0b6f4a0729 outdated
    216@@ -211,10 +217,23 @@ std::string serviceFlagToStr(const uint64_t mask, const int bit)
    217     stream.imbue(std::locale::classic());
    218     stream << "UNKNOWN[";
    219     if (bit < 8) {
    220-        stream << mask;
    221+        stream << service_flag;
    


    MarcoFalke commented at 2:03 pm on May 29, 2020:
    Why is this branch needed? 2^7 or similar should read just fine and help with consistency, no?

    vasild commented at 2:16 pm on May 29, 2020:

    It was like this in the original code and I am trying to limit the amount of changes. Especially this would be user-visible change. Otherwise I agree that for consistency we may just print 2^N.

    I do not know why it was done like that. Maybe with the assumption that e.g. 64 is more human-readable than 2^6, but 524288 is not more human-readable than 2^19.

    It was introduced in https://github.com/bitcoin/bitcoin/commit/df77de8c2#diff-a459a939be4fe065dd5f64ab60bb30d0L842 @luke-jr ?


    luke-jr commented at 6:40 pm on May 29, 2020:
    No, it predates that commit too.
  5. in src/qt/guiutil.cpp:758 in 0b6f4a0729 outdated
    759-        uint64_t check = 1ull << i;
    760-        if (mask & check)
    761-        {
    762-            strList.append(QString::fromStdString(serviceFlagToStr(mask, i)));
    763-        }
    764+    for (const auto& flag : serviceFlagsToStr((uint64_t)mask)) {
    


    MarcoFalke commented at 2:04 pm on May 29, 2020:
    0    for (const auto& flag : serviceFlagsToStr(uint64_t{mask})) {
    

    nit: this is slightly preferable (assuming it compiles)


    vasild commented at 5:04 pm on May 29, 2020:

    Thanks for the hint. I wasn’t aware that

    0double a = 1.2;
    1uint64_t b;
    2b = uint64_t{a};
    

    wouldn’t compile due to type narrowing (whereas b = (uint64_t)a would compile).

    In our case however the typecast is not needed, so I have removed it altogether.

  6. in src/rpc/util.cpp:846 in 0b6f4a0729 outdated
    847-    for (int i = 0; i < 64; ++i) {
    848-        const uint64_t mask = 1ull << i;
    849-        if (services_n & mask) {
    850-            servicesNames.push_back(serviceFlagToStr(mask, i));
    851-        }
    852+    for (const auto& flag : serviceFlagsToStr((uint64_t)services)) {
    


    MarcoFalke commented at 2:04 pm on May 29, 2020:
    Same
  7. MarcoFalke approved
  8. MarcoFalke commented at 2:05 pm on May 29, 2020: member
    ACK, just some questions.
  9. luke-jr commented at 3:01 pm on May 29, 2020: member

    You have 3 changes here, but only 1 commit. Each logical change should be a separate commit. So bugfix, refactor/simplification, and then new serviceFlagsToStr.

    I don’t think serviceFlagsToStr has any purpose though… both callers need to loop over the vector to transform it.

  10. util: simplify the interface of serviceFlagToStr()
    Don't take two redundant arguments in `serviceFlagToStr()`.
    
    As a side effect this fixes an issue introduced in
    https://github.com/bitcoin/bitcoin/pull/18165 due to which the GUI could
    print something like `UNKNOWN[1033] & UNKNOWN[1033] & UNKNOWN[2^10]`
    instead of `NETWORK & WITNESS`.
    fbacad1880
  11. util: dedup code in callers of serviceFlagToStr()
    Introduce `serviceFlagsToStr()` which hides the internals of the bitmask
    and simplifies callers of `serviceFlagToStr()`.
    189ae0c38b
  12. vasild force-pushed on May 29, 2020
  13. MarcoFalke commented at 5:06 pm on May 29, 2020: member
    ACK 189ae0c38b7d4927c5c73b94664e9542b2b06ed9
  14. vasild commented at 5:07 pm on May 29, 2020: member

    You have 3 changes here, but only 1 commit. Each logical change should be a separate commit. So bugfix, refactor/simplification, and then new serviceFlagsToStr.

    The refactor/simplification of changing serviceFlagToStr() to take 1 argument instead of 2 is also the bug fix. So we have 2 changes. I split it to two commits even though the second one overwrites parts of the first one.

    I don’t think serviceFlagsToStr has any purpose though… both callers need to loop over the vector to transform it.

    Its purpose is to simplify the two callers which had duplicated code like:

    0for (int i = 0; i < 64; i++) {
    1    uint64_t check = 1ull << i;
    2    if (mask & check)
    3    {
    4        ...
    5    }
    6}
    

    to:

    0for (const auto& flag : serviceFlagsToStr(mask)) {
    1    ...
    2}
    
  15. jonasschnelli commented at 5:42 pm on May 29, 2020: contributor
    Tested ACK 189ae0c38b7d4927c5c73b94664e9542b2b06ed9
  16. jonasschnelli merged this on May 29, 2020
  17. jonasschnelli closed this on May 29, 2020

  18. vasild deleted the branch on May 29, 2020
  19. jasonbcox referenced this in commit 3114174d88 on Nov 25, 2020
  20. PastaPastaPasta referenced this in commit c5f3b478bf on Sep 21, 2021
  21. kittywhiskers referenced this in commit 83c7694c59 on Oct 12, 2021
  22. 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: 2024-07-05 19:13 UTC

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