Skip to content

Commit

Permalink
preflight speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcashier committed Nov 1, 2024
1 parent 5ee963d commit 3e5014e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions crates/preflight/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
let mut contracts: HashSet<Bytecode> = Default::default();
let mut ancestor_headers: Vec<R::Header> = Default::default();

for _ in 0..block_count {
for num_blocks in 0..block_count {
// Run the engine
info!("Pre execution validation ...");
engine.validate_header::<<Self as PreflightClient<N, R, P>>::Validation>()?;
Expand Down Expand Up @@ -221,6 +221,7 @@ where

// Increment block number counter
preflight_db.advance_provider_block()?;
preflight_db.clear()?;

// Give db back to engine
engine.replace_db(Wrapper::from(preflight_db))?;
Expand All @@ -240,14 +241,14 @@ where
"Storage tries: {storage_nodes} total nodes over {} accounts",
storage_tries.len()
);
info!("Witness now covers {num_blocks} blocks.");
}
info!("Blocks: {}", data.blocks.len());
let transactions: u64 = data
.blocks
.iter()
.map(|b| P::count_transactions(b) as u64)
.sum();
info!("Transactions: {transactions} total transactions");
info!("{transactions} total transactions.");

Ok(StatelessClientData::<R::Block, R::Header> {
chain: data.chain,
Expand Down
5 changes: 5 additions & 0 deletions crates/preflight/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ impl<N: Network, R: CoreDriver, P: PreflightDriver<R, N>> From<Rescued<PrePostDB
}

impl<N: Network, R: CoreDriver, P: PreflightDriver<R, N>> PreflightDB<N, R, P> {
pub fn clear(&mut self) -> anyhow::Result<()> {
let cleared = Self::from(self.inner.db.db.borrow().db.clone());
Ok(drop(core::mem::replace(self, cleared)))
}

pub fn save_provider(&mut self) -> anyhow::Result<()> {
self.inner.db.db.borrow_mut().db.save_provider()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/preflight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
type PreflightClient: PreflightClient<N, R, P>;
type StatelessClient: StatelessClient<R, D>;

async fn build_block(
async fn build_blocks(
chain_id: Option<u64>,
cache_dir: Option<PathBuf>,
rpc_url: Option<String>,
Expand Down Expand Up @@ -117,6 +117,7 @@ where
.unwrap();

let chain = provider_mut.get_chain().unwrap() as u64;
provider_mut.save().unwrap();

(validation_tip, chain)
})
Expand Down
11 changes: 10 additions & 1 deletion crates/preflight/src/provider/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ use std::marker::PhantomData;
use std::rc::Rc;
use zeth_core::driver::CoreDriver;

#[derive(Clone)]
pub struct ProviderDB<N: Network, R: CoreDriver, P: PreflightDriver<R, N>> {
pub provider: Rc<RefCell<dyn Provider<N>>>,
pub block_no: u64,
pub driver: PhantomData<(R, P)>,
}

impl<N: Network, R: CoreDriver, P: PreflightDriver<R, N>> Clone for ProviderDB<N, R, P> {
fn clone(&self) -> Self {
Self {
provider: self.provider.clone(),
block_no: self.block_no,
driver: self.driver,
}
}
}

impl<N: Network, R: CoreDriver, P: PreflightDriver<R, N>> ProviderDB<N, R, P> {
pub fn new(provider: Rc<RefCell<dyn Provider<N>>>, block_no: u64) -> Self {
ProviderDB {
Expand Down
2 changes: 1 addition & 1 deletion crates/zeth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ where
}

// preflight the block building process
let build_result = Handle::current().block_on(B::build_block(
let build_result = Handle::current().block_on(B::build_blocks(
chain_id,
cache_dir.clone(),
build_args.rpc_url.clone(),
Expand Down

0 comments on commit 3e5014e

Please sign in to comment.