Skip to content

Commit 68c45fa

Browse files
authored
feat: Switch the source of truth for the lookup_application_with_origin map (#3353)
* first * second * Refer to the new map for generating new app numbers
1 parent ef43a56 commit 68c45fa

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

src/internet_identity/src/storage.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,8 @@ impl<M: Memory + Clone> Storage<M> {
463463
lookup_anchor_with_device_credential_memory,
464464
),
465465

466-
// TODO[ID-352]: Change this to use `lookup_application_with_origin_memory`.
467466
lookup_application_with_origin_memory_wrapper: MemoryWrapper::new(
468-
lookup_application_with_origin_memory_old.clone(),
467+
lookup_application_with_origin_memory.clone(),
469468
),
470469
// TODO[ID-354]: Remove this after the production data is migrated to the new map.
471470
lookup_application_with_origin_memory_old: StableBTreeMap::init(
@@ -737,22 +736,20 @@ impl<M: Memory + Clone> Storage<M> {
737736
let origin_hash = StorableOriginHash::from_origin(origin);
738737
let origin_sha256 = StorableOriginSha256::from_origin(origin);
739738

740-
// TODO[ID_352]: Switch to `lookup_application_with_origin_memory`.
741739
if let Some(existing_number) = self
742-
.lookup_application_with_origin_memory_old
743-
.get(&origin_hash)
740+
.lookup_application_with_origin_memory
741+
.get(&origin_sha256)
744742
{
745743
existing_number
746744
} else {
747-
let new_number: ApplicationNumber =
748-
self.lookup_application_with_origin_memory_old.len();
745+
let new_number: ApplicationNumber = self.lookup_application_with_origin_memory.len();
749746

750-
// Update the source of truth.
747+
// Update the old map (will eventually be removed).
751748
// TODO[ID_353]: Remove this line.
752749
self.lookup_application_with_origin_memory_old
753750
.insert(origin_hash, new_number);
754751

755-
// Update the new map (will eventually become the source of truth).
752+
// Update the source of truth.
756753
self.lookup_application_with_origin_memory
757754
.insert(origin_sha256, new_number);
758755

@@ -772,9 +769,8 @@ impl<M: Memory + Clone> Storage<M> {
772769
&self,
773770
origin: &FrontendHostname,
774771
) -> Option<ApplicationNumber> {
775-
// TODO[ID-352]: Start reading from the new map (`lookup_application_with_origin_memory`).
776-
self.lookup_application_with_origin_memory_old
777-
.get(&StorableOriginHash::from_origin(origin))
772+
self.lookup_application_with_origin_memory
773+
.get(&StorableOriginSha256::from_origin(origin))
778774
}
779775

780776
/// Only used in tests.

src/internet_identity/src/storage/tests.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ mod application_lookup_tests {
397397

398398
assert_eq!(
399399
storage.lookup_application_number_with_origin(&origin),
400-
expected_old,
400+
expected_new,
401401
"Unexpected result from lookup_application_number_with_origin for origin {}",
402402
origin
403403
);
@@ -484,41 +484,47 @@ mod application_lookup_tests {
484484

485485
// These origins have hash prefix collisions with 8-byte hash but not with full SHA-256.
486486
// Example taken from https://github.com/yugt/sha256-prefix-collision
487-
let origin1 = "08RTz8".to_string();
488-
let origin2 = "4iRDWF".to_string();
487+
let origin0 = "08RTz8".to_string();
488+
let origin1 = "4iRDWF".to_string();
489489

490490
// Smoke test: these are indeed distinct origins.
491-
assert_ne!(origin1, origin2);
491+
assert_ne!(origin0, origin1);
492492
// However, the SHA256 prefix is the same.
493493
assert_eq!(
494-
StorableOriginHash::from_origin(&origin1),
495-
StorableOriginHash::from_origin(&origin2)
494+
StorableOriginHash::from_origin(&origin0),
495+
StorableOriginHash::from_origin(&origin1)
496496
);
497497
// But the entire SHA256 sum is different.
498498
assert_ne!(
499-
StorableOriginSha256::from_origin(&origin1),
500-
StorableOriginSha256::from_origin(&origin2)
499+
StorableOriginSha256::from_origin(&origin0),
500+
StorableOriginSha256::from_origin(&origin1)
501501
);
502502

503+
assert_application_lookup(&storage, &origin1, None, None);
504+
assert_application_lookup(&storage, &origin0, None, None);
505+
503506
// Test what happens if we insert both origins
504-
let app_num1 = storage.lookup_or_insert_application_number_with_origin(&origin1);
505-
let app_num2 = storage.lookup_or_insert_application_number_with_origin(&origin2);
507+
let app_num0 = storage.lookup_or_insert_application_number_with_origin(&origin0);
506508

507-
// Should get the same application number, as we do not yet detect collisions (since the
508-
// source of truth is still the old lookup map).
509-
// TODO[ID-352]: This assertion should be changed to `assert_ne`.
510-
assert_eq!(app_num1, app_num2);
511-
assert_eq!(app_num1, 0);
509+
assert_application_lookup(&storage, &origin1, None, Some(0));
510+
assert_application_lookup(&storage, &origin0, Some(0), Some(0));
512511

513-
// Both should be stored correctly in new SHA-256 map
512+
let app_num1 = storage.lookup_or_insert_application_number_with_origin(&origin1);
514513

515-
assert_application_lookup(&storage, &origin1, Some(0), Some(0));
516-
assert_application_lookup(&storage, &origin2, None, Some(0));
514+
// Should get the different application numbers, as we prevented the collision by using
515+
// the full SHA-256, not just a 8-byte prefix thereof.
516+
assert_eq!(app_num0, 0);
517+
assert_eq!(app_num1, 1);
518+
519+
// Both should be stored correctly in new SHA-256 map, but the collision in the old map
520+
// would cause the application to be overwritten (hence we expect app ID 1 for origin0).
521+
assert_application_lookup(&storage, &origin1, Some(1), Some(1));
522+
assert_application_lookup(&storage, &origin0, Some(0), Some(1));
517523

518524
// Applications should be stored with correct origins
525+
let stored_app0 = storage.stable_application_memory.get(&app_num0).unwrap();
519526
let stored_app1 = storage.stable_application_memory.get(&app_num1).unwrap();
520-
521-
// The first origin took place, not the second one
527+
assert_eq!(stored_app0.origin, origin0);
522528
assert_eq!(stored_app1.origin, origin1);
523529
}
524530

0 commit comments

Comments
 (0)