Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

populate parent hash for indexers #1044

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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),
}
Expand Down
5 changes: 5 additions & 0 deletions client/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct Eth<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, EC: EthConfig<B
/// When using eth_call/eth_estimateGas, the maximum allowed gas limit will be
/// block.gas_limit * execute_gas_limit_multiplier
execute_gas_limit_multiplier: u64,
forced_parent_hashes: Option<BTreeMap<H256, H256>>,
_marker: PhantomData<(B, BE, EC)>,
}

Expand All @@ -106,6 +107,7 @@ impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A
fee_history_cache: FeeHistoryCache,
fee_history_cache_limit: FeeHistoryCacheLimit,
execute_gas_limit_multiplier: u64,
forced_parent_hashes: Option<BTreeMap<H256, H256>>,
) -> Self {
Self {
client,
Expand All @@ -121,6 +123,7 @@ impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
_marker: PhantomData,
}
}
Expand All @@ -144,6 +147,7 @@ impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, EC: EthConfig<B, C>>
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
_marker: _,
} = self;

Expand All @@ -161,6 +165,7 @@ impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, EC: EthConfig<B, C>>
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
_marker: PhantomData,
}
}
Expand Down
8 changes: 7 additions & 1 deletion template/node/src/rpc/eth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{collections::BTreeMap, sync::Arc};

use jsonrpsee::RpcModule;
// Substrate
Expand All @@ -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;
Expand Down Expand Up @@ -54,6 +55,8 @@ pub struct EthDeps<C, P, A: ChainApi, CT, B: BlockT> {
/// 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<BTreeMap<H256, H256>>,
}

impl<C, P, A: ChainApi, CT: Clone, B: BlockT> Clone for EthDeps<C, P, A, CT, B> {
Expand All @@ -74,6 +77,7 @@ impl<C, P, A: ChainApi, CT: Clone, B: BlockT> Clone for EthDeps<C, P, A, CT, B>
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(),
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -141,6 +146,7 @@ where
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
)
.replace_config::<EC>()
.into_rpc(),
Expand Down
1 change: 1 addition & 0 deletions template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down