Skip to content

Commit fbacad1

Browse files
committed
util: simplify the interface of serviceFlagToStr()
Don't take two redundant arguments in `serviceFlagToStr()`. As a side effect this fixes an issue introduced in bitcoin/bitcoin#18165 due to which the GUI could print something like `UNKNOWN[1033] & UNKNOWN[1033] & UNKNOWN[2^10]` instead of `NETWORK & WITNESS`.
1 parent f2e2c5e commit fbacad1

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/protocol.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ const std::vector<std::string> &getAllNetMessageTypes()
195195
return allNetMessageTypesVec;
196196
}
197197

198-
std::string serviceFlagToStr(const uint64_t mask, const int bit)
198+
std::string serviceFlagToStr(size_t bit)
199199
{
200-
switch (ServiceFlags(mask)) {
200+
const uint64_t service_flag = 1ULL << bit;
201+
switch ((ServiceFlags)service_flag) {
201202
case NODE_NONE: abort(); // impossible
202203
case NODE_NETWORK: return "NETWORK";
203204
case NODE_GETUTXO: return "GETUTXO";
@@ -211,7 +212,7 @@ std::string serviceFlagToStr(const uint64_t mask, const int bit)
211212
stream.imbue(std::locale::classic());
212213
stream << "UNKNOWN[";
213214
if (bit < 8) {
214-
stream << mask;
215+
stream << service_flag;
215216
} else {
216217
stream << "2^" << bit;
217218
}

src/protocol.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,12 @@ enum ServiceFlags : uint64_t {
288288
// BIP process.
289289
};
290290

291-
std::string serviceFlagToStr(uint64_t mask, int bit);
291+
/**
292+
* Convert a service flag (NODE_*) to a human readable string.
293+
* It supports unknown service flags which will be returned as "UNKNOWN[...]".
294+
* @param[in] bit the service flag is calculated as (1 << bit)
295+
*/
296+
std::string serviceFlagToStr(size_t bit);
292297

293298
/**
294299
* Gets the set of service flags which are "desirable" for a given peer.

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ QString formatServicesStr(quint64 mask)
759759
uint64_t check = 1ull << i;
760760
if (mask & check)
761761
{
762-
strList.append(QString::fromStdString(serviceFlagToStr(mask, i)));
762+
strList.append(QString::fromStdString(serviceFlagToStr(i)));
763763
}
764764
}
765765

src/rpc/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ UniValue GetServicesNames(ServiceFlags services)
847847
for (int i = 0; i < 64; ++i) {
848848
const uint64_t mask = 1ull << i;
849849
if (services_n & mask) {
850-
servicesNames.push_back(serviceFlagToStr(mask, i));
850+
servicesNames.push_back(serviceFlagToStr(i));
851851
}
852852
}
853853

0 commit comments

Comments
 (0)