Skip to content

Commit 7d22fcc

Browse files
authored
Merge branch 'main' into edgl_snapshots_v2
2 parents 1d6ad11 + 23627eb commit 7d22fcc

File tree

8 files changed

+61
-44
lines changed

8 files changed

+61
-44
lines changed

crates/blockchain/vm.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bytes::Bytes;
22
use ethrex_common::{
3-
types::{AccountInfo, BlockHash, ChainConfig},
3+
types::{AccountInfo, BlockHash, ChainConfig, EMPTY_KECCACK_HASH},
44
Address, H256, U256,
55
};
66
use ethrex_storage::Store;
@@ -41,13 +41,23 @@ impl VmDatabase for StoreVmDatabase {
4141
}
4242
}
4343

44-
fn get_chain_config(&self) -> ChainConfig {
45-
self.store.get_chain_config().unwrap()
46-
}
47-
48-
fn get_account_code(&self, code_hash: H256) -> Result<Option<Bytes>, EvmError> {
44+
fn get_chain_config(&self) -> Result<ChainConfig, EvmError> {
4945
self.store
50-
.get_account_code(code_hash)
46+
.get_chain_config()
5147
.map_err(|e| EvmError::DB(e.to_string()))
5248
}
49+
50+
fn get_account_code(&self, code_hash: H256) -> Result<Bytes, EvmError> {
51+
if code_hash == *EMPTY_KECCACK_HASH {
52+
return Ok(Bytes::new());
53+
}
54+
match self.store.get_account_code(code_hash) {
55+
Ok(Some(code)) => Ok(code),
56+
Ok(None) => Err(EvmError::DB(format!(
57+
"Code not found for hash: {:?}",
58+
code_hash
59+
))),
60+
Err(e) => Err(EvmError::DB(e.to_string())),
61+
}
62+
}
5363
}

crates/l2/prover/bench/src/rpc/db.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,11 @@ impl LevmDatabase for RpcDB {
425425
.is_ok_and(|account| matches!(account, Account::Existing { .. }))
426426
}
427427

