fix(contracts): prevent same-block borrow-to-liquidation edge case#223
Merged
Baskarayelu merged 3 commits intoCredenceOrg:mainfrom Apr 1, 2026
Merged
Conversation
|
@solomonadzape95 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 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.
fix(contracts): prevent same-ledger borrow-to-liquidation edge case
fixes #169
Summary
This change prevents a same-ledger (same block/sequence) sandwich-style edge where collateral can be increased and a subsequent slash (liquidation equivalent) is executed before the next ledger, enabling unfair penalties. The guard rejects slashing only when the last collateral increase happened in the current ledger, while allowing legitimate liquidations in later ledgers.
Design
e.ledger().sequence()as the “block height”.This is intentionally narrow and does not impose a global throttle. It only guards slash/liquidation-like operations; withdrawals, early exits, renewals, attestations, claims, and governance remain unaffected.
Implementation
New guard module
contracts/credence_bond/src/same_ledger_liquidation_guard.rsrecord_collateral_increase(e): storese.ledger().sequence()in storagerequire_slash_allowed_after_collateral_increase(e): panics with:slash blocked: collateral increased in this ledgerStorage key
DataKey::LastCollateralIncreaseLedgerincontracts/credence_bond/src/lib.rsWhere we record collateral increases
create_bond_with_rollingincontracts/credence_bond/src/lib.rstop_upincontracts/credence_bond/src/lib.rsincrease_bondincontracts/credence_bond/src/lib.rscreate_batch_bondsincontracts/credence_bond/src/batch.rs(after creating the bonds)Where we enforce the guard on slashing
contracts/credence_bond/src/slashing.rs: called insideslash_bondso it covers the slashing flowcontracts/credence_bond/src/lib.rs: called insideCredenceBond::slash_bondentrypoint as wellTests
Added
contracts/credence_bond/src/test_same_ledger_liquidation_guard.rsincrease_bondtop_upcreate_bond_with_rollingUpdated
contracts/credence_bond/src/test_helpers.rswith a test utility to advance ledger sequence so existing slashing/integration tests can perform slashing in a subsequent ledger (ensuring no unintended regression of healthy flows).Backwards compatibility / migration notes
LastCollateralIncreaseLedgeris unset, so existing positions prior to deployment should not get bricked until the account performs a new collateral increase.DataKeyenum variants can have upgrade/migration implications depending on how the contract is deployed/upgraded. If this is a live contract, validate Soroban storage migration expectations or consider switching to a standaloneSymbolkey for the new storage slot.Security Considerations
Follow-up (optional)