Skip to content

Commit d634f31

Browse files
authored
chore: Make inspector use generics, rm associated types (#1934)
* chore: Make inspector use generics, rm associated types * prev context * default impl for ImputsImpl * ContextWire and Wiring * rename and few cleanup * fmt/clippy * simplify context * auto_impl on error getter trait * simpify host * impl &mut and Box over setter traits * relax JournalExt * compile no std * string * box
1 parent 8dc2e2f commit d634f31

File tree

22 files changed

+266
-337
lines changed

22 files changed

+266
-337
lines changed

crates/context/interface/src/block.rs

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub use blob::{calc_blob_gasprice, calc_excess_blob_gas, BlobExcessGasAndPrice};
44

55
use auto_impl::auto_impl;
66
use primitives::{Address, B256, U256};
7+
use std::boxed::Box;
78

89
/// Trait for retrieving block information required for execution.
910
#[auto_impl(&, &mut, Box, Arc)]
@@ -79,3 +80,15 @@ pub trait BlockGetter {
7980
pub trait BlockSetter: BlockGetter {
8081
fn set_block(&mut self, block: <Self as BlockGetter>::Block);
8182
}
83+
84+
impl<T: BlockSetter> BlockSetter for &mut T {
85+
fn set_block(&mut self, block: <Self as BlockGetter>::Block) {
86+
(**self).set_block(block)
87+
}
88+
}
89+
90+
impl<T: BlockSetter> BlockSetter for Box<T> {
91+
fn set_block(&mut self, block: <Self as BlockGetter>::Block) {
92+
(**self).set_block(block)
93+
}
94+
}

crates/context/interface/src/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use auto_impl::auto_impl;
2+
13
// TODO : Change the name of the trait
4+
#[auto_impl(&mut, Box)]
25
pub trait ErrorGetter {
36
type Error;
47

crates/context/interface/src/host.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,16 @@ pub use dummy::DummyHost;
55

66
use crate::{
77
journaled_state::{AccountLoad, Eip7702CodeLoad},
8-
Block, Cfg, Transaction,
8+
BlockGetter, CfgGetter, TransactionGetter,
99
};
10+
use auto_impl::auto_impl;
1011
use primitives::{Address, Bytes, Log, B256, U256};
1112

1213
/// EVM context host.
1314
// TODO : Move to context-interface
14-
pub trait Host {
15-
/// Chain specification
16-
type BLOCK: Block;
17-
type TX: Transaction;
18-
type CFG: Cfg;
19-
20-
/// Returns a reference to the environment.
21-
fn tx(&self) -> &Self::TX;
22-
23-
/// Returns a mutable reference to the environment.
24-
fn block(&self) -> &Self::BLOCK;
25-
26-
// TODO : Make it generic in future
27-
fn cfg(&self) -> &Self::CFG;
28-
29-
/// Loads an account code.
15+
#[auto_impl(&mut, Box)]
16+
pub trait Host: TransactionGetter + BlockGetter + CfgGetter {
17+
/// Load an account code.
3018
fn load_account_delegated(&mut self, address: Address) -> Option<AccountLoad>;
3119

3220
/// Gets the block hash of the given block `number`.

crates/context/interface/src/host/dummy.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{Host, SStoreResult, SelfDestructResult};
2-
use crate::{Block, Cfg, Transaction};
2+
use crate::{Block, BlockGetter, Cfg, CfgGetter, Transaction, TransactionGetter};
33
use primitives::{hash_map::Entry, Address, Bytes, HashMap, Log, B256, KECCAK_EMPTY, U256};
44
use std::vec::Vec;
55

@@ -48,26 +48,31 @@ where
4848
}
4949
}
5050

51-
impl<TX: Transaction, BLOCK: Block, CFG: Cfg> Host for DummyHost<BLOCK, TX, CFG> {
52-
type TX = TX;
53-
type BLOCK = BLOCK;
54-
type CFG = CFG;
51+
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> BlockGetter for DummyHost<BLOCK, TX, CFG> {
52+
type Block = BLOCK;
5553

56-
#[inline]
57-
fn tx(&self) -> &Self::TX {
58-
&self.tx
54+
fn block(&self) -> &Self::Block {
55+
&self.block
5956
}
57+
}
6058

61-
#[inline]
62-
fn block(&self) -> &Self::BLOCK {
63-
&self.block
59+
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> TransactionGetter for DummyHost<BLOCK, TX, CFG> {
60+
type Transaction = TX;
61+
62+
fn tx(&self) -> &Self::Transaction {
63+
&self.tx
6464
}
65+
}
6566

66-
#[inline]
67-
fn cfg(&self) -> &Self::CFG {
67+
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> CfgGetter for DummyHost<BLOCK, TX, CFG> {
68+
type Cfg = CFG;
69+
70+
fn cfg(&self) -> &Self::Cfg {
6871
&self.cfg
6972
}
73+
}
7074

75+
impl<TX: Transaction, BLOCK: Block, CFG: Cfg> Host for DummyHost<BLOCK, TX, CFG> {
7176
#[inline]
7277
fn load_account_delegated(&mut self, _address: Address) -> Option<AccountLoad> {
7378
Some(AccountLoad::default())

crates/context/interface/src/journaled_state.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -286,28 +286,38 @@ impl<T> Eip7702CodeLoad<T> {
286286
}
287287
}
288288

289-
/// Helper that extracts database error from [`JournalStateGetter`]
290-
pub type JournalStateGetterDBError<CTX> =
291-
<<<CTX as JournalStateGetter>::Journal as Journal>::Database as Database>::Error;
289+
/// Helper that extracts database error from [`JournalGetter`].
290+
pub type JournalDBError<CTX> =
291+
<<<CTX as JournalGetter>::Journal as Journal>::Database as Database>::Error;
292292

293-
pub trait JournalStateGetter: DatabaseGetter {
293+
pub trait JournalGetter: DatabaseGetter {
294294
type Journal: Journal<Database = <Self as DatabaseGetter>::Database>;
295295

296296
fn journal(&mut self) -> &mut Self::Journal;
297+
298+
fn journal_ref(&self) -> &Self::Journal;
297299
}
298300

299-
impl<T: JournalStateGetter> JournalStateGetter for &mut T {
301+
impl<T: JournalGetter> JournalGetter for &mut T {
300302
type Journal = T::Journal;
301303

302304
fn journal(&mut self) -> &mut Self::Journal {
303305
T::journal(*self)
304306
}
307+
308+
fn journal_ref(&self) -> &Self::Journal {
309+
T::journal_ref(*self)
310+
}
305311
}
306312

307-
impl<T: JournalStateGetter> JournalStateGetter for Box<T> {
313+
impl<T: JournalGetter> JournalGetter for Box<T> {
308314
type Journal = T::Journal;
309315

310316
fn journal(&mut self) -> &mut Self::Journal {
311317
T::journal(self.as_mut())
312318
}
319+
320+
fn journal_ref(&self) -> &Self::Journal {
321+
T::journal_ref(self.as_ref())
322+
}
313323
}

crates/context/interface/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ pub use block::{Block, BlockGetter};
1717
pub use cfg::{Cfg, CfgGetter, CreateScheme, TransactTo};
1818
pub use database_interface::{DBErrorMarker, Database, DatabaseGetter};
1919
pub use errors::ErrorGetter;
20-
pub use journaled_state::{Journal, JournalStateGetter, JournalStateGetterDBError};
20+
pub use journaled_state::{Journal, JournalDBError, JournalGetter};
2121
pub use transaction::{Transaction, TransactionGetter, TransactionType};

crates/context/interface/src/transaction.rs

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use auto_impl::auto_impl;
2020
use core::cmp::min;
2121
use core::fmt::Debug;
2222
use primitives::{TxKind, U256};
23+
use std::boxed::Box;
2324

2425
/// Transaction validity error types.
2526
pub trait TransactionError: Debug + core::error::Error {}
@@ -164,3 +165,15 @@ pub trait TransactionGetter {
164165
pub trait TransactionSetter: TransactionGetter {
165166
fn set_tx(&mut self, tx: <Self as TransactionGetter>::Transaction);
166167
}
168+
169+
impl<T: TransactionSetter> TransactionSetter for &mut T {
170+
fn set_tx(&mut self, block: <Self as TransactionGetter>::Transaction) {
171+
(**self).set_tx(block)
172+
}
173+
}
174+
175+
impl<T: TransactionSetter> TransactionSetter for Box<T> {
176+
fn set_tx(&mut self, block: <Self as TransactionGetter>::Transaction) {
177+
(**self).set_tx(block)
178+
}
179+
}

crates/context/src/context.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use context_interface::{
55
journaled_state::{AccountLoad, Eip7702CodeLoad},
66
result::EVMError,
77
transaction::TransactionSetter,
8-
Block, BlockGetter, Cfg, CfgGetter, DatabaseGetter, ErrorGetter, Journal, JournalStateGetter,
8+
Block, BlockGetter, Cfg, CfgGetter, DatabaseGetter, ErrorGetter, Journal, JournalGetter,
99
Transaction, TransactionGetter,
1010
};
1111
use database_interface::{Database, EmptyDB};
@@ -24,17 +24,17 @@ pub struct Context<
2424
JOURNAL: Journal<Database = DB> = JournaledState<DB>,
2525
CHAIN = (),
2626
> {
27-
/// Transaction information
28-
pub tx: TX,
29-
/// Block information
27+
/// Block information.
3028
pub block: BLOCK,
31-
/// Configurations
29+
/// Transaction information.
30+
pub tx: TX,
31+
/// Configurations.
3232
pub cfg: CFG,
33-
/// EVM State with journaling support and database
33+
/// EVM State with journaling support and database.
3434
pub journaled_state: JOURNAL,
35-
/// Inner context
35+
/// Inner context.
3636
pub chain: CHAIN,
37-
/// Error that happened during execution
37+
/// Error that happened during execution.
3838
pub error: Result<(), <DB as Database>::Error>,
3939
}
4040

@@ -380,22 +380,6 @@ where
380380
DB: Database,
381381
JOURNAL: Journal<Database = DB>,
382382
{
383-
type BLOCK = BLOCK;
384-
type TX = TX;
385-
type CFG = CFG;
386-
387-
fn tx(&self) -> &Self::TX {
388-
&self.tx
389-
}
390-
391-
fn block(&self) -> &Self::BLOCK {
392-
&self.block
393-
}
394-
395-
fn cfg(&self) -> &Self::CFG {
396-
&self.cfg
397-
}
398-
399383
fn block_hash(&mut self, requested_number: u64) -> Option<B256> {
400384
let block_number = as_u64_saturated!(*self.block().number());
401385

@@ -498,7 +482,7 @@ impl<BLOCK, TX, CFG: Cfg, DB: Database, JOURNAL: Journal<Database = DB>, CHAIN>
498482
}
499483
}
500484

501-
impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> JournalStateGetter
485+
impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> JournalGetter
502486
for Context<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN>
503487
where
504488
DB: Database,
@@ -509,6 +493,10 @@ where
509493
fn journal(&mut self) -> &mut Self::Journal {
510494
&mut self.journaled_state
511495
}
496+
497+
fn journal_ref(&self) -> &Self::Journal {
498+
&self.journaled_state
499+
}
512500
}
513501

514502
impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> DatabaseGetter

crates/context/src/default.rs

-3
This file was deleted.

crates/database/interface/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use core::convert::Infallible;
1010
use auto_impl::auto_impl;
1111
use primitives::{Address, HashMap, B256, U256};
1212
use state::{Account, AccountInfo, Bytecode};
13+
use std::string::String;
1314

1415
#[cfg(feature = "asyncdb")]
1516
pub mod async_db;
@@ -28,6 +29,7 @@ pub trait DBErrorMarker {}
2829
/// Implement marker for `()`.
2930
impl DBErrorMarker for () {}
3031
impl DBErrorMarker for Infallible {}
32+
impl DBErrorMarker for String {}
3133

3234
/// EVM database interface.
3335
#[auto_impl(&mut, Box)]

crates/handler/src/execution.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::{frame_data::FrameResult, EthFrame, EthPrecompileProvider};
22
use bytecode::EOF_MAGIC_BYTES;
33
use context_interface::{
4-
result::InvalidTransaction, BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalStateGetter,
5-
JournalStateGetterDBError, Transaction, TransactionGetter,
4+
result::InvalidTransaction, BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalDBError,
5+
JournalGetter, Transaction, TransactionGetter,
66
};
77
use handler_interface::{util::FrameOrFrameResult, ExecutionHandler, Frame as FrameTrait};
88
use interpreter::{
@@ -125,30 +125,24 @@ impl<CTX, ERROR, FRAME> EthExecution<CTX, ERROR, FRAME> {
125125
}
126126

127127
pub trait EthExecutionContext<ERROR>:
128-
TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalStateGetter + CfgGetter
128+
TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter
129129
{
130130
}
131131

132132
impl<
133133
ERROR,
134-
T: TransactionGetter
135-
+ ErrorGetter<Error = ERROR>
136-
+ BlockGetter
137-
+ JournalStateGetter
138-
+ CfgGetter,
134+
T: TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter,
139135
> EthExecutionContext<ERROR> for T
140136
{
141137
}
142138

143-
pub trait EthExecutionError<CTX: JournalStateGetter>:
144-
From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>
139+
pub trait EthExecutionError<CTX: JournalGetter>:
140+
From<InvalidTransaction> + From<JournalDBError<CTX>>
145141
{
146142
}
147143

148-
impl<
149-
CTX: JournalStateGetter,
150-
T: From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>,
151-
> EthExecutionError<CTX> for T
144+
impl<CTX: JournalGetter, T: From<InvalidTransaction> + From<JournalDBError<CTX>>>
145+
EthExecutionError<CTX> for T
152146
{
153147
}
154148

crates/handler/src/frame.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::frame_data::*;
22
use bytecode::{Eof, EOF_MAGIC_BYTES};
33
use context_interface::{
44
journaled_state::{Journal, JournalCheckpoint},
5-
BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalStateGetter, JournalStateGetterDBError,
6-
Transaction, TransactionGetter,
5+
BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalDBError, JournalGetter, Transaction,
6+
TransactionGetter,
77
};
88
use core::{cell::RefCell, cmp::min};
99
use handler_interface::{Frame, FrameOrResultGen, PrecompileProvider};
@@ -45,7 +45,7 @@ pub struct EthFrame<CTX, ERROR, IW: InterpreterTypes, PRECOMPILE, INSTRUCTIONS>
4545

4646
impl<CTX, IW, ERROR, PRECOMP, INST> EthFrame<CTX, ERROR, IW, PRECOMP, INST>
4747
where
48-
CTX: JournalStateGetter,
48+
CTX: JournalGetter,
4949
IW: InterpreterTypes,
5050
{
5151
pub fn new(
@@ -792,7 +792,7 @@ pub fn return_eofcreate<JOURNAL: Journal>(
792792
}
793793

794794
pub trait EthFrameContext<ERROR>:
795-
TransactionGetter + Host + ErrorGetter<Error = ERROR> + BlockGetter + JournalStateGetter + CfgGetter
795+
TransactionGetter + Host + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter
796796
{
797797
}
798798

@@ -801,19 +801,19 @@ impl<
801801
CTX: TransactionGetter
802802
+ ErrorGetter<Error = ERROR>
803803
+ BlockGetter
804-
+ JournalStateGetter
804+
+ JournalGetter
805805
+ CfgGetter
806806
+ Host,
807807
> EthFrameContext<ERROR> for CTX
808808
{
809809
}
810810

811-
pub trait EthFrameError<CTX: JournalStateGetter>:
812-
From<JournalStateGetterDBError<CTX>> + From<PrecompileErrors>
811+
pub trait EthFrameError<CTX: JournalGetter>:
812+
From<JournalDBError<CTX>> + From<PrecompileErrors>
813813
{
814814
}
815815

816-
impl<CTX: JournalStateGetter, T: From<JournalStateGetterDBError<CTX>> + From<PrecompileErrors>>
817-
EthFrameError<CTX> for T
816+
impl<CTX: JournalGetter, T: From<JournalDBError<CTX>> + From<PrecompileErrors>> EthFrameError<CTX>
817+
for T
818818
{
819819
}

0 commit comments

Comments
 (0)