From ec85145f5950fb1eb64204920135648fb8e8a1a6 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Tue, 22 Nov 2022 00:38:48 +0100 Subject: [PATCH] Fix gas used ratio (#898) --- client/rpc/src/eth/cache/mod.rs | 31 ++++-------------------------- ts-tests/tests/test-fee-history.ts | 2 +- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/client/rpc/src/eth/cache/mod.rs b/client/rpc/src/eth/cache/mod.rs index 8096247a7de32..d81b0327918bf 100644 --- a/client/rpc/src/eth/cache/mod.rs +++ b/client/rpc/src/eth/cache/mod.rs @@ -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 ) { @@ -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 { @@ -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::( - 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() { @@ -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); } } diff --git a/ts-tests/tests/test-fee-history.ts b/ts-tests/tests/test-fee-history.ts index fce8908171d4b..2677d39ab7503 100644 --- a/ts-tests/tests/test-fee-history.ts +++ b/ts-tests/tests/test-fee-history.ts @@ -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.