From fbacad1880341ace31f669530c66d4e322d19235 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Fri, 29 May 2020 18:49:26 +0200 Subject: 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`. --- src/protocol.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/protocol.cpp') diff --git a/src/protocol.cpp b/src/protocol.cpp index 737baff36..56071f474 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -195,9 +195,10 @@ const std::vector &getAllNetMessageTypes() return allNetMessageTypesVec; } -std::string serviceFlagToStr(const uint64_t mask, const int bit) +std::string serviceFlagToStr(size_t bit) { - switch (ServiceFlags(mask)) { + const uint64_t service_flag = 1ULL << bit; + switch ((ServiceFlags)service_flag) { case NODE_NONE: abort(); // impossible case NODE_NETWORK: return "NETWORK"; case NODE_GETUTXO: return "GETUTXO"; @@ -211,7 +212,7 @@ std::string serviceFlagToStr(const uint64_t mask, const int bit) stream.imbue(std::locale::classic()); stream << "UNKNOWN["; if (bit < 8) { - stream << mask; + stream << service_flag; } else { stream << "2^" << bit; } -- cgit v1.2.3 From 189ae0c38b7d4927c5c73b94664e9542b2b06ed9 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Fri, 29 May 2020 18:52:59 +0200 Subject: util: dedup code in callers of serviceFlagToStr() Introduce `serviceFlagsToStr()` which hides the internals of the bitmask and simplifies callers of `serviceFlagToStr()`. --- src/protocol.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/protocol.cpp') diff --git a/src/protocol.cpp b/src/protocol.cpp index 56071f474..93e76f1f1 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -195,7 +195,12 @@ const std::vector &getAllNetMessageTypes() return allNetMessageTypesVec; } -std::string serviceFlagToStr(size_t bit) +/** + * Convert a service flag (NODE_*) to a human readable string. + * It supports unknown service flags which will be returned as "UNKNOWN[...]". + * @param[in] bit the service flag is calculated as (1 << bit) + */ +static std::string serviceFlagToStr(size_t bit) { const uint64_t service_flag = 1ULL << bit; switch ((ServiceFlags)service_flag) { @@ -219,3 +224,16 @@ std::string serviceFlagToStr(size_t bit) stream << "]"; return stream.str(); } + +std::vector serviceFlagsToStr(uint64_t flags) +{ + std::vector str_flags; + + for (size_t i = 0; i < sizeof(flags) * 8; ++i) { + if (flags & (1ULL << i)) { + str_flags.emplace_back(serviceFlagToStr(i)); + } + } + + return str_flags; +} -- cgit v1.2.3