-
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 latest block hash to change. When it does, we read backwards from the latest block 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 fef2b62
Showing
26 changed files
with
1,273 additions
and
1,366 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
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,53 @@ | ||
use std::time::Duration; | ||
|
||
use ethers::types::{Block, BlockNumber, H256}; | ||
use tokio::time; | ||
use tracing::error; | ||
|
||
use crate::common::{retry, retry::UnlimitedRetryOpts, types::ProviderLike}; | ||
|
||
pub async fn wait_for_new_block( | ||
provider: &impl ProviderLike, | ||
last_block_hash: H256, | ||
poll_interval: Duration, | ||
) -> (H256, Block<H256>) { | ||
loop { | ||
let block = retry::with_unlimited_retries( | ||
"watch latest block", | ||
|| provider.get_block(BlockNumber::Latest), | ||
UnlimitedRetryOpts::default(), | ||
) | ||
.await; | ||
let Some(block) = block else { | ||
error!("Latest block should be present when waiting for new block."); | ||
continue; | ||
}; | ||
let Some(hash) = block.hash else { | ||
error!("Latest block should have hash."); | ||
continue; | ||
}; | ||
if last_block_hash != hash { | ||
return (hash, block); | ||
} | ||
time::sleep(poll_interval).await; | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.