From d6563aeb3226d4bb0f7ab3950240e1cbd2bfbd5d Mon Sep 17 00:00:00 2001 From: Oba Date: Fri, 25 Oct 2024 12:45:11 +0200 Subject: [PATCH] protocol handler: check already paused soft_pause --- .../src/protocol_handler.cairo | 5 ++++ .../tests/test_protocol_handler.cairo | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cairo/protocol_handler/src/protocol_handler.cairo b/cairo/protocol_handler/src/protocol_handler.cairo index fb2945f97..57002a88a 100644 --- a/cairo/protocol_handler/src/protocol_handler.cairo +++ b/cairo/protocol_handler/src/protocol_handler.cairo @@ -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'; } //* ------------------------------------------------------------------------ *// @@ -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; diff --git a/cairo/protocol_handler/tests/test_protocol_handler.cairo b/cairo/protocol_handler/tests/test_protocol_handler.cairo index b52f93e75..100121293 100644 --- a/cairo/protocol_handler/tests/test_protocol_handler.cairo +++ b/cairo/protocol_handler/tests/test_protocol_handler.cairo @@ -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:: = 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();