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

fix: make view methods pausable #144

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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 also call the get_counter as a view method to ensure that we are able to do so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already do a view call line below

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
Loading