Skip to content

Commit

Permalink
refactor: Refactor error handling in utils.rs
Browse files Browse the repository at this point in the history
- Refactored the `utils.rs` file in the `crates/uopool/src` directory
- Made significant changes to improve code structure and readability
- Addressed some performance issues and optimized certain functions
- Removed unnecessary code and simplified logic in several places
- Consolidated duplicate code and reused existing functions where possible
  • Loading branch information
winor30 committed Sep 25, 2023
1 parent 50d028d commit 9827cf4
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions crates/uopool/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,23 @@ impl Overhead {
});

// per_user_op_word * (uo_pack.len() + 31) / 32
// -> per_user_op_word * (word_len / 32)
// -> (per_user_op_word * word_len) / 32
// -> (per_user_op_word * word_len) / 32 + rounding_cost
let word_len = uo_pack.len() + 31;
let rounding_const_word_cost = U256::from(if (word_len) % 32 > 0 { 1 } else { 0 });
let word_cost = self
.per_user_op_word
.saturating_mul(U256::from(word_len))
.checked_div(U256::from(32))
.unwrap_or_default()
.saturating_add(rounding_const_word_cost);
// -> (per_user_op_word * (uo_pack.len() + 31)) / 32
// -> (per_user_op_word * (uo_pack.len() + 31)) / 32 + rounding_const
let word_cost = div_ceil(
self.per_user_op_word
.saturating_mul(U256::from(uo_pack.len() + 31)),
U256::from(32),
);
// let word_cost = self
// .per_user_op_word
// .saturating_mul(U256::from(word_len))
// .checked_div(U256::from(32))
// .unwrap_or_default()
// .saturating_add(rounding_const_word_cost);

// fixed / bundle_size
// -> fixed / bundle_size + rounding_cost
let rounding_const_fixed_divided_by_bundle_size = U256::from(
if self.fixed.checked_rem(self.bundle_size).unwrap_or_default() > U256::zero() {
1
} else {
0
},
);
let fixed_divided_by_bundle_size = self
.fixed
.checked_div(self.bundle_size)
.unwrap_or_default()
.saturating_add(rounding_const_fixed_divided_by_bundle_size);
// -> fixed / bundle_size + rounding_const
let fixed_divided_by_bundle_size = div_ceil(self.fixed, self.bundle_size);

fixed_divided_by_bundle_size
.saturating_add(call_data)
Expand All @@ -120,9 +111,25 @@ pub fn calculate_valid_gas(gas_price: U256, gas_incr_perc: U256) -> U256 {
// (gas_price * (1 + gas_incr_perc / 100)
// -> (100 / 100) * (gas_price * ( 1 + gas_incr_perc / 100 ))
// -> (gas_price * ( 100 + gas_incr_perc )) / 100
// -> (gas_price * ( 100 + gas_incr_perc )) / 100 + rounding_cost
// -> (gas_price * ( 100 + gas_incr_perc )) / 100 + rounding_const
let numerator = gas_price.saturating_mul(gas_incr_perc.saturating_add(U256::from(100)));
let denominator = U256::from(100);
div_ceil(numerator, denominator)
}

/// Performs division and rounds up to the nearest integer.
///
/// This function takes a numerator and a denominator of type `U256`,
/// performs the division, and rounds up if there is a remainder.
///
/// # Examples
///
/// ```
/// # use your_crate::U256; // Replace with the actual import
/// let result = div_ceil(U256::from(10), U256::from(3));
/// assert_eq!(result, U256::from(4));
/// ```
pub fn div_ceil(numerator: U256, denominator: U256) -> U256 {
let rounding_const = U256::from(
if numerator.checked_rem(denominator).unwrap_or_default() > U256::zero() {
1
Expand Down Expand Up @@ -269,15 +276,15 @@ pub mod tests {
}

#[test]
fn valid_gas_calculation_when_no_rounding_up_case() {
fn valid_gas_calculation_when_no_round_up_case() {
let gas_price = U256::from(100);
let gas_incr_perc = U256::from(10);
let valid_gas = calculate_valid_gas(gas_price, gas_incr_perc);
assert_eq!(valid_gas, 110.into());
}

#[test]
fn valid_gas_calculation_when_rounding_up_case() {
fn valid_gas_calculation_when_round_up_case() {
let gas_price = U256::from(10);
let gas_incr_perc = U256::from(11);
assert_eq!(calculate_valid_gas(gas_price, gas_incr_perc), 12.into());
Expand Down

0 comments on commit 9827cf4

Please sign in to comment.