util: dedup code in callers of serviceFlagToStr()

Introduce `serviceFlagsToStr()` which hides the internals of the bitmask
and simplifies callers of `serviceFlagToStr()`.
This commit is contained in:
Vasil Dimov 2020-05-29 18:52:59 +02:00
parent fbacad1880
commit 189ae0c38b
No known key found for this signature in database
GPG key ID: 54DF06F64B55CBBF
4 changed files with 26 additions and 16 deletions

View file

@ -195,7 +195,12 @@ const std::vector<std::string> &getAllNetMessageTypes()
return allNetMessageTypesVec; 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; const uint64_t service_flag = 1ULL << bit;
switch ((ServiceFlags)service_flag) { switch ((ServiceFlags)service_flag) {
@ -219,3 +224,16 @@ std::string serviceFlagToStr(size_t bit)
stream << "]"; stream << "]";
return stream.str(); return stream.str();
} }
std::vector<std::string> serviceFlagsToStr(uint64_t flags)
{
std::vector<std::string> 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;
}

View file

@ -289,11 +289,11 @@ enum ServiceFlags : uint64_t {
}; };
/** /**
* Convert a service flag (NODE_*) to a human readable string. * Convert service flags (a bitmask of NODE_*) to human readable strings.
* It supports unknown service flags which will be returned as "UNKNOWN[...]". * It supports unknown service flags which will be returned as "UNKNOWN[...]".
* @param[in] bit the service flag is calculated as (1 << bit) * @param[in] flags multiple NODE_* bitwise-OR-ed together
*/ */
std::string serviceFlagToStr(size_t bit); std::vector<std::string> serviceFlagsToStr(uint64_t flags);
/** /**
* Gets the set of service flags which are "desirable" for a given peer. * Gets the set of service flags which are "desirable" for a given peer.

View file

@ -755,12 +755,8 @@ QString formatServicesStr(quint64 mask)
{ {
QStringList strList; QStringList strList;
for (int i = 0; i < 64; i++) { for (const auto& flag : serviceFlagsToStr(mask)) {
uint64_t check = 1ull << i; strList.append(QString::fromStdString(flag));
if (mask & check)
{
strList.append(QString::fromStdString(serviceFlagToStr(i)));
}
} }
if (strList.size()) if (strList.size())

View file

@ -841,14 +841,10 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
UniValue GetServicesNames(ServiceFlags services) UniValue GetServicesNames(ServiceFlags services)
{ {
const uint64_t services_n = services;
UniValue servicesNames(UniValue::VARR); UniValue servicesNames(UniValue::VARR);
for (int i = 0; i < 64; ++i) { for (const auto& flag : serviceFlagsToStr(services)) {
const uint64_t mask = 1ull << i; servicesNames.push_back(flag);
if (services_n & mask) {
servicesNames.push_back(serviceFlagToStr(i));
}
} }
return servicesNames; return servicesNames;