Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cheatcode): use rate limit args in create fork cheatcode #6193

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -96,9 +96,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 @@ -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>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have a correspond cli arg for this like

/// Initial retry backoff on encountering errors.
///
/// See --fork-url.
#[clap(long, requires = "fork_url", value_name = "BACKOFF")]
#[serde(skip_serializing_if = "Option::is_none")]
pub fork_retry_backoff: Option<u64>,

so this will also be None


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

Expand Down