Skip to content

Commit

Permalink
feat: use batch api instead of customize api for history headers
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Apr 20, 2024
1 parent b06c222 commit 49d147f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 51 deletions.
6 changes: 3 additions & 3 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
12 changes: 6 additions & 6 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
25 changes: 2 additions & 23 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TaikoHeaderPrepStrategy>()?
Expand Down Expand Up @@ -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<Vec<AlloyBlock>> {
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,
Expand Down
74 changes: 55 additions & 19 deletions host/src/provider_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -46,31 +49,64 @@ pub struct ProviderDb {
impl ProviderDb {
pub fn new(
provider: ReqwestProvider,
initial_history_blocks: Option<Vec<Block>>,
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<Self, anyhow::Error> {
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<Vec<Block>, 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 {
Expand Down

0 comments on commit 49d147f

Please sign in to comment.