From 35c4f2a80725ffed34724acf21a3718b0afecffd Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 19 Apr 2023 19:12:00 +0200 Subject: [PATCH 1/2] populate parent hash for indexers --- client/rpc/src/eth/block.rs | 76 +++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/client/rpc/src/eth/block.rs b/client/rpc/src/eth/block.rs index aeb3be6be4..45fa04902d 100644 --- a/client/rpc/src/eth/block.rs +++ b/client/rpc/src/eth/block.rs @@ -24,7 +24,7 @@ use jsonrpsee::core::RpcResult as Result; use sc_client_api::backend::{Backend, StorageProvider}; use sc_network_common::ExHashT; use sc_transaction_pool::ChainApi; -use sp_api::ProvideRuntimeApi; +use sp_api::{HeaderT, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; use sp_core::hashing::keccak_256; use sp_runtime::traits::Block as BlockT; @@ -68,19 +68,39 @@ 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, + ); + + // Populate parent hash as the indexers heavily rely on it. + let number = rich_block.inner.header.number.unwrap_or_default(); + if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() { + if let Ok(Some(header)) = client.header(substrate_hash) { + let parent_hash = *header.parent_hash(); + + let schema = fc_storage::onchain_storage_schema::( + client.as_ref(), + parent_hash, + ); + if let Some(block) = + block_data_cache.current_block(schema, parent_hash).await + { + rich_block.inner.header.parent_hash = H256::from_slice( + keccak_256(&rlp::encode(&block.header)).as_slice(), + ); + } + } + } + Ok(Some(rich_block)) + } _ => Ok(None), } } @@ -113,22 +133,40 @@ 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, + ); + + // Populate parent hash as the indexers heavily rely on it. + let number = rich_block.inner.header.number.unwrap_or_default(); + if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() { + if let Ok(Some(header)) = client.header(substrate_hash) { + let parent_hash = *header.parent_hash(); + + let schema = fc_storage::onchain_storage_schema::( + client.as_ref(), + parent_hash, + ); + if let Some(block) = + block_data_cache.current_block(schema, parent_hash).await + { + rich_block.inner.header.parent_hash = H256::from_slice( + keccak_256(&rlp::encode(&block.header)).as_slice(), + ); + } + } + } + Ok(Some(rich_block)) } _ => Ok(None), } From 0f8b32efadc16be2119de6ecbf66c5dab581392c Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Thu, 27 Apr 2023 15:11:06 +0200 Subject: [PATCH 2/2] use forced_parent_hashes --- client/rpc/src/eth/block.rs | 54 ++++++++++++------------------------ client/rpc/src/eth/mod.rs | 5 ++++ template/node/src/rpc/eth.rs | 8 +++++- template/node/src/service.rs | 1 + 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/client/rpc/src/eth/block.rs b/client/rpc/src/eth/block.rs index 45fa04902d..ec72db96be 100644 --- a/client/rpc/src/eth/block.rs +++ b/client/rpc/src/eth/block.rs @@ -24,7 +24,7 @@ use jsonrpsee::core::RpcResult as Result; use sc_client_api::backend::{Backend, StorageProvider}; use sc_network_common::ExHashT; use sc_transaction_pool::ChainApi; -use sp_api::{HeaderT, ProvideRuntimeApi}; +use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_core::hashing::keccak_256; use sp_runtime::traits::Block as BlockT; @@ -80,25 +80,15 @@ where base_fee, ); - // Populate parent hash as the indexers heavily rely on it. - let number = rich_block.inner.header.number.unwrap_or_default(); - if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() { - if let Ok(Some(header)) = client.header(substrate_hash) { - let parent_hash = *header.parent_hash(); - - let schema = fc_storage::onchain_storage_schema::( - client.as_ref(), - parent_hash, - ); - if let Some(block) = - block_data_cache.current_block(schema, parent_hash).await - { - rich_block.inner.header.parent_hash = H256::from_slice( - keccak_256(&rlp::encode(&block.header)).as_slice(), - ); - } - } + 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), @@ -147,25 +137,15 @@ where base_fee, ); - // Populate parent hash as the indexers heavily rely on it. - let number = rich_block.inner.header.number.unwrap_or_default(); - if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() { - if let Ok(Some(header)) = client.header(substrate_hash) { - let parent_hash = *header.parent_hash(); - - let schema = fc_storage::onchain_storage_schema::( - client.as_ref(), - parent_hash, - ); - if let Some(block) = - block_data_cache.current_block(schema, parent_hash).await - { - rich_block.inner.header.parent_hash = H256::from_slice( - keccak_256(&rlp::encode(&block.header)).as_slice(), - ); - } - } + 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 = {