From 9a158725161f726eb6747811e9e9335eef83f616 Mon Sep 17 00:00:00 2001 From: David Gumberg Date: Mon, 5 May 2025 20:28:18 -0700 Subject: [PATCH] wallet: Make encryption derivation clock mockable Adds a special case where if the elapsed time during measurement of DKF performance is 0, the default derive iterations are used so that behavior is stable for testing and benchmarks. --- src/wallet/wallet.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 782667cc35d..dae37e05349 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -584,9 +584,15 @@ static bool EncryptMasterKey(const SecureString& wallet_passphrase, const CKeyin // Get the weighted average of iterations we can do in 100ms over 2 runs. for (int i = 0; i < 2; i++){ - auto start_time{SteadyClock::now()}; + auto start_time{NodeClock::now()}; crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod); - auto elapsed_time{SteadyClock::now() - start_time}; + auto elapsed_time{NodeClock::now() - start_time}; + + if (elapsed_time <= 0s) { + // We are probably in a test with a mocked clock. + master_key.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS; + break; + } // target_iterations : elapsed_iterations :: target_time : elapsed_time unsigned int target_iterations = master_key.nDeriveIterations * target_time / elapsed_time;