Skip to content

Commit

Permalink
Fix gas used ratio (paritytech#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel authored Nov 21, 2022
1 parent 445c639 commit ec85145
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
31 changes: 4 additions & 27 deletions client/rpc/src/eth/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,13 @@ where
fee_history_cache: FeeHistoryCache,
block_limit: u64,
) {
use sp_runtime::Permill;

struct TransactionHelper {
gas_used: u64,
effective_reward: u64,
}
// Calculates the cache for a single block
#[rustfmt::skip]
let fee_history_cache_item = |hash: H256, elasticity: Permill| -> (
let fee_history_cache_item = |hash: H256| -> (
FeeHistoryCacheItem,
Option<u64>
) {
Expand Down Expand Up @@ -360,17 +358,9 @@ where
};
if let (Some(block), Some(receipts)) = (block, receipts) {
block_number = Some(block.header.number.as_u64());
// Calculate the gas used ratio.
// TODO this formula needs the pallet-base-fee configuration.
// By now we assume just the default 0.125 (elasticity multiplier 8).
let gas_used = block.header.gas_used.as_u64() as f64;
let gas_limit = block.header.gas_limit.as_u64() as f64;
let elasticity_multiplier: f64 = (elasticity / Permill::from_parts(1_000_000))
.deconstruct()
.into();
let gas_target = gas_limit / elasticity_multiplier;

result.gas_used_ratio = gas_used / (gas_target * elasticity_multiplier);
result.gas_used_ratio = gas_used / gas_limit;

let mut previous_cumulative_gas = U256::zero();
let used_gas = |current: U256, previous: &mut U256| -> u64 {
Expand Down Expand Up @@ -449,19 +439,6 @@ where

while let Some(notification) = notification_st.next().await {
if notification.is_new_best {
let hash = notification.hash;
let id = BlockId::Hash(hash);
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
id,
);
let handler = overrides
.schemas
.get(&schema)
.unwrap_or(&overrides.fallback);

let default_elasticity = Permill::from_parts(125_000);
let elasticity = handler.elasticity(&id).unwrap_or(default_elasticity);
// In case a re-org happened on import.
if let Some(tree_route) = notification.tree_route {
if let Ok(fee_history_cache) = &mut fee_history_cache.lock() {
Expand All @@ -475,13 +452,13 @@ where
// Insert enacted.
let _ = tree_route.enacted().iter().map(|hash_and_number| {
let (result, block_number) =
fee_history_cache_item(hash_and_number.hash, elasticity);
fee_history_cache_item(hash_and_number.hash);
commit_if_any(result, block_number);
});
}
}
// Cache the imported block.
let (result, block_number) = fee_history_cache_item(hash, elasticity);
let (result, block_number) = fee_history_cache_item(notification.hash);
commit_if_any(result, block_number);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ts-tests/tests/test-fee-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describeWithFrontier("Frontier RPC (Fee History)", (context) => {
// baseFeePerGas is always the requested block range + 1 (the next derived base fee).
expect(result.baseFeePerGas.length).to.be.eq(blockCount + 1);
// gasUsedRatio for the requested block range.
expect(result.gasUsedRatio.length).to.be.eq(blockCount);
expect(result.gasUsedRatio).to.be.deep.eq(Array(blockCount).fill(0.03575712));
// two-dimensional reward list for the requested block range.
expect(result.reward.length).to.be.eq(blockCount);
// each block has a reward list which's size is the requested percentile list.
Expand Down

0 comments on commit ec85145

Please sign in to comment.