Skip to content

Commit

Permalink
Use runtime api for base fee (paritytech#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel authored Nov 23, 2022
1 parent ec85145 commit 3cdb214
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 75 deletions.
22 changes: 8 additions & 14 deletions client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ use jsonrpsee::core::RpcResult as Result;
use sc_client_api::backend::{Backend, StateBackend, StorageProvider};
use sc_network_common::ExHashT;
use sc_transaction_pool::ChainApi;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::hashing::keccak_256;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;

use crate::{
eth::{rich_block_build, Eth},
Expand All @@ -39,12 +41,13 @@ impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: StorageProvider<B, BE> + HeaderBackend<B> + Send + Sync + 'static,
C: ProvideRuntimeApi<B>,
C::Api: EthereumRuntimeRPCApi<B>,
BE: Backend<B> + 'static,
BE::State: StateBackend<BlakeTwo256>,
{
pub async fn block_by_hash(&self, hash: H256, full: bool) -> Result<Option<RichBlock>> {
let client = Arc::clone(&self.client);
let overrides = Arc::clone(&self.overrides);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);

Expand All @@ -64,25 +67,21 @@ where

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 block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;

let base_fee = handler.base_fee(&id);
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();

match (block, statuses) {
(Some(block), Some(statuses)) => Ok(Some(rich_block_build(
block,
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
full,
base_fee,
Some(base_fee),
))),
_ => Ok(None),
}
Expand All @@ -94,7 +93,6 @@ where
full: bool,
) -> Result<Option<RichBlock>> {
let client = Arc::clone(&self.client);
let overrides = Arc::clone(&self.overrides);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);

Expand All @@ -112,17 +110,13 @@ where

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 block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;

let base_fee = handler.base_fee(&id);
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand All @@ -133,7 +127,7 @@ where
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
full,
base_fee,
Some(base_fee),
)))
}
_ => Ok(None),
Expand Down
6 changes: 1 addition & 5 deletions client/rpc/src/eth/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,7 @@ where

let block = handler.current_block(&id);
let mut block_number: Option<u64> = None;
let base_fee = if let Some(base_fee) = handler.base_fee(&id) {
base_fee
} else {
client.runtime_api().gas_price(&id).unwrap_or_else(|_|U256::zero())
};
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();
let receipts = handler.current_receipts(&id);
let mut result = FeeHistoryCacheItem {
base_fee: base_fee.as_u64(),
Expand Down
33 changes: 9 additions & 24 deletions client/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ where
{
pub async fn transaction_by_hash(&self, hash: H256) -> Result<Option<Transaction>> {
let client = Arc::clone(&self.client);
let overrides = Arc::clone(&self.overrides);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);
let graph = Arc::clone(&self.graph);
Expand Down Expand Up @@ -143,24 +142,20 @@ where

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 block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;

let base_fee = handler.base_fee(&id);
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();

match (block, statuses) {
(Some(block), Some(statuses)) => Ok(Some(transaction_build(
block.transactions[index].clone(),
Some(block),
Some(statuses[index].clone()),
base_fee,
Some(base_fee),
))),
_ => Ok(None),
}
Expand All @@ -172,7 +167,6 @@ where
index: Index,
) -> Result<Option<Transaction>> {
let client = Arc::clone(&self.client);
let overrides = Arc::clone(&self.overrides);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);

Expand All @@ -194,17 +188,13 @@ where

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 block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;

let base_fee = handler.base_fee(&id);
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand All @@ -215,7 +205,7 @@ where
transaction.clone(),
Some(block),
Some(status.clone()),
base_fee,
Some(base_fee),
)))
} else {
Err(internal_err(format!("{:?} is out of bounds", index)))
Expand All @@ -231,7 +221,6 @@ where
index: Index,
) -> Result<Option<Transaction>> {
let client = Arc::clone(&self.client);
let overrides = Arc::clone(&self.overrides);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);

