Skip to content

Commit 4cd6ad4

Browse files
phrwlk0xrusowskygrandizzyDaniPopes
authored
fix(cheats): correct impersonate() return semantics to match documentation (#12309)
* fix(cheats): correct impersonate() return semantics to match documentation * clean --------- Co-authored-by: 0xrusowsky <[email protected]> Co-authored-by: grandizzy <[email protected]> Co-authored-by: DaniPopes <[email protected]>
1 parent f4d6789 commit 4cd6ad4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

crates/anvil/src/eth/backend/cheats.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@ impl CheatsManager {
3030
///
3131
/// Returns `true` if the account is already impersonated
3232
pub fn impersonate(&self, addr: Address) -> bool {
33-
trace!(target: "cheats", "Start impersonating {:?}", addr);
34-
let mut state = self.state.write();
33+
trace!(target: "cheats", %addr, "start impersonating");
3534
// When somebody **explicitly** impersonates an account we need to store it so we are able
3635
// to return it from `eth_accounts`. That's why we do not simply call `is_impersonated()`
3736
// which does not check that list when auto impersonation is enabled.
38-
if state.impersonated_accounts.contains(&addr) {
39-
// need to check if already impersonated, so we don't overwrite the code
40-
return true;
41-
}
42-
state.impersonated_accounts.insert(addr)
37+
!self.state.write().impersonated_accounts.insert(addr)
4338
}
4439

4540
/// Removes the account that from the impersonated set
4641
pub fn stop_impersonating(&self, addr: &Address) {
47-
trace!(target: "cheats", "Stop impersonating {:?}", addr);
42+
trace!(target: "cheats", %addr, "stop impersonating");
4843
self.state.write().impersonated_accounts.remove(addr);
4944
}
5045

@@ -145,3 +140,16 @@ impl Precompile for CheatEcrecover {
145140
pub struct CheatEcrecover {
146141
cheats: Arc<CheatsManager>,
147142
}
143+
144+
#[cfg(test)]
145+
mod tests {
146+
use super::*;
147+
148+
#[test]
149+
fn impersonate_returns_false_then_true() {
150+
let mgr = CheatsManager::default();
151+
let addr = Address::from([1u8; 20]);
152+
assert!(!mgr.impersonate(addr));
153+
assert!(mgr.impersonate(addr));
154+
}
155+
}

0 commit comments

Comments
 (0)