Skip to content

Commit f0b6ed0

Browse files
prestwichdylanlott
authored andcommitted
refactor sim envs to hold Env objects instead of Box<dyn _> (#60)
* refactor: change sim to hold envs instead of boxdyns, set block number * lint: fmt
1 parent a1c9bfc commit f0b6ed0

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

crates/sim/src/built.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,28 @@ impl fmt::Debug for BuiltBlock {
3838
.field("transactions", &self.transactions.len())
3939
.field("gas_used", &self.gas_used)
4040
.field("block_number", &self.block_number)
41-
.field("hash", &self.contents_hash())
42-
.finish()
41+
.finish_non_exhaustive()
4342
}
4443
}
4544

4645
impl BuiltBlock {
4746
/// Create a new `BuiltBlock`
48-
pub const fn new() -> Self {
47+
pub const fn new(block_number: u64) -> Self {
4948
Self {
5049
host_fills: Vec::new(),
5150
transactions: Vec::new(),
52-
block_number: 0,
51+
block_number,
5352
gas_used: 0,
5453
raw_encoding: OnceLock::new(),
5554
hash: OnceLock::new(),
5655
}
5756
}
5857

5958
/// Gets the block number for the block.
60-
pub fn block_number(&self) -> u64 {
59+
pub const fn block_number(&self) -> u64 {
6160
self.block_number
6261
}
6362

64-
/// Sets the block number for the block.
65-
pub fn set_block_number(&mut self, block_number: u64) {
66-
self.block_number = block_number;
67-
}
68-
6963
/// Get the amount of gas used by the block.
7064
pub const fn gas_used(&self) -> u64 {
7165
self.gas_used

crates/sim/src/env.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ use tokio::{
99
select,
1010
sync::{mpsc, watch},
1111
};
12-
use tracing::{instrument, trace, trace_span};
12+
use tracing::{instrument, error, trace, trace_span};
1313
use trevm::{
1414
db::{cow::CacheOnWrite, TryCachingDb},
1515
helpers::Ctx,
1616
inspectors::{Layered, TimeLimit},
1717
revm::{
18-
context::result::{EVMError, ExecutionResult},
18+
context::{
19+
result::{EVMError, ExecutionResult},
20+
BlockEnv, CfgEnv,
21+
},
1922
database::{Cache, CacheDB},
2023
inspector::NoOpInspector,
2124
DatabaseRef, Inspector,
@@ -63,15 +66,19 @@ where
6366
Insp: Inspector<Ctx<SimDb<Db>>> + Default + Sync + 'static,
6467
{
6568
/// Creates a new `SimEnv` instance.
66-
pub fn new(
69+
pub fn new<C, B>(
6770
db: Db,
6871
constants: SignetSystemConstants,
69-
cfg: Box<dyn Cfg>,
70-
block: Box<dyn Block>,
72+
cfg: C,
73+
block: B,
7174
finish_by: std::time::Instant,
7275
concurrency_limit: usize,
7376
sim_items: SimCache,
74-
) -> Self {
77+
) -> Self
78+
where
79+
C: Cfg,
80+
B: Block,
81+
{
7582
SimEnv::new(db, constants, cfg, block, finish_by, concurrency_limit, sim_items).into()
7683
}
7784

@@ -125,10 +132,10 @@ pub struct SimEnv<Db, Insp = NoOpInspector> {
125132
constants: SignetSystemConstants,
126133

127134
/// Chain cfg to use for the simulation.
128-
cfg: Box<dyn Cfg>,
135+
cfg: CfgEnv,
129136

130137
/// Block to use for the simulation.
131-
block: Box<dyn Block>,
138+
block: BlockEnv,
132139

133140
/// The instant by which the simulation should finish.
134141
finish_by: std::time::Instant,
@@ -151,15 +158,24 @@ impl<Db, Insp> fmt::Debug for SimEnv<Db, Insp> {
151158

152159
impl<Db, Insp> SimEnv<Db, Insp> {
153160
/// Creates a new `SimFactory` instance.
154-
pub fn new(
161+
pub fn new<C, B>(
155162
db: Db,
156163
constants: SignetSystemConstants,
157-
cfg: Box<dyn Cfg>,
158-
block: Box<dyn Block>,
164+
cfg_ref: C,
165+
block_ref: B,
159166
finish_by: std::time::Instant,
160167
concurrency_limit: usize,
161168
sim_items: SimCache,
162-
) -> Self {
169+
) -> Self
170+
where
171+
C: Cfg,
172+
B: Block,
173+
{
174+
let mut cfg = CfgEnv::default();
175+
cfg_ref.fill_cfg_env(&mut cfg);
176+
let mut block = BlockEnv::default();
177+
block_ref.fill_block_env(&mut block);
178+
163179
Self {
164180
db: Arc::new(CacheDB::new(db)),
165181
constants,
@@ -188,12 +204,12 @@ impl<Db, Insp> SimEnv<Db, Insp> {
188204
}
189205

190206
/// Get a reference to the chain cfg.
191-
pub fn cfg(&self) -> &dyn Cfg {
207+
pub const fn cfg(&self) -> &CfgEnv {
192208
&self.cfg
193209
}
194210

195211
/// Get a reference to the block.
196-
pub fn block(&self) -> &dyn Block {
212+
pub const fn block(&self) -> &BlockEnv {
197213
&self.block
198214
}
199215

@@ -298,7 +314,10 @@ where
298314
// Create the outcome
299315
Ok(SimOutcomeWithCache { identifier, score, cache, gas_used })
300316
}
301-
Err(e) => Err(SignetEthBundleError::from(e.into_error())),
317+
Err(e) => {
318+
error!(err = ?e.error(), "simulation error details");
319+
Err(SignetEthBundleError::from(e.into_error()))
320+
}
302321
}
303322
}
304323

crates/sim/src/task.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ where
4242
max_gas: u64,
4343
) -> Self
4444
where
45-
C: Cfg + 'static,
46-
B: Block + 'static,
45+
C: Cfg,
46+
B: Block,
4747
{
48-
let cfg: Box<dyn Cfg> = Box::new(cfg);
49-
let block: Box<dyn Block> = Box::new(block);
50-
5148
let env = SimEnv::<Db, Insp>::new(
5249
db,
5350
constants,
@@ -58,7 +55,8 @@ where
5855
sim_items,
5956
);
6057
let finish_by = env.finish_by();
61-
Self { env: env.into(), block: BuiltBlock::new(), finish_by, max_gas }
58+
let number = env.block().number;
59+
Self { env: env.into(), block: BuiltBlock::new(number), finish_by, max_gas }
6260
}
6361

6462
/// Run a simulation round, and accumulate the results into the block.

0 commit comments

Comments
 (0)