Skip to content

Commit d277942

Browse files
committed
refactor: error and transacted are states
1 parent 623753a commit d277942

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/evm.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,22 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmNeedsT
873873
) -> Result<EvmTransacted<'a, Ext, Db, C>, EvmErrored<'a, Ext, Db, C>> {
874874
self.fill_tx(filler).execute_tx()
875875
}
876+
877+
/// Execute a transaction, accept the output, and ignore errors.
878+
pub fn run_tx<T: Tx>(self, filler: &T) -> Self {
879+
match self.execute_tx(filler) {
880+
Ok(evm) => evm.accept(),
881+
Err(evm) => evm.discard_error(),
882+
}
883+
}
876884
}
877885

878886
// --- READY
879887

880888
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmReady<'a, Ext, Db, C> {
881889
/// Clear the current transaction environment.
882890
pub fn clear_tx(self) -> EvmNeedsTx<'a, Ext, Db, C> {
883-
// NB: we do not clear the tx env here, as we read it during `BlockContext::apply_tx`
891+
// NB: we do not clear the tx env here, as we may read it during `BlockContext::post_tx`
884892
EvmNeedsTx { inner: self.inner, state: NeedsTx(self.state.0) }
885893
}
886894

@@ -899,6 +907,14 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmReady<
899907
}
900908
}
901909
}
910+
911+
/// Execute the loaded transaction, accept the output, and ignore errors.
912+
pub fn run(self) -> EvmNeedsTx<'a, Ext, Db, C> {
913+
match self.execute_tx() {
914+
Ok(evm) => evm.accept(),
915+
Err(evm) => evm.discard_error(),
916+
}
917+
}
902918
}
903919

904920
// --- ERRORED
@@ -928,6 +944,13 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>, E>
928944
pub fn into_error(self) -> E {
929945
self.state.error
930946
}
947+
948+
/// Reset the EVM, returning the error and the EVM ready for the next
949+
/// transaction.
950+
pub fn take_error(self) -> (EvmNeedsTx<'a, Ext, Db, C>, E) {
951+
let Trevm { inner, state: ErroredState { context, error } } = self;
952+
(Trevm { inner, state: NeedsTx(context) }, error)
953+
}
931954
}
932955

933956
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>>
@@ -1063,7 +1086,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>>
10631086
}
10641087

10651088
/// Accept the state changes, skipping the [`BlockContext::after_tx`]
1066-
/// method, and applying them directly to the database.
1089+
/// method, and committing them directly to the database.
10671090
///
10681091
/// This is a low-level API, and is not intended for general use. It is
10691092
/// almost always better to use [`Self::accept`] instead.

