Skip to content

Commit

Permalink
feat(anvil): Anvil --unlocked for auto-impersonation on instance cr…
Browse files Browse the repository at this point in the history
…eation (#5335)

* add config and cli cmd fields

* rename flag

* add call to backend

* Update anvil/src/lib.rs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
Sabnock01 and mattsse authored Jul 8, 2023
1 parent 32e8e83 commit 60e1a74
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ pub enum EthRequest {
with = "sequence"
)
)]
/// Will make every account impersonated
StopImpersonatingAccount(Address),
/// Will make every account impersonated
#[cfg_attr(
feature = "serde",
serde(
Expand Down
5 changes: 5 additions & 0 deletions anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl NodeArgs {
.with_transaction_order(self.order)
.with_genesis(self.init)
.with_steps_tracing(self.evm_opts.steps_tracing)
.with_auto_impersonate(self.evm_opts.auto_impersonate)
.with_ipc(self.ipc)
.with_code_size_limit(self.evm_opts.code_size_limit)
.set_pruned_history(self.prune_history)
Expand Down Expand Up @@ -442,6 +443,10 @@ pub struct AnvilEvmArgs {
/// Enable steps tracing used for debug calls returning geth-style traces
#[clap(long, visible_alias = "tracing")]
pub steps_tracing: bool,

/// Enable autoImpersonate on startup
#[clap(long, visible_alias = "auto-impersonate")]
pub auto_impersonate: bool,
}

/// Resolves an alias passed as fork-url to the matching url defined in the rpc_endpoints section
Expand Down
10 changes: 10 additions & 0 deletions anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub struct NodeConfig {
pub ipc_path: Option<Option<String>>,
/// Enable transaction/call steps tracing for debug calls returning geth-style traces
pub enable_steps_tracing: bool,
/// Enable auto impersonation of accounts on startup
pub enable_auto_impersonate: bool,
/// Configure the code size limit
pub code_size_limit: Option<usize>,
/// Configures how to remove historic state.
Expand Down Expand Up @@ -374,6 +376,7 @@ impl Default for NodeConfig {
base_fee: None,
enable_tracing: true,
enable_steps_tracing: false,
enable_auto_impersonate: false,
no_storage_caching: false,
server_config: Default::default(),
host: None,
Expand Down Expand Up @@ -699,6 +702,13 @@ impl NodeConfig {
self
}

/// Sets whether to enable autoImpersonate
#[must_use]
pub fn with_auto_impersonate(mut self, enable_auto_impersonate: bool) -> Self {
self.enable_auto_impersonate = enable_auto_impersonate;
self
}

#[must_use]
pub fn with_server_config(mut self, config: ServerConfig) -> Self {
self.server_config = config;
Expand Down
2 changes: 1 addition & 1 deletion anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ impl EthApi {
/// Handler for ETH RPC call: `anvil_autoImpersonateAccount`
pub async fn anvil_auto_impersonate_account(&self, enabled: bool) -> Result<()> {
node_info!("anvil_autoImpersonateAccount");
self.backend.auto_impersonate_account(enabled).await?;
self.backend.auto_impersonate_account(enabled).await;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ impl Backend {
}

/// If set to true will make every account impersonated
pub async fn auto_impersonate_account(&self, enabled: bool) -> DatabaseResult<()> {
pub async fn auto_impersonate_account(&self, enabled: bool) {
self.cheats.set_auto_impersonate_account(enabled);
Ok(())
}

/// Returns the configured fork, if any
Expand Down
4 changes: 4 additions & 0 deletions anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub async fn spawn(mut config: NodeConfig) -> (EthApi, NodeHandle) {

let backend = Arc::new(config.setup().await);

if config.enable_auto_impersonate {
backend.auto_impersonate_account(true).await;
}

let fork = backend.get_fork().cloned();

let NodeConfig {
Expand Down

0 comments on commit 60e1a74

Please sign in to comment.