Skip to content

Commit

Permalink
protocol handler: check already paused soft_pause
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Nov 26, 2024
1 parent 0f79db8 commit 9ed9f78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cairo/protocol_handler/src/protocol_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub mod ProtocolHandler {

pub mod errors {
pub const ONLY_KAKAROT_CAN_BE_CALLED: felt252 = 'ONLY_KAKAROT_CAN_BE_CALLED';
pub const PROTOCOL_ALREADY_PAUSED: felt252 = 'PROTOCOL_ALREADY_PAUSED';
}

//* ------------------------------------------------------------------------ *//
Expand Down Expand Up @@ -237,6 +238,10 @@ pub mod ProtocolHandler {
// Check only guardians can call
self.accesscontrol.assert_only_role(GUARDIAN_ROLE);

// Check if the protocol is already paused
let protocol_frozen_until = self.protocol_frozen_until.read();
assert(protocol_frozen_until == 0, errors::PROTOCOL_ALREADY_PAUSED);

// Cache the protocol frozen until timestamp
let protocol_frozen_until = get_block_timestamp().into() + SOFT_PAUSE_DELAY;

Expand Down
25 changes: 25 additions & 0 deletions cairo/protocol_handler/tests/test_protocol_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,31 @@ fn test_protocol_handler_soft_pause_should_fail_wrong_caller() {
protocol_handler.soft_pause();
}

#[test]
#[should_panic(expected: 'PROTOCOL_ALREADY_PAUSED')]
fn test_protocol_handler_soft_pause_should_fail_already_paused() {
let (protocol_handler, _) = setup_contracts_for_testing();

// Simulate pausing by writing in the storage
// Find the storage address for the ProtocolFrozenUntil
let mut state = ProtocolHandler::contract_state_for_testing();
let storage_address = state.protocol_frozen_until;
let value = (get_block_timestamp() + 1);
let mut serialized_value: Array::<felt252> = array![];
Serde::serialize(@value, ref serialized_value);
// Store the value in the storage of the protocol handler
store(
protocol_handler.contract_address, storage_address.__base_address__, serialized_value.span()
);

// Change caller to a guardian
let guardians = guardians_mock();
start_cheat_caller_address(protocol_handler.contract_address, *guardians[0]);

// Call the protocol handler soft_pause, should fail as protocol is already paused
protocol_handler.soft_pause();
}

#[test]
fn test_protocol_handler_soft_pause_should_pass() {
let (protocol_handler, _) = setup_contracts_for_testing();
Expand Down

0 comments on commit 9ed9f78

Please sign in to comment.