src/lib.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//!
4949
//! ```
5050
//! use revm::{EvmBuilder, db::InMemoryDB};
51-
//! use trevm::{TrevmBuilder, TransactedError, Cfg, Block, Tx, };
51+
//! use trevm::{TrevmBuilder, EvmErrored, Cfg, Block, Tx, };
5252
//!
5353
//! # fn t<C: Cfg, B: Block, T: Tx>(cfg: &C, block: &B, tx: &T)
5454
//! # -> Result<(), Box<dyn std::error::Error>> {
@@ -57,7 +57,7 @@
5757
//! .build_trevm()
5858
//! .fill_cfg(cfg)
5959
//! .fill_block(block)
60-
//! .apply_tx(tx);
60+
//! .execute_tx(tx);
6161
//! # Ok(())
6262
//! # }
6363
//!
@@ -107,9 +107,9 @@
107107
//! ```
108108
//! # use revm::{EvmBuilder, db::{InMemoryDB, BundleState}, State,
109109
//! # StateBuilder};
110-
//! # use trevm::{TrevmBuilder, TransactedError, Cfg, Block, Tx, BlockOutput,
110+
//! # use trevm::{TrevmBuilder, EvmErrored, Cfg, Block, Tx, BlockOutput,
111111
//! # EvmNeedsCfg, EvmNeedsFirstBlock, EvmNeedsTx, EvmReady, EvmNeedsNextBlock,
112-
//! # EvmBlockComplete, Shanghai};
112+
//! # EvmBlockComplete, Shanghai, EvmTransacted};
113113
//! # fn t<C: Cfg, B: Block, T: Tx>(cfg: &C, block: &B, tx: &T)
114114
//! # -> Result<(), Box<dyn std::error::Error>> {
115115
//! let state = StateBuilder::new_with_database(InMemoryDB::default()).build();
@@ -128,21 +128,18 @@
128128
//! let trevm: EvmNeedsTx<'_, _, _, _> = trevm.open_block(
129129
//! block,
130130
//! Shanghai::default()
131-
//! ).map_err(TransactedError::into_error)?;
131+
//! ).map_err(EvmErrored::into_error)?;
132132
//! // Filling the tx gets us to `EvmReady`.
133133
//! let trevm: EvmReady<'_, _, _, _> = trevm.fill_tx(tx);
134134
//!
135135
//! // Applying the tx or ignoring the error gets us back to `EvmNeedsTx``.
136-
//! let trevm: EvmNeedsTx<'_, _, _, _> = match trevm.execute_tx() {
137-
//! Ok(transacted) => transacted.apply(),
138-
//! Err(e) => e.discard_error(),
139-
//! };
136+
//! let trevm: EvmNeedsTx<'_, _, _, _> = trevm.run();
140137
//!
141138
//! // Clearing or closing a block gets us to `EvmNeedsNextBlock`, ready for the
142139
//! // next block.
143140
//! let trevm: EvmBlockComplete<'_, _, _, _> = trevm
144141
//! .close_block()
145-
//! .map_err(TransactedError::into_error)?;;
142+
//! .map_err(EvmErrored::into_error)?;;
146143
//!
147144
//! // During block execution, a context object
148145
//! let (context, trevm): (Shanghai<'_>, EvmNeedsNextBlock<'_, _, _>) = trevm
@@ -195,7 +192,7 @@
195192
//!
196193
//! ```
197194
//! # use revm::{EvmBuilder, db::InMemoryDB};
198-
//! # use trevm::{TrevmBuilder, TransactedError, Cfg, Block, Tx,
195+
//! # use trevm::{TrevmBuilder, EvmErrored, Cfg, Block, Tx,
199196
//! # Shanghai, Cancun};
200197
//! # use alloy_primitives::B256;
201198
//! # fn t<C: Cfg, B: Block, T: Tx>(cfg: &C, block: &B, tx: &T)
@@ -213,12 +210,13 @@
213210
//! // The pre-block logic is applied here
214211
//! .open_block(block, context)
215212
//! // Note that the logic is fallible, so we have to handle errors
216-
//! .map_err(TransactedError::into_error)?
217-
//! .apply_tx(tx)
218-
//! .map_err(TransactedError::into_error)?
213+
//! .map_err(EvmErrored::into_error)?
214+
//! .execute_tx(tx)
215+
//! .map_err(EvmErrored::into_error)?
216+
//! .accept()
219217
//! // Closing the block applies the post-block logic, and is also fallible
220218
//! .close_block()
221-
//! .map_err(TransactedError::into_error)?
219+
//! .map_err(EvmErrored::into_error)?
222220
//! // This splits the context, and puts trevm into `EvmNeedsNextBlock`
223221
//! .take_context();
224222
//! # Ok(())
@@ -238,7 +236,7 @@
238236
//!
239237
//! ```
240238
//! # use revm::{EvmBuilder, db::InMemoryDB};
241-
//! # use trevm::{TrevmBuilder, TransactedError, Cfg, Block, Tx,
239+
//! # use trevm::{TrevmBuilder, EvmErrored, Cfg, Block, Tx,
242240
//! # Shanghai, Cancun};
243241
//! # use alloy_primitives::B256;
244242
//! # fn t<C: Cfg, B: Block, T: Tx>(cfg: &C, block: &B, tx: &T)
@@ -259,7 +257,7 @@
259257
//! trevm
260258
//! }
261259
//! Err(transacted_error) => {
262-
//! let (trevm, error) = transacted_error.into_parts();
260+
//! let (trevm, error) = transacted_error.take_error();
263261
//! // Handle the error here, and return the EVM if you want
264262
//! // to keep going
265263
//! trevm
@@ -360,12 +358,12 @@
360358
//! +------+ +----------+ |
361359
//! |Finish} |EvmNeedsTx| <------ open_block() -----------+
362360
//! +------+ +----------+
363-
//! ^ | +--------+
364-
//! | +------- fill_tx() ------------> |EvmReady|
365-
//! | +--------+
366-
//! | +----------+ |
367-
//! +- apply() ---|Transacted| <-- execute_tx() -+
368-
//! or discard() +----------+
361+
//! ^ | +--------+
362+
//! | +------- fill_tx() ---------------> |EvmReady|
363+
//! | +--------+
364+
//! | +-------------+ |
365+
//! +- apply() ---|EvmTransacted| <-- execute_tx() --+
366+
//! or discard() +-------------+
369367
//! ```
370368
//!
371369
//! A basic block loop should look like this:
@@ -377,10 +375,7 @@
377375
//!
378376
//! for tx in txs {
379377
//! // apply the transaction, discard the error if any
380-
//! evm = match evm.apply_tx(tx) {
381-
//! Ok(evm) => evm,
382-
//! Err(e) => { e.discard_error() }
383-
//! }
378+
//! evm = match evm.run_tx(tx);
384379
//! }
385380
//!
386381
//! // close out the EVM, getting the final state

src/lifecycle/trait.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ pub trait BlockContext<Ext, Db: Database + DatabaseCommit> {
5656
/// state. This will be called by [`Trevm`] for each transaction in a block.
5757
///
5858
/// This is the hook to produce receipts, update cumulative gas used,
59-
/// inspect logs, etc etc.
59+
/// inspect logs, etc etc. You can read transaction details from `evm.tx()`
60+
/// and the result of the transaction from `result`.
6061
///
6162
/// Generally this should end by calling `evm.db_mut().commit(result.state)`
6263
/// however, it is allowed to discard the transaction instead of committing

0 commit comments

Comments
 (0)