-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move impersonation management into its own component (#391)
* move impersonation state into its own subcomponent * use frozen lockfile for contracts
- Loading branch information
Showing
6 changed files
with
111 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::collections::HashSet; | ||
use std::sync::{Arc, RwLock}; | ||
use zksync_types::Address; | ||
|
||
/// Manages impersonated accounts across the system. | ||
/// | ||
/// Clones always agree on the set of impersonated accounts and updating one affects all other | ||
/// instances. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct ImpersonationManager { | ||
state: Arc<RwLock<HashSet<Address>>>, | ||
} | ||
|
||
impl ImpersonationManager { | ||
/// Starts impersonation for the provided account. | ||
/// | ||
/// Returns `true` if the account was not impersonated before. | ||
pub fn impersonate(&self, addr: Address) -> bool { | ||
tracing::trace!(?addr, "start impersonation"); | ||
let mut state = self | ||
.state | ||
.write() | ||
.expect("ImpersonationManager lock is poisoned"); | ||
state.insert(addr) | ||
} | ||
|
||
/// Stops impersonation for the provided account. | ||
/// | ||
/// Returns `true` if the account was impersonated before. | ||
pub fn stop_impersonating(&self, addr: &Address) -> bool { | ||
tracing::trace!(?addr, "stop impersonation"); | ||
self.state | ||
.write() | ||
.expect("ImpersonationManager lock is poisoned") | ||
.remove(addr) | ||
} | ||
|
||
/// Returns whether the provided account is currently impersonated. | ||
pub fn is_impersonating(&self, addr: &Address) -> bool { | ||
self.state | ||
.read() | ||
.expect("ImpersonationManager lock is poisoned") | ||
.contains(addr) | ||
} | ||
|
||
/// Returns all accounts that are currently being impersonated. | ||
pub fn impersonated_accounts(&self) -> HashSet<Address> { | ||
self.state | ||
.read() | ||
.expect("ImpersonationManager lock is poisoned") | ||
.clone() | ||
} | ||
|
||
/// Overrides currently impersonated accounts with the provided value. | ||
pub fn set_impersonated_accounts(&self, accounts: HashSet<Address>) { | ||
*self | ||
.state | ||
.write() | ||
.expect("ImpersonationManager lock is poisoned") = accounts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters