forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 6
anvil-polkadot: add chain reversion RPCs #336
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
Merged
+1,238
−429
Merged
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
d1ae4c4
anvil-polkadot: add snapshot manager & evm snapshot RPC
iulianbarbu 22d3d36
cargo: format toml & lock
iulianbarbu afcadaa
Revert "cargo: format toml & lock"
iulianbarbu 2725161
evm_revert: wip debug test failure
iulianbarbu 486d13d
anvil-polkadot: seal best blocks only
iulianbarbu 6707ca8
anvil-polkadot: test snapshot and revert
iulianbarbu f3376ab
tomls: update formatting and polkadot-sdk dep
iulianbarbu 6728dc5
anvil-polkadot: revert uneeded changes
iulianbarbu 22050bf
Merge branch 'master' of github.com:paritytech/foundry-polkadot into …
iulianbarbu 877fff8
Update crates/revive-env/Cargo.toml
iulianbarbu aaba52f
Update crates/revive-strategy/Cargo.toml
iulianbarbu bbc1731
anvil-polkadot: update block provider best block after revert
iulianbarbu 43b980f
anvil-polkadot: set db pruning to archive
iulianbarbu 9b1deca
anvil-polkadot: impl rollback & add tests
iulianbarbu d8098d1
anvil-polkadot(tests): add todos for block provider testing
iulianbarbu 5b32dc1
anvil-polkadot(tests): finalize existing tests
iulianbarbu c93d1ae
anvil-polkadot: update evm_revert & anvil-rollback return type
iulianbarbu 3d46f1e
anvil-polkadot(tests): add revert and txs in mempool test
iulianbarbu a3b01e2
anvil-polkadot: code polish
iulianbarbu 0f720de
Merge branch 'master' of github.com:paritytech/foundry-polkadot into …
iulianbarbu 63a25f9
anvil-polkadot(tests): fix timestmap with evm revert test
iulianbarbu e6173bd
anvil-polkadot(tests): final polish
iulianbarbu 37debaa
anvil-polkadot: remove leftovers
iulianbarbu b5b641b
Merge branch 'master' of github.com:paritytech/foundry-polkadot into …
iulianbarbu dcacce6
anvil-polkadot: fix merge issues
iulianbarbu b9ad0f0
anvil-polkadot: simplify time updating after revert
iulianbarbu 75599db
anvil-polkadot: remove extra timestamp setting in storage
iulianbarbu 9801c35
Merge branch 'master' of github.com:paritytech/foundry-polkadot into …
iulianbarbu dbb8204
anvil-polkadot: address leftovers after merge
iulianbarbu 4b5e922
Apply suggestion from @iulianbarbu
iulianbarbu cd2dbd8
Apply suggestion from @iulianbarbu
iulianbarbu 0b99c7d
anvil-polkadot: revert new backend error variants
iulianbarbu 4e7b0cc
Update crates/anvil-polkadot/src/substrate_node/service/backend.rs
iulianbarbu 22984b0
anvil-polkadot: address feedback
iulianbarbu 6f56dc4
Update crates/anvil-polkadot/src/substrate_node/snapshot.rs
iulianbarbu 04b5f0b
Update crates/anvil-polkadot/src/substrate_node/snapshot.rs
iulianbarbu 4f0aa31
anvil-polkadot(tests): address feedback
iulianbarbu aab7fa5
Merge branch 'master' into ib-add-chain-reversion
iulianbarbu fb484e0
anvil-polkadot(tests): fix merge issue
iulianbarbu 6e56070
anvil-polkadot: address feedback
iulianbarbu 2b6fc22
anvil-polkadot(tests): simplify evm_snapshot result assert
iulianbarbu b57c04f
doc: fix clippy
iulianbarbu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ pub mod impersonation; | |
| pub mod in_mem_rpc; | ||
| pub mod mining_engine; | ||
| pub mod service; | ||
| pub mod snapshot; | ||
This file contains hidden or 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 hidden or 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,84 @@ | ||
| use crate::substrate_node::service::Backend; | ||
| use alloy_primitives::U256; | ||
| use polkadot_sdk::{ | ||
| sc_client_api::Backend as BackendT, | ||
| sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata, Result}, | ||
| }; | ||
| use std::{collections::BTreeMap, sync::Arc}; | ||
| use substrate_runtime::OpaqueBlock; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct Snapshot { | ||
iulianbarbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub best_number: u64, | ||
| } | ||
|
|
||
| pub struct SnapshotManager<C> { | ||
| client: Arc<C>, | ||
| backend: Arc<Backend>, | ||
| next_snapshot_id: U256, | ||
| snapshots: BTreeMap<U256, Snapshot>, | ||
| } | ||
|
|
||
| impl<C> SnapshotManager<C> { | ||
| pub fn new(client: Arc<C>, backend: Arc<Backend>, genesis_block_number: u64) -> Self { | ||
| let snapshot = Snapshot { best_number: genesis_block_number }; | ||
| let mut map = BTreeMap::new(); | ||
| map.insert(U256::ZERO, snapshot); | ||
|
|
||
| Self { | ||
| client, | ||
| backend, | ||
| // Start with 1 to mimic Ganache | ||
| next_snapshot_id: U256::ONE, | ||
| snapshots: map, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<C> SnapshotManager<C> | ||
| where | ||
| C: HeaderBackend<OpaqueBlock> | ||
| + HeaderMetadata<OpaqueBlock, Error = BlockChainError> | ||
| + Send | ||
| + Sync | ||
| + 'static, | ||
| { | ||
| /// Create a snapshot id corresponding to the best block number. | ||
| pub fn snapshot(&mut self) -> U256 { | ||
| let one = U256::ONE; | ||
| self.next_snapshot_id += one; | ||
iulianbarbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let snapshot = Snapshot { best_number: self.client.info().best_number.into() }; | ||
| self.snapshots.insert(self.next_snapshot_id - one, snapshot); | ||
| // Safe since there is always a snapshot representing genesis. | ||
| self.next_snapshot_id - one | ||
| } | ||
|
|
||
| /// Revert the chain to the block number represented by the snapshot `id`. | ||
| pub fn revert(&mut self, id: U256) -> Result<bool> { | ||
| // Remove the snapshot when reverting. We do not want to keep it around | ||
| // since reverting to an existing snapshot could mean going back to future, | ||
| // which is not supported. | ||
iulianbarbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let maybe_snapshot = self.snapshots.remove(&id); | ||
| let Some(snap) = maybe_snapshot else { return Ok(false) }; | ||
|
|
||
| let current_best_number: u64 = self.client.info().best_number.into(); | ||
| let number_of_blocks_to_revert = current_best_number - snap.best_number; | ||
|
|
||
| self.backend.revert( | ||
| number_of_blocks_to_revert.try_into().expect("to not surpass u32 bounds"), | ||
| true, | ||
| )?; | ||
|
|
||
| self.snapshots | ||
| .retain(|&k, snap_to_remove| k < id || snap_to_remove.best_number >= snap.best_number); | ||
|
|
||
| Ok(true) | ||
| } | ||
|
|
||
iulianbarbu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub fn rollback(&self) -> Result<bool> { | ||
| let current_best_number = self.client.info().best_number; | ||
| self.backend.revert(current_best_number, true)?; | ||
|
|
||
| Ok(true) | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| mod impersonation; | ||
| mod mining; | ||
| mod snapshot; | ||
| mod standard_rpc; | ||
| mod time_machine; | ||
| mod utils; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.