Skip to content

Commit

Permalink
refactor(cheatcode): use rate limit args in create fork cheatcode (re…
Browse files Browse the repository at this point in the history
…based)
  • Loading branch information
Evalir committed Nov 1, 2023
1 parent c931b70 commit c76d372
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
13 changes: 13 additions & 0 deletions crates/common/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ impl ProviderBuilder {
self
}

/// How often to retry a failed request. If `None`, defaults to the already-set value.
pub fn maybe_max_retry(mut self, max_retry: Option<u32>) -> Self {
self.max_retry = max_retry.unwrap_or(self.max_retry);
self
}

/// The starting backoff delay to use after the first failed request. If `None`, defaults to
/// the already-set value.
pub fn maybe_initial_backoff(mut self, initial_backoff: Option<u64>) -> Self {
self.initial_backoff = initial_backoff.unwrap_or(self.initial_backoff);
self
}

/// How often to retry a failed request due to connection issues
pub fn timeout_retry(mut self, timeout_retry: u32) -> Self {
self.timeout_retry = timeout_retry;
Expand Down
23 changes: 4 additions & 19 deletions crates/evm/core/src/fork/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,6 @@ pub struct MultiForkHandler {
/// All created Forks in order to reuse them
forks: HashMap<ForkId, CreatedFork>,

/// The retries to allow for new providers
retries: u32,

/// Initial backoff delay for requests
backoff: u64,

/// Optional periodic interval to flush rpc cache
flush_cache_interval: Option<tokio::time::Interval>,
}
Expand All @@ -217,9 +211,6 @@ impl MultiForkHandler {
handlers: Default::default(),
pending_tasks: Default::default(),
forks: Default::default(),
retries: 8,
// 800ms
backoff: 800,
flush_cache_interval: None,
}
}
Expand Down Expand Up @@ -258,10 +249,8 @@ impl MultiForkHandler {
return
}

let retries = self.retries;
let backoff = self.backoff;
// need to create a new fork
let task = Box::pin(create_fork(fork, retries, backoff));
let task = Box::pin(create_fork(fork));
self.pending_tasks.push(ForkTask::Create(task, fork_id, sender, Vec::new()));
}
}
Expand Down Expand Up @@ -459,15 +448,11 @@ fn create_fork_id(url: &str, num: Option<u64>) -> ForkId {
/// Creates a new fork
///
/// This will establish a new `Provider` to the endpoint and return the Fork Backend
async fn create_fork(
mut fork: CreateFork,
retries: u32,
backoff: u64,
) -> eyre::Result<(CreatedFork, Handler)> {
async fn create_fork(mut fork: CreateFork) -> eyre::Result<(CreatedFork, Handler)> {
let provider = Arc::new(
ProviderBuilder::new(fork.url.as_str())
.max_retry(retries)
.initial_backoff(backoff)
.maybe_max_retry(fork.evm_opts.fork_retries)
.maybe_initial_backoff(fork.evm_opts.fork_retry_backoff)
.compute_units_per_second(fork.evm_opts.get_compute_units_per_second())
.build()?,
);
Expand Down
3 changes: 3 additions & 0 deletions crates/evm/core/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct EvmOpts {
/// pins the block number for the state fork
pub fork_block_number: Option<u64>,

/// The number of retries
pub fork_retries: Option<u32>,

/// initial retry backoff
pub fork_retry_backoff: Option<u64>,

Expand Down

0 comments on commit c76d372

Please sign in to comment.