-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Improve Pausable Migration Guide and Add Migration Tests #163
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
21 changes: 21 additions & 0 deletions
21
near-plugins-derive/tests/contracts/pausable_new/Cargo.toml
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,21 @@ | ||
| [package] | ||
| name = "pausable_new" | ||
| version = "0.0.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| near-plugins = { path = "../../../../near-plugins" } | ||
| near-sdk = "5.2" | ||
|
|
||
| [profile.release] | ||
| codegen-units = 1 | ||
| opt-level = "z" | ||
| lto = true | ||
| debug = false | ||
| panic = "abort" | ||
| overflow-checks = true | ||
|
|
||
| [workspace] |
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,8 @@ | ||
| build: | ||
| cargo build --target wasm32-unknown-unknown --release | ||
|
|
||
| # Helpful for debugging. Requires `cargo-expand`. | ||
| expand: | ||
| cargo expand > expanded.rs | ||
|
|
||
| .PHONY: build expand |
4 changes: 4 additions & 0 deletions
4
near-plugins-derive/tests/contracts/pausable_new/rust-toolchain
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,4 @@ | ||
| [toolchain] | ||
| channel = "1.84.0" | ||
| components = ["clippy", "rustfmt"] | ||
| targets = [ "wasm32-unknown-unknown" ] |
105 changes: 105 additions & 0 deletions
105
near-plugins-derive/tests/contracts/pausable_new/src/lib.rs
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,105 @@ | ||
| use near_plugins::{ | ||
| access_control, if_paused, pause, AccessControlRole, AccessControllable, Pausable, | ||
| }; | ||
| use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
| use near_sdk::serde::{Deserialize, Serialize}; | ||
| use near_sdk::{env, near, AccountId, PanicOnDefault}; | ||
|
|
||
| /// Define roles for access control of `Pausable` features. | ||
| /// IMPORTANT: Keep the same order of existing variants to preserve permission mappings. | ||
| #[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)] | ||
| #[serde(crate = "near_sdk::serde")] | ||
| pub enum Role { | ||
| /// Now only used for pausing features | ||
| PauseManager, | ||
| /// Existing roles kept in the same order | ||
| Unrestricted4Increaser, | ||
| /// Existing roles kept in the same order | ||
| Unrestricted4Decreaser, | ||
| /// Existing roles kept in the same order | ||
| Unrestricted4Modifier, | ||
| /// Add new roles at the end | ||
| UnpauseManager, | ||
| } | ||
|
|
||
| #[access_control(role_type(Role))] | ||
| #[near(contract_state)] | ||
| #[derive(Pausable, PanicOnDefault)] | ||
| #[pausable(pause_roles(Role::PauseManager), unpause_roles(Role::UnpauseManager))] | ||
| pub struct Counter { | ||
| counter: u64, | ||
| } | ||
|
|
||
| #[near] | ||
| impl Counter { | ||
| /// Constructor initializes the counter to 0 and sets up ACL. | ||
| #[init] | ||
| pub fn new(pause_manager: AccountId, unpause_manager: AccountId) -> Self { | ||
| let mut contract = Self { counter: 0 }; | ||
|
|
||
| // Make the contract itself super admin | ||
| near_sdk::require!( | ||
| contract.acl_init_super_admin(env::current_account_id()), | ||
| "Failed to initialize super admin", | ||
| ); | ||
|
|
||
| // Grant roles to the provided accounts | ||
| let result = contract.acl_grant_role(Role::PauseManager.into(), pause_manager); | ||
| near_sdk::require!(Some(true) == result, "Failed to grant pause role"); | ||
|
|
||
| let result = contract.acl_grant_role(Role::UnpauseManager.into(), unpause_manager); | ||
| near_sdk::require!(Some(true) == result, "Failed to grant unpause role"); | ||
|
|
||
| contract | ||
| } | ||
|
|
||
| /// Returns the current counter value | ||
| #[pause] | ||
| pub fn get_counter(&self) -> u64 { | ||
| self.counter | ||
| } | ||
|
|
||
| /// Increments the counter - can be paused | ||
| #[pause] | ||
| pub fn increment(&mut self) { | ||
| self.counter += 1; | ||
| } | ||
|
|
||
| /// Similar to `#[pause]` but use an explicit name for the feature. | ||
| #[pause(name = "Increase by two")] | ||
| pub fn increase_2(&mut self) { | ||
| self.counter += 2; | ||
| } | ||
|
|
||
| /// Similar to `#[pause]` but roles passed as argument may still successfully call this method | ||
| /// even when the corresponding feature is paused. | ||
| #[pause(except(roles(Role::Unrestricted4Increaser, Role::Unrestricted4Modifier)))] | ||
| pub fn increase_4(&mut self) { | ||
| self.counter += 4; | ||
| } | ||
|
|
||
| /// This method can only be called when "increment" is paused. | ||
| #[if_paused(name = "increment")] | ||
| pub fn decrease_1(&mut self) { | ||
| self.counter -= 1; | ||
| } | ||
|
|
||
| /// For verifying that an account has a specific role | ||
| pub fn has_role(&self, role: String, account_id: AccountId) -> bool { | ||
| self.acl_has_role(role, account_id) | ||
| } | ||
|
|
||
| /// Migration function to maintain backward compatibility | ||
| /// Grants UnpauseManager role to existing PauseManager accounts | ||
| #[private] | ||
| pub fn migrate_pause_unpause_roles(&mut self) { | ||
| // Get all accounts with PauseManager role | ||
| let pause_managers = self.acl_get_grantees("PauseManager".to_string(), 0, 100); | ||
|
|
||
| // Grant UnpauseManager role to all existing PauseManager accounts | ||
| for account_id in pause_managers { | ||
| let result = self.acl_grant_role(Role::UnpauseManager.into(), account_id); | ||
| near_sdk::require!(result.is_some(), "Failed to grant UnpauseManager role"); | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
near-plugins-derive/tests/contracts/pausable_old/Cargo.toml
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,21 @@ | ||
| [package] | ||
| name = "pausable_old" | ||
| version = "0.0.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| near-plugins = { path = "../../../../near-plugins" } | ||
| near-sdk = "5.2" | ||
|
|
||
| [profile.release] | ||
| codegen-units = 1 | ||
| opt-level = "z" | ||
| lto = true | ||
| debug = false | ||
| panic = "abort" | ||
| overflow-checks = true | ||
|
|
||
| [workspace] |
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,8 @@ | ||
| build: | ||
| cargo build --target wasm32-unknown-unknown --release | ||
|
|
||
| # Helpful for debugging. Requires `cargo-expand`. | ||
| expand: | ||
| cargo expand > expanded.rs | ||
|
|
||
| .PHONY: build expand |
4 changes: 4 additions & 0 deletions
4
near-plugins-derive/tests/contracts/pausable_old/rust-toolchain
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,4 @@ | ||
| [toolchain] | ||
| channel = "1.84.0" | ||
| components = ["clippy", "rustfmt"] | ||
| targets = [ "wasm32-unknown-unknown" ] |
85 changes: 85 additions & 0 deletions
85
near-plugins-derive/tests/contracts/pausable_old/src/lib.rs
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,85 @@ | ||
| use near_plugins::{ | ||
| access_control, if_paused, pause, AccessControlRole, AccessControllable, Pausable, | ||
| }; | ||
| use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
| use near_sdk::serde::{Deserialize, Serialize}; | ||
| use near_sdk::{env, near, AccountId, PanicOnDefault}; | ||
|
|
||
| /// Define roles for access control of `Pausable` features. | ||
| #[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)] | ||
| #[serde(crate = "near_sdk::serde")] | ||
| pub enum Role { | ||
| /// The pause manager in the old style can both pause and unpause | ||
| PauseManager, | ||
| /// For testing except functionality | ||
| Unrestricted4Increaser, | ||
| /// For testing except functionality | ||
| Unrestricted4Decreaser, | ||
| /// For testing except functionality | ||
| Unrestricted4Modifier, | ||
| } | ||
|
|
||
| #[access_control(role_type(Role))] | ||
| #[near(contract_state)] | ||
| #[derive(Pausable, PanicOnDefault)] | ||
| #[pausable(pause_roles(Role::PauseManager), unpause_roles(Role::PauseManager))] | ||
| pub struct Counter { | ||
| counter: u64, | ||
| } | ||
|
|
||
| #[near] | ||
| impl Counter { | ||
| /// Constructor initializes the counter to 0 and sets up ACL. | ||
| #[init] | ||
| pub fn new(pause_manager: AccountId) -> Self { | ||
| let mut contract = Self { counter: 0 }; | ||
|
|
||
| // Make the contract itself super admin | ||
| near_sdk::require!( | ||
| contract.acl_init_super_admin(env::current_account_id()), | ||
| "Failed to initialize super admin", | ||
| ); | ||
|
|
||
| // Grant role to the provided account | ||
| let result = contract.acl_grant_role(Role::PauseManager.into(), pause_manager); | ||
| near_sdk::require!(Some(true) == result, "Failed to grant pause role"); | ||
|
|
||
| contract | ||
| } | ||
|
|
||
| /// Returns the current counter value | ||
| #[pause] | ||
| pub fn get_counter(&self) -> u64 { | ||
| self.counter | ||
| } | ||
|
|
||
| /// Increments the counter - can be paused | ||
| #[pause] | ||
| pub fn increment(&mut self) { | ||
| self.counter += 1; | ||
| } | ||
|
|
||
| /// Similar to `#[pause]` but use an explicit name for the feature. | ||
| #[pause(name = "Increase by two")] | ||
| pub fn increase_2(&mut self) { | ||
| self.counter += 2; | ||
| } | ||
|
|
||
| /// Similar to `#[pause]` but roles passed as argument may still successfully call this method | ||
| /// even when the corresponding feature is paused. | ||
| #[pause(except(roles(Role::Unrestricted4Increaser, Role::Unrestricted4Modifier)))] | ||
| pub fn increase_4(&mut self) { | ||
| self.counter += 4; | ||
| } | ||
|
|
||
| /// This method can only be called when "increment" is paused. | ||
| #[if_paused(name = "increment")] | ||
| pub fn decrease_1(&mut self) { | ||
| self.counter -= 1; | ||
| } | ||
|
|
||
| /// For verifying that an account has a specific role | ||
| pub fn has_role(&self, role: String, account_id: AccountId) -> bool { | ||
| self.acl_has_role(role, account_id) | ||
| } | ||
| } |
Oops, something went wrong.
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.