Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jul 14, 2022
1 parent 0a47717 commit 55a4d6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion evm/src/executor/backend/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a> DatabaseExt for FuzzBackendWrapper<'a> {
&mut self,
env: &mut Env,
block_number: U256,
id: Option<U256>,
id: Option<LocalForkId>,
) -> eyre::Result<()> {
self.backend.to_mut().roll_fork(env, block_number, id)
}
Expand Down
32 changes: 20 additions & 12 deletions evm/src/executor/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ pub trait DatabaseExt: Database {
fork: CreateFork,
env: &mut Env,
subroutine: &mut SubRoutine,
) -> eyre::Result<U256> {
) -> eyre::Result<LocalForkId> {
let id = self.create_fork(fork, subroutine)?;
self.select_fork(id, env, subroutine)?;
Ok(id)
}

/// Creates a new fork but does _not_ select it
fn create_fork(&mut self, fork: CreateFork, subroutine: &SubRoutine) -> eyre::Result<U256>;
fn create_fork(
&mut self,
fork: CreateFork,
subroutine: &SubRoutine,
) -> eyre::Result<LocalForkId>;

/// Selects the fork's state
///
Expand All @@ -83,7 +87,7 @@ pub trait DatabaseExt: Database {
/// Returns an error if no fork with the given `id` exists
fn select_fork(
&mut self,
id: U256,
id: LocalForkId,
env: &mut Env,
subroutine: &mut SubRoutine,
) -> eyre::Result<()>;
Expand All @@ -99,7 +103,7 @@ pub trait DatabaseExt: Database {
&mut self,
env: &mut Env,
block_number: U256,
id: Option<U256>,
id: Option<LocalForkId>,
) -> eyre::Result<()>;

/// Returns the `ForkId` that's currently used in the database, if fork mode is on
Expand All @@ -115,10 +119,10 @@ pub trait DatabaseExt: Database {
/// Returns an error if the given `id` does not match any forks
///
/// Returns an error if no fork exits
fn ensure_fork(&self, id: Option<U256>) -> eyre::Result<U256>;
fn ensure_fork(&self, id: Option<LocalForkId>) -> eyre::Result<LocalForkId>;

/// Ensures that a corresponding `ForkId` exists for the given local `id`
fn ensure_fork_id(&self, id: U256) -> eyre::Result<&ForkId>;
fn ensure_fork_id(&self, id: LocalForkId) -> eyre::Result<&ForkId>;
}

/// Provides the underlying `revm::Database` implementation.
Expand All @@ -140,11 +144,13 @@ pub trait DatabaseExt: Database {
/// (`Backend::clone`). This way each contract uses its own encapsulated evm state. For in-memory
/// testing, the database is just an owned `revm::InMemoryDB`.
///
/// The `db` if fork-mode basically consists of 2 halves:
/// Each `Fork`, identified by a unique id, uses completely separate storage, write operations are
/// performed only in the fork's own database, `ForkDB`.
///
/// A `ForkDB` consists of 2 halves:
/// - everything fetched from the remote is readonly
/// - all local changes (instructed by the contract) are written to the backend's `db` and don't
/// alter the state of the remote client. This way a fork (`SharedBackend`), can be used by
/// multiple contracts at the same time.
/// alter the state of the remote client.
///
/// # Fork swapping
///
Expand All @@ -154,8 +160,10 @@ pub trait DatabaseExt: Database {
/// When swapping forks (`Backend::select_fork()`) we also update the current `Env` of the `EVM`
/// accordingly, so that all `block.*` config values match
///
/// **Note:** this only affects the readonly half of the `db`, local changes are persistent across
/// fork-state swaps.
/// When another for is selected [`DatabaseExt::select_fork()`] the entire storage, including
/// `Subroutine` is swapped, but the storage of the caller's and the test contract account is
/// _always_ cloned. This way a fork has entirely separate storage but data can still be shared
/// across fork boundaries via stack and contract variables.
///
/// # Snapshotting
///
Expand Down Expand Up @@ -708,7 +716,7 @@ pub struct BackendInner {
/// Tracks the caller of the test function
pub caller: Option<Address>,
/// Tracks numeric identifiers for forks
pub next_fork_id: U256,
pub next_fork_id: LocalForkId,
}

// === impl BackendInner ===
Expand Down

0 comments on commit 55a4d6d

Please sign in to comment.