Skip to content

Commit

Permalink
feat: batch get 256 history headers (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko authored Apr 10, 2024
1 parent ed61e39 commit fd3434a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
20 changes: 17 additions & 3 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fn get_blob_data(beacon_rpc_url: &str, block_id: u64) -> Result<GetBlobsResponse
// CommitmentInclusionProof []string
// `json:"kzg_commitment_inclusion_proof"` }
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GetBlobData {
struct GetBlobData {
pub index: String,
pub blob: String,
// pub signed_block_header: SignedBeaconBlockHeader, // ignore for now
Expand All @@ -308,7 +308,7 @@ pub struct GetBlobData {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GetBlobsResponse {
struct GetBlobsResponse {
pub data: Vec<GetBlobData>,
}

Expand All @@ -325,7 +325,21 @@ pub fn get_block(provider: &ReqwestProvider, block_number: u64, full: bool) -> R
}
}

pub fn get_block_proposed_event(
pub fn batch_get_history_headers(
provider: &ReqwestProvider,
handle: &tokio::runtime::Handle,
block_number: u64,
) -> Result<Vec<AlloyBlock>> {
let response = handle.block_on(async {
provider
.client()
.request("taiko_getL2ParentHeaders", (block_number,))
.await
})?;
Ok(response)
}

fn get_block_proposed_event(
provider: &ReqwestProvider,
network: Network,
block_hash: B256,
Expand Down
26 changes: 10 additions & 16 deletions host/src/provider_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use revm::{
};
use tokio::runtime::Handle;

use crate::preflight::get_block;
use crate::preflight::{batch_get_history_headers, get_block};

pub struct ProviderDb {
pub provider: ReqwestProvider,
Expand Down Expand Up @@ -224,22 +224,16 @@ impl Database for ProviderDb {
return Ok(block_hash);
}

// Get the block hash from the provider.
// Get the 256 history block hashes from the provider at first time for anchor
// transaction.
let block_number = u64::try_from(number).unwrap();
let block_hash = self.async_executor.block_on(async {
self.provider
.get_block_by_number(block_number.into(), false)
.await
.unwrap()
.unwrap()
.header
.hash
.unwrap()
.0
.into()
});
self.initial_db.insert_block_hash(block_number, block_hash);
Ok(block_hash)
for block in batch_get_history_headers(&self.provider, &self.async_executor, block_number)?
{
let block_number = block.header.number.unwrap().try_into().unwrap();
let block_hash = block.header.hash.unwrap();
self.initial_db.insert_block_hash(block_number, block_hash);
}
self.block_hash(number)
}

fn code_by_hash(&mut self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
Expand Down

0 comments on commit fd3434a

Please sign in to comment.