Skip to content

Commit 6fe4a67

Browse files
JereSaloiovoid
andauthored
fix(levm): make account_exists() return a result (#2983)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> - The goal is to return an error if there's a problem when accessing the database instead of panicking. **Description** <!-- A clear and concise general description of the changes this PR introduces --> <!-- Link to issues: Resolves #111, Resolves #222 --> Co-authored-by: Lucas Fiegl <[email protected]>
1 parent d202d1d commit 6fe4a67

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

cmd/ethrex_replay/src/rpc/db.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl RpcDB {
412412
}
413413

414414
impl LevmDatabase for RpcDB {
415-
fn account_exists(&self, address: Address) -> bool {
415+
fn account_exists(&self, address: Address) -> Result<bool, DatabaseError> {
416416
// look into the cache
417417
{
418418
if self
@@ -422,11 +422,12 @@ impl LevmDatabase for RpcDB {
422422
.get(&address)
423423
.is_some_and(|account| matches!(account, Account::Existing { .. }))
424424
{
425-
return true;
425+
return Ok(true);
426426
}
427427
}
428-
self.fetch_account_blocking(address, &[], false)
429-
.is_ok_and(|account| matches!(account, Account::Existing { .. }))
428+
Ok(self
429+
.fetch_account_blocking(address, &[], false)
430+
.is_ok_and(|account| matches!(account, Account::Existing { .. })))
430431
}
431432

432433
fn get_account_code(&self, _code_hash: H256) -> Result<Bytes, DatabaseError> {

crates/vm/backends/levm/db.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ impl LevmDatabase for DatabaseLogger {
4444
.get_account(address)
4545
}
4646

47-
fn account_exists(&self, address: CoreAddress) -> bool {
48-
self.store.lock().unwrap().account_exists(address)
47+
fn account_exists(&self, address: CoreAddress) -> Result<bool, DatabaseError> {
48+
self.store
49+
.lock()
50+
.map_err(|_| DatabaseError::Custom("Could not lock mutex".to_string()))?
51+
.account_exists(address)
4952
}
5053

5154
fn get_storage_value(
@@ -114,9 +117,10 @@ impl LevmDatabase for DynVmDatabase {
114117
))
115118
}
116119

117-
fn account_exists(&self, address: CoreAddress) -> bool {
118-
let acc_info = <dyn VmDatabase>::get_account_info(self.as_ref(), address).unwrap();
119-
acc_info.is_some()
120+
fn account_exists(&self, address: CoreAddress) -> Result<bool, DatabaseError> {
121+
let acc_info = <dyn VmDatabase>::get_account_info(self.as_ref(), address)
122+
.map_err(|e| DatabaseError::Custom(e.to_string()))?;
123+
Ok(acc_info.is_some())
120124
}
121125

122126
fn get_storage_value(
@@ -172,8 +176,8 @@ impl LevmDatabase for ProverDB {
172176
))
173177
}
174178

175-
fn account_exists(&self, address: CoreAddress) -> bool {
176-
self.accounts.contains_key(&address)
179+
fn account_exists(&self, address: CoreAddress) -> Result<bool, DatabaseError> {
180+
Ok(self.accounts.contains_key(&address))
177181
}
178182

179183
fn get_block_hash(&self, block_number: u64) -> Result<CoreH256, DatabaseError> {

crates/vm/backends/levm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl LEVM {
260260
// If account is now empty and it didn't exist in the trie before, no need to make changes.
261261
// This check is necessary just in case we keep empty accounts in the trie. If we're sure we don't, this code can be removed.
262262
// `initial_state_account.is_empty()` check can be removed but I added it because it's short-circuiting, this way we don't hit the db very often.
263-
if removed && initial_state_account.is_empty() && !db.store.account_exists(*address) {
263+
if removed && initial_state_account.is_empty() && !db.store.account_exists(*address)? {
264264
continue;
265265
}
266266

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait Database: Send + Sync {
1414
fn get_account(&self, address: Address) -> Result<Account, DatabaseError>;
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>;
17-
fn account_exists(&self, address: Address) -> bool;
17+
fn account_exists(&self, address: Address) -> Result<bool, DatabaseError>;
1818
fn get_chain_config(&self) -> Result<ChainConfig, DatabaseError>;
1919
fn get_account_code(&self, code_hash: H256) -> Result<Bytes, DatabaseError>;
2020
}

0 commit comments

Comments
 (0)