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: contributor

    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. vasild cross-referenced this on May 29, 2020 from issue Consolidate service flag bit-to-name conversion to a shared serviceFlagToStr function by luke-jr
  4. hebasto commented at 2:00 PM on May 29, 2020: member

    Concept ACK.

  5. 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.

  6. 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:
        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

    double a = 1.2;
    uint64_t b;
    b = 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.

  7. 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

  8. MarcoFalke approved
  9. MarcoFalke commented at 2:05 PM on May 29, 2020: member

    ACK, just some questions.

  10. 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.

  11. 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
  12. util: dedup code in callers of serviceFlagToStr()
    Introduce `serviceFlagsToStr()` which hides the internals of the bitmask
    and simplifies callers of `serviceFlagToStr()`.
    189ae0c38b
  13. vasild force-pushed on May 29, 2020
  14. MarcoFalke commented at 5:06 PM on May 29, 2020: member

    ACK 189ae0c38b7d4927c5c73b94664e9542b2b06ed9

  15. vasild commented at 5:07 PM on May 29, 2020: contributor

    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:

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

    to:

    for (const auto& flag : serviceFlagsToStr(mask)) {
        ...
    }
    
  16. jonasschnelli commented at 5:42 PM on May 29, 2020: contributor

    Tested ACK 189ae0c38b7d4927c5c73b94664e9542b2b06ed9

  17. jonasschnelli merged this on May 29, 2020
  18. jonasschnelli closed this on May 29, 2020

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