From eaa2ea830a223b59e3acd624de42c3ad4a8f6a06 Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Mon, 30 Sep 2024 14:51:41 +0200 Subject: [PATCH] WIP --- .../pre_derived_keys_cache.rs | 1 + .../helpers/accounts.rs | 41 +++++++++++++++++++ .../helpers/probably_free_factor_instances.rs | 20 +-------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/pre_derived_keys_cache/pre_derived_keys_cache.rs b/src/pre_derived_keys_cache/pre_derived_keys_cache.rs index e36355fa..8d45eff5 100644 --- a/src/pre_derived_keys_cache/pre_derived_keys_cache.rs +++ b/src/pre_derived_keys_cache/pre_derived_keys_cache.rs @@ -87,6 +87,7 @@ impl PreDerivedKeysCache { Self { probably_free_factor_instances: RwLock::new( probably_free_factor_instances + .0 .into_iter() .into_group_map_by(|x| UnquantifiedUnindexDerivationRequest::from(x.clone())) .into_iter() diff --git a/src/recovery_securify_cache/helpers/accounts.rs b/src/recovery_securify_cache/helpers/accounts.rs index 29eef5f2..6f91cc8e 100644 --- a/src/recovery_securify_cache/helpers/accounts.rs +++ b/src/recovery_securify_cache/helpers/accounts.rs @@ -44,6 +44,7 @@ impl Accounts { self.accounts.len() } + /// Should never be true, since we do not allow empty. pub fn is_empty(&self) -> bool { self.accounts.is_empty() } @@ -52,3 +53,43 @@ impl Accounts { self.network_id } } + +#[cfg(test)] +mod tests { + use super::*; + + type Sut = Accounts; + + #[test] + fn empty_throws() { + assert!(matches!( + Sut::new(NetworkID::Mainnet, IndexSet::new()), + Err(CommonError::EmptyCollection) + )); + } + + #[test] + fn wrong_network_single() { + assert!(matches!( + Sut::new(NetworkID::Stokenet, IndexSet::just(Account::sample())), + Err(CommonError::WrongNetwork) + )); + } + + #[test] + fn wrong_network_two() { + assert!(matches!( + Sut::new( + NetworkID::Stokenet, + IndexSet::from_iter([Account::sample_other(), Account::sample(),]) + ), + Err(CommonError::WrongNetwork) + )); + } + + #[test] + fn ok_new() { + let sut = Sut::new(NetworkID::Mainnet, IndexSet::just(Account::sample())).unwrap(); + assert!(!sut.is_empty()); + } +} diff --git a/src/recovery_securify_cache/helpers/probably_free_factor_instances.rs b/src/recovery_securify_cache/helpers/probably_free_factor_instances.rs index 72d10e9c..669d87b9 100644 --- a/src/recovery_securify_cache/helpers/probably_free_factor_instances.rs +++ b/src/recovery_securify_cache/helpers/probably_free_factor_instances.rs @@ -4,27 +4,11 @@ use crate::prelude::*; /// Gateway might not keep track of past FactorInstances, some of the FactorInstances /// in KeySpace::Securified might in fact have been used in the past for some entity. #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub struct ProbablyFreeFactorInstances { - factor_instances: Vec, -} - -impl IntoIterator for ProbablyFreeFactorInstances { - type Item = HierarchicalDeterministicFactorInstance; - type IntoIter = as IntoIterator>::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.factor_instances().into_iter() - } -} +pub struct ProbablyFreeFactorInstances(pub FactorInstances); impl ProbablyFreeFactorInstances { - pub fn factor_instances(&self) -> IndexSet { - self.factor_instances.iter().cloned().collect() - } pub fn new(instances: IndexSet) -> Self { - Self { - factor_instances: instances.into_iter().collect(), - } + Self(instances.into()) } }