Skip to content

Commit

Permalink
Client level patch for parent hash (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
librelois authored and notlesh committed Sep 30, 2022
1 parent f948d5b commit 452e588
Showing 1 changed file with 59 additions and 9 deletions.
68 changes: 59 additions & 9 deletions client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use jsonrpsee::core::RpcResult as Result;
use sc_client_api::backend::{Backend, StateBackend, StorageProvider};
use sc_network::ExHashT;
use sc_transaction_pool::ChainApi;
use sp_api::{BlockId, HeaderT};
use sp_blockchain::HeaderBackend;
use sp_core::hashing::keccak_256;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
Expand Down Expand Up @@ -73,13 +74,38 @@ where
let base_fee = handler.base_fee(&id);

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(block), Some(statuses)) => {
let mut rich_block = rich_block_build(
block,
statuses.into_iter().map(|s| Some(s)).collect(),
Some(hash),
full,
base_fee,
);
// Indexers heavily rely on the parent hash.
// Moonbase client-level patch for inconsistent runtime 1200 state.
let number = rich_block.inner.header.number.unwrap_or_default();
if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() {
let id = BlockId::Hash(substrate_hash);
if let Ok(Some(header)) = client.header(id) {
let parent_hash = *header.parent_hash();

let parent_id = BlockId::Hash(parent_hash);
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
parent_id,
);
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),
}
}
Expand Down Expand Up @@ -124,13 +150,37 @@ where
(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,
base_fee,
)))
);

// Indexers heavily rely on the parent hash.
// Moonbase client-level patch for inconsistent runtime 1200 state.
let number = rich_block.inner.header.number.unwrap_or_default();
if rich_block.inner.header.parent_hash == H256::default() && number > U256::zero() {
let id = BlockId::Hash(substrate_hash);
if let Ok(Some(header)) = client.header(id) {
let parent_hash = *header.parent_hash();

let parent_id = BlockId::Hash(parent_hash);
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
parent_id,
);
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),
}
Expand Down

0 comments on commit 452e588

Please sign in to comment.