From 71fbdd6044b66482b6b99292fb1452581def13c9 Mon Sep 17 00:00:00 2001 From: w0xlt <94266259+w0xlt@users.noreply.github.com> Date: Tue, 10 May 2022 20:54:12 -0300 Subject: [PATCH] wallet: Change log interval to use `steady_clock` This refactors the log interval variables to use `steady_clock` as it is best suitable for measuring intervals. --- .vscode/settings.json | 5 +++++ src/wallet/wallet.cpp | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000000000..5e09a03c384f8a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "chrono": "cpp" + } +} \ No newline at end of file diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 356d0ce5a05533..5a23f739d3f164 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1707,8 +1707,10 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r */ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional max_height, const WalletRescanReserver& reserver, bool fUpdate) { - int64_t nNow = GetTime(); - int64_t start_time = GetTimeMillis(); + using Clock = std::chrono::steady_clock; + constexpr auto LOG_INTERVAL{60s}; + auto current_time{Clock::now()}; + auto start_time{Clock::now()}; assert(reserver.isReserved()); @@ -1735,8 +1737,8 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc if (block_height % 100 == 0 && progress_end - progress_begin > 0.0) { ShowProgress(strprintf("%s " + _("Rescanning…").translated, GetDisplayName()), std::max(1, std::min(99, (int)(m_scanning_progress * 100)))); } - if (GetTime() >= nNow + 60) { - nNow = GetTime(); + if (Clock::now() >= current_time + LOG_INTERVAL) { + current_time = Clock::now(); WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n", block_height, progress_current); } @@ -1803,7 +1805,8 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", block_height, progress_current); result.status = ScanResult::USER_ABORT; } else { - WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - start_time); + auto duration_milliseconds = std::chrono::duration_cast(Clock::now() - start_time); + WalletLogPrintf("Rescan completed in %15dms\n", duration_milliseconds.count()); } return result; }