Skip to content
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

Simplify canister state lifecycle #6302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jasonz-dfinity
Copy link
Contributor

@jasonz-dfinity jasonz-dfinity commented Jan 31, 2025

Motivation

When deserializing the State and reconstruct it, an AccountsDbAsMap is first created when deserializing, then gets replaced with AccountsDbAsUnboundedStableBTreeMap. This makes the removal of AccountsDbAsMap (which is no longer used in production difficult.

Changes

  • Define types representing the exact data serialized/deserialized during upgrades
  • Establish a clear lifecycle:
    • Before an upgrade, there is a thread_local! variable State
    • State (conversion->) SerializableState
    • SerializableState (encode ->) Vec<u8>
    • Vec (write stable memory ->) stable memory partition
    • Pre-upgrade completed, post-upgrade start
    • Stable memory partition (read stable memory->) Vec<u8>
    • Vec<u8> (decode ->) SerializableState
    • SerializableState (conversion->) State

Tests

N/A since there are no functionality changes. The test in rs/backend/src/state/tests.rs should (still) cover the code being changed in this PR.

Related PRs

Previous | Next

@jasonz-dfinity jasonz-dfinity force-pushed the jason/NNS1-3030-3 branch 2 times, most recently from d5dc422 to 00893e4 Compare January 31, 2025 21:07
@jasonz-dfinity jasonz-dfinity force-pushed the jason/NNS1-3030-4 branch 2 times, most recently from 494327d to 3b7de2f Compare January 31, 2025 21:30
github-merge-queue bot pushed a commit that referenced this pull request Jan 31, 2025
…6301)

# Motivation

`accounts_db_stats_recomputed_on_upgrade` is no longer useful after the
migration of accounts to stable structures. It is always `opt false`,
which can be verified by `dfx canister --ic call
qoctq-giaaa-aaaaa-aaaea-cai get_stats | grep
accounts_db_stats_recomputed_on_upgrade`

# Changes

Remove everything related to `accounts_db_stats_recomputed_on_upgrade`

# Tests

N/A

# Related PRs

[Previous](#6298) |
[Next](#6302)
Base automatically changed from jason/NNS1-3030-3 to main January 31, 2025 21:55
@jasonz-dfinity jasonz-dfinity force-pushed the jason/NNS1-3030-4 branch 2 times, most recently from e527938 to 46a8a44 Compare January 31, 2025 22:07
@jasonz-dfinity jasonz-dfinity marked this pull request as ready for review January 31, 2025 22:09
@jasonz-dfinity jasonz-dfinity requested a review from a team as a code owner January 31, 2025 22:09
Comment on lines +44 to +49
let expected_accounts: BTreeMap<_, _> = accounts_store.range(..).collect();
let mut expected_account_stats = Stats::default();
accounts_store.get_stats(&mut expected_account_stats);
let expected_assets = assets.clone();
let expected_tvl_state = tvl_state.clone();
let expected_asset_hashes = asset_hashes.clone();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this is a side effect of doing a conversion from State to SerializableState, since during the conversion, State is destroyed, so we need to clone parts of the state before consuming it. An alternative is impl From<&State> for SerializableState. However, this would worsen the production code, since the conversion would have to clone data unnecessarily. The downside of having to clone the parts of the state for comparison in a unit test seems to acceptable.

@jasonz-dfinity jasonz-dfinity force-pushed the jason/NNS1-3030-4 branch 4 times, most recently from ad26122 to 19088a5 Compare February 1, 2025 00:37
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.

1 participant