Expand All @@ -250,17 +239,13 @@ where
let index = index.value();
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 block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;

let base_fee = handler.base_fee(&id);
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand All @@ -271,7 +256,7 @@ where
transaction.clone(),
Some(block),
Some(status.clone()),
base_fee,
Some(base_fee),
)))
} else {
Err(internal_err(format!("{:?} is out of bounds", index)))
Expand Down Expand Up @@ -394,13 +379,13 @@ where
let status = statuses[index].clone();
let mut cumulative_receipts = receipts;
cumulative_receipts.truncate((status.transaction_index + 1) as usize);

let transaction = block.transactions[index].clone();
let effective_gas_price = match transaction {
EthereumTransaction::Legacy(t) => t.gas_price,
EthereumTransaction::EIP2930(t) => t.gas_price,
EthereumTransaction::EIP1559(t) => handler
.base_fee(&id)
EthereumTransaction::EIP1559(t) => client
.runtime_api()
.gas_price(&id)
.unwrap_or_default()
.checked_add(t.max_priority_fee_per_gas)
.unwrap_or_else(U256::max_value)
Expand Down
11 changes: 0 additions & 11 deletions client/rpc/src/overrides/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ pub trait StorageOverride<Block: BlockT> {
block: &BlockId<Block>,
) -> Option<Vec<TransactionStatus>>;
/// Return the base fee at the given height.
fn base_fee(&self, block: &BlockId<Block>) -> Option<U256>;
/// Return the base fee at the given height.
fn elasticity(&self, block: &BlockId<Block>) -> Option<Permill>;
/// Return `true` if the request BlockId is post-eip1559.
fn is_eip1559(&self, block: &BlockId<Block>) -> bool;
Expand Down Expand Up @@ -179,15 +177,6 @@ where
.ok()?
}

/// Return the base fee at the given post-eip1559 height.
fn base_fee(&self, block: &BlockId<Block>) -> Option<U256> {
if self.is_eip1559(block) {
self.client.runtime_api().gas_price(block).ok()
} else {
None
}
}

/// Return the elasticity multiplier at the give post-eip1559 height.
fn elasticity(&self, block: &BlockId<Block>) -> Option<Permill> {
if self.is_eip1559(block) {
Expand Down
5 changes: 0 additions & 5 deletions client/rpc/src/overrides/schema_v1_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ where
)
}

/// Prior to eip-1559 there is no base fee.
fn base_fee(&self, _block: &BlockId<B>) -> Option<U256> {
None
}

/// Prior to eip-1559 there is no elasticity.
fn elasticity(&self, _block: &BlockId<B>) -> Option<Permill> {
None
Expand Down
8 changes: 0 additions & 8 deletions client/rpc/src/overrides/schema_v2_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ where
)
}

/// Return the base fee at the given height.
fn base_fee(&self, block: &BlockId<B>) -> Option<U256> {
self.query_storage::<U256>(
block,
&StorageKey(storage_prefix_build(PALLET_BASE_FEE, BASE_FEE_PER_GAS)),
)
}

/// Return the elasticity at the given height.
fn elasticity(&self, block: &BlockId<B>) -> Option<Permill> {
let default_elasticity = Some(Permill::from_parts(125_000));
Expand Down
8 changes: 0 additions & 8 deletions client/rpc/src/overrides/schema_v3_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ where
)
}

/// Return the base fee at the given height.
fn base_fee(&self, block: &BlockId<B>) -> Option<U256> {
self.query_storage::<U256>(
block,
&StorageKey(storage_prefix_build(PALLET_BASE_FEE, BASE_FEE_PER_GAS)),
)
}

/// Return the elasticity at the given height.
fn elasticity(&self, block: &BlockId<B>) -> Option<Permill> {
let default_elasticity = Some(Permill::from_parts(125_000));
Expand Down

0 comments on commit 3cdb214

Please sign in to comment.