Skip to content

Commit

Permalink
Adapt arbitrage weight calculations to proof sizes (#1120)
Browse files Browse the repository at this point in the history
Include proof size into arbitrage weight estimates
  • Loading branch information
maltekliemann authored Sep 23, 2023
1 parent a12b24c commit 628c559
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mod pallet {
pub(crate) const ARBITRAGE_MAX_ITERATIONS: usize = 30;
const ARBITRAGE_THRESHOLD: u128 = CENT;
const MIN_BALANCE: u128 = CENT;
const ON_IDLE_MIN_WEIGHT: Weight = Weight::from_ref_time(1_000_000);
const ON_IDLE_MIN_WEIGHT: Weight = Weight::from_parts(1_000_000, 100_000);

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -1271,19 +1271,33 @@ mod pallet {
// The division can fail if the benchmark of `apply_to_cached_pools` is not linear in
// the number of pools. This shouldn't ever happen, but if it does, we ensure that
// `pool_count` is zero (this isn't really a runtime error).
let pool_count = weight
let weight_minus_overhead = weight.saturating_sub(overhead);
let max_pool_count_by_ref_time = weight_minus_overhead
.ref_time()
.saturating_sub(overhead.ref_time())
.checked_div(extra_weight_per_pool.ref_time())
.unwrap_or_else(|| {
log::warn!("Unexpected zero division when calculating arbitrage weight");
debug_assert!(
false,
"Unexpected zero division when calculating arbitrage ref time"
);
0_u64
});
let max_pool_count_by_proof_size = weight_minus_overhead
.proof_size()
.checked_div(extra_weight_per_pool.proof_size())
.unwrap_or_else(|| {
debug_assert!(
false,
"Unexpected zero division when calculating arbitrage proof size"
);
0_u64
});
if pool_count == 0_u64 {
let max_pool_count = max_pool_count_by_ref_time.min(max_pool_count_by_proof_size);
if max_pool_count == 0_u64 {
return weight;
}
Self::apply_to_cached_pools(
pool_count.saturated_into(),
max_pool_count.saturated_into(),
|pool_id| Self::execute_arbitrage(pool_id, ARBITRAGE_MAX_ITERATIONS),
extra_weight_per_pool,
)
Expand Down

0 comments on commit 628c559

Please sign in to comment.