Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Apr 29, 2024
1 parent cde4538 commit 1a5e3ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/pausing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ mod events;
mod storage;

pub use errors::contract_paused_error;
pub use storage::{ensure_not_paused, is_paused_since_block, pause_contract, resume_contract};
pub use storage::{
ensure_not_paused, is_paused, pause_contract, paused_since_block, resume_contract,
};
61 changes: 29 additions & 32 deletions src/pausing/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ use cw_storage_plus::Item;

const PAUSED_SINCE_BLOCK: Item<u64> = Item::new("paused_since");

pub fn is_paused_since_block(storage: &dyn Storage) -> StdResult<Option<u64>> {
pub fn paused_since_block(storage: &dyn Storage) -> StdResult<Option<u64>> {
PAUSED_SINCE_BLOCK.may_load(storage)
}

pub fn is_paused(storage: &dyn Storage, env: &Env) -> StdResult<bool> {
// Return error if contract is paused
if let Some(paused_since_block) = paused_since_block(storage)? {
if env.block.height >= paused_since_block {
return Ok(true);
}
}
Ok(false)
}

pub fn pause_contract(
storage: &mut dyn Storage,
response_handler: &mut ResponseHandler,
Expand All @@ -28,10 +38,8 @@ pub fn resume_contract(storage: &mut dyn Storage, response_handler: &mut Respons

pub fn ensure_not_paused(storage: &dyn Storage, env: &Env) -> StdResult<()> {
// Return error if contract is paused
if let Some(paused_since_block) = is_paused_since_block(storage)? {
if env.block.height >= paused_since_block {
return Err(contract_paused_error());
}
if is_paused(storage, env)? {
return Err(contract_paused_error());
}
Ok(())
}
Expand All @@ -46,14 +54,14 @@ mod tests {
fn test_pausing() {
let mut deps = mock_dependencies();
let pause_height = 123u64;
let env_before_env = mock_env_with_height(pause_height - 1);
let env_at_paused = mock_env_with_height(pause_height);
let env_after_paused = mock_env_with_height(pause_height + 1);

// State before pausing
assert!(is_paused_since_block(deps.as_ref().storage)
.unwrap()
.is_none());
assert!(
ensure_not_paused(deps.as_ref().storage, &mock_env_with_height(pause_height)).is_ok()
);
assert!(paused_since_block(deps.as_ref().storage).unwrap().is_none());
assert!(!is_paused(deps.as_ref().storage, &env_at_paused).unwrap());
assert!(ensure_not_paused(deps.as_ref().storage, &env_at_paused).is_ok());

// Pause contract
assert!(pause_contract(
Expand All @@ -65,42 +73,31 @@ mod tests {

// Contract is paused
assert_eq!(
is_paused_since_block(deps.as_ref().storage)
.unwrap()
.unwrap(),
paused_since_block(deps.as_ref().storage).unwrap().unwrap(),
pause_height
);

assert!(ensure_not_paused(
deps.as_ref().storage,
&mock_env_with_height(pause_height - 1)
)
.is_ok());
assert!(ensure_not_paused(deps.as_ref().storage, &env_before_env).is_ok());
assert!(!is_paused(deps.as_ref().storage, &env_before_env).unwrap());

assert_err(
&ensure_not_paused(deps.as_ref().storage, &mock_env_with_height(pause_height)),
&ensure_not_paused(deps.as_ref().storage, &env_at_paused),
&contract_paused_error(),
);
assert!(is_paused(deps.as_ref().storage, &env_at_paused).unwrap());

assert_err(
&ensure_not_paused(
deps.as_ref().storage,
&mock_env_with_height(pause_height + 1),
),
&ensure_not_paused(deps.as_ref().storage, &env_after_paused),
&contract_paused_error(),
);

// Resume contract
resume_contract(deps.as_mut().storage, &mut ResponseHandler::default());

// Contract is resumed
assert!(is_paused_since_block(deps.as_ref().storage)
.unwrap()
.is_none());
assert!(paused_since_block(deps.as_ref().storage).unwrap().is_none());

assert!(ensure_not_paused(
deps.as_ref().storage,
&mock_env_with_height(pause_height - 1)
)
.is_ok());
assert!(ensure_not_paused(deps.as_ref().storage, &env_before_env).is_ok());
assert!(
ensure_not_paused(deps.as_ref().storage, &mock_env_with_height(pause_height)).is_ok()
);
Expand Down

0 comments on commit 1a5e3ce

Please sign in to comment.