Skip to content
Open
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: 2 additions & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 175 additions & 0 deletions contracts/access_control/src/circuit_breaker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//! Reusable two-tier emergency circuit breaker.
//!
//! A designated guardian (or the admin) can pause a contract instantly, but
//! resuming requires the admin — the "security council" — to propose the
//! unpause and wait out [`UNPAUSE_TIMELOCK_SECONDS`]. This keeps a compromised
//! guardian from flipping the protocol back on during an incident while still
//! allowing a single operator to stop the bleeding immediately.
//!
//! Consuming contracts initialise the breaker once, gate state-mutating
//! entrypoints with [`crate::require_not_paused!`], and deliberately leave
//! emergency-exit paths ungated.
//!
//! State lives under a dedicated key namespace so it cannot collide with the
//! host contract's own storage.

use crate::AcError;
use soroban_sdk::{contracttype, panic_with_error, symbol_short, Address, Env};

/// Delay between proposing and executing an unpause: 24 hours.
pub const UNPAUSE_TIMELOCK_SECONDS: u64 = 24 * 60 * 60;

const CB_TOPIC: soroban_sdk::Symbol = symbol_short!("CB");

#[contracttype]
#[derive(Clone)]
enum BreakerKey {
Admin,
Guardian,
Paused,
UnpauseUnlockAt,
}

/// Initialise the breaker for the calling contract.
///
/// The caller (the host contract's `initialize`) is responsible for
/// authenticating `admin`; this function only stores the hierarchy. The admin
/// is also the initial guardian so a fresh deployment can always pause.
pub fn initialize(env: &Env, admin: &Address, guardian: &Address) {
env.storage().instance().set(&BreakerKey::Admin, admin);
env.storage()
.instance()
.set(&BreakerKey::Guardian, guardian);
env.storage().instance().set(&BreakerKey::Paused, &false);
env.storage()
.instance()
.remove(&BreakerKey::UnpauseUnlockAt);
}

/// Returns the configured breaker admin, if initialised.
pub fn admin(env: &Env) -> Option<Address> {
env.storage().instance().get(&BreakerKey::Admin)
}

/// Returns the configured guardian, if any.
pub fn guardian(env: &Env) -> Option<Address> {
env.storage().instance().get(&BreakerKey::Guardian)
}

/// Replace the guardian. Admin only; a new guardian can pause but never unpause.
pub fn set_guardian(env: &Env, caller: &Address, new_guardian: &Address) {
require_admin(env, caller);
env.storage()
.instance()
.set(&BreakerKey::Guardian, new_guardian);
env.events().publish(
(CB_TOPIC, symbol_short!("guardian")),
(caller.clone(), new_guardian.clone()),
);
}

/// Pause immediately. The admin and the guardian may call this; anyone else is
/// rejected. Re-pausing clears any pending unpause so the timelock restarts.
pub fn pause(env: &Env, caller: &Address) {
caller.require_auth();
let is_admin = admin(env).as_ref() == Some(caller);
let is_guardian = guardian(env).as_ref() == Some(caller);
if !is_admin && !is_guardian {
panic_with_error!(env, AcError::Unauthorized);
}
env.storage().instance().set(&BreakerKey::Paused, &true);
env.storage()
.instance()
.remove(&BreakerKey::UnpauseUnlockAt);
env.events()
.publish((CB_TOPIC, symbol_short!("pause")), (caller.clone(), true));
}

/// Schedule an unpause and return the timestamp at which it becomes executable.
///
/// Admin only. Requires the contract to be paused, otherwise there is nothing
/// to resume and the call likely indicates a mistaken operator.
pub fn propose_unpause(env: &Env, caller: &Address) -> u64 {
require_admin(env, caller);
if !is_paused(env) {
panic_with_error!(env, AcError::NotPaused);
}
let unlock_at = env
.ledger()
.timestamp()
.checked_add(UNPAUSE_TIMELOCK_SECONDS)
.unwrap_or_else(|| panic_with_error!(env, AcError::Overflow));
env.storage()
.instance()
.set(&BreakerKey::UnpauseUnlockAt, &unlock_at);
env.events().publish(
(CB_TOPIC, symbol_short!("propose")),
(caller.clone(), unlock_at),
);
unlock_at
}

/// Resume the contract. Admin only, and only after the unpause proposal has
/// passed its timelock. A guardian can never unpause.
pub fn unpause(env: &Env, caller: &Address) {
require_admin(env, caller);
if !is_paused(env) {
panic_with_error!(env, AcError::NotPaused);
}
let unlock_at: u64 = env
.storage()
.instance()
.get(&BreakerKey::UnpauseUnlockAt)
.unwrap_or_else(|| panic_with_error!(env, AcError::NoPendingUnpause));
if env.ledger().timestamp() < unlock_at {
panic_with_error!(env, AcError::TimelockNotExpired);
}
env.storage().instance().set(&BreakerKey::Paused, &false);
env.storage()
.instance()
.remove(&BreakerKey::UnpauseUnlockAt);
env.events().publish(
(CB_TOPIC, symbol_short!("unpause")),
(caller.clone(), false),
);
}

