Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/ethrex/bench/build_block_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async fn setup_genesis(accounts: &Vec<Address>) -> (Store, Genesis) {
}
let genesis_file = include_bytes!("../../../fixtures/genesis/l1-dev.json");
let mut genesis: Genesis = serde_json::from_slice(genesis_file).unwrap();
let store = Store::new(storage_path, EngineType::RocksDB).unwrap();
let store = Store::new(storage_path, EngineType::RocksDB, true).unwrap();
for address in accounts {
let account_info = GenesisAccount {
code: Bytes::new(),
Expand Down
8 changes: 4 additions & 4 deletions cmd/ethrex/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ pub async fn import_blocks(
) -> Result<(), ChainError> {
let start_time = Instant::now();
init_datadir(datadir);
let store = init_store(datadir, genesis).await;
let blockchain = init_blockchain(store.clone(), blockchain_type, false);
let (store, secondary) = init_store(datadir, genesis).await;
let blockchain = init_blockchain(store.clone(), secondary.clone(), blockchain_type, false);
let path_metadata = metadata(path).expect("Failed to read path");

// If it's an .rlp file it will be just one chain, but if it's a directory there can be multiple chains.
Expand Down Expand Up @@ -423,7 +423,7 @@ pub async fn import_blocks(
}

// Check if the block is already in the blockchain, if it is do nothing, if not add it
let block_number = store.get_block_number(hash).await.map_err(|_e| {
let block_number = secondary.get_block_number(hash).await.map_err(|_e| {
ChainError::Custom(String::from(
"Couldn't check if block is already in the blockchain",
))
Expand Down Expand Up @@ -476,7 +476,7 @@ pub async fn export_blocks(
last_number: Option<u64>,
) {
init_datadir(datadir);
let store = load_store(datadir).await;
let (store, _secondary) = load_store(datadir).await;
let start = first_number.unwrap_or_default();
// If we have no latest block then we don't have any blocks to export
let latest_number = match store.get_latest_block_number().await {
Expand Down
37 changes: 25 additions & 12 deletions cmd/ethrex/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,39 @@ pub fn init_metrics(opts: &Options, tracker: TaskTracker) {
}

/// Opens a new or pre-existing Store and loads the initial state provided by the network
pub async fn init_store(datadir: impl AsRef<Path>, genesis: Genesis) -> Store {
let store = open_store(datadir.as_ref());
pub async fn init_store(datadir: impl AsRef<Path>, genesis: Genesis) -> (Store, Store) {
let (store, secondary) = open_store(datadir.as_ref());
store
.add_initial_state(genesis)
.await
.expect("Failed to create genesis block");
store
secondary
.catch_up_with_primary()
.await
.expect("Failed to catch up");
(store, secondary)
}

/// Initializes a pre-existing Store
pub async fn load_store(datadir: &Path) -> Store {
let store = open_store(datadir);
pub async fn load_store(datadir: &Path) -> (Store, Store) {
let (store, secondary) = open_store(datadir);
store
.load_initial_state()
.await
.expect("Failed to load store");
store
secondary
.catch_up_with_primary()
.await
.expect("Failed to catch up");
(store, secondary)
}

/// Opens a pre-existing Store or creates a new one
pub fn open_store(datadir: &Path) -> Store {
pub fn open_store(datadir: &Path) -> (Store, Store) {
if datadir.ends_with("memory") {
Store::new(datadir, EngineType::InMemory).expect("Failed to create Store")
let store =
Store::new(datadir, EngineType::InMemory, true).expect("Failed to create Store");
(store.clone(), store)
} else {
cfg_if::cfg_if! {
if #[cfg(feature = "rocksdb")] {
Expand All @@ -110,20 +120,23 @@ pub fn open_store(datadir: &Path) -> Store {
panic!("Specify the desired database engine.");
}
};
Store::new(datadir, engine_type).expect("Failed to create Store")
let store = Store::new(datadir, engine_type, true).expect("Failed to create Store");
let secondary = Store::new(datadir, engine_type, false).expect("Failed to create Store");
(store, secondary)
}
}

pub fn init_blockchain(
store: Store,
secondary: Store,
blockchain_type: BlockchainType,
perf_logs_enabled: bool,
) -> Arc<Blockchain> {
#[cfg(feature = "revm")]
info!("Initiating blockchain with revm");
#[cfg(not(feature = "revm"))]
info!("Initiating blockchain with levm");
Blockchain::new(store, blockchain_type, perf_logs_enabled).into()
Blockchain::new(store, secondary, blockchain_type, perf_logs_enabled).into()
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -383,12 +396,12 @@ pub async fn init_l1(

let genesis = network.get_genesis()?;
display_chain_initialization(&genesis);
let store = init_store(datadir, genesis).await;
let (store, secondary) = init_store(datadir, genesis).await;

#[cfg(feature = "sync-test")]
set_sync_block(&store).await;

let blockchain = init_blockchain(store.clone(), BlockchainType::L1, true);
let blockchain = init_blockchain(store.clone(), secondary, BlockchainType::L1, true);

let signer = get_signer(datadir);

Expand Down
8 changes: 4 additions & 4 deletions cmd/ethrex/l2/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub enum Command {
#[arg(
long,
value_parser = parse_private_key,
env = "SEQUENCER_PRIVATE_KEY",
help = "The private key of the sequencer",
env = "SEQUENCER_PRIVATE_KEY",
help = "The private key of the sequencer",
help_heading = "Sequencer account options",
group = "sequencer_signing",
)]
Expand Down Expand Up @@ -396,7 +396,7 @@ impl Command {
};

// Init stores
let store = Store::new_from_genesis(
let (store, _secondary) = Store::new_from_genesis(
&store_path,
store_type,
genesis.to_str().expect("Invalid genesis path"),
Expand Down Expand Up @@ -708,7 +708,7 @@ async fn delete_blocks_from_batch(
let genesis = network.get_genesis()?;

let mut block_to_delete = last_kept_block + 1;
let store = init_store(datadir, genesis).await;
let (store, _secondary) = init_store(datadir, genesis).await;

while store
.get_canonical_block_hash(block_to_delete)
Expand Down
4 changes: 2 additions & 2 deletions cmd/ethrex/l2/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ pub async fn init_l2(
let network = get_network(&opts.node_opts);

let genesis = network.get_genesis()?;
let store = init_store(&datadir, genesis).await;
let (store, secondary) = init_store(&datadir, genesis).await;
let rollup_store = init_rollup_store(&rollup_store_dir).await;

let blockchain = init_blockchain(store.clone(), BlockchainType::L2, true);
let blockchain = init_blockchain(store.clone(), secondary, BlockchainType::L2, true);

let signer = get_signer(&datadir);

Expand Down
10 changes: 8 additions & 2 deletions cmd/ethrex_replay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ async fn replay_no_zkvm(cache: Cache, opts: &EthrexReplayOptions) -> eyre::Resul
engine: Arc::new(in_memory_store),
chain_config: Arc::new(RwLock::new(chain_config)),
latest_block_header: Arc::new(RwLock::new(BlockHeader::default())),
is_primary: true,
};

// Add codes to DB
Expand Down Expand Up @@ -819,12 +820,17 @@ pub async fn replay_custom_l1_blocks(
let genesis = network.get_genesis()?;

let mut store = {
let store_inner = Store::new("./", EngineType::InMemory)?;
let store_inner = Store::new("./", EngineType::InMemory, true)?;
store_inner.add_initial_state(genesis.clone()).await?;
store_inner
};

let blockchain = Arc::new(Blockchain::new(store.clone(), BlockchainType::L1, false));
let blockchain = Arc::new(Blockchain::new(
store.clone(),
store.clone(),
BlockchainType::L1,
false,
));

let blocks = produce_l1_blocks(
blockchain.clone(),
Expand Down
Loading