Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
KeysCollector: Throw, dont panic, for unknown factor source
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Aug 27, 2024
1 parent a477b85 commit 4a4d0fe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/derivation/collector/keys_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ impl KeysCollector {
all_factor_sources_in_profile: impl Into<IndexSet<HDFactorSource>>,
interactors: Arc<dyn KeysCollectingInteractors>,
preprocessor: KeysCollectorPreprocessor,
) -> Self {
) -> Result<Self> {
let all_factor_sources_in_profile = all_factor_sources_in_profile.into();
let (state, factors) = preprocessor.preprocess(all_factor_sources_in_profile);
let (state, factors) = preprocessor.preprocess(all_factor_sources_in_profile)?;

let dependencies = KeysCollectorDependencies::new(interactors, factors);

Self {
Ok(Self {
dependencies,
state: RefCell::new(state),
}
})
}

pub fn new(
all_factor_sources_in_profile: IndexSet<HDFactorSource>,
derivation_paths: IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>>,
interactors: Arc<dyn KeysCollectingInteractors>,
) -> Self {
) -> Result<Self> {
let preprocessor = KeysCollectorPreprocessor::new(derivation_paths);
Self::with_preprocessor(all_factor_sources_in_profile, interactors, preprocessor)
}
Expand Down
30 changes: 16 additions & 14 deletions src/derivation/collector/keys_collector_preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ impl KeysCollectorPreprocessor {
pub(crate) fn preprocess(
&self,
all_factor_sources_in_profile: IndexSet<HDFactorSource>,
) -> (KeysCollectorState, IndexSet<FactorSourcesOfKind>) {
) -> Result<(KeysCollectorState, IndexSet<FactorSourcesOfKind>)> {
let all_factor_sources_in_profile = all_factor_sources_in_profile
.into_iter()
.map(|f| (f.factor_source_id(), f))
.collect::<HashMap<FactorSourceIDFromHash, HDFactorSource>>();

let factor_sources_of_kind = sort_group_factors(
self.derivation_paths
.clone()
.keys()
.map(|id| {
all_factor_sources_in_profile
.get(id)
.expect("Should have all factor sources")
.clone()
})
.collect::<HashSet<_>>(),
);
let unsorted = self
.derivation_paths
.clone()
.keys()
.map(|id| {
all_factor_sources_in_profile
.get(id)
.cloned()
.ok_or(CommonError::UnknownFactorSource)
})
.collect::<Result<HashSet<_>>>()?;

let factor_sources_of_kind = sort_group_factors(unsorted);
let state = KeysCollectorState::new(self.derivation_paths.clone());
(state, factor_sources_of_kind)

Ok((state, factor_sources_of_kind))
}
}
1 change: 1 addition & 0 deletions src/testing/derivation/test_keys_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl KeysCollector {
derivation_paths.into_iter().collect(),
Arc::new(TestDerivationInteractors::default()),
)
.unwrap()
}

pub fn new_test(
Expand Down
24 changes: 21 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,25 @@ mod key_derivation_tests {
use super::*;

#[actix_rt::test]
async fn failure() {
async fn failure_unknown_factor() {
let res = KeysCollector::new(
IndexSet::new(),
IndexMap::from_iter([(
FactorSourceIDFromHash::fs0(),
IndexSet::from_iter([DerivationPath::new(
Mainnet,
Account,
T9n,
HDPathComponent::securified(0),
)]),
)]),
Arc::new(TestDerivationInteractors::default()),
);
assert!(matches!(res, Err(CommonError::UnknownFactorSource)));
}

#[actix_rt::test]
async fn failure_from_interactor() {
let factor_source = fs_at(0);
let paths = [0, 1, 2]
.into_iter()
Expand All @@ -51,9 +69,9 @@ mod key_derivation_tests {
.into_iter()
.collect::<IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>>>(),
Arc::new(TestDerivationInteractors::fail()),
);
)
.unwrap();
let outcome = collector.collect_keys().await;
println!("{:#?}", outcome);
assert!(outcome.all_factors().is_empty())
}

Expand Down

0 comments on commit 4a4d0fe

Please sign in to comment.