-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dex: fold execution budget in
DexParameters
(#4620)
## Describe your changes Close #4602, and simplifies the execution breaker as well, fixing an edge case that would cause only successful executions to be counted. ## Checklist before requesting a review - [x] If this code contains consensus-breaking changes, I have added the "consensus-breaking" label. Otherwise, I declare my belief that there are not consensus-breaking changes, for the following reason: > This is state breaking, and contains a migration. --------- Co-authored-by: Tal Derei <70081547+TalDerei@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
103 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
53 changes: 16 additions & 37 deletions
53
crates/core/component/dex/src/component/circuit_breaker/execution.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,28 @@ | ||
const MAX_PATH_SEARCHES: u32 = 64; | ||
const MAX_EXECUTIONS: u32 = 64; | ||
|
||
/// Holds the state of the execution circuit breaker. | ||
/// Responsible for managing the conditions of halting execution of | ||
/// a single batch swap. All execution circuit breaker triggers are | ||
/// non-fatal and will allow the swap to be partially fulfilled up | ||
/// to the search and execution limits managed by the circuit breaker. | ||
/// The execution circuit breaker meters the number of operations (search and execution) | ||
/// performed to fulfill a batch swap. | ||
/// | ||
/// The Dex component MUST call `CircuitBreaker::exceed_limits` before an execution round. | ||
/// | ||
/// The circuit breaker ensures the swap will not use unbounded time complexity. | ||
/// Note that in practice, this means that a batch swap can result in a partial fill | ||
/// even if there were enough liquidity to fulfill all of it. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct ExecutionCircuitBreaker { | ||
/// The maximum number of times to perform path searches before stopping. | ||
pub max_path_searches: u32, | ||
/// The number of times path searches have been performed. | ||
pub current_path_searches: u32, | ||
/// The maximum number of times to execute against liquidity positions before stopping. | ||
pub max_executions: u32, | ||
/// The number of times liquidity positions have been executed against. | ||
pub current_executions: u32, | ||
/// The current number of operations performed. | ||
pub counter: u32, | ||
/// The maximum number of operations allowed. | ||
pub max: u32, | ||
} | ||
|
||
impl ExecutionCircuitBreaker { | ||
#[allow(dead_code)] | ||
pub fn new(max_path_searches: u32, max_executions: u32) -> Self { | ||
Self { | ||
max_path_searches, | ||
current_path_searches: 0, | ||
max_executions, | ||
current_executions: 0, | ||
} | ||
pub fn new(max: u32) -> Self { | ||
Self { max, counter: 0 } | ||
} | ||
|
||
pub fn exceeded_limits(&self) -> bool { | ||
self.current_path_searches > self.max_path_searches | ||
|| self.current_executions > self.max_executions | ||
pub fn increment(&mut self) { | ||
self.counter += 1; | ||
} | ||
} | ||
|
||
impl Default for ExecutionCircuitBreaker { | ||
fn default() -> Self { | ||
Self { | ||
max_path_searches: MAX_PATH_SEARCHES, | ||
current_path_searches: 0, | ||
max_executions: MAX_EXECUTIONS, | ||
current_executions: 0, | ||
} | ||
pub fn exceeded_limits(&self) -> bool { | ||
self.counter >= self.max | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.