Skip to content

Commit

Permalink
chore: improve DatabaseError error messages (#7183)
Browse files Browse the repository at this point in the history
* chore: improve DatabaseError error messages

* chore: pass Address by reference
  • Loading branch information
DaniPopes authored Feb 19, 2024
1 parent 107dc41 commit a436a0d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl Cheatcodes {

// ensure the caller is allowed to execute cheatcodes,
// but only if the backend is in forking mode
data.db.ensure_cheatcode_access_forking_mode(caller)?;
data.db.ensure_cheatcode_access_forking_mode(&caller)?;

apply_dispatch(&decoded, &mut CheatsCtxt { state: self, data, caller })
}
Expand All @@ -255,7 +255,7 @@ impl Cheatcodes {
.unwrap_or_default();
let created_address = inputs.created_address(old_nonce);

if data.journaled_state.depth > 1 && !data.db.has_cheatcode_access(inputs.caller) {
if data.journaled_state.depth > 1 && !data.db.has_cheatcode_access(&inputs.caller) {
// we only grant cheat code access for new contracts if the caller also has
// cheatcode access and the new contract is created in top most call
return created_address;
Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Cheatcode for serializeBytes32_0Call {
impl Cheatcode for serializeString_0Call {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { objectKey, valueKey, value } = self;
serialize_json(state, objectKey, Some(valueKey), &value.to_string())
serialize_json(state, objectKey, Some(valueKey), value)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/evm/core/src/backend/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub type DatabaseResult<T> = Result<T, DatabaseError>;
pub enum DatabaseError {
#[error("{0}")]
Message(String),
#[error("no cheats available for {0}")]
#[error("cheatcodes are not enabled for {0}; see `vm.allowCheatcodes(address)`")]
NoCheats(Address),
#[error("failed to fetch AccountInfo {0}")]
#[error("failed to fetch account info for {0}")]
MissingAccount(Address),
#[error("code should already be loaded: {0}")]
#[error("missing bytecode for code hash {0}")]
MissingCode(B256),
#[error(transparent)]
Recv(#[from] RecvError),
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/core/src/backend/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ impl<'a> DatabaseExt for FuzzBackendWrapper<'a> {
self.backend.to_mut().allow_cheatcode_access(account)
}

fn revoke_cheatcode_access(&mut self, account: Address) -> bool {
fn revoke_cheatcode_access(&mut self, account: &Address) -> bool {
self.backend.to_mut().revoke_cheatcode_access(account)
}

fn has_cheatcode_access(&self, account: Address) -> bool {
fn has_cheatcode_access(&self, account: &Address) -> bool {
self.backend.has_cheatcode_access(account)
}
}
Expand Down
30 changes: 15 additions & 15 deletions crates/evm/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,24 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
/// Revokes cheatcode access for the given account
///
/// Returns true if the `account` was previously allowed cheatcode access
fn revoke_cheatcode_access(&mut self, account: Address) -> bool;
fn revoke_cheatcode_access(&mut self, account: &Address) -> bool;

/// Returns `true` if the given account is allowed to execute cheatcodes
fn has_cheatcode_access(&self, account: Address) -> bool;
fn has_cheatcode_access(&self, account: &Address) -> bool;

/// Ensures that `account` is allowed to execute cheatcodes
///
/// Returns an error if [`Self::has_cheatcode_access`] returns `false`
fn ensure_cheatcode_access(&self, account: Address) -> Result<(), DatabaseError> {
fn ensure_cheatcode_access(&self, account: &Address) -> Result<(), DatabaseError> {
if !self.has_cheatcode_access(account) {
return Err(DatabaseError::NoCheats(account));
return Err(DatabaseError::NoCheats(*account));
}
Ok(())
}

/// Same as [`Self::ensure_cheatcode_access()`] but only enforces it if the backend is currently
/// in forking mode
fn ensure_cheatcode_access_forking_mode(&self, account: Address) -> Result<(), DatabaseError> {
fn ensure_cheatcode_access_forking_mode(&self, account: &Address) -> Result<(), DatabaseError> {
if self.is_forked_mode() {
return self.ensure_cheatcode_access(account);
}
Expand Down Expand Up @@ -531,7 +531,7 @@ impl Backend {
// toggle the previous sender
if let Some(current) = self.inner.test_contract_address.take() {
self.remove_persistent_account(&current);
self.revoke_cheatcode_access(acc);
self.revoke_cheatcode_access(&acc);
}

self.add_persistent_account(acc);
Expand Down Expand Up @@ -1363,32 +1363,32 @@ impl DatabaseExt for Backend {
Ok(())
}

fn is_persistent(&self, acc: &Address) -> bool {
self.inner.persistent_accounts.contains(acc)
fn add_persistent_account(&mut self, account: Address) -> bool {
trace!(?account, "add persistent account");
self.inner.persistent_accounts.insert(account)
}

fn remove_persistent_account(&mut self, account: &Address) -> bool {
trace!(?account, "remove persistent account");
self.inner.persistent_accounts.remove(account)
}

fn add_persistent_account(&mut self, account: Address) -> bool {
trace!(?account, "add persistent account");
self.inner.persistent_accounts.insert(account)
fn is_persistent(&self, acc: &Address) -> bool {
self.inner.persistent_accounts.contains(acc)
}

fn allow_cheatcode_access(&mut self, account: Address) -> bool {
trace!(?account, "allow cheatcode access");
self.inner.cheatcode_access_accounts.insert(account)
}

fn revoke_cheatcode_access(&mut self, account: Address) -> bool {
fn revoke_cheatcode_access(&mut self, account: &Address) -> bool {
trace!(?account, "revoke cheatcode access");
self.inner.cheatcode_access_accounts.remove(&account)
self.inner.cheatcode_access_accounts.remove(account)
}

fn has_cheatcode_access(&self, account: Address) -> bool {
self.inner.cheatcode_access_accounts.contains(&account)
fn has_cheatcode_access(&self, account: &Address) -> bool {
self.inner.cheatcode_access_accounts.contains(account)
}
}

Expand Down

0 comments on commit a436a0d

Please sign in to comment.