Skip to content

Commit

Permalink
IntoIter and FroIter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Sep 21, 2024
1 parent 74dbeb7 commit 0056c5e
Show file tree
Hide file tree
Showing 16 changed files with 474 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/derivation/collector/key_derivation_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl KeyDerivationOutcome {
}

/// ALL factor instances derived by the KeysCollector
pub fn all_factors(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
pub fn all_factors(&self) -> FactorInstances {
self.factors_by_source
.clone()
.into_iter()
Expand Down
6 changes: 3 additions & 3 deletions src/derivation/tests/derivation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod key_derivation_tests {
)
.unwrap();
let outcome = collector.collect_keys().await;
assert!(outcome.all_factors().is_empty())
assert!(outcome.all_factors().factor_instances().is_empty())
}

mod multi_key {
Expand Down Expand Up @@ -378,7 +378,7 @@ mod key_derivation_tests {
paths
);

assert!(outcome.all_factors().len() > 200);
assert!(outcome.all_factors().factor_instances().len() > 200);

assert_eq!(
outcome
Expand Down Expand Up @@ -413,7 +413,7 @@ mod key_derivation_tests {
KeysCollector::with(factor_source, network_id, key_kind, entity_kind, key_space);

let outcome = collector.collect_keys().await;
let factors = outcome.all_factors();
let factors = outcome.all_factors().factor_instances();
assert_eq!(factors.len(), 1);
let factor = factors.first().unwrap();
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,42 @@ async fn _account_recovery_scan(
DerivationAndAnalysisAccountRecoveryScan::try_from(analysis)
}

pub async fn account_recovery_scan(
pub async fn onboarding_account_recovery_scan(

Check warning on line 11 in src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs#L11

Added line #L11 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
) -> Result<AccountRecoveryScanOutcome> {
let analysis = _account_recovery_scan(DeriveAndAnalyzeAccountRecoveryScanInput::new(
factor_sources,
gateway,
derivation_interactors,
))
let analysis = _account_recovery_scan(
DeriveAndAnalyzeAccountRecoveryScanInput::onboarding_account_recovery_scan(
factor_sources,
gateway,
derivation_interactors,
is_done_query,
),
)
.await?;
Ok(AccountRecoveryScanOutcome::from(analysis))
}

pub async fn manual_account_recovery_scan(
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
cache: impl Into<Option<PreDerivedKeysCache>>,
profile: ProfileAnalyzer,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
) -> Result<AccountRecoveryScanOutcome> {
let analysis = _account_recovery_scan(
DeriveAndAnalyzeAccountRecoveryScanInput::manual_account_recovery_scan(
factor_sources,
gateway,
cache,
profile,
derivation_interactors,
is_done_query,

Check warning on line 44 in src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs#L38-L44

Added lines #L38 - L44 were not covered by tests
),
)
.await?;
Ok(AccountRecoveryScanOutcome::from(analysis))

Check warning on line 48 in src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs#L47-L48

Added lines #L47 - L48 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,143 @@
#![allow(unused)]
#![allow(unused_variables)]

use crate::prelude::*;
use crate::{gateway, prelude::*};

/// Use by OARS (Onboarding Account Recovery Scan) and MARS
/// (Manual Account Recovery Scan).
pub struct DeriveAndAnalyzeAccountRecoveryScanInput {
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,

/// Empty for OARS, **maybe** not empty for MARS
cache: PreDerivedKeysCache,
/// Empty for OARS, **guaranteed** not empty for MARS
profile: ProfileAnalyzer,

derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
}

impl DeriveAndAnalyzeAccountRecoveryScanInput {
pub fn new(
fn new(

Check warning on line 22 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L22

Added line #L22 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
cache: impl Into<Option<PreDerivedKeysCache>>,
profile: impl Into<Option<ProfileAnalyzer>>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
) -> Self {
Self {
factor_sources,
gateway,
cache: cache.into().unwrap_or_default(),
profile: profile.into().unwrap_or_default(),

Check warning on line 34 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L33-L34

Added lines #L33 - L34 were not covered by tests
derivation_interactors,
is_done_query,
}
}

/// OARS
pub fn onboarding_account_recovery_scan(

Check warning on line 41 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L41

Added line #L41 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
) -> Self {
Self::new(
factor_sources,
gateway,
None,
None,

Check warning on line 51 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
derivation_interactors,
is_done_query,
)
}

/// MARS
pub fn manual_account_recovery_scan(
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
cache: impl Into<Option<PreDerivedKeysCache>>,
profile: ProfileAnalyzer,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
is_done_query: Arc<dyn IsDerivationDoneQuery>,
) -> Self {
Self::new(
factor_sources,
gateway,
cache,
profile,
derivation_interactors,
is_done_query,

Check warning on line 72 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L67-L72

Added lines #L67 - L72 were not covered by tests
)
}
}

#[derive(Debug, Default)]
pub struct ProfileAnalyzer;

pub struct FactorInstancesProviderImpl {
cache: PreDerivedKeysCache,
gateway: Option<Arc<dyn Gateway>>,
profile: ProfileAnalyzer,
}
impl FactorInstancesProviderImpl {
/// # Panics
/// Panics if all arguments are `None`.
fn new(

Check warning on line 88 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L88

Added line #L88 was not covered by tests
cache: impl Into<Option<PreDerivedKeysCache>>,
gateway: impl Into<Option<Arc<dyn Gateway>>>,
profile: impl Into<Option<ProfileAnalyzer>>,
) -> Self {
let cache = cache.into();
let gateway = gateway.into();
let profile = profile.into();
assert!(
!(cache.is_none() && gateway.is_none() && profile.is_none()),
"All arguments are None"

Check warning on line 98 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L93-L98

Added lines #L93 - L98 were not covered by tests
);
Self {
cache: cache.unwrap_or_default(),

Check warning on line 101 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L101

Added line #L101 was not covered by tests
gateway,
profile: profile.unwrap_or_default(),

Check warning on line 103 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L103

Added line #L103 was not covered by tests
}
}

/// OARS
pub fn onboarding_account_recovery_scan(gateway: Arc<dyn Gateway>) -> Self {
Self::new(None, gateway, None)

Check warning on line 109 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L108-L109

Added lines #L108 - L109 were not covered by tests
}

/// MARS
pub fn manual_account_recovery_scan(
cache: impl Into<Option<PreDerivedKeysCache>>,
gateway: Arc<dyn Gateway>,
profile: ProfileAnalyzer,
) -> Self {
Self::new(cache, gateway, profile)

Check warning on line 118 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L118

Added line #L118 was not covered by tests
}
}

#[async_trait::async_trait]
impl IsFactorInstancesProvider for FactorInstancesProviderImpl {
async fn provide_instances(
&self,
derivation_requests: DerivationRequests,
) -> Result<FactorInstances> {
let cache_outcome = self.cache.load(&derivation_requests).await?;

Check warning on line 128 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L128

Added line #L128 was not covered by tests
todo!();
}
}

#[async_trait::async_trait]
impl IsIntermediaryDerivationAnalyzer for FactorInstancesProviderImpl {
async fn analyze(
&self,
factor_instances: FactorInstances,
) -> Result<IntermediaryDerivationAnalysis> {
todo!()
}
}

impl From<DeriveAndAnalyzeAccountRecoveryScanInput> for DeriveAndAnalyzeInput {
Expand All @@ -44,12 +161,13 @@ impl From<DeriveAndAnalyzeAccountRecoveryScanInput> for DeriveAndAnalyzeInput {
.into_iter()
.map(move |u| u.derivation_request_with_factor_source_id(factor_source_id))

Check warning on line 162 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L162

Added line #L162 was not covered by tests
})
.collect::<IndexSet<_>>();
.collect::<DerivationRequests>();

let factor_instances_provider: Arc<dyn IsFactorInstancesProvider> = { unreachable!() };
let analyze_factor_instances: Arc<dyn IsIntermediaryDerivationAnalyzer> =
{ unreachable!() };
let is_done: Arc<dyn IsDerivationDoneQuery> = { unreachable!() };
let analyzing_factor_instance_provider = Arc::new(FactorInstancesProviderImpl::new(

Check warning on line 166 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L166

Added line #L166 was not covered by tests
value.cache,
value.gateway,

Check warning on line 168 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L168

Added line #L168 was not covered by tests
value.profile,
));

Self::new(
value.factor_sources.clone(),
Expand All @@ -59,9 +177,9 @@ impl From<DeriveAndAnalyzeAccountRecoveryScanInput> for DeriveAndAnalyzeInput {
.map(|f| f.factor_source_id())
.collect(),
initial_derivation_requests,
factor_instances_provider,
analyze_factor_instances,
is_done,
analyzing_factor_instance_provider.clone(),
analyzing_factor_instance_provider.clone(),
value.is_done_query,

Check warning on line 182 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L177-L182

Added lines #L177 - L182 were not covered by tests
)
}
}
Expand All @@ -76,7 +194,7 @@ pub struct UncachedFactorInstanceProvider {
impl UncachedFactorInstanceProvider {
fn derivation_paths_for_requests(

Check warning on line 195 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L195

Added line #L195 was not covered by tests
&self,
derivation_requests: IndexSet<DerivationRequest>,
derivation_requests: DerivationRequests,
) -> IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>> {
todo!()
}
Expand All @@ -86,8 +204,8 @@ impl UncachedFactorInstanceProvider {
impl IsFactorInstancesProvider for UncachedFactorInstanceProvider {
async fn provide_instances(
&self,
derivation_requests: IndexSet<DerivationRequest>,
) -> Result<IndexSet<HierarchicalDeterministicFactorInstance>> {
derivation_requests: DerivationRequests,
) -> Result<FactorInstances> {
let derivation_paths = self.derivation_paths_for_requests(derivation_requests);

Check warning on line 209 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L209

Added line #L209 was not covered by tests
let keys_collector = KeysCollector::new(
self.factor_sources.clone(),
Expand Down
12 changes: 5 additions & 7 deletions src/recovery_securify_cache/derivation_and_analysis/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct DeriveAndAnalyzeInput {
factor_sources: IndexSet<HDFactorSource>,
ids_of_new_factor_sources: IndexSet<FactorSourceIDFromHash>,

next_requests: IndexSet<DerivationRequest>,
next_requests: DerivationRequests,

factor_instances_provider: Arc<dyn IsFactorInstancesProvider>,

Expand All @@ -27,7 +27,7 @@ impl DeriveAndAnalyzeInput {
pub fn new(

Check warning on line 27 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L27

Added line #L27 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
ids_of_new_factor_sources: IndexSet<FactorSourceIDFromHash>,
initial_derivation_requests: IndexSet<DerivationRequest>,
initial_derivation_requests: DerivationRequests,
factor_instances_provider: Arc<dyn IsFactorInstancesProvider>,
analyze_factor_instances: Arc<dyn IsIntermediaryDerivationAnalyzer>,
is_done: Arc<dyn IsDerivationDoneQuery>,
Expand All @@ -54,7 +54,7 @@ impl DeriveAndAnalyzeInput {
impl IsIntermediaryDerivationAnalyzer for DeriveAndAnalyzeInput {
async fn analyze(
&self,
factor_instances: IndexSet<HierarchicalDeterministicFactorInstance>,
factor_instances: FactorInstances,
) -> Result<IntermediaryDerivationAnalysis> {
self.analyze_factor_instances
.analyze(factor_instances)
Expand All @@ -70,13 +70,11 @@ impl IsDerivationDoneQuery for DeriveAndAnalyzeInput {
}

impl DeriveAndAnalyzeInput {
fn next_requests(&self) -> IndexSet<DerivationRequest> {
fn next_requests(&self) -> DerivationRequests {
self.next_requests.clone()

Check warning on line 74 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
}

pub async fn load_cached_or_derive_new_instances(
&self,
) -> Result<IndexSet<HierarchicalDeterministicFactorInstance>> {
pub async fn load_cached_or_derive_new_instances(&self) -> Result<FactorInstances> {

Check warning on line 77 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L77

Added line #L77 was not covered by tests
let factor_sources = self.all_factor_sources();
let requests = self.next_requests();
let factor_instances = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use crate::prelude::*;
pub trait IsFactorInstancesProvider: Sync + Send {
async fn provide_instances(
&self,
derivation_requests: IndexSet<DerivationRequest>,
) -> Result<IndexSet<HierarchicalDeterministicFactorInstance>>;
derivation_requests: DerivationRequests,
) -> Result<FactorInstances>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use crate::prelude::*;
pub trait IsIntermediaryDerivationAnalyzer: Sync + Send {
async fn analyze(
&self,
factor_instances: IndexSet<HierarchicalDeterministicFactorInstance>,
factor_instances: FactorInstances,
) -> Result<IntermediaryDerivationAnalysis>;
}
Loading

0 comments on commit 0056c5e

Please sign in to comment.