Skip to content

Commit

Permalink
refactor(cheatcode): use rate limit args in create fork cheatcode (#6193
Browse files Browse the repository at this point in the history
)

* refactor(cheatcode): use rate limit args in create fork cheatcode (rebased)

* chore: add cli opt for fork-retries

* fmt

* chore: update provider defaults
  • Loading branch information
Evalir authored Nov 2, 2023
1 parent 3546f16 commit 9421571
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
7 changes: 7 additions & 0 deletions crates/common/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ pub struct EvmArgs {
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_block_number: Option<u64>,

/// Number of retries.
///
/// See --fork-url.
#[clap(long, requires = "fork_url", value_name = "RETRIES")]
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_retries: Option<u32>,

/// Initial retry backoff on encountering errors.
///
/// See --fork-url.
Expand Down
19 changes: 16 additions & 3 deletions crates/common/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ impl ProviderBuilder {
Self {
url,
chain: Chain::Mainnet,
max_retry: 100,
timeout_retry: 5,
initial_backoff: 100,
max_retry: 8,
timeout_retry: 8,
initial_backoff: 800,
timeout: REQUEST_TIMEOUT,
// alchemy max cpus <https://github.com/alchemyplatform/alchemy-docs/blob/master/documentation/compute-units.md#rate-limits-cups>
compute_units_per_second: ALCHEMY_FREE_TIER_CUPS,
Expand Down Expand Up @@ -132,6 +132,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 9421571

Please sign in to comment.