Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/internet_identity/src/account_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ pub fn get_account_delegation(
})
}

/// This is needed to migrate the application lookup after dropping the 8-byte based version of that table,
/// and moving to the 32-byte version of it. This should be removed after the migration is done.
pub fn migrate_application_lookup() {
storage_borrow_mut(|storage| storage.rebuild_lookup_application_with_origin_memory());
}

/// Checks whether the stored number of accounts as per the counter exceeds the maximum permitted number.
/// If it does, it rebuilds the counter. If it still exceeds, it will return an error.
fn check_or_rebuild_max_anchor_accounts(
Expand Down
4 changes: 4 additions & 0 deletions src/internet_identity/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::account_management::migrate_application_lookup;
use crate::anchor_management::tentative_device_registration;
use crate::anchor_management::tentative_device_registration::{
TentativeDeviceRegistrationError, TentativeRegistrationInfo, VerifyTentativeDeviceError,
Expand Down Expand Up @@ -515,6 +516,9 @@ fn post_upgrade(maybe_arg: Option<InternetIdentityInit>) {
state::load_persistent_state();

initialize(maybe_arg);

// XXX: This is to migrate the application lookup to the 32 byte origin hash lookup. Remove it after that has been deployed.
migrate_application_lookup();
}

fn initialize(maybe_arg: Option<InternetIdentityInit>) {
Expand Down
14 changes: 13 additions & 1 deletion src/internet_identity/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ const REGISTRATION_CURRENT_RATE_MEMORY_INDEX: u8 = 6u8;
const LOOKUP_ANCHOR_WITH_DEVICE_CREDENTIAL_MEMORY_INDEX: u8 = 9u8;
const STABLE_ACCOUNT_MEMORY_INDEX: u8 = 10u8;
const STABLE_APPLICATION_MEMORY_INDEX: u8 = 11u8;
const LOOKUP_APPLICATION_WITH_ORIGIN_MEMORY_INDEX: u8 = 12u8;
// This memory index has been abandoned, do not use it
// const LOOKUP_APPLICATION_WITH_ORIGIN_MEMORY_INDEX: u8 = 12u8;
const LOOKUP_APPLICATION_WITH_ORIGIN_MEMORY_INDEX: u8 = 19u8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have this after the STABLE_ACCOUNT_COUNTER_DISCREPANCY_COUNTER_MEMORY_INDEX which is the 18 index?

Otherwise I can see ourselves adding the 19 again...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, why not.

const STABLE_ACCOUNT_REFERENCE_LIST_MEMORY_INDEX: u8 = 13u8;
const STABLE_ANCHOR_ACCOUNT_COUNTER_MEMORY_INDEX: u8 = 14u8;
const STABLE_ACCOUNT_COUNTER_MEMORY_INDEX: u8 = 15u8;
Expand Down Expand Up @@ -749,6 +751,16 @@ impl<M: Memory + Clone> Storage<M> {
.and_then(|application_number| self.stable_application_memory.get(&application_number))
}

/// Used for migrating from 8-byte to 32-byte origin hash
pub fn rebuild_lookup_application_with_origin_memory(&mut self) {
self.stable_application_memory
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be pointing to the new stable memory with index 19?

We need to keep both accessible until we finish the migration, right?

Copy link
Contributor Author

@LXIF LXIF Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.stable_application_memory points to memory ID 11. We only need to rebuild the lookup table, as it is the only place the origin hash is used. The old lookup table can be discarded.

.iter()
.for_each(|(app_num, app)| {
self.lookup_application_with_origin_memory
.insert(StorableOriginHash::from_origin(&app.origin), app_num);
})
}

fn lookup_account_references(
&self,
anchor_number: AnchorNumber,
Expand Down
13 changes: 5 additions & 8 deletions src/internet_identity/src/storage/storable/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,18 @@ impl Storable for StorableApplication {

#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct StorableOriginHash {
hash: [u8; 8],
hash: [u8; 32],
}

impl StorableOriginHash {
pub fn from_origin(origin: &FrontendHostname) -> Self {
let mut hasher = Sha256::new();
hasher.update(origin.as_bytes());
let full_hash_result = hasher.finalize();
// Truncate the 32-byte SHA-256 hash to the first 8 bytes.
let truncated_hash_slice: &[u8] = &full_hash_result[0..8];
let hash_8_bytes: [u8; 8] = truncated_hash_slice
.try_into()
.expect("Failed to truncate SHA256 hash to 8 bytes; slice length should be 8.");

Self { hash: hash_8_bytes }
Self {
hash: full_hash_result.into(),
}
}
}

Expand All @@ -61,7 +58,7 @@ impl Storable for StorableOriginHash {
}

const BOUND: Bound = Bound::Bounded {
max_size: 8,
max_size: 32,
is_fixed_size: true,
};
}
Loading