Skip to content
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
75 changes: 40 additions & 35 deletions contracts/admin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@ pub enum ContractError {
}

const MIN_COOLDOWN_SECONDS: u64 = 60;
fn get_admin_cooldown_internal(env: &Env) -> u64 {
env.storage()
.persistent()
.get(&DataKey::AdminCooldownSeconds)
.unwrap_or(0)
}

fn check_admin_cooldown_internal(
env: &Env,
admin: &Address,
function_name: &Symbol,
) -> Result<(), ContractError> {
admin.require_auth();
let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.ok_or(ContractError::AdminNotSet)?;
if *admin != stored_admin {
return Err(ContractError::Unauthorized);
}
let cooldown = get_admin_cooldown_internal(env);
if cooldown == 0 {
return Ok(());
}
let now = env.ledger().timestamp();
let last_key = DataKey::AdminLastAction(function_name.clone());
let last_action: u64 = env
.storage()
.persistent()
.get(&last_key)
.unwrap_or(0);
if last_action > 0 && now < last_action.saturating_add(cooldown) {
return Err(ContractError::AdminActionTimelocked);
}
env.storage().persistent().set(&last_key, &now);
Ok(())
}

#[contract]
pub struct AdminContract;
Expand Down Expand Up @@ -95,44 +133,11 @@ impl AdminContract {

/// Get the configured admin cooldown period in seconds.
pub fn get_admin_cooldown(env: Env) -> u64 {
env.storage()
.persistent()
.get(&DataKey::AdminCooldownSeconds)
.unwrap_or(0)
get_admin_cooldown_internal(&env)
}

/// Check and enforce admin cooldown for a specific function.
pub fn check_admin_cooldown(env: Env, admin: Address, function_name: Symbol) -> Result<(), ContractError> {
admin.require_auth();

let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.ok_or(ContractError::AdminNotSet)?;

if admin != stored_admin {
return Err(ContractError::Unauthorized);
}

let cooldown = Self::get_admin_cooldown(env);
if cooldown == 0 {
return Ok(());
}

let now = env.ledger().timestamp();
let last_key = DataKey::AdminLastAction(function_name);
let last_action: u64 = env
.storage()
.persistent()
.get(&last_key)
.unwrap_or(0);

if last_action > 0 && now < last_action.saturating_add(cooldown) {
return Err(ContractError::AdminActionTimelocked);
}

env.storage().persistent().set(&last_key, &now);
Ok(())
check_admin_cooldown_internal(&env, &admin, &function_name)
}
}
5 changes: 0 additions & 5 deletions contracts/fees/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,6 @@ impl FeesContract {
let config = Self::get_fee_config_internal(&env)?;

if collected < config.collection_threshold {
let mut schedule = Self::get_withdrawal_schedule_internal(&env);
schedule.status = FeeWithdrawalStatus::Failed;
env.storage()
.persistent()
.set(&Symbol::new(&env, WITHDRAWAL_SCHEDULE_KEY), &schedule);
return Err(ContractError::BelowCollectionThreshold);
}

Expand Down
10 changes: 5 additions & 5 deletions contracts/fees/tests/auth_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,15 @@ fn test_collect_fees_fails_below_threshold() {
other => panic!("Collection below threshold should fail, got: {:?}", other),
}

// Schedule status must be updated to Failed
// A threshold rejection does not change the withdrawal schedule.
assert_eq!(
client.get_withdrawal_schedule().status,
fees::FeeWithdrawalStatus::Failed
fees::FeeWithdrawalStatus::Ready
);
}

#[test]
fn test_fee_withdrawal_status_failed_and_recovery() {
fn test_fee_withdrawal_status_unchanged_and_recovery() {
let env = Env::default();
let admin = Address::generate(&env);
let client = register_and_initialize(&env, &admin);
Expand All @@ -440,8 +440,8 @@ fn test_fee_withdrawal_status_failed_and_recovery() {
fail_result,
Err(Ok(ContractError::BelowCollectionThreshold))
);
let failed_schedule = client.get_withdrawal_schedule();
assert_eq!(failed_schedule.status, fees::FeeWithdrawalStatus::Failed);
let unchanged_schedule = client.get_withdrawal_schedule();
assert_eq!(unchanged_schedule.status, fees::FeeWithdrawalStatus::Ready);

// 3. Deposit sufficient fees exceeding threshold (150M stroops > 100M threshold)
let _ = invoke_as_authorized_caller(&env, &client, 150_000_000, |market, fees, caller, amount| {
Expand Down
Loading
Loading