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.
This commit is contained in:
David Gumberg 2025-05-05 20:28:18 -07:00
parent ae5485fa0d
commit 9a15872516

View File

@ -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;