Repository navigation
fix(paid-service): persist init args across canister upgrades #231
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
AntonioVentilii
merged 3 commits into
main
from
av/paid-service-post-upgrade-state-79d062
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
72b93ce
fix(paid_service): persist init args across canister upgrades
AntonioVentilii e5ae1ae
fix(paid-service): tolerate upgrades from un-persisted versions
AntonioVentilii c55edbb
Merge branch 'main' into av/paid-service-post-upgrade-state-79d062
AntonioVentilii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| //! Regression tests: the paid service must remain usable after a canister upgrade. | ||
| //! | ||
| //! The init args (which include the payment ledger) are held in a non-stable `thread_local`. | ||
| //! Without a `post_upgrade` hook to restore them, `cost_1b` traps with "No init args provided" | ||
| //! after any upgrade. See the `pre_upgrade`/`post_upgrade` hooks in `src/lib.rs`. | ||
| use crate::util::pic_canister::PicCanisterTrait; | ||
| use crate::util::test_environment::{PaidMethods, TestSetup, LEDGER_FEE}; | ||
| use example_paid_service_api::InitArgs; | ||
| use ic_papi_api::caller::CallerPaysIcrc2Tokens; | ||
| use ic_papi_api::cycles::cycles_ledger_canister_id; | ||
| use ic_papi_api::{PaymentError, PaymentType}; | ||
|
|
||
| /// Drives a `cost_1b` call and asserts it succeeds, i.e. the payment ledger config is available. | ||
| fn assert_cost_1b_succeeds(setup: &TestSetup) { | ||
| let method = PaidMethods::Cost1b; | ||
| setup.user_approves_payment_for_paid_service(method.cost() + LEDGER_FEE); | ||
| let response: Result<String, PaymentError> = setup.call_paid_service( | ||
| setup.user, | ||
| method, | ||
| PaymentType::CallerPaysIcrc2Tokens(CallerPaysIcrc2Tokens { | ||
| ledger: cycles_ledger_canister_id(), | ||
| }), | ||
| ); | ||
| assert_eq!( | ||
| response, | ||
| Ok("Yes, you paid 1 billion cycles!".to_string()), | ||
| "cost_1b should succeed after a canister upgrade", | ||
| ); | ||
| } | ||
|
|
||
| /// Verifies that `cost_1b` still works after an upgrade that relies on stable memory. | ||
| /// | ||
| /// The upgrade happens before any call has lazily initialised the payment guard, so the guard is | ||
| /// first built (reading the init args) only after the upgrade. This is exactly the path that used | ||
| /// to trap when the init args were not persisted across upgrades. | ||
| #[test] | ||
| fn cost_1b_works_after_upgrade_restoring_from_stable_memory() { | ||
| let setup = TestSetup::default(); | ||
| // Upgrade with no explicit args: the init args must be restored from stable memory. | ||
| setup.upgrade_paid_service(None); | ||
| assert_cost_1b_succeeds(&setup); | ||
| } | ||
|
|
||
| /// Verifies that init args supplied explicitly at upgrade time are used. | ||
| /// | ||
| /// This is the path an operator takes when upgrading from a version that never persisted its args | ||
| /// (e.g. one without `pre_upgrade`): the ledger config is provided in the upgrade itself. | ||
| #[test] | ||
| fn cost_1b_works_after_upgrade_with_explicit_args() { | ||
| let setup = TestSetup::default(); | ||
| let ledger = setup.ledger.canister_id(); | ||
| setup.upgrade_paid_service(Some(InitArgs { ledger })); | ||
| assert_cost_1b_succeeds(&setup); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.