-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oppool): Return reorged ops to the mempool
Previously, ops could be lost forever if they were mined into a block, but then that block got reorged away. Now, we detect reorgs and return any ops contained in them to our mempool. This requires some involved changes: * We replace the entire `events` mod and all its listeners. Instead, we introduce a new type, `Chain`, which represents our current knowledge of what blocks make up the blockchain. * We watch for the block number to go up. When it does (possibly by more than one at once), we read backwards from the latest number until we connect back to our known blocks, which lets us see any blocks that were replaced as well. * This process produces a `ChainUpdate` event, which is sent to the op pool (replacing `NewBlockEvent`). This may cause the op pool to not only remove mined ops but also restore unmined ops. * In order for the op pool to do so, it remembers mined ops for a time rather than deleting them fully. We add new methods to the op pool for restoring unmined blocks to make this possible.
- Loading branch information
1 parent
716844a
commit b81df53
Showing
26 changed files
with
802 additions
and
1,281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::time::Duration; | ||
|
||
use tokio::time; | ||
|
||
use crate::common::{retry, retry::UnlimitedRetryOpts, types::ProviderLike}; | ||
|
||
pub async fn wait_for_new_block_number( | ||
provider: &impl ProviderLike, | ||
last_block_number: u64, | ||
poll_interval: Duration, | ||
) -> u64 { | ||
loop { | ||
let block_number = retry::with_unlimited_retries( | ||
"watch latest block number", | ||
|| provider.get_block_number(), | ||
UnlimitedRetryOpts::default(), | ||
) | ||
.await; | ||
if last_block_number < block_number { | ||
return block_number; | ||
} | ||
time::sleep(poll_interval).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::{future::Future, time::Duration}; | ||
|
||
use rand::Rng; | ||
use tokio::time; | ||
use tracing::warn; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct RetryOpts { | ||
pub max_attempts: u64, | ||
/// The first retry is immediately after the first failure (plus jitter). | ||
/// The next retry after that will wait this long. | ||
pub min_nonzero_wait: Duration, | ||
pub max_wait: Duration, | ||
pub max_jitter: Duration, | ||
} | ||
|
||
impl Default for RetryOpts { | ||
fn default() -> Self { | ||
UnlimitedRetryOpts::default().to_retry_opts_with_max_attempts(10) | ||
} | ||
} | ||
|
||
pub async fn with_retries<Func, Fut, Out, Err>( | ||
description: &str, | ||
func: Func, | ||
opts: RetryOpts, | ||
) -> Result<Out, Err> | ||
where | ||
Func: Fn() -> Fut, | ||
Fut: Future<Output = Result<Out, Err>>, | ||
{ | ||
let mut next_wait = Duration::ZERO; | ||
let mut last_error: Option<Err> = None; | ||
for attempt_number in 1..opts.max_attempts + 1 { | ||
match func().await { | ||
Ok(out) => return Ok(out), | ||
Err(error) => { | ||
last_error = Some(error); | ||
warn!("Failed to {description} (attempt {attempt_number})"); | ||
} | ||
} | ||
// Grab a new rng each iteration because we can't hold it across awaits. | ||
let jitter = rand::thread_rng().gen_range(Duration::ZERO..opts.max_jitter); | ||
time::sleep(next_wait + jitter).await; | ||
next_wait = (2 * next_wait).clamp(opts.min_nonzero_wait, opts.max_wait); | ||
} | ||
Err(last_error.unwrap()) | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct UnlimitedRetryOpts { | ||
pub min_nonzero_wait: Duration, | ||
pub max_wait: Duration, | ||
pub max_jitter: Duration, | ||
} | ||
|
||
impl Default for UnlimitedRetryOpts { | ||
fn default() -> Self { | ||
Self { | ||
min_nonzero_wait: Duration::from_secs(1), | ||
max_wait: Duration::from_secs(10), | ||
max_jitter: Duration::from_secs(1), | ||
} | ||
} | ||
} | ||
|
||
impl UnlimitedRetryOpts { | ||
fn to_retry_opts_with_max_attempts(self, max_attempts: u64) -> RetryOpts { | ||
RetryOpts { | ||
max_attempts, | ||
min_nonzero_wait: self.min_nonzero_wait, | ||
max_wait: self.max_wait, | ||
max_jitter: self.max_jitter, | ||
} | ||
} | ||
} | ||
|
||
pub async fn with_unlimited_retries<Func, Fut, Out, Err>( | ||
description: &str, | ||
func: Func, | ||
opts: UnlimitedRetryOpts, | ||
) -> Out | ||
where | ||
Func: Fn() -> Fut, | ||
Fut: Future<Output = Result<Out, Err>>, | ||
{ | ||
let opts = opts.to_retry_opts_with_max_attempts(u64::MAX); | ||
with_retries(description, func, opts).await.ok().unwrap() | ||
} |
Oops, something went wrong.