Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove C++17 workarounds #6380

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
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) : "",
Expand Down
13 changes: 3 additions & 10 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,16 +1120,9 @@ constexpr std::array<std::string_view, 256> V2ShortIDs() {
static_assert(std::size(V2_DASH_IDS) <= 128);

std::array<std::string_view, 256> 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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/test/fuzz/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 9 additions & 29 deletions src/util/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,20 @@
#ifndef BITCOIN_UTIL_RANGES_H
#define BITCOIN_UTIL_RANGES_H

#include <algorithm>
#include <ranges>
#include <optional>

//#if __cplusplus > 201703L // C++20 compiler
//namespace ranges = std::ranges;
//#else

#define MK_RANGE(FUN) \
template <typename X, typename Z> \
inline auto FUN(const X& ds, const Z& fn) { \
return std::FUN(cbegin(ds), cend(ds), fn); \
} \
template <typename X, typename Z> \
inline auto FUN(X& ds, const Z& fn) { \
return std::FUN(begin(ds), end(ds), fn); \
}


namespace ranges {
MK_RANGE(all_of)
MK_RANGE(any_of)
MK_RANGE(count_if)
MK_RANGE(find_if)
using namespace std::ranges;

template <typename X, typename Z>
constexpr inline auto find_if_opt(const X& ds, const Z& fn) {
const auto it = ranges::find_if(ds, fn);
if (it != end(ds)) {
return std::make_optional(*it);
}
return std::optional<std::decay_t<decltype(*it)>>{};
template <typename X, typename Z>
constexpr inline auto find_if_opt(const X& ds, const Z& fn) {
const auto it = std::ranges::find_if(ds, fn);
if (it != std::end(ds)) {
return std::make_optional(*it);
}

return std::optional<std::decay_t<decltype(*it)>>{};
}
}

//#endif // C++20 compiler
#endif // BITCOIN_UTIL_RANGES_H
38 changes: 12 additions & 26 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <policy/settings.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <reverse_iterator.h>
#include <script/descriptor.h>
#include <script/script.h>
#include <script/sign.h>
Expand Down Expand Up @@ -5309,44 +5310,29 @@ bool CWallet::AutoBackupWallet(const fs::path& wallet_path, bilingual_str& error
}

// Keep only the last 10 backups, including the new one of course
typedef std::multimap<std::time_t, fs::path> folder_set_t;
folder_set_t folder_set;
fs::directory_iterator end_iter;
std::multimap<fs::file_time_type, fs::path> folder_set;
// Build map of backup files for current(!) wallet sorted by last write time
fs::path currentFile;
for (fs::directory_iterator dir_iter(backupsDir); dir_iter != end_iter; ++dir_iter)
{
for (const auto& entry : fs::directory_iterator(backupsDir)) {
// Only check regular files
if ( fs::is_regular_file(dir_iter->status()))
{
currentFile = dir_iter->path().filename();
if (entry.is_regular_file()) {
currentFile = entry.path().filename();
// Only add the backups for the current wallet, e.g. wallet.dat.*
if (fs::PathToString(dir_iter->path().stem()) == strWalletName) {
folder_set.insert(folder_set_t::value_type(
// TODO: C++17 compliant time conversion code is abominable, switch to C++20
// compliant code when C++17 support is dropped
std::chrono::system_clock::to_time_t(
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
fs::last_write_time(dir_iter->path()) - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
)
),
*dir_iter
));
if (fs::PathToString(entry.path().stem()) == strWalletName) {
folder_set.insert(decltype(folder_set)::value_type(fs::last_write_time(entry.path()), entry));
}
}
}

// Loop backward through backup files and keep the N newest ones (1 <= N <= 10)
int counter = 0;
for(auto it = folder_set.rbegin(); it != folder_set.rend(); ++it) {
std::pair<const std::time_t, fs::path> file = *it;
int counter{0};
for (const auto& [entry_time, entry] : reverse_iterate(folder_set)) {
counter++;
if (counter > nWalletBackups)
{
if (counter > nWalletBackups) {
// More than nWalletBackups backups: delete oldest one(s)
try {
fs::remove(file.second);
WalletLogPrintf("Old backup deleted: %s\n", fs::PathToString(file.second));
fs::remove(entry);
WalletLogPrintf("Old backup deleted: %s\n", fs::PathToString(entry));
} catch(fs::filesystem_error &error) {
warnings.push_back(strprintf(_("Failed to delete backup, error: %s"), fsbridge::get_filesystem_error_message(error)));
WalletLogPrintf("%s\n", Join(warnings, Untranslated("\n")).original);
Expand Down
Loading