From a477b8579dce6a3daea28eb5e9835570c9141558 Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Tue, 27 Aug 2024 11:27:14 +0200 Subject: [PATCH] Merge 'Keyrings' into KeysCollectorState. Break out 'Keyring' type into seperate file --- src/derivation/collector/key_ring.rs | 37 ++++++++ src/derivation/collector/keys_collector.rs | 13 +-- .../collector/keys_collector_preprocessor.rs | 93 +------------------ .../collector/keys_collector_state.rs | 39 +++++++- src/derivation/collector/mod.rs | 2 + 5 files changed, 81 insertions(+), 103 deletions(-) create mode 100644 src/derivation/collector/key_ring.rs diff --git a/src/derivation/collector/key_ring.rs b/src/derivation/collector/key_ring.rs new file mode 100644 index 00000000..5807a2ae --- /dev/null +++ b/src/derivation/collector/key_ring.rs @@ -0,0 +1,37 @@ +use crate::prelude::*; + +#[derive(Clone, Debug)] +pub struct Keyring { + pub factor_source_id: FactorSourceIDFromHash, + pub paths: IndexSet, + derived: RefCell>, +} + +impl Keyring { + pub fn new(factor_source_id: FactorSourceIDFromHash, paths: IndexSet) -> Self { + Self { + factor_source_id, + paths, + derived: RefCell::new(IndexSet::new()), + } + } + pub fn factors(&self) -> IndexSet { + self.derived.borrow().clone() + } + + pub(crate) fn process_response( + &self, + response: IndexSet, + ) { + assert!(response + .iter() + .all(|f| f.factor_source_id == self.factor_source_id + && !self + .derived + .borrow() + .iter() + .any(|x| x.public_key == f.public_key))); + + self.derived.borrow_mut().extend(response) + } +} diff --git a/src/derivation/collector/keys_collector.rs b/src/derivation/collector/keys_collector.rs index c1375df5..ae0093b4 100644 --- a/src/derivation/collector/keys_collector.rs +++ b/src/derivation/collector/keys_collector.rs @@ -25,10 +25,9 @@ impl KeysCollector { preprocessor: KeysCollectorPreprocessor, ) -> Self { let all_factor_sources_in_profile = all_factor_sources_in_profile.into(); - let (keyrings, factors) = preprocessor.preprocess(all_factor_sources_in_profile); + let (state, factors) = preprocessor.preprocess(all_factor_sources_in_profile); let dependencies = KeysCollectorDependencies::new(interactors, factors); - let state = KeysCollectorState::new(keyrings); Self { dependencies, @@ -69,13 +68,7 @@ impl KeysCollector { &self, factor_source_id: &FactorSourceIDFromHash, ) -> SerialBatchKeyDerivationRequest { - let keyring = self - .state - .borrow() - .keyrings - .borrow() - .keyring_for(factor_source_id) - .unwrap(); + let keyring = self.state.borrow().keyring_for(factor_source_id).unwrap(); assert_eq!(keyring.factors().len(), 0); let paths = keyring.paths.clone(); SerialBatchKeyDerivationRequest::new(*factor_source_id, paths) @@ -111,7 +104,7 @@ impl KeysCollector { .derive_with_factors() // in decreasing "friction order" .await .inspect_err(|e| eprintln!("Failed to use factor sources: {:#?}", e)); - self.state.into_inner().keyrings.into_inner().outcome() + self.state.into_inner().outcome() } } diff --git a/src/derivation/collector/keys_collector_preprocessor.rs b/src/derivation/collector/keys_collector_preprocessor.rs index b881ebd7..c15dd664 100644 --- a/src/derivation/collector/keys_collector_preprocessor.rs +++ b/src/derivation/collector/keys_collector_preprocessor.rs @@ -1,91 +1,4 @@ use crate::prelude::*; - -#[derive(Clone, Debug)] -pub struct Keyring { - pub factor_source_id: FactorSourceIDFromHash, - pub paths: IndexSet, - derived: RefCell>, -} - -impl Keyring { - pub fn new(factor_source_id: FactorSourceIDFromHash, paths: IndexSet) -> Self { - Self { - factor_source_id, - paths, - derived: RefCell::new(IndexSet::new()), - } - } - pub fn factors(&self) -> IndexSet { - self.derived.borrow().clone() - } - - pub(crate) fn process_response( - &self, - response: IndexSet, - ) { - assert!(response - .iter() - .all(|f| f.factor_source_id == self.factor_source_id - && !self - .derived - .borrow() - .iter() - .any(|x| x.public_key == f.public_key))); - - self.derived.borrow_mut().extend(response) - } -} - -#[derive(Default, Clone, Debug)] -pub struct Keyrings { - keyrings: RefCell>, -} - -impl Keyrings { - pub fn new( - derivation_paths: IndexMap>, - ) -> Self { - let keyrings = derivation_paths - .into_iter() - .map(|(factor_source_id, derivation_paths)| { - ( - factor_source_id, - Keyring::new(factor_source_id, derivation_paths), - ) - }) - .collect::>(); - Self { - keyrings: RefCell::new(keyrings), - } - } - - pub fn outcome(self) -> KeyDerivationOutcome { - let key_rings = self.keyrings.into_inner(); - KeyDerivationOutcome::new( - key_rings - .into_iter() - .map(|(k, v)| (k, v.factors())) - .collect(), - ) - } - - pub fn keyring_for(&self, factor_source_id: &FactorSourceIDFromHash) -> Option { - self.keyrings - .borrow() - .get(factor_source_id) - .cloned() - .inspect(|k| assert_eq!(k.factor_source_id, *factor_source_id)) - } - - pub(crate) fn process_batch_response(&self, response: BatchDerivationResponse) { - for (factor_source_id, factors) in response.per_factor_source.into_iter() { - let mut rings = self.keyrings.borrow_mut(); - let keyring = rings.get_mut(&factor_source_id).unwrap(); - keyring.process_response(factors) - } - } -} - pub struct KeysCollectorPreprocessor { derivation_paths: IndexMap>, } @@ -100,7 +13,7 @@ impl KeysCollectorPreprocessor { pub(crate) fn preprocess( &self, all_factor_sources_in_profile: IndexSet, - ) -> (Keyrings, IndexSet) { + ) -> (KeysCollectorState, IndexSet) { let all_factor_sources_in_profile = all_factor_sources_in_profile .into_iter() .map(|f| (f.factor_source_id(), f)) @@ -118,7 +31,7 @@ impl KeysCollectorPreprocessor { }) .collect::>(), ); - let keyrings = Keyrings::new(self.derivation_paths.clone()); - (keyrings, factor_sources_of_kind) + let state = KeysCollectorState::new(self.derivation_paths.clone()); + (state, factor_sources_of_kind) } } diff --git a/src/derivation/collector/keys_collector_state.rs b/src/derivation/collector/keys_collector_state.rs index 26ae01bf..9bad698f 100644 --- a/src/derivation/collector/keys_collector_state.rs +++ b/src/derivation/collector/keys_collector_state.rs @@ -1,17 +1,50 @@ use crate::prelude::*; pub struct KeysCollectorState { - pub(super) keyrings: RefCell, + pub(super) keyrings: RefCell>, } impl KeysCollectorState { - pub fn new(keyrings: Keyrings) -> Self { + pub fn new( + derivation_paths: IndexMap>, + ) -> Self { + let keyrings = derivation_paths + .into_iter() + .map(|(factor_source_id, derivation_paths)| { + ( + factor_source_id, + Keyring::new(factor_source_id, derivation_paths), + ) + }) + .collect::>(); Self { keyrings: RefCell::new(keyrings), } } + pub fn outcome(self) -> KeyDerivationOutcome { + let key_rings = self.keyrings.into_inner(); + KeyDerivationOutcome::new( + key_rings + .into_iter() + .map(|(k, v)| (k, v.factors())) + .collect(), + ) + } + + pub fn keyring_for(&self, factor_source_id: &FactorSourceIDFromHash) -> Option { + self.keyrings + .borrow() + .get(factor_source_id) + .cloned() + .inspect(|k| assert_eq!(k.factor_source_id, *factor_source_id)) + } + pub(crate) fn process_batch_response(&self, response: BatchDerivationResponse) { - self.keyrings.borrow_mut().process_batch_response(response) + for (factor_source_id, factors) in response.per_factor_source.into_iter() { + let mut rings = self.keyrings.borrow_mut(); + let keyring = rings.get_mut(&factor_source_id).unwrap(); + keyring.process_response(factors) + } } } diff --git a/src/derivation/collector/mod.rs b/src/derivation/collector/mod.rs index fd908aa3..cf1238c2 100644 --- a/src/derivation/collector/mod.rs +++ b/src/derivation/collector/mod.rs @@ -1,9 +1,11 @@ +mod key_ring; mod keys_collector; mod keys_collector_dependencies; mod keys_collector_preprocessor; mod keys_collector_state; mod used_derivation_indices; +pub use key_ring::*; pub use keys_collector::*; pub use keys_collector_dependencies::*; pub use keys_collector_preprocessor::*;