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

added anvil_rollback to anvil API provider #1971

Merged
merged 3 commits into from
Jan 31, 2025
Merged
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
32 changes: 32 additions & 0 deletions crates/provider/src/ext/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ pub trait AnvilApi<N: Network>: Send + Sync {
/// Reorg the chain
async fn anvil_reorg(&self, options: ReorgOptions) -> TransportResult<()>;

/// Rollback the chain
async fn anvil_rollback(&self, depth: Option<u64>) -> TransportResult<()>;

/// Execute a transaction regardless of signature status.
async fn eth_send_unsigned_transaction(
&self,
Expand Down Expand Up @@ -310,6 +313,10 @@ where
self.client().request("anvil_reorg", options).await
}

async fn anvil_rollback(&self, depth: Option<u64>) -> TransportResult<()> {
self.client().request("anvil_rollback", (depth,)).await
}

async fn eth_send_unsigned_transaction(
&self,
request: N::TransactionRequest,
Expand Down Expand Up @@ -972,6 +979,31 @@ mod tests {
assert_ne!(reorged_block.header.hash, new_block.header.hash);
}

#[tokio::test]
#[ignore]
async fn test_anvil_rollback() {
let provider = ProviderBuilder::new().on_anvil();

// Mine two blocks
provider.anvil_mine(Some(2), None).await.unwrap();

let target_height = provider
.get_block_by_number(1.into(), BlockTransactionsKind::Hashes)
.await
.unwrap()
.unwrap();

provider.anvil_rollback(Some(1)).await.unwrap();

let new_head = provider
.get_block_by_number(BlockNumberOrTag::Latest, BlockTransactionsKind::Hashes)
.await
.unwrap()
.unwrap();

assert_eq!(target_height, new_head);
}

#[tokio::test]
async fn test_eth_send_unsigned_transaction() {
let provider = ProviderBuilder::new().on_anvil();
Expand Down