Skip to content

Commit

Permalink
Handle u64 conversion (#1091)
Browse files Browse the repository at this point in the history
* Handle `u64` conversion

* suggestion switch to saturated conversion
  • Loading branch information
tgmichel authored Jul 29, 2023
1 parent aab7cfc commit 071d030
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
23 changes: 12 additions & 11 deletions client/rpc/src/eth/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>::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::<u64>::unique_saturated_into(block.header.number));
let gas_used = UniqueSaturatedInto::<u64>::unique_saturated_into(block.header.gas_used) as f64;
let gas_limit = UniqueSaturatedInto::<u64>::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::<u64>::unique_saturated_into(current.saturating_sub(*previous));
*previous = current;
r
};
Expand All @@ -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::<u64>::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::<u64>::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::<u64>::unique_saturated_into(
t
.max_priority_fee_per_gas
.min(t.max_fee_per_gas.saturating_sub(base_fee))
),
None => 0,
},
})
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/src/eth/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>::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;
Expand Down

0 comments on commit 071d030

Please sign in to comment.