Skip to content

Commit de369c7

Browse files
committed
Merge bitcoin#18165: Consolidate service flag bit-to-name conversion to a shared serviceFlagToStr function
c31bc5b Consolidate service flag bit-to-name conversion to a shared serviceFlagToStr function (Luke Dashjr) cea91a1 Bugfix: GUI: Use unsigned long long type to avoid implicit conversion of MSB check (Luke Dashjr) Pull request description: Side effect: this results in the RPC showing unknown service bits as "UNKNOWN[n]" like the GUI. Note that there is no common mask-to-`vector<string>` function because both GUI and RPC would need to iterate through it to convert to their desired target formats. ACKs for top commit: jonasschnelli: utACK ~~cea91a1e40e12029140ebfba969ce3ef2965029c~~ c31bc5b Tree-SHA512: 32c7ba8ac7ef2d4087f4f317447ae93a328ec9fb9ad81301df2fbaeeb21a3db7a503187a369552b05a9414251b7cf8e15bcde74c1ea2ef36591ea7ffb6721f60
2 parents f4b603c + c31bc5b commit de369c7

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

src/protocol.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,27 @@ const std::vector<std::string> &getAllNetMessageTypes()
194194
{
195195
return allNetMessageTypesVec;
196196
}
197+
198+
std::string serviceFlagToStr(const uint64_t mask, const int bit)
199+
{
200+
switch (ServiceFlags(mask)) {
201+
case NODE_NONE: abort(); // impossible
202+
case NODE_NETWORK: return "NETWORK";
203+
case NODE_GETUTXO: return "GETUTXO";
204+
case NODE_BLOOM: return "BLOOM";
205+
case NODE_WITNESS: return "WITNESS";
206+
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
207+
// Not using default, so we get warned when a case is missing
208+
}
209+
210+
std::ostringstream stream;
211+
stream.imbue(std::locale::classic());
212+
stream << "UNKNOWN[";
213+
if (bit < 8) {
214+
stream << mask;
215+
} else {
216+
stream << "2^" << bit;
217+
}
218+
stream << "]";
219+
return stream.str();
220+
}

src/protocol.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ const std::vector<std::string>& getAllNetMessageTypes();
257257

258258
/** nServices flags */
259259
enum ServiceFlags : uint64_t {
260-
// NOTE: When adding here, be sure to update qt/guiutil.cpp's formatServicesStr too
260+
// NOTE: When adding here, be sure to update serviceFlagToStr too
261261
// Nothing
262262
NODE_NONE = 0,
263263
// NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
@@ -288,6 +288,8 @@ enum ServiceFlags : uint64_t {
288288
// BIP process.
289289
};
290290

291+
std::string serviceFlagToStr(uint64_t mask, int bit);
292+
291293
/**
292294
* Gets the set of service flags which are "desirable" for a given peer.
293295
*

src/qt/guiutil.cpp

+2-20
Original file line numberDiff line numberDiff line change
@@ -751,33 +751,15 @@ QString formatDurationStr(int secs)
751751
return strList.join(" ");
752752
}
753753

754-
QString serviceFlagToStr(const quint64 mask, const int bit)
755-
{
756-
switch (ServiceFlags(mask)) {
757-
case NODE_NONE: abort(); // impossible
758-
case NODE_NETWORK: return "NETWORK";
759-
case NODE_GETUTXO: return "GETUTXO";
760-
case NODE_BLOOM: return "BLOOM";
761-
case NODE_WITNESS: return "WITNESS";
762-
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
763-
// Not using default, so we get warned when a case is missing
764-
}
765-
if (bit < 8) {
766-
return QString("%1[%2]").arg("UNKNOWN").arg(mask);
767-
} else {
768-
return QString("%1[2^%2]").arg("UNKNOWN").arg(bit);
769-
}
770-
}
771-
772754
QString formatServicesStr(quint64 mask)
773755
{
774756
QStringList strList;
775757

776758
for (int i = 0; i < 64; i++) {
777-
uint64_t check = 1LL << i;
759+
uint64_t check = 1ull << i;
778760
if (mask & check)
779761
{
780-
strList.append(serviceFlagToStr(check, i));
762+
strList.append(QString::fromStdString(serviceFlagToStr(mask, i)));
781763
}
782764
}
783765

src/rpc/util.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -841,18 +841,15 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
841841

842842
UniValue GetServicesNames(ServiceFlags services)
843843
{
844+
const uint64_t services_n = services;
844845
UniValue servicesNames(UniValue::VARR);
845846

846-
if (services & NODE_NETWORK)
847-
servicesNames.push_back("NETWORK");
848-
if (services & NODE_GETUTXO)
849-
servicesNames.push_back("GETUTXO");
850-
if (services & NODE_BLOOM)
851-
servicesNames.push_back("BLOOM");
852-
if (services & NODE_WITNESS)
853-
servicesNames.push_back("WITNESS");
854-
if (services & NODE_NETWORK_LIMITED)
855-
servicesNames.push_back("NETWORK_LIMITED");
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+
}
856853

857854
return servicesNames;
858855
}

0 commit comments

Comments
 (0)