From 071d0306d107ea3281b600ab195dc3e0f8e54c07 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Sat, 29 Jul 2023 23:24:15 +0200 Subject: [PATCH] Handle `u64` conversion (#1091) * Handle `u64` conversion * suggestion switch to saturated conversion --- client/rpc/src/eth/cache/mod.rs | 23 ++++++++++++----------- client/rpc/src/eth/fee.rs | 3 ++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/client/rpc/src/eth/cache/mod.rs b/client/rpc/src/eth/cache/mod.rs index a3aba1822d..22f9cde219 100644 --- a/client/rpc/src/eth/cache/mod.rs +++ b/client/rpc/src/eth/cache/mod.rs @@ -338,19 +338,19 @@ where let base_fee = client.runtime_api().gas_price(hash).unwrap_or_default(); let receipts = handler.current_receipts(hash); let mut result = FeeHistoryCacheItem { - base_fee: if base_fee > U256::from(u64::MAX) { u64::MAX } else { base_fee.low_u64() }, + base_fee: UniqueSaturatedInto::::unique_saturated_into(base_fee), gas_used_ratio: 0f64, rewards: Vec::new(), }; if let (Some(block), Some(receipts)) = (block, receipts) { - block_number = Some(block.header.number.as_u64()); - let gas_used = block.header.gas_used.as_u64() as f64; - let gas_limit = block.header.gas_limit.as_u64() as f64; + block_number = Some(UniqueSaturatedInto::::unique_saturated_into(block.header.number)); + let gas_used = UniqueSaturatedInto::::unique_saturated_into(block.header.gas_used) as f64; + let gas_limit = UniqueSaturatedInto::::unique_saturated_into(block.header.gas_limit) as f64; result.gas_used_ratio = gas_used / gas_limit; let mut previous_cumulative_gas = U256::zero(); let used_gas = |current: U256, previous: &mut U256| -> u64 { - let r = current.saturating_sub(*previous).as_u64(); + let r = UniqueSaturatedInto::::unique_saturated_into(current.saturating_sub(*previous)); *previous = current; r }; @@ -364,15 +364,16 @@ where }, effective_reward: match block.transactions.get(i) { Some(ethereum::TransactionV2::Legacy(t)) => { - t.gas_price.saturating_sub(base_fee).as_u64() + UniqueSaturatedInto::::unique_saturated_into(t.gas_price.saturating_sub(base_fee)) } Some(ethereum::TransactionV2::EIP2930(t)) => { - t.gas_price.saturating_sub(base_fee).as_u64() + UniqueSaturatedInto::::unique_saturated_into(t.gas_price.saturating_sub(base_fee)) } - Some(ethereum::TransactionV2::EIP1559(t)) => t - .max_priority_fee_per_gas - .min(t.max_fee_per_gas.saturating_sub(base_fee)) - .as_u64(), + Some(ethereum::TransactionV2::EIP1559(t)) => UniqueSaturatedInto::::unique_saturated_into( + t + .max_priority_fee_per_gas + .min(t.max_fee_per_gas.saturating_sub(base_fee)) + ), None => 0, }, }) diff --git a/client/rpc/src/eth/fee.rs b/client/rpc/src/eth/fee.rs index 5b4b0e4ae6..92b0ee10c2 100644 --- a/client/rpc/src/eth/fee.rs +++ b/client/rpc/src/eth/fee.rs @@ -150,7 +150,8 @@ where .unwrap_or(default_elasticity) .deconstruct(); let elasticity = elasticity as f64 / 1_000_000f64; - let last_fee_per_gas = last_fee_per_gas.as_u64() as f64; + let last_fee_per_gas = + UniqueSaturatedInto::::unique_saturated_into(*last_fee_per_gas) as f64; if last_gas_used > &0.5 { // Increase base gas let increase = ((last_gas_used - 0.5) * 2f64) * elasticity;