/// Cancel a pending unpause while remaining paused. Admin only.
pub fn cancel_unpause(env: &Env, caller: &Address) {
require_admin(env, caller);
if !env.storage().instance().has(&BreakerKey::UnpauseUnlockAt) {
panic_with_error!(env, AcError::NoPendingUnpause);
}
env.storage()
.instance()
.remove(&BreakerKey::UnpauseUnlockAt);
env.events()
.publish((CB_TOPIC, symbol_short!("cancel")), (caller.clone(), true));
}

/// Returns the timestamp at which a pending unpause becomes executable.
pub fn unpause_unlock_at(env: &Env) -> Option<u64> {
env.storage().instance().get(&BreakerKey::UnpauseUnlockAt)
}

/// Returns true while the breaker is active.
pub fn is_paused(env: &Env) -> bool {
env.storage()
.instance()
.get(&BreakerKey::Paused)
.unwrap_or(false)
}

/// Panics with [`AcError::ContractPaused`] while the breaker is active.
pub fn assert_not_paused(env: &Env) {
if is_paused(env) {
panic_with_error!(env, AcError::ContractPaused);
}
}

fn require_admin(env: &Env, caller: &Address) {
caller.require_auth();
if admin(env).as_ref() != Some(caller) {
panic_with_error!(env, AcError::Unauthorized);
}
}
17 changes: 16 additions & 1 deletion contracts/access_control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

use soroban_sdk::{contracterror, Env};

pub mod circuit_breaker;
pub mod roles;
pub mod timelock;

pub use roles::{grant_role, has_role, initialize, is_paused, pause, revoke_role, unpause, Role};
pub use circuit_breaker::{
assert_not_paused, cancel_unpause, is_paused, pause, propose_unpause, unpause,
unpause_unlock_at,
};
pub use roles::{grant_role, has_role, initialize, revoke_role, Role};
pub use timelock::{
admin, cancel_admin_proposal, claim_admin, pending_admin, propose_admin, AdminProposal,
ADMIN_HANDOVER_DELAY,
Expand All @@ -27,16 +32,26 @@ pub enum AcError {
RoleNotGranted = 306,
InvalidRole = 307,
InvalidAdminProposal = 308,
NoPendingUnpause = 309,
NotPaused = 310,
}

/// Panics with `AcError::ContractPaused` if the circuit-breaker is active.
///
/// A second form lets a host contract surface the pause in its own error
/// space: `require_not_paused!(&env, AmmError::ContractPaused)`.
#[macro_export]
macro_rules! require_not_paused {
($env:expr) => {
if $crate::is_paused($env) {
soroban_sdk::panic_with_error!($env, $crate::AcError::ContractPaused);
}
};
($env:expr, $error:expr) => {
if $crate::is_paused($env) {
soroban_sdk::panic_with_error!($env, $error);
}
};
}

/// Panics with `AcError::Unauthorized` if `addr` does not hold `role`.
Expand Down
37 changes: 4 additions & 33 deletions contracts/access_control/src/roles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::AcError;
use soroban_sdk::{contracttype, symbol_short, Address, Env, Symbol};
use soroban_sdk::{contracttype, symbol_short, Address, Env};

/// Roles supported by the shared access-control module.
///
Expand All @@ -21,12 +21,9 @@ pub enum Role {
#[derive(Clone)]
pub(crate) enum StorageKey {
Admin,
Paused,
RoleMember(Role, Address),
}

pub(crate) const PAUSED_KEY: Symbol = symbol_short!("AC_PAUSED");

/// Initializes access control once. Repeated calls never replace the admin.
pub fn initialize(env: &Env, initial_admin: &Address) {
let key = StorageKey::Admin;
Expand All @@ -35,7 +32,9 @@ pub fn initialize(env: &Env, initial_admin: &Address) {
}
initial_admin.require_auth();
env.storage().instance().set(&key, initial_admin);
env.storage().instance().set(&PAUSED_KEY, &false);
// The admin is the initial guardian; the breaker's unpause timelock lives
// in `circuit_breaker`.
crate::circuit_breaker::initialize(env, initial_admin, initial_admin);
env.events().publish(
(symbol_short!("AC_ROLE"), Role::Admin),
(initial_admin.clone(), true, initial_admin.clone()),
Expand Down Expand Up @@ -109,31 +108,3 @@ pub fn revoke_role(env: &Env, caller: &Address, role: Role, account: &Address) {
(account.clone(), false, caller.clone()),
);
}

/// Returns true if the contract is currently paused.
pub fn is_paused(env: &Env) -> bool {
env.storage()
.instance()
.get::<Symbol, bool>(&PAUSED_KEY)
.unwrap_or(false)
}

/// Pause application operations. Admins and guardians may pause.
pub fn pause(env: &Env, caller: &Address) {
caller.require_auth();
if !has_role(env, caller, Role::Admin) && !has_role(env, caller, Role::Guardian) {
soroban_sdk::panic_with_error!(env, AcError::Unauthorized);
}
env.storage().instance().set(&PAUSED_KEY, &true);
env.events()
.publish((symbol_short!("AC_PAUSE"),), caller.clone());
}

/// Resume application operations. Only the admin may unpause.
pub fn unpause(env: &Env, caller: &Address) {
caller.require_auth();
crate::require_admin(env, caller);
env.storage().instance().set(&PAUSED_KEY, &false);
env.events()
.publish((symbol_short!("AC_UNPAUS"),), caller.clone());
}
Loading
Loading