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

Commit

Permalink
remove some prints
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Sep 17, 2024
1 parent 33a6b0a commit f05ab09
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 107 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Quartier",
"Rémy",
"scrypto",
"securifies",
"securify",
"securifying",
"substate",
Expand Down
2 changes: 1 addition & 1 deletion src/derivation/collector/keys_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl KeysCollector {
_ = self
.derive_with_factors() // in decreasing "friction order"
.await
.inspect_err(|e| eprintln!("Failed to use factor sources: {:#?}", e));
.inspect_err(|e| error!("Failed to use factor sources: {:#?}", e));
self.state.into_inner().outcome()
}
}
Expand Down
93 changes: 2 additions & 91 deletions src/factor_instance_provider/factor_instance_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,7 @@ impl IsPreDerivedKeysCache for InMemoryPreDerivedKeysCache {
.clone();
assert!(
read_from_cache.matches(&tuple.request),
"VERY BAD! BAD CACHE LOGIC, 'read_from_cache' {:?} does not match request: {:?}",
read_from_cache,
tuple.request
"incorrect implementation"
);
for_key.shift_remove(&read_from_cache);
instances_read_from_cache.insert(tuple.request, read_from_cache);
Expand All @@ -449,65 +447,6 @@ impl IsPreDerivedKeysCache for InMemoryPreDerivedKeysCache {
/// This FactorInstanceProvider is used when creating new entities or when
/// securing existing entities. It is used to provide the "next"
/// `HierarchicalDeterministicFactorInstance` in both cases for the given request.
///
/// A `DerivationRequest` is a tuple of `(KeySpace, EntityKind, KeyKind, FactorSourceID, NetworkID)`.
///
/// Gateway MUST be able find entities by PAST public key hashes, not only current ones, ponder this
/// scenario:
///
/// NOTATION:
/// Accounts are written as `A`, `B`, `C`, etc.
/// FactorSources are written as `L`, `M`, `N`, etc.
/// FactorInstances derived in:
/// * Unsecurified Keyspace are written as: `🔴α` (alpha), `🔴β` (beta), `🔴γ` (gamma), `🔴δ` (delta).
/// * Securified KeySpace are written as: `🔵π` (pi), `🔵ρ` (rho), `🔵σ` (sigma), `🔵τ` (tau), `🔵φ (Phi)`
/// Those FactorInstances are spelled out as:
/// `🔴α=(0', L)`, derived using FactorSource `L` at index `0'`.
/// `🔵π=(0^, M)`, derived using FactorSource `M` at index `0^`.
///
/// Derivation Entity Index: `0'` means 0 hardened, which is in the Unsecurified KeySpace.
/// Derivation Entity Index: `0^` means 0 hardened, which is in the Securified KeySpace.
/// The FactorInstance which was used to form the Address of an entity is called a
/// "genesis factor instance".
///
/// SCENARIO
/// 1. User creates account `A` with genesis FactorInstance `🔴α=(0', L)`
/// 2. User securifies account `A` with `{ override_factors: [🔵π=(0^, L), 🔵ρ=(0^, M)] }`
/// 3. User updates security shield of account `A` with `{ override_factors: [🔵ρ=(0^, M) }`
/// 4. If user tries to create a new account `🔴(❓, L) which value to use for index?
/// 5. Naive implementation will assign `index = 0'`, since that DerivationPath is
/// not referenced anywhere in Profile.
/// 6. FAILURE! We cannot create a new account with `0'`, since it will have same address as account `A`.
/// Solution: We must **retain** the genesis factor instance `🔴α=(0', L)` for account `A`
/// when it gets securified, and persist this in Profile.
///
/// Let us talk about recovery without Profile...
///
/// 7. Imagine user tosses this Profile, and performs recovery using only `M`, then securified account `A`
/// is found and recovered.
/// 8. Later user (re-)add FactorSource `L`, which SHOULD immediately trigger it to derive many keys
/// to be put in the cache. We MUST NOT put `🔴α = (0', L)` in the cache as the "next free" instance
/// to use, since we would have same problem as in step 3-5. Since `🔴α=(0', L)` is in unsecurified
/// key space we would be able to form an Address from it and see that it is the same address of A.
/// However, we MUST NOT save 🔵π=(0^, L) as a free factor instance either. It has already been used by in
/// step 2, with account `A`.
///
/// The conclusion is this:
/// 💡🔮 Gateway MUST be able find entities by PAST public key hashes, not only current ones. 🔮💡
///
/// Using Gateway's lookup by past and present public key hashes, it will tell use that the hash of both
/// `🔴α =(0', L)` and 🔵π=(0^, L) have been used, so when `L` is saved into Profile and the derived instances
/// are saved into the cache, we will NOT save `🔴α=(0', L)` not `🔵π=(0^, L)`, the cached instances will be
/// `🔴β=(1', L)`, `🔴γ=(2', L)`, ... and 🔵σ=(1^, L), `🔵τ`=(2^, L), ..., up to some batch size (e.g. `30`).
/// Next time user creates an unsecurified entity it will use FactorInstanceProvider which uses the cache,
/// to consume the next-never-used FactorInstance from the Unsecurified KeySpace. And the next time the user
/// securifies an entity it will consume the next-never-used FactorInstances from the Securified KeySpace - for
/// each FactorSource.
///
/// 9. Later user uses `L` to create a new account `B`, which using this `FactorInstanceProcider` will
/// find the "next" FactorInstance `🔴β=(1', L)`, success!
/// 10. User securifies account `B` with `{ override_factors: [🔵σ=(1^, L), 🔵φ=(1^, M)] }`, success!
/// 11. User know has two securified accounts, none of them share any public key hashes.
pub struct FactorInstanceProvider {
pub gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
Expand Down Expand Up @@ -592,7 +531,6 @@ impl FactorInstanceProvider {
profile: &'p Profile,
unfulfillable_requests: &UnfulfillableRequests,
) -> Result<()> {
println!("🌈 derive_new_and_fill_cache...");
let factor_sources = profile.factor_sources.clone();
let unfulfillable_requests = unfulfillable_requests.unfulfillable();

Expand Down Expand Up @@ -715,11 +653,6 @@ impl FactorInstanceProvider {
}
}

println!(
"🚗 dispatching to KeysCollector, derivation paths: {:?}",
derivation_paths
);

let keys_collector = KeysCollector::new(
factor_sources,
derivation_paths,
Expand All @@ -734,8 +667,6 @@ impl FactorInstanceProvider {
IndexSet<HierarchicalDeterministicFactorInstance>,
> = IndexMap::new();

Check warning on line 668 in src/factor_instance_provider/factor_instance_provider.rs

View check run for this annotation

Codecov / codecov/patch

src/factor_instance_provider/factor_instance_provider.rs#L666-L668

Added lines #L666 - L668 were not covered by tests

println!("🛍️ derived factors: {:?}", derived_factors);

for derived_factor in derived_factors {
let key = PreDeriveKeysCacheKey::from(derived_factor.clone());
if let Some(existing) = to_insert.get_mut(&key) {
Expand All @@ -761,21 +692,7 @@ impl FactorInstanceProvider {
.union(&unfulfillable.requests())
.cloned()
.collect::<IndexSet<_>>();
println!("🐝 requests: {:?}", requests);
let instances = self.cache.consume_next_factor_instances(requests).await;
println!("🐝 instances: {:?}", instances);

if let Ok(map) = instances.clone() {
for (key, value) in map.iter() {
assert_eq!(
value.factor_source_id(),
key.factor_source_id,
"BAD! Wrong factor source!"
);
}
}

instances
self.cache.consume_next_factor_instances(requests).await
}
}

Expand Down Expand Up @@ -814,19 +731,13 @@ impl FactorInstanceProvider {
})
.collect::<IndexSet<_>>();

println!("🐙 requests: {:?}", requests);

let derived_factors_map = self.provide_factor_instances(profile, requests).await?;

println!("🐙 derived_factors_map: {:?}", derived_factors_map);

let derived_factors = derived_factors_map
.values()
.cloned()
.collect::<IndexSet<_>>();

println!("🐙 derived_factors: {:?}", derived_factors);

let matrix = MatrixOfFactorInstances::fulfilling_matrix_of_factor_sources_with_instances(
derived_factors,
matrix.clone(),
Expand Down
16 changes: 1 addition & 15 deletions src/types/sargon_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,21 +1502,7 @@ impl MatrixOfFactorInstances {
matrix_of_factor_sources: MatrixOfFactorSources,
) -> Result<Self> {
let instances = instances.into_iter().collect_vec();
let mut actual = instances
.clone()
.into_iter()
.map(|f| f.factor_source_id())
.collect::<IndexSet<_>>();
actual.sort();
println!("🍬 actual: {:?}", actual);
let mut expected = matrix_of_factor_sources
.clone()
.all_factors()
.into_iter()
.map(|f| f.factor_source_id())
.collect::<IndexSet<_>>();
expected.sort();
println!("🍬 expect: {:?}", expected);

let get_factors =
|required: Vec<HDFactorSource>| -> Result<Vec<HierarchicalDeterministicFactorInstance>> {
required
Expand Down

0 comments on commit f05ab09

Please sign in to comment.