From d8e04255e0a3d4b5f09c555e3e2acb1d60b990de Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:41:28 +0000 Subject: [PATCH 1/4] refactor: remove `rfind` workaround with `starts_with` --- src/bitcoin-cli.cpp | 2 +- src/test/fuzz/rpc.cpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index cd691e151c..bd121d01e3 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -551,7 +551,7 @@ public: peer.is_outbound ? "out" : "in", ConnectionTypeForNetinfo(peer.conn_type), peer.network, - peer.transport_protocol_type.rfind('v', 0) == 0 ? peer.transport_protocol_type[1] : ' ', + peer.transport_protocol_type.starts_with('v') == 0 ? peer.transport_protocol_type[1] : ' ', PingTimeToString(peer.min_ping), PingTimeToString(peer.ping), peer.last_send ? ToString(m_time_now - peer.last_send) : "", diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp index 435ebc1b91..fe65a71736 100644 --- a/src/test/fuzz/rpc.cpp +++ b/src/test/fuzz/rpc.cpp @@ -357,9 +357,7 @@ FUZZ_TARGET_INIT(rpc, initialize_rpc) rpc_testing_setup->CallRPC(rpc_command, arguments); } catch (const UniValue& json_rpc_error) { const std::string error_msg{find_value(json_rpc_error, "message").get_str()}; - // Once c++20 is allowed, starts_with can be used. - // if (error_msg.starts_with("Internal bug detected")) { - if (0 == error_msg.rfind("Internal bug detected", 0)) { + if (error_msg.starts_with("Internal bug detected")) { // Only allow the intentional internal bug assert(error_msg.find("trigger_internal_bug") != std::string::npos); } From acda80ed4ffa61644593d575930e13da75843403 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:54:19 +0000 Subject: [PATCH 2/4] refactor: use `constexpr` `std::`{`copy`, `fill`} in `V2ShortIDs` --- src/net.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 3b54c7422c..9fac69f7f9 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1120,16 +1120,9 @@ constexpr std::array V2ShortIDs() { static_assert(std::size(V2_DASH_IDS) <= 128); std::array ret{}; - for (size_t idx{0}; idx < std::size(ret); idx++) { - if (idx < 128 && idx < std::size(V2_BITCOIN_IDS)) { - ret[idx] = V2_BITCOIN_IDS[idx]; - } else if (idx >= 128 && idx - 128 < std::size(V2_DASH_IDS)) { - ret[idx] = V2_DASH_IDS[idx - 128]; - } else { - ret[idx] = ""; - } - } - + std::fill(ret.begin(), ret.end(), ""); + std::copy(V2_BITCOIN_IDS.begin(), V2_BITCOIN_IDS.end(), ret.begin()); + std::copy(V2_DASH_IDS.begin(), V2_DASH_IDS.end(), ret.begin() + 128); return ret; } From 4717fe8045c887db365ab48a3a17a4980d4fd373 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:06:59 +0000 Subject: [PATCH 3/4] refactor: avoid convoluted casting by not using `std::time_t` std::clock_cast is a part of C++20, hence the original TODO comment but it's still not implemented in Clang, as of this writing. We can sidestep the cast by not using time_t to begin with. --- src/wallet/wallet.cpp | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 576703ab60..79ecd2318b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include