Skip to content

Commit

Permalink
Merge 'Keyrings' into KeysCollectorState. Break out 'Keyring' type in…
Browse files Browse the repository at this point in the history
…to seperate file
  • Loading branch information
CyonAlexRDX committed Aug 27, 2024
1 parent c66a239 commit a477b85
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 103 deletions.
37 changes: 37 additions & 0 deletions src/derivation/collector/key_ring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::prelude::*;

#[derive(Clone, Debug)]
pub struct Keyring {
pub factor_source_id: FactorSourceIDFromHash,
pub paths: IndexSet<DerivationPath>,
derived: RefCell<IndexSet<HierarchicalDeterministicFactorInstance>>,
}

impl Keyring {
pub fn new(factor_source_id: FactorSourceIDFromHash, paths: IndexSet<DerivationPath>) -> Self {
Self {
factor_source_id,
paths,
derived: RefCell::new(IndexSet::new()),
}
}
pub fn factors(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.derived.borrow().clone()
}

pub(crate) fn process_response(
&self,
response: IndexSet<HierarchicalDeterministicFactorInstance>,
) {
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)
}
}
13 changes: 3 additions & 10 deletions src/derivation/collector/keys_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}

Expand Down
93 changes: 3 additions & 90 deletions src/derivation/collector/keys_collector_preprocessor.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,4 @@
use crate::prelude::*;

#[derive(Clone, Debug)]
pub struct Keyring {
pub factor_source_id: FactorSourceIDFromHash,
pub paths: IndexSet<DerivationPath>,
derived: RefCell<IndexSet<HierarchicalDeterministicFactorInstance>>,
}

impl Keyring {
pub fn new(factor_source_id: FactorSourceIDFromHash, paths: IndexSet<DerivationPath>) -> Self {
Self {
factor_source_id,
paths,
derived: RefCell::new(IndexSet::new()),
}
}
pub fn factors(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.derived.borrow().clone()
}

pub(crate) fn process_response(
&self,
response: IndexSet<HierarchicalDeterministicFactorInstance>,
) {
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<IndexMap<FactorSourceIDFromHash, Keyring>>,
}

impl Keyrings {
pub fn new(
derivation_paths: IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>>,
) -> Self {
let keyrings = derivation_paths
.into_iter()
.map(|(factor_source_id, derivation_paths)| {
(
factor_source_id,
Keyring::new(factor_source_id, derivation_paths),
)
})
.collect::<IndexMap<FactorSourceIDFromHash, Keyring>>();
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<Keyring> {
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<FactorSourceIDFromHash, IndexSet<DerivationPath>>,
}
Expand All @@ -100,7 +13,7 @@ impl KeysCollectorPreprocessor {
pub(crate) fn preprocess(
&self,
all_factor_sources_in_profile: IndexSet<HDFactorSource>,
) -> (Keyrings, IndexSet<FactorSourcesOfKind>) {
) -> (KeysCollectorState, IndexSet<FactorSourcesOfKind>) {
let all_factor_sources_in_profile = all_factor_sources_in_profile
.into_iter()
.map(|f| (f.factor_source_id(), f))
Expand All @@ -118,7 +31,7 @@ impl KeysCollectorPreprocessor {
})
.collect::<HashSet<_>>(),
);
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)
}
}
39 changes: 36 additions & 3 deletions src/derivation/collector/keys_collector_state.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
use crate::prelude::*;

pub struct KeysCollectorState {
pub(super) keyrings: RefCell<Keyrings>,
pub(super) keyrings: RefCell<IndexMap<FactorSourceIDFromHash, Keyring>>,
}

impl KeysCollectorState {
pub fn new(keyrings: Keyrings) -> Self {
pub fn new(
derivation_paths: IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>>,
) -> Self {
let keyrings = derivation_paths
.into_iter()
.map(|(factor_source_id, derivation_paths)| {
(
factor_source_id,
Keyring::new(factor_source_id, derivation_paths),
)
})
.collect::<IndexMap<FactorSourceIDFromHash, Keyring>>();
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<Keyring> {
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)
}
}
}
2 changes: 2 additions & 0 deletions src/derivation/collector/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down

0 comments on commit a477b85

Please sign in to comment.