Skip to content

Commit bc9aafd

Browse files
authored
chore(l1): standarize revm/levm behaviour when importing blocks. (#2452)
**Description** - standarize revm/levm behaviour when importing blocks - Remove fork choice when importing blocks. - Move block importing out of blockchain module
1 parent 775bc45 commit bc9aafd

File tree

2 files changed

+32
-59
lines changed

2 files changed

+32
-59
lines changed

cmd/ethrex/cli.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

77
use clap::{ArgAction, Parser as ClapParser, Subcommand as ClapSubcommand};
8+
use ethrex_blockchain::fork_choice::apply_fork_choice;
89
use ethrex_p2p::{sync::SyncMode, types::Node};
910
use ethrex_vm::EvmEngine;
1011
use tracing::{info, warn, Level};
@@ -55,7 +56,7 @@ pub struct Options {
5556
)]
5657
pub datadir: String,
5758
#[arg(
58-
long = "force",
59+
long = "force",
5960
help = "Force remove the database",
6061
long_help = "Delete the database without confirmation.",
6162
action = clap::ArgAction::SetTrue,
@@ -298,7 +299,7 @@ pub async fn import_blocks(path: &str, data_dir: &str, network: &str, evm: EvmEn
298299

299300
let store = init_store(&data_dir, network).await;
300301

301-
let blockchain = init_blockchain(evm, store);
302+
let blockchain = init_blockchain(evm, store.clone());
302303

303304
let path_metadata = metadata(path).expect("Failed to read path");
304305
let blocks = if path_metadata.is_dir() {
@@ -317,5 +318,32 @@ pub async fn import_blocks(path: &str, data_dir: &str, network: &str, evm: EvmEn
317318
info!("Importing blocks from chain file: {path}");
318319
utils::read_chain_file(path)
319320
};
320-
blockchain.import_blocks(&blocks).await;
321+
322+
let size = blocks.len();
323+
324+
for block in &blocks {
325+
let hash = block.hash();
326+
327+
info!(
328+
"Adding block {} with hash {:#x}.",
329+
block.header.number, hash
330+
);
331+
332+
if let Err(error) = blockchain.add_block(block).await {
333+
warn!(
334+
"Failed to add block {} with hash {:#x}: {}.",
335+
block.header.number, hash, error
336+
);
337+
return;
338+
}
339+
}
340+
341+
if let Some(last_block) = blocks.last() {
342+
let hash = last_block.hash();
343+
if let Err(error) = apply_fork_choice(&store, hash, hash, hash).await {
344+
warn!("Failed to apply fork choice: {}", error);
345+
}
346+
}
347+
348+
info!("Added {size} blocks to blockchain");
321349
}

crates/blockchain/blockchain.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ use std::{ops::Div, time::Instant};
2626
use ethrex_storage::error::StoreError;
2727
use ethrex_storage::{AccountUpdate, Store};
2828
use ethrex_vm::{BlockExecutionResult, Evm, EvmEngine};
29-
use fork_choice::apply_fork_choice;
30-
use tracing::{error, info, warn};
29+
use tracing::info;
3130

3231
//TODO: Implement a struct Chain or BlockChain to encapsulate
3332
//functionality and canonical chain state and config
@@ -315,60 +314,6 @@ impl Blockchain {
315314
Ok(())
316315
}
317316

318-
//TODO: Forkchoice Update shouldn't be part of this function
319-
pub async fn import_blocks(&self, blocks: &[Block]) {
320-
let size = blocks.len();
321-
for block in blocks {
322-
let hash = block.hash();
323-
info!(
324-
"Adding block {} with hash {:#x}.",
325-
block.header.number, hash
326-
);
327-
if let Err(error) = self.add_block(block).await {
328-
warn!(
329-
"Failed to add block {} with hash {:#x}: {}.",
330-
block.header.number, hash, error
331-
);
332-
}
333-
if self
334-
.storage
335-
.update_latest_block_number(block.header.number)
336-
.await
337-
.is_err()
338-
{
339-
error!("Fatal: added block {} but could not update the block number -- aborting block import", block.header.number);
340-
break;
341-
};
342-
if self
343-
.storage
344-
.set_canonical_block(block.header.number, hash)
345-
.await
346-
.is_err()
347-
{
348-
error!(
349-
"Fatal: added block {} but could not set it as canonical -- aborting block import",
350-
block.header.number
351-
);
352-
break;
353-
};
354-
}
355-
if let Some(last_block) = blocks.last() {
356-
let hash = last_block.hash();
357-
match self.evm_engine {
358-
EvmEngine::LEVM => {
359-
// We are allowing this not to unwrap so that tests can run even if block execution results in the wrong root hash with LEVM.
360-
let _ = apply_fork_choice(&self.storage, hash, hash, hash).await;
361-
}
362-
EvmEngine::REVM => {
363-
apply_fork_choice(&self.storage, hash, hash, hash)
364-
.await
365-
.unwrap();
366-
}
367-
}
368-
}
369-
info!("Added {size} blocks to blockchain");
370-
}
371-
372317
/// Add a blob transaction and its blobs bundle to the mempool checking that the transaction is valid
373318
#[cfg(feature = "c-kzg")]
374319
pub async fn add_blob_transaction_to_pool(

0 commit comments

Comments
 (0)