Repository navigation
Conversation
… unpause - add a reusable circuit_breaker module: guardian or admin pauses instantly, admin-only unpause requires a 24h proposal, and guardians can never unpause - integrate the AMM and lending pool, mapping pause rejections into each contract's existing error space - keep emergency exits open while paused: AMM liquidity removal and lending pool emergency_withdraw (unencumbered collateral only) - fix the AMM pause test that was missing #[test] and add regression coverage Closes StellarDevHub#1871
|
@Mhidesav is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Mhidesav Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds the reusable emergency circuit breaker the issue asks for in
contracts/access_control/src/circuit_breaker.rsand wires it into the AMM and lending pool.Plan: one shared breaker owns pause state, the guardian address, and the unpause timelock. A guardian (or the admin) pauses instantly; only the admin can schedule an unpause, and it cannot execute for 24 hours. Host contracts gate mutations through
require_not_paused!()but deliberately leave depositor exit paths open. Per-contract error codes are preserved so the ABI does not silently change its failure surface.Closes #1871
Issue: [SC-074] Contract Circuit-Breaker Emergency Pause Framework with Guardian Hierarchy
Root cause
The problem wasn't a missing
if pausedin one function — it was that three contracts each had a different, incomplete idea of what a pause is:access_controlhad no reusable breaker. Pause state lived inroles.rsunder anAC_PAUSEDkey withpause/unpausehelpers.pauseaccepted the admin or any address holding theGuardianrole, butunpausewas immediately executable by the admin. There was no timelock, no single designated guardian, and no way for a host contract to map the pause into its own error space. Because the pause flag lived in role storage, any contract using it also had to use access-control's role initialisation.amm/src/circuit_breaker.rsheld a second pause flag and guardian slot. It correctly blocked swaps, liquidity additions and liquidity removals — but blocking removals contradicts the acceptance criterion that emergency withdrawals keep working during a pause. Worse,test_guardian_pause_blocks_amm_operationswas missing its#[test]attribute, so the only pause test in the AMM never ran.set_pausedwas admin-only and instant in both directions.emergency_withdrawexisted but was admin-gated and transferred the user's collateral back to that user — so a depositor had no emergency exit during a pause, which is exactly when they need one.The fix and why
1. New shared module:
access_control::circuit_breakerinitialize(admin, guardian)— called from each host'sinitialize; the admin is the initial guardian. State lives in its ownBreakerKeynamespace, so it cannot collide with the host contract's storage and does not require access-control role initialisation.pause(caller)— admin or guardian, authenticated, instant. Re-pausing clears any pending unpause, so a second incident cannot ride the first incident's unlock schedule.propose_unpause(admin) -> u64— admin only, requires the contract to actually be paused, storesnow + 24h(UNPAUSE_TIMELOCK_SECONDS).unpause(admin)— admin only, requires a proposal and a matured timelock; a guardian can never call it.cancel_unpause(admin),unpause_unlock_at(),is_paused(),assert_not_paused().roles::pause/unpause/is_pausedwere removed andlib.rsre-exports the breaker instead, so there is exactly one pause flag in the crate. Therequire_not_paused!()macro gained a second form that takes the host's error, e.g.require_not_paused!(&env, AmmError::ContractPaused), keeping AMM409and lending pool14unchanged.Why a separate 24h timelock instead of reusing the 48h admin-handover delay? Admin handover is a governance action; resuming a paused market is incident response. 24 hours gives reviewers time to inspect the exploit while keeping recovery practical. It is a single named constant if maintainers want a different value.
Why not keep one pause flag per contract? That is the bug: three sources of truth and three different rule sets. The shared module is the only place the hierarchy is encoded, so the next contract gets pause/unpause semantics for free.
2. AMM integration
initializeseeds the shared breaker;set_guardiannow delegates to it.pause/propose_unpause/cancel_unpause/unpause/unpause_unlock_atreplaceset_paused(breaking ABI — see risks).add_liquiditygates use the shared macro withAmmError::ContractPaused.remove_liquidityis intentionally no longer pause-gated so LPs can exit during an incident. The existing rolling 10%-per-24h outflow budget still applies, so a pause cannot be used to drain the pool in one transaction.circuit_breaker.rsis now only the outflow budget, documented as such.3. Lending pool integration
initializeseeds the shared breaker; newset_guardian,pause,propose_unpause,cancel_unpause,unpause,unpause_unlock_at.paused()reads the shared state.check_not_paused()call now routes through the shared macro, so deposits, borrows, collateral withdrawals, liquidations, flash loans and staking are covered without touching each entrypoint.emergency_withdrawis now depositor-callable and pause-exempt, but only for collateral that does not back an outstanding borrow in that token. A borrower with debt getsLPError::DebtOutstanding(15) instead of walking away from the loan. Unstaking and reward claims were already pause-exempt and stay that way.4. Docs
docs/contracts/SC-023-CIRCUIT-BREAKER.mdnow describes the shared breaker, the per-contract exemptions and the new error codes. The deployment checklist's rollback command uses the newpauseentrypoint.Files changed
contracts/access_control/src/circuit_breaker.rscontracts/access_control/src/lib.rscontracts/access_control/src/roles.rscontracts/access_control/src/test.rscontracts/amm/src/lib.rs,circuit_breaker.rs,test.rscontracts/lending_pool/src/lib.rs,Cargo.tomldocs/contracts/SC-023-CIRCUIT-BREAKER.md,docs/deployment/DEPLOYMENT_CHECKLIST.mdHow it was tested
New regression tests:
access_control(6)unpausewithout a proposalunpause_unlock_atis exposed and cleared afterwards300)310)300)AMM (rewritten + new)
test_guardian_pause_blocks_swaps_but_allows_liquidity_exit(previously missing#[test], so it never ran): guardian pause blocks swaps/adds;remove_liquiditystill succeeds; the guardian cannot unpause; the admin must propose and wait; after the timelock swaps work again.Lending pool (3)
paused_contract_rejects_depositupdated to the newpauseentrypointemergency_withdrawstill pays out; guardian cannot unpause; admin unpause only after the timelockemergency_withdraw_rejected_with_outstanding_debt(15)Commands run locally from
contracts/(Rust 1.100.0, soroban-sdk 28):The WASM artifact step still cannot run in this environment (soroban-sdk 28 requires
stellar-clifor the wasm target); that CI step iscontinue-on-error. There is no UI here — verification is against the Soroban test host, no testnet deployment was performed.Risks and trade-offs (critical path: auth / fund movement)
set_paused(caller, bool)is gone from both the AMM and lending pool, replaced bypause,propose_unpause,unpauseandcancel_unpause. Any deployment script, keeper, frontend or indexer calling the old selector will fail until updated. I updated the in-repo runbook; off-repo callers need a follow-up.cancel_unpauseand re-pause. A compromised admin can do both after the timelock — that is unavoidable without a multisig council, which is out of scope.remove_liquiditywhile paused, but the pre-existing 10%-per-24h outflow budget still applies, so a very large LP may not exit in one transaction. That limiter exists to stop a drained pool and was intentionally left in place.Follow-ups worth filing separately
PAUSEDkeys into the shared breaker so a paused contract cannot resume silently on upgrade.emergency_withdrawsafe across collateral tokens.repaywhile paused so borrowers can reduce risk during an incident.amm/src/circuit_breaker.rstooutflow.rsnow that it only holds the rolling budget.pre-commitfails becauseprettieris missing from the pnpm install, andpre-pushfails on the frontendtsconfigtarget: "ES5"error. Both are pre-existing and unrelated; this commit/push used the documented--no-verifybypass after confirming the Rust gates pass.Reviewer checklist
fmt,clippy, and full workspace tests pass