Skip to content
Merged
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/l2/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub async fn init_l2(
// We wrap fee_config in an Arc<RwLock> to let the watcher
// update the L1 fee periodically.
let l2_config = L2Config {
fee_config: Arc::new(tokio::sync::RwLock::new(fee_config)),
fee_config: Arc::new(std::sync::RwLock::new(fee_config)),
};

let blockchain_opts = ethrex_blockchain::BlockchainOptions {
Expand Down
32 changes: 20 additions & 12 deletions crates/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use mempool::Mempool;
use payload::PayloadOrTask;
use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, RwLock};
use std::time::Instant;
use tokio::sync::{Mutex as TokioMutex, RwLock};
use tokio::sync::Mutex as TokioMutex;
use tokio_util::sync::CancellationToken;

use vm::StoreVmDatabase;
Expand Down Expand Up @@ -156,7 +156,7 @@ impl Blockchain {
validate_block(block, &parent_header, &chain_config, ELASTICITY_MULTIPLIER)?;

let vm_db = StoreVmDatabase::new(self.storage.clone(), block.header.parent_hash);
let mut vm = self.new_evm(vm_db).await?;
let mut vm = self.new_evm(vm_db)?;

let execution_result = vm.execute_block(block)?;
let account_updates = vm.get_state_transitions()?;
Expand Down Expand Up @@ -641,7 +641,7 @@ impl Blockchain {
first_block_header.parent_hash,
block_hash_cache,
);
let mut vm = self.new_evm(vm_db).await.map_err(|e| (e.into(), None))?;
let mut vm = self.new_evm(vm_db).map_err(|e| (e.into(), None))?;

let blocks_len = blocks.len();
let mut all_receipts: Vec<(BlockHash, Vec<Receipt>)> = Vec::with_capacity(blocks_len);
Expand Down Expand Up @@ -1018,14 +1018,8 @@ impl Blockchain {
Ok(result)
}

pub async fn new_evm(&self, vm_db: StoreVmDatabase) -> Result<Evm, EvmError> {
let evm = match &self.options.r#type {
BlockchainType::L1 => Evm::new_for_l1(vm_db),
BlockchainType::L2(l2_config) => {
Evm::new_for_l2(vm_db, *l2_config.fee_config.read().await)?
}
};
Ok(evm)
pub fn new_evm(&self, vm_db: StoreVmDatabase) -> Result<Evm, EvmError> {
new_evm(&self.options.r#type, vm_db)
}

/// Get the current fork of the chain, based on the latest block's timestamp
Expand All @@ -1040,6 +1034,20 @@ impl Blockchain {
}
}

pub fn new_evm(blockchain_type: &BlockchainType, vm_db: StoreVmDatabase) -> Result<Evm, EvmError> {
let evm = match blockchain_type {
BlockchainType::L1 => Evm::new_for_l1(vm_db),
BlockchainType::L2(l2_config) => {
let fee_config = *l2_config
.fee_config
.read()
.map_err(|_| EvmError::Custom("Fee config lock was poisoned".to_string()))?;
Evm::new_for_l2(vm_db, fee_config)?
}
};
Ok(evm)
}

pub fn validate_requests_hash(
header: &BlockHeader,
chain_config: &ChainConfig,
Expand Down
15 changes: 5 additions & 10 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
constants::{GAS_LIMIT_BOUND_DIVISOR, MIN_GAS_LIMIT, TX_GAS_COST},
error::{ChainError, InvalidBlockError},
mempool::PendingTxFilter,
new_evm,
vm::StoreVmDatabase,
};

Expand Down Expand Up @@ -219,10 +220,10 @@ pub struct PayloadBuildContext {
}

impl PayloadBuildContext {
pub async fn new(
pub fn new(
payload: Block,
storage: &Store,
blockchain_type: BlockchainType,
blockchain_type: &BlockchainType,
) -> Result<Self, EvmError> {
let config = storage
.get_chain_config()
Expand All @@ -236,12 +237,7 @@ impl PayloadBuildContext {
);

let vm_db = StoreVmDatabase::new(storage.clone(), payload.header.parent_hash);
let vm = match blockchain_type {
BlockchainType::L1 => Evm::new_for_l1(vm_db),
BlockchainType::L2(l2_config) => {
Evm::new_for_l2(vm_db, *l2_config.fee_config.read().await)?
}
};
let vm = new_evm(blockchain_type, vm_db)?;

Ok(PayloadBuildContext {
remaining_gas: payload.header.gas_limit,
Expand Down Expand Up @@ -393,8 +389,7 @@ impl Blockchain {

debug!("Building payload");
let base_fee = payload.header.base_fee_per_gas.unwrap_or_default();
let mut context =
PayloadBuildContext::new(payload, &self.storage, self.options.r#type.clone()).await?;
let mut context = PayloadBuildContext::new(payload, &self.storage, &self.options.r#type)?;

if let BlockchainType::L1 = self.options.r#type {
self.apply_system_operations(&mut context)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Blockchain {
parent_hash,
block_hash_cache,
);
let mut vm = self.new_evm(vm_db).await?;
let mut vm = self.new_evm(vm_db)?;
// Run parents to rebuild pre-state
for block in blocks_to_re_execute.iter().rev() {
vm.rerun_block(block, None)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/l2/based/block_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl BlockFetcher {
let mut acc_account_updates: HashMap<H160, AccountUpdate> = HashMap::new();
for block in batch {
let vm_db = StoreVmDatabase::new(self.store.clone(), block.header.parent_hash);
let mut vm = self.blockchain.new_evm(vm_db).await?;
let mut vm = self.blockchain.new_evm(vm_db)?;
vm.execute_block(block)
.map_err(BlockFetcherError::EvmError)?;
let account_updates = vm
Expand Down
5 changes: 4 additions & 1 deletion crates/l2/sequencer/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ impl BlockProducer {
return Err(BlockProducerError::Custom("Invalid blockchain type".into()));
};

let fee_config = *l2_config.fee_config.read().await;
let fee_config = *l2_config
.fee_config
.read()
.map_err(|_| BlockProducerError::Custom("Fee config lock was poisoned".to_string()))?;

self.rollup_store
.store_fee_config_by_block(block_number, fee_config)
Expand Down
3 changes: 1 addition & 2 deletions crates/l2/sequencer/block_producer/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ pub async fn build_payload(
let gas_limit = payload.header.gas_limit;

debug!("Building payload");
let mut context =
PayloadBuildContext::new(payload, store, blockchain.options.r#type.clone()).await?;
let mut context = PayloadBuildContext::new(payload, store, &blockchain.options.r#type)?;

fill_transactions(
blockchain.clone(),
Expand Down
5 changes: 4 additions & 1 deletion crates/l2/sequencer/l1_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ impl GenServer for L1Watcher {
return CastResponse::NoReply;
};

let mut fee_config_guard = l2_config.fee_config.write().await;
let Ok(mut fee_config_guard) = l2_config.fee_config.write() else {
error!("Fee config lock was poisoned when updating L1 blob base fee");
return CastResponse::NoReply;
};

let Some(l1_fee_config) = fee_config_guard.l1_fee_config.as_mut() else {
warn!("L1 fee config is not set. Skipping L1 blob base fee update.");
Expand Down
5 changes: 4 additions & 1 deletion crates/networking/rpc/eth/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ impl RpcHandler for GasPrice {

// Add the operator fee to the gas price if configured
if let BlockchainType::L2(l2_config) = &context.blockchain.options.r#type {
let fee_config = *l2_config.fee_config.read().await;
let fee_config = *l2_config
.fee_config
.read()
.map_err(|_| RpcErr::Internal("Fee config lock was poisoned".to_string()))?;
if let Some(operator_fee_config) = &fee_config.operator_fee_config {
gas_price += operator_fee_config.operator_fee_per_gas;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/networking/rpc/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl RpcHandler for CreateAccessListRequest {
};

let vm_db = StoreVmDatabase::new(context.storage.clone(), header.hash());
let mut vm = context.blockchain.new_evm(vm_db).await?;
let mut vm = context.blockchain.new_evm(vm_db)?;

// Run transaction and obtain access list
let (gas_used, access_list, error) = vm.create_access_list(&self.transaction, &header)?;
Expand Down Expand Up @@ -572,7 +572,7 @@ async fn simulate_tx(
blockchain: Arc<Blockchain>,
) -> Result<ExecutionResult, RpcErr> {
let vm_db = StoreVmDatabase::new(storage.clone(), block_header.hash());
let mut vm = blockchain.new_evm(vm_db).await?;
let mut vm = blockchain.new_evm(vm_db)?;

match vm.simulate_tx_from_generic(transaction, block_header)? {
ExecutionResult::Revert {
Expand Down