428-
fn get_account_code(&self, _code_hash: H256) -> Result<Option<Bytes>, DatabaseError> {
429-
Ok(None) // code is stored in account info
428+
fn get_account_code(&self, _code_hash: H256) -> Result<Bytes, DatabaseError> {
429+
Err(DatabaseError::Custom(
430+
"get_account_code is not supported for RpcDB: code is stored in account info"
431+
.to_string(),
432+
))
430433
}
431434

432435
fn get_account(
@@ -491,8 +494,8 @@ impl LevmDatabase for RpcDB {
491494
Ok(hash)
492495
}
493496

494-
fn get_chain_config(&self) -> ethrex_common::types::ChainConfig {
495-
*CANCUN_CONFIG
497+
fn get_chain_config(&self) -> Result<ethrex_common::types::ChainConfig, DatabaseError> {
498+
Ok(*CANCUN_CONFIG)
496499
}
497500
}
498501

crates/vm/backends/levm/db.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ impl LevmDatabase for DatabaseLogger {
7878
Ok(block_hash)
7979
}
8080

81-
fn get_chain_config(&self) -> ethrex_common::types::ChainConfig {
81+
fn get_chain_config(&self) -> Result<ethrex_common::types::ChainConfig, DatabaseError> {
8282
self.store.lock().unwrap().get_chain_config()
8383
}
8484

85-
fn get_account_code(&self, code_hash: CoreH256) -> Result<Option<bytes::Bytes>, DatabaseError> {
85+
fn get_account_code(&self, code_hash: CoreH256) -> Result<bytes::Bytes, DatabaseError> {
8686
{
8787
let mut code_accessed = self
8888
.code_accessed
@@ -104,8 +104,7 @@ impl LevmDatabase for DynVmDatabase {
104104
.unwrap_or_default();
105105

106106
let acc_code = <dyn VmDatabase>::get_account_code(self.as_ref(), acc_info.code_hash)
107-
.map_err(|e| DatabaseError::Custom(e.to_string()))?
108-
.unwrap_or_default();
107+
.map_err(|e| DatabaseError::Custom(e.to_string()))?;
109108

110109
Ok(Account::new(
111110
acc_info.balance,
@@ -137,11 +136,12 @@ impl LevmDatabase for DynVmDatabase {
137136
.map_err(|e| DatabaseError::Custom(e.to_string()))
138137
}
139138

140-
fn get_chain_config(&self) -> ethrex_common::types::ChainConfig {
139+
fn get_chain_config(&self) -> Result<ethrex_common::types::ChainConfig, DatabaseError> {
141140
<dyn VmDatabase>::get_chain_config(self.as_ref())
141+
.map_err(|e| DatabaseError::Custom(e.to_string()))
142142
}
143143

144-
fn get_account_code(&self, code_hash: CoreH256) -> Result<Option<bytes::Bytes>, DatabaseError> {
144+
fn get_account_code(&self, code_hash: CoreH256) -> Result<bytes::Bytes, DatabaseError> {
145145
<dyn VmDatabase>::get_account_code(self.as_ref(), code_hash)
146146
.map_err(|e| DatabaseError::Custom(e.to_string()))
147147
}
@@ -198,11 +198,17 @@ impl LevmDatabase for ProverDB {
198198
Ok(*storage.get(&key).unwrap_or(&CoreU256::default()))
199199
}
200200

201-
fn get_chain_config(&self) -> ethrex_common::types::ChainConfig {
202-
self.get_chain_config()
201+
fn get_chain_config(&self) -> Result<ethrex_common::types::ChainConfig, DatabaseError> {
202+
Ok(self.get_chain_config())
203203
}
204204

205-
fn get_account_code(&self, code_hash: CoreH256) -> Result<Option<bytes::Bytes>, DatabaseError> {
206-
Ok(self.code.get(&code_hash).cloned())
205+
fn get_account_code(&self, code_hash: CoreH256) -> Result<bytes::Bytes, DatabaseError> {
206+
match self.code.get(&code_hash) {
207+
Some(code) => Ok(code.clone()),
208+
None => Err(DatabaseError::Custom(format!(
209+
"Could not find code for hash {}",
210+
code_hash
211+
))),
212+
}
207213
}
208214
}

crates/vm/backends/levm/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl LEVM {
4646
) -> Result<BlockExecutionResult, EvmError> {
4747
cfg_if::cfg_if! {
4848
if #[cfg(not(feature = "l2"))] {
49-
let chain_config = db.store.get_chain_config();
49+
let chain_config = db.store.get_chain_config()?;
5050
let block_header = &block.header;
5151
let fork = chain_config.fork(block_header.timestamp);
5252
if block_header.parent_beacon_block_root.is_some() && fork >= Fork::Cancun {
@@ -99,7 +99,7 @@ impl LEVM {
9999
block_header: &BlockHeader,
100100
db: &mut GeneralizedDatabase,
101101
) -> Result<Environment, EvmError> {
102-
let chain_config = db.store.get_chain_config();
102+
let chain_config = db.store.get_chain_config()?;
103103
let gas_price: U256 = tx
104104
.effective_gas_price(block_header.base_fee_per_gas)
105105
.ok_or(VMError::TxValidation(
@@ -436,7 +436,7 @@ pub fn generic_system_contract_levm(
436436
contract_address: Address,
437437
system_address: Address,
438438
) -> Result<ExecutionReport, EvmError> {
439-
let chain_config = db.store.get_chain_config();
439+
let chain_config = db.store.get_chain_config()?;
440440
let config = EVMConfig::new_from_chain_config(&chain_config, block_header);
441441
let system_account_backup = db.cache.get(&system_address).cloned();
442442
let coinbase_backup = db.cache.get(&block_header.coinbase).cloned();
@@ -492,7 +492,7 @@ pub fn extract_all_requests_levm(
492492
db: &mut GeneralizedDatabase,
493493
header: &BlockHeader,
494494
) -> Result<Vec<Requests>, EvmError> {
495-
let chain_config = db.store.get_chain_config();
495+
let chain_config = db.store.get_chain_config()?;
496496
let fork = chain_config.fork(header.timestamp);
497497

498498
if fork < Fork::Prague {
@@ -564,7 +564,7 @@ fn env_from_generic(
564564
header: &BlockHeader,
565565
db: &GeneralizedDatabase,
566566
) -> Result<Environment, VMError> {
567-
let chain_config = db.store.get_chain_config();
567+
let chain_config = db.store.get_chain_config()?;
568568
let gas_price = calculate_gas_price(tx, header.base_fee_per_gas.unwrap_or(INITIAL_BASE_FEE));
569569
let config = EVMConfig::new_from_chain_config(&chain_config, header);
570570
Ok(Environment {

crates/vm/backends/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Evm {
211211
Ok(())
212212
}
213213
Evm::LEVM { db } => {
214-
let chain_config = db.store.get_chain_config();
214+
let chain_config = db.store.get_chain_config()?;
215215
let fork = chain_config.fork(block_header.timestamp);
216216

217217
if block_header.parent_beacon_block_root.is_some() && fork >= Fork::Cancun {

crates/vm/backends/revm/db.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl EvmState {
4949
/// Gets the stored chain config
5050
pub fn chain_config(&self) -> Result<ChainConfig, EvmError> {
5151
match self {
52-
EvmState::Store(db) => Ok(db.database.get_chain_config()),
52+
EvmState::Store(db) => db.database.get_chain_config(),
5353
EvmState::Execution(db) => Ok(db.db.get_chain_config()),
5454
}
5555
}
@@ -128,21 +128,20 @@ impl revm::Database for DynVmDatabase {
128128
None => return Ok(None),
129129
Some(acc_info) => acc_info,
130130
};
131-
let code = <dyn VmDatabase>::get_account_code(self.as_ref(), acc_info.code_hash)?
132-
.map(|b| RevmBytecode::new_raw(RevmBytes(b)));
131+
let code = self.code_by_hash(acc_info.code_hash.0.into())?;
133132

134133
Ok(Some(RevmAccountInfo {
135134
balance: RevmU256::from_limbs(acc_info.balance.0),
136135
nonce: acc_info.nonce,
137136
code_hash: RevmB256::from(acc_info.code_hash.0),
138-
code,
137+
code: Some(code),
139138
}))
140139
}
141140

142141
fn code_by_hash(&mut self, code_hash: RevmB256) -> Result<RevmBytecode, Self::Error> {
143-
<dyn VmDatabase>::get_account_code(self.as_ref(), CoreH256::from(code_hash.as_ref()))?
144-
.map(|b| RevmBytecode::new_raw(RevmBytes(b)))
145-
.ok_or_else(|| EvmError::DB(format!("No code for hash {code_hash}")))
142+
let code =
143+
<dyn VmDatabase>::get_account_code(self.as_ref(), CoreH256::from(code_hash.as_ref()))?;
144+
Ok(RevmBytecode::new_raw(RevmBytes(code)))
146145
}
147146

148147
fn storage(&mut self, address: RevmAddress, index: RevmU256) -> Result<RevmU256, Self::Error> {
@@ -172,21 +171,20 @@ impl revm::DatabaseRef for DynVmDatabase {
172171
None => return Ok(None),
173172
Some(acc_info) => acc_info,
174173
};
175-
let code = <dyn VmDatabase>::get_account_code(self.as_ref(), acc_info.code_hash)?
176-
.map(|b| RevmBytecode::new_raw(RevmBytes(b)));
174+
let code = self.code_by_hash_ref(acc_info.code_hash.0.into())?;
177175

178176
Ok(Some(RevmAccountInfo {
179177
balance: RevmU256::from_limbs(acc_info.balance.0),
180178
nonce: acc_info.nonce,
181179
code_hash: RevmB256::from(acc_info.code_hash.0),
182-
code,
180+
code: Some(code),
183181
}))
184182
}
185183

186184
fn code_by_hash_ref(&self, code_hash: RevmB256) -> Result<RevmBytecode, Self::Error> {
187-
<dyn VmDatabase>::get_account_code(self.as_ref(), CoreH256::from(code_hash.as_ref()))?
188-
.map(|b| RevmBytecode::new_raw(RevmBytes(b)))
189-
.ok_or_else(|| EvmError::DB(format!("No code for hash {code_hash}")))
185+
let code =
186+
<dyn VmDatabase>::get_account_code(self.as_ref(), CoreH256::from(code_hash.as_ref()))?;
187+
Ok(RevmBytecode::new_raw(RevmBytes(code)))
190188
}
191189

192190
fn storage_ref(&self, address: RevmAddress, index: RevmU256) -> Result<RevmU256, Self::Error> {

crates/vm/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pub trait VmDatabase: Send + Sync + DynClone {
1010
fn get_account_info(&self, address: Address) -> Result<Option<AccountInfo>, EvmError>;
1111
fn get_storage_slot(&self, address: Address, key: H256) -> Result<Option<U256>, EvmError>;
1212
fn get_block_hash(&self, block_number: u64) -> Result<H256, EvmError>;
13-
fn get_chain_config(&self) -> ChainConfig;
14-
fn get_account_code(&self, code_hash: H256) -> Result<Option<Bytes>, EvmError>;
13+
fn get_chain_config(&self) -> Result<ChainConfig, EvmError>;
14+
fn get_account_code(&self, code_hash: H256) -> Result<Bytes, EvmError>;
1515
}
1616

1717
dyn_clone::clone_trait_object!(VmDatabase);

crates/vm/levm/src/db/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ pub trait Database: Send + Sync {
1515
fn get_storage_value(&self, address: Address, key: H256) -> Result<U256, DatabaseError>;
1616
fn get_block_hash(&self, block_number: u64) -> Result<H256, DatabaseError>;
1717
fn account_exists(&self, address: Address) -> bool;
18-
fn get_chain_config(&self) -> ChainConfig;
19-
fn get_account_code(&self, code_hash: H256) -> Result<Option<Bytes>, DatabaseError>;
18+
fn get_chain_config(&self) -> Result<ChainConfig, DatabaseError>;
19+
fn get_account_code(&self, code_hash: H256) -> Result<Bytes, DatabaseError>;
2020
}

0 commit comments

Comments
 (0)