From 3315ac639e97287b5b3322d00d40384bc6e073ae Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Thu, 20 Jun 2024 10:00:45 +0200 Subject: [PATCH] speed up other search only used in tests --- .../core/src/transactions/key_manager/inner.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/base_layer/core/src/transactions/key_manager/inner.rs b/base_layer/core/src/transactions/key_manager/inner.rs index 629ad852c9..21a037051c 100644 --- a/base_layer/core/src/transactions/key_manager/inner.rs +++ b/base_layer/core/src/transactions/key_manager/inner.rs @@ -343,11 +343,20 @@ where TBackend: KeyManagerBackend + 'static let current_index = km.key_index(); - for i in 0u64..current_index + TRANSACTION_KEY_MANAGER_MAX_SEARCH_DEPTH { - let public_key = PublicKey::from_secret_key(&km.derive_key(i)?.key); + for i in 0u64..TRANSACTION_KEY_MANAGER_MAX_SEARCH_DEPTH { + let index = current_index + i; + let public_key = PublicKey::from_secret_key(&km.derive_key(index)?.key); if public_key == *key { trace!(target: LOG_TARGET, "Key found in {} Key Chain at index {}", branch, i); - return Ok(i); + return Ok(index); + } + if i <= current_index && i != 0u64 { + let index = current_index - i; + let public_key = PublicKey::from_secret_key(&km.derive_key(index)?.key); + if public_key == *key { + trace!(target: LOG_TARGET, "Key found in {} Key Chain at index {}", branch, index); + return Ok(index); + } } } @@ -373,7 +382,7 @@ where TBackend: KeyManagerBackend + 'static trace!(target: LOG_TARGET, "Key found in {} Key Chain at index {}", branch, index); return Ok(index); } - if i <= current_index { + if i <= current_index && i != 0u64 { let index = current_index - i; let private_key = &km.derive_key(index)?.key; if private_key == key {