Skip to content

Storage layer for multiple accounts #3059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
May 12, 2025
Merged

Storage layer for multiple accounts #3059

merged 49 commits into from
May 12, 2025

Conversation

LXIF
Copy link
Contributor

@LXIF LXIF commented Apr 30, 2025

Motivation

This is a preliminary draft of the Multi-ACcount IDentity storage implementation.

Changes

A lot! Created the storage structures and several structs and access methods and updated some of the lookup methods and such. This needs to be refactored though, and is definitely incomplete.

Tests

Added one unit test, which runs fine.

@LXIF LXIF changed the title [DO NOT MERGE]: MACID Vomit Draft [DO NOT MERGE]: MACID Storage Vomit Draft Apr 30, 2025
@LXIF LXIF changed the title [DO NOT MERGE]: MACID Storage Vomit Draft [DO NOT MERGE]: MACID Storage Preliminary Draft Apr 30, 2025
@LXIF LXIF marked this pull request as draft April 30, 2025 15:03
@lmuntaner lmuntaner force-pushed the andri/macid-storage branch from 314e465 to a51af68 Compare May 5, 2025 15:10
@lmuntaner lmuntaner force-pushed the andri/macid-storage branch 2 times, most recently from 5cb9f12 to 063ff24 Compare May 7, 2025 14:07
@lmuntaner lmuntaner changed the title [DO NOT MERGE]: MACID Storage Preliminary Draft Storage layer for multiple accounts May 7, 2025
@lmuntaner lmuntaner force-pushed the andri/macid-storage branch from 1fe3545 to 55a65aa Compare May 7, 2025 15:01
@lmuntaner lmuntaner requested a review from sea-snake May 7, 2025 15:05
@lmuntaner
Copy link
Collaborator

lmuntaner commented May 7, 2025

@sea-snake this is finally ready for review. On the other hand, it includes all the planned functionality in the storage layer.

I finally decided to create a different struct for each method.

Here is the comparison table:
Screenshot 2025-05-07 at 17 19 49

This can be improved, but adding extra parameters or making them optional when we don't want them optional made the methods more complex and I think those should be handled gracefully in the business logic, not at the storage layer.

@lmuntaner lmuntaner marked this pull request as ready for review May 8, 2025 11:57
Copy link
Contributor

@sea-snake sea-snake left a comment

Choose a reason for hiding this comment

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

I don't see a account limit and/or counter that keeps track of the number of stored default/additional accounts.

Given that we'd need to read all account references (with a range) every time we'd want to check this, should we consider two separate counters in stable memory for this?

Comment on lines +861 to +863
/// If the account number doesn't esist, returns a default Account.
/// If the account number exists but the account doesn't exist, returns None.
/// If the account exists, returns it as Account.
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment didn't match implementation, see below suggestion:

Suggested change
/// If the account number doesn't esist, returns a default Account.
/// If the account number exists but the account doesn't exist, returns None.
/// If the account exists, returns it as Account.
/// If the application doesn't exist, returns a default Account.
/// If the application exists but the account doesn't exist, returns None.
/// If the account exists, returns it as Account.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you say it doesn't match? This is the read_account, which checks the account_number. If None, returns a default account. Otherwise, it looks it up in memory.

Copy link
Contributor

Choose a reason for hiding this comment

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

/// Line 933-942: If the application doesn't exist, returns a default Account.
/// Line 944: If the application exists but the account doesn't exist, returns None.
/// Line 945-950: If the account exists, returns it as Account.

@lmuntaner lmuntaner force-pushed the andri/macid-storage branch from 88dde91 to e83a539 Compare May 8, 2025 19:59
@lmuntaner lmuntaner requested a review from sea-snake May 9, 2025 05:27
@lmuntaner
Copy link
Collaborator

@sea-snake changes done and passing pipeline green.

Please take a look, thanks!

vec![default_account_reference, additional_account_reference].into(),
);
// Two new account references were created.
self.update_counters(app_num, anchor_number, AccountType::AccountReference)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should always increase counters before inserting, increasing it without inserting an account due to an error is less critical than inserting an account and not increasing it due to an error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point, done!

@lmuntaner lmuntaner added this pull request to the merge queue May 12, 2025
Merged via the queue into main with commit 2736bc4 May 12, 2025
71 checks passed
@lmuntaner lmuntaner deleted the andri/macid-storage branch May 12, 2025 07:34
Comment on lines +820 to +825
for storable_ref in storable_refs_vec {
all_accounts.push(AccountReference {
account_number: storable_ref.account_number,
last_used: storable_ref.last_used,
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: you can probably use flat_map to achieve the same thing

Comment on lines +1067 to +1070
return Err(StorageError::MissingAccount {
anchor_number: params.anchor_number,
name: params.name.clone(),
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

At this exit point, is it OK that some mutations have happened? (lookup_or_insert_application_number_with_origin and update_counters

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, we will be adding a check to fix the counters in case they run out of sync.

And one more application isn't so bad either, it's just a bit more memory.

Comment on lines +1056 to +1063
for r_mut in refs_vec.iter_mut() {
if r_mut.account_number.is_none() {
// Found the default account reference.
r_mut.account_number = Some(new_account_number);
found_and_updated = true;
break;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you do .iter_mut().find(...), I think it will be more apparent that no mutations have happened in the error case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants