Skip to content

Commit

Permalink
fix: make view methods pausable (#144)
Browse files Browse the repository at this point in the history
* Make view method pausable

* Fix near-sdk version

* Skip except roles verification if it is empty

* Improve `near-sdk` version

* Replace `if/else` with early return
  • Loading branch information
karim-en authored Sep 11, 2024
1 parent eff928f commit 4f8466b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keywords = ["near", "smart contract", "plugin"]

[workspace.dependencies]
bitflags = "1.3"
near-sdk = "5.2"
near-sdk = ">=5.2, <5.4"
near-plugins = { path = "near-plugins" }
near-plugins-derive = { path = "near-plugins-derive" }
serde = "1"
Expand Down
4 changes: 4 additions & 0 deletions near-plugins-derive/src/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ pub fn if_paused(attrs: TokenStream, item: TokenStream) -> TokenStream {
}

fn get_bypass_condition(args: &ExceptSubArgs) -> proc_macro2::TokenStream {
if args.roles.len() == 0 {
return quote!();
}

let except_roles = args.roles.clone();
quote!(
let __except_roles: Vec<&str> = vec![#(#except_roles.into()),*];
Expand Down
10 changes: 10 additions & 0 deletions near-plugins-derive/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ pub fn assert_method_is_paused(res: ExecutionFinalResult) {
);
}

pub fn assert_view_method_is_paused(err: anyhow::Error) {
let err = format!("{:?}", err);
let must_contain = "Pausable: Method is paused";
assert!(
err.contains(must_contain),
"Expected method to be paused, instead it failed with: {}",
err
);
}

pub fn assert_pausable_escape_hatch_is_closed(res: ExecutionFinalResult, feature: &str) {
let must_contain = format!("Pausable: {feature} must be paused to use this function");
assert_failure_with(res, &must_contain);
Expand Down
1 change: 1 addition & 0 deletions near-plugins-derive/tests/contracts/pausable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Counter {
}

/// Returns the value of the counter.
#[pause]
pub fn get_counter(&self) -> u64 {
self.counter
}
Expand Down
25 changes: 25 additions & 0 deletions near-plugins-derive/tests/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use common::pausable_contract::PausableContract;
use common::utils::{
assert_failure_with, assert_insufficient_acl_permissions, assert_method_is_paused,
assert_pausable_escape_hatch_is_closed, assert_success_with, assert_success_with_unit_return,
assert_view_method_is_paused,
};
use near_sdk::serde_json::json;
use near_workspaces::network::Sandbox;
Expand Down Expand Up @@ -319,6 +320,11 @@ async fn test_pause_with_all_allows_except() -> anyhow::Result<()> {
.call_counter_modifier(&exempted_account, "increase_4")
.await?;
assert_success_with_unit_return(res);
let res = setup
.pausable_contract
.pa_unpause_feature(&setup.pause_manager, "ALL")
.await?;
assert_success_with(res, true);
assert_eq!(setup.get_counter().await?, 4);
Ok(())
}
Expand All @@ -341,6 +347,25 @@ async fn test_not_paused_with_different_key() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test]
async fn test_pause_view_method() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let res = setup
.call_counter_modifier(&setup.unauth_account, "increase_1")
.await?;
assert_success_with_unit_return(res);
assert_eq!(setup.get_counter().await?, 1);

let res = setup
.pausable_contract
.pa_pause_feature(&setup.pause_manager, "get_counter")
.await?;
assert_success_with(res, true);
assert_view_method_is_paused(setup.get_counter().await.unwrap_err());

Ok(())
}

#[tokio::test]
async fn test_work_after_unpause() -> anyhow::Result<()> {
let setup = Setup::new().await?;
Expand Down

0 comments on commit 4f8466b

Please sign in to comment.