Skip to content

Commit

Permalink
feat(lazy-loading): Support querying old blocks by number (#3035)
Browse files Browse the repository at this point in the history
* feat(lazy-loading): support querying old ethereum blocks by number when using lazy-loading

* format code
  • Loading branch information
RomarQ authored Nov 7, 2024
1 parent 40129ec commit c155768
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions node/service/src/lazy_loading/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,22 @@ impl<Block: BlockT + DeserializeOwned> Blockchain<Block> {
pub fn id(&self, id: BlockId<Block>) -> Option<Block::Hash> {
match id {
BlockId::Hash(h) => Some(h),
BlockId::Number(n) => self.storage.read().hashes.get(&n).cloned(),
BlockId::Number(n) => {
let block_hash = self.storage.read().hashes.get(&n).cloned();
match block_hash {
None => {
let block_hash =
self.rpc_client.block_hash::<Block>(Some(n)).ok().flatten();

block_hash.clone().map(|h| {
self.storage.write().hashes.insert(n, h);
});

block_hash
}
block_hash => block_hash,
}
}
}
}

Expand Down Expand Up @@ -1506,17 +1521,17 @@ impl RPC {

pub fn block_hash<Block: BlockT + DeserializeOwned>(
&self,
block_number: Option<BlockNumber>,
block_number: Option<<Block::Header as HeaderT>::Number>,
) -> Result<Option<Block::Hash>, jsonrpsee::core::ClientError> {
let request = &|| {
substrate_rpc_client::ChainApi::<
BlockNumber,
<Block::Header as HeaderT>::Number,
Block::Hash,
Block::Header,
SignedBlock<Block>,
>::block_hash(
&self.http_client,
block_number.map(|n| ListOrValue::Value(NumberOrHex::Number(n.into()))),
block_number.map(|n| ListOrValue::Value(NumberOrHex::Hex(n.into()))),
)
};

Expand Down
2 changes: 1 addition & 1 deletion node/service/src/lazy_loading/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn produce_genesis_block<TBl: BlockT + sp_runtime::DeserializeOwned>(

let genesis_block_hash: TBl::Hash = backend
.rpc_client
.block_hash::<TBl>(Some(0))
.block_hash::<TBl>(Some(Default::default()))
.unwrap()
.expect("Not able to obtain genesis block hash");

Expand Down

0 comments on commit c155768

Please sign in to comment.