diff --git a/client/rpc/src/eth/block.rs b/client/rpc/src/eth/block.rs index aeb3be6be4..ec72db96be 100644 --- a/client/rpc/src/eth/block.rs +++ b/client/rpc/src/eth/block.rs @@ -68,19 +68,29 @@ where .current_transaction_statuses(schema, substrate_hash) .await; - let base_fee = client - .runtime_api() - .gas_price(substrate_hash) - .unwrap_or_default(); + let base_fee = client.runtime_api().gas_price(substrate_hash).ok(); match (block, statuses) { - (Some(block), Some(statuses)) => Ok(Some(rich_block_build( - block, - statuses.into_iter().map(Some).collect(), - Some(hash), - full, - Some(base_fee), - ))), + (Some(block), Some(statuses)) => { + let mut rich_block = rich_block_build( + block, + statuses.into_iter().map(Option::Some).collect(), + Some(hash), + full, + base_fee, + ); + + let substrate_hash = H256::from_slice(substrate_hash.as_ref()); + if let Some(parent_hash) = self + .forced_parent_hashes + .as_ref() + .and_then(|parent_hashes| parent_hashes.get(&substrate_hash).cloned()) + { + rich_block.inner.header.parent_hash = parent_hash + } + + Ok(Some(rich_block)) + } _ => Ok(None), } } @@ -113,22 +123,30 @@ where .current_transaction_statuses(schema, substrate_hash) .await; - let base_fee = client - .runtime_api() - .gas_price(substrate_hash) - .unwrap_or_default(); + let base_fee = client.runtime_api().gas_price(substrate_hash).ok(); match (block, statuses) { (Some(block), Some(statuses)) => { let hash = H256::from(keccak_256(&rlp::encode(&block.header))); - Ok(Some(rich_block_build( + let mut rich_block = rich_block_build( block, statuses.into_iter().map(Option::Some).collect(), Some(hash), full, - Some(base_fee), - ))) + base_fee, + ); + + let substrate_hash = H256::from_slice(substrate_hash.as_ref()); + if let Some(parent_hash) = self + .forced_parent_hashes + .as_ref() + .and_then(|parent_hashes| parent_hashes.get(&substrate_hash).cloned()) + { + rich_block.inner.header.parent_hash = parent_hash + } + + Ok(Some(rich_block)) } _ => Ok(None), } diff --git a/client/rpc/src/eth/mod.rs b/client/rpc/src/eth/mod.rs index 4bbdca1bb7..14f4da5e80 100644 --- a/client/rpc/src/eth/mod.rs +++ b/client/rpc/src/eth/mod.rs @@ -88,6 +88,7 @@ pub struct Eth>, _marker: PhantomData<(B, BE, EC)>, } @@ -106,6 +107,7 @@ impl Eth>, ) -> Self { Self { client, @@ -121,6 +123,7 @@ impl Eth> fee_history_cache, fee_history_cache_limit, execute_gas_limit_multiplier, + forced_parent_hashes, _marker: _, } = self; @@ -161,6 +165,7 @@ impl> fee_history_cache, fee_history_cache_limit, execute_gas_limit_multiplier, + forced_parent_hashes, _marker: PhantomData, } } diff --git a/template/node/src/rpc/eth.rs b/template/node/src/rpc/eth.rs index 32bc4ecd25..82103c3626 100644 --- a/template/node/src/rpc/eth.rs +++ b/template/node/src/rpc/eth.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc}; use jsonrpsee::RpcModule; // Substrate @@ -13,6 +13,7 @@ use sc_transaction_pool_api::TransactionPool; use sp_api::{CallApiAt, ProvideRuntimeApi}; use sp_block_builder::BlockBuilder as BlockBuilderApi; use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata}; +use sp_core::H256; use sp_runtime::traits::Block as BlockT; // Frontier use fc_db::Backend as FrontierBackend; @@ -54,6 +55,8 @@ pub struct EthDeps { /// Maximum allowed gas limit will be ` block.gas_limit * execute_gas_limit_multiplier` when /// using eth_call/eth_estimateGas. pub execute_gas_limit_multiplier: u64, + /// Mandated parent hashes for a given block hash. + pub forced_parent_hashes: Option>, } impl Clone for EthDeps { @@ -74,6 +77,7 @@ impl Clone for EthDeps fee_history_cache: self.fee_history_cache.clone(), fee_history_cache_limit: self.fee_history_cache_limit, execute_gas_limit_multiplier: self.execute_gas_limit_multiplier, + forced_parent_hashes: self.forced_parent_hashes.clone(), } } } @@ -119,6 +123,7 @@ where fee_history_cache, fee_history_cache_limit, execute_gas_limit_multiplier, + forced_parent_hashes, } = deps; let mut signers = Vec::new(); @@ -141,6 +146,7 @@ where fee_history_cache, fee_history_cache_limit, execute_gas_limit_multiplier, + forced_parent_hashes, ) .replace_config::() .into_rpc(), diff --git a/template/node/src/service.rs b/template/node/src/service.rs index b78f3713b1..015a2222b2 100644 --- a/template/node/src/service.rs +++ b/template/node/src/service.rs @@ -366,6 +366,7 @@ where fee_history_cache: fee_history_cache.clone(), fee_history_cache_limit, execute_gas_limit_multiplier: eth_config.execute_gas_limit_multiplier, + forced_parent_hashes: None, }; let rpc_builder = {