Skip to content

Commit

Permalink
add function to force block build with parent hash
Browse files Browse the repository at this point in the history
  • Loading branch information
noandrea committed Oct 12, 2023
1 parent aea5281 commit 8fc1b03
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
44 changes: 23 additions & 21 deletions client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use crate::{
frontier_backend_client, internal_err,
};

use super::rich_block_with_parent_build;

impl<B, C, P, CT, BE, A, CIDP, EC> Eth<B, C, P, CT, BE, A, CIDP, EC>
where
B: BlockT,
Expand Down Expand Up @@ -116,24 +118,17 @@ where
match (block, statuses) {
(Some(block), Some(statuses)) => {
let hash = H256::from(keccak_256(&rlp::encode(&block.header)));
let mut rich_block = rich_block_build(
let substrate_hash = H256::from_slice(substrate_hash.as_ref());
let rich_block = rich_block_with_parent_build(
block,
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
statuses,
hash,
substrate_hash,
&self.forced_parent_hashes,
full,
base_fee,
false,
);

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 @@ -171,14 +166,21 @@ where
let base_fee = api.gas_price(best_hash).ok();

match (block, statuses) {
(Some(block), Some(statuses)) => Ok(Some(rich_block_build(
block,
statuses.into_iter().map(Option::Some).collect(),
None,
full,
base_fee,
true,
))),
(Some(block), Some(statuses)) => {
let hash = H256::from(keccak_256(&rlp::encode(&block.header)));
let substrate_hash = H256::from_slice(best_hash.as_ref());
let rich_block = rich_block_with_parent_build(
block,
statuses,
hash,
substrate_hash,
&self.forced_parent_hashes,
full,
base_fee,
true,
);
Ok(Some(rich_block))
}
_ => Ok(None),
}
}
Expand Down
29 changes: 29 additions & 0 deletions client/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,35 @@ where
}
}

fn rich_block_with_parent_build(
block: EthereumBlock,
statuses: Vec<TransactionStatus>,
hash: H256,
substrate_hash: H256,
parent_hashes: &Option<BTreeMap<H256, H256>>,
full_transactions: bool,
base_fee: Option<U256>,
is_pending: bool,
) -> RichBlock {
let mut rich_block = rich_block_build(
block,
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
full_transactions,
base_fee,
is_pending,
);
let substrate_hash = H256::from_slice(substrate_hash.as_ref());

if let Some(parent_hash) = parent_hashes
.as_ref()
.and_then(|parent_hashes| parent_hashes.get(&substrate_hash).cloned())
{
rich_block.inner.header.parent_hash = parent_hash
}
rich_block
}

fn rich_block_build(
block: EthereumBlock,
statuses: Vec<Option<TransactionStatus>>,
Expand Down

0 comments on commit 8fc1b03

Please sign in to comment.