From 49d147f54fc187a0cffd1767af47fcc5783496a6 Mon Sep 17 00:00:00 2001 From: john xu Date: Sat, 20 Apr 2024 14:09:23 +0800 Subject: [PATCH] feat: use batch api instead of customize api for history headers --- host/Cargo.toml | 6 ++-- host/src/main.rs | 12 +++---- host/src/preflight.rs | 25 ++------------ host/src/provider_db.rs | 74 ++++++++++++++++++++++++++++++----------- 4 files changed, 66 insertions(+), 51 deletions(-) diff --git a/host/Cargo.toml b/host/Cargo.toml index 6a12bf46..c575aeb5 100644 --- a/host/Cargo.toml +++ b/host/Cargo.toml @@ -7,9 +7,9 @@ default-run = "raiko-host" [dependencies] # provers -sp1-prover = { path = "../provers/sp1/prover", optional = true} -risc0-prover = { path = "../provers/risc0", optional = true} -sgx-prover = { path = "../provers/sgx/prover", optional = true} +sp1-prover = { path = "../provers/sp1/prover", optional = true } +risc0-prover = { path = "../provers/risc0", optional = true } +sgx-prover = { path = "../provers/sgx/prover", optional = true } # raiko raiko-lib = { workspace = true } diff --git a/host/src/main.rs b/host/src/main.rs index d668e9e5..832ee745 100644 --- a/host/src/main.rs +++ b/host/src/main.rs @@ -14,12 +14,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub mod error; -pub mod execution; -pub mod preflight; -pub mod provider_db; -pub mod request; -pub mod server; +mod error; +mod execution; +mod preflight; +mod provider_db; +mod request; +mod server; use std::{alloc, fmt::Debug, fs::File, io::BufReader, path::PathBuf}; diff --git a/host/src/preflight.rs b/host/src/preflight.rs index 75fafeb6..3e1e124c 100644 --- a/host/src/preflight.rs +++ b/host/src/preflight.rs @@ -184,19 +184,12 @@ pub fn preflight( taiko: taiko_guest_input, }; - // Get the 256 history block hashes from the provider at first time for anchor - // transaction. - let initial_history_blocks = if network.is_taiko() { - Some(batch_get_history_headers(&provider, block_number)?) - } else { - None - }; // Create the block builder, run the transactions and extract the DB let provider_db = ProviderDb::new( provider, - initial_history_blocks, + network, parent_block.header.number.unwrap().try_into().unwrap(), - ); + )?; let mut builder = BlockBuilder::new(&input) .with_db(MeasuredProviderDb::new(provider_db)) .prepare_header::()? @@ -337,20 +330,6 @@ pub fn get_block(provider: &ReqwestProvider, block_number: u64, full: bool) -> R } } -fn batch_get_history_headers( - provider: &ReqwestProvider, - block_number: u64, -) -> Result> { - let tokio_handle = tokio::runtime::Handle::current(); - let response = tokio_handle.block_on(async { - provider - .client() - .request("taiko_getL2ParentHeaders", (block_number,)) - .await - })?; - Ok(response) -} - fn get_block_proposed_event( provider: &ReqwestProvider, network: Network, diff --git a/host/src/provider_db.rs b/host/src/provider_db.rs index 13c71f9c..be99bf3f 100644 --- a/host/src/provider_db.rs +++ b/host/src/provider_db.rs @@ -19,10 +19,13 @@ use std::{ use alloy_consensus::Header as AlloyConsensusHeader; use alloy_primitives::{Bytes, Uint}; use alloy_provider::{Provider, ReqwestProvider}; -use alloy_rpc_client::{ClientBuilder, RpcClient}; +use alloy_rpc_client::{BatchRequest, ClientBuilder, RpcClient}; use alloy_rpc_types::{Block, BlockId, EIP1186AccountProofResponse}; use alloy_transport_http::Http; -use raiko_lib::{clear_line, inplace_print, mem_db::MemDb, print_duration, taiko_utils::to_header}; +use raiko_lib::{ + clear_line, consts::Network, inplace_print, mem_db::MemDb, print_duration, + taiko_utils::to_header, +}; use raiko_primitives::{Address, B256, U256}; use reqwest_alloy::Client; use revm::{ @@ -46,31 +49,64 @@ pub struct ProviderDb { impl ProviderDb { pub fn new( provider: ReqwestProvider, - initial_history_blocks: Option>, + network: Network, block_number: u64, - ) -> Self { - let mut initial_db = MemDb::default(); - let mut initial_headers = HashMap::new(); - if let Some(initial_history_blocks) = initial_history_blocks { - for block in initial_history_blocks { - let block_number: u64 = block.header.number.unwrap().try_into().unwrap(); - let block_hash = block.header.hash.unwrap(); - initial_db.insert_block_hash(block_number, block_hash); - initial_headers.insert(block_number, to_header(&block.header)); - } - } - // The client used for batch requests + ) -> Result { let client = ClientBuilder::default() .reqwest_http(reqwest::Url::parse(&provider.client().transport().url()).unwrap()); - ProviderDb { + + let mut provider_db = ProviderDb { provider, client, block_number, - initial_db, - initial_headers, - current_db: MemDb::default(), + initial_db: Default::default(), + initial_headers: Default::default(), + current_db: Default::default(), async_executor: tokio::runtime::Handle::current(), + }; + if network.is_taiko() { + // Get the 256 history block hashes from the provider at first time for anchor + // transaction. + let initial_history_blocks = provider_db.batch_get_history_headers(block_number + 1)?; + for block in initial_history_blocks { + let block_number: u64 = block.header.number.unwrap().try_into().unwrap(); + let block_hash = block.header.hash.unwrap(); + provider_db + .initial_db + .insert_block_hash(block_number, block_hash); + provider_db + .initial_headers + .insert(block_number, to_header(&block.header)); + } } + Ok(provider_db) + } + + fn batch_get_history_headers( + &mut self, + block_number: u64, + ) -> Result, anyhow::Error> { + let mut batch = self.client.new_batch(); + let start = block_number.saturating_sub(255); + let mut requests = vec![]; + + for block_number in start..=block_number { + requests.push(Box::pin( + batch.add_call("eth_getBlockByNumber", &(block_number, false))?, + )); + } + + let blocks = self.async_executor.block_on(async { + batch.send().await?; + let mut blocks = vec![]; + // Collect the data from the batch + for request in requests.into_iter() { + blocks.push(request.await?); + } + Ok::<_, anyhow::Error>(blocks) + })?; + + Ok(blocks) } pub fn get_initial_db(&self) -> &MemDb {