Skip to content

Commit

Permalink
feat: adds --auto-impersonate cli cmd (#430)
Browse files Browse the repository at this point in the history
* feat: adds auto-impersonate cli cmd

* chore: add to config setup
  • Loading branch information
dutterbutter authored Nov 26, 2024
1 parent 80982f3 commit 76bdf23
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ pub struct Cli {
/// [default: m/44'/60'/0'/0/]
#[arg(long, help_heading = "Account Configuration")]
pub derivation_path: Option<String>,

/// Enables automatic impersonation on startup. This allows any transaction sender to be
/// simulated as different accounts, which is useful for testing contract behavior.
#[arg(
long,
visible_alias = "auto-unlock",
help_heading = "Account Configuration"
)]
pub auto_impersonate: bool,
}

#[derive(Debug, Subcommand, Clone)]
Expand Down Expand Up @@ -295,6 +304,7 @@ impl Cli {
.with_log_level(self.log)
.with_log_file_path(self.log_file_path.clone())
.with_account_generator(self.account_generator())
.with_auto_impersonate(self.auto_impersonate)
.with_genesis_balance(genesis_balance)
.with_cache_config(self.cache.map(|cache_type| {
match cache_type {
Expand Down
10 changes: 10 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct TestNodeConfig {
pub account_generator: Option<AccountGenerator>,
/// Signer accounts that can sign messages/transactions
pub signer_accounts: Vec<PrivateKeySigner>,
/// Enable auto impersonation of accounts on startup
pub enable_auto_impersonate: bool,
/// Whether the node operates in offline mode
pub offline: bool,
/// The host the server will listen on
Expand Down Expand Up @@ -144,6 +146,7 @@ impl Default for TestNodeConfig {
account_generator: None,
genesis_accounts: genesis_accounts.clone(),
signer_accounts: genesis_accounts,
enable_auto_impersonate: false,
// 100ETH default balance
genesis_balance: U256::from(100u128 * 10u128.pow(18)),

Expand Down Expand Up @@ -628,6 +631,13 @@ impl TestNodeConfig {
.with_genesis_accounts(accounts)
}

/// 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
}

/// Set the offline mode
#[must_use]
pub fn with_offline(mut self, offline: Option<bool>) -> Self {
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ async fn main() -> anyhow::Result<()> {
let fork_details = match command {
Command::Run => {
if config.offline {
tracing::warn!(
"Running in offline mode: default fee parameters will be used. \
To override, specify values in `config.toml` and use the `--config` flag."
);
tracing::warn!("Running in offline mode: default fee parameters will be used.");
None
} else {
// Initialize the client to get the fee params
Expand Down
15 changes: 13 additions & 2 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
f.estimate_gas_scale_factor,
)
};
let impersonation_manager = ImpersonationManager::default();
if config.enable_auto_impersonate {
// Enable auto impersonation if configured
impersonation_manager.set_auto_impersonation(true);
}

InMemoryNodeInner {
time: TimestampManager::new(f.block_timestamp),
Expand All @@ -286,7 +291,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
&updated_config.system_contracts_options,
updated_config.use_evm_emulator,
),
impersonation: Default::default(),
impersonation: impersonation_manager,
rich_accounts: HashSet::new(),
previous_states: Default::default(),
observability,
Expand All @@ -299,6 +304,12 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
blocks.insert(block_hash, create_genesis(NON_FORK_FIRST_BLOCK_TIMESTAMP));
let fee_input_provider = TestNodeFeeInputProvider::default();

let impersonation_manager = ImpersonationManager::default();
if config.enable_auto_impersonate {
// Enable auto impersonation if configured
impersonation_manager.set_auto_impersonation(true);
}

InMemoryNodeInner {
time: TimestampManager::new(NON_FORK_FIRST_BLOCK_TIMESTAMP),
current_batch: 0,
Expand All @@ -321,7 +332,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
&config.system_contracts_options,
config.use_evm_emulator,
),
impersonation: Default::default(),
impersonation: impersonation_manager,
rich_accounts: HashSet::new(),
previous_states: Default::default(),
observability,
Expand Down

0 comments on commit 76bdf23

Please sign in to comment.