Skip to content

Commit ba5894f

Browse files
authored
feat: add block number property to BuiltBlock (#59)
# feat: add block number property to BuiltBlock This PR adds a block number field and a setter for tracking the block number of a given built block.
1 parent 18ffaa4 commit ba5894f

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

crates/sim/src/built.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct BuiltBlock {
1919
pub(crate) host_fills: Vec<SignedFill>,
2020
/// Transactions in the block.
2121
pub(crate) transactions: Vec<TxEnvelope>,
22+
/// The block number for the block.
23+
pub(crate) block_number: u64,
2224

2325
/// The amount of gas used by the block so far
2426
pub(crate) gas_used: u64,
@@ -35,22 +37,29 @@ impl fmt::Debug for BuiltBlock {
3537
.field("host_fills", &self.host_fills.len())
3638
.field("transactions", &self.transactions.len())
3739
.field("gas_used", &self.gas_used)
38-
.finish()
40+
.field("block_number", &self.block_number)
41+
.finish_non_exhaustive()
3942
}
4043
}
4144

4245
impl BuiltBlock {
4346
/// Create a new `BuiltBlock`
44-
pub const fn new() -> Self {
47+
pub const fn new(block_number: u64) -> Self {
4548
Self {
4649
host_fills: Vec::new(),
4750
transactions: Vec::new(),
51+
block_number,
4852
gas_used: 0,
4953
raw_encoding: OnceLock::new(),
5054
hash: OnceLock::new(),
5155
}
5256
}
5357

58+
/// Gets the block number for the block.
59+
pub const fn block_number(&self) -> u64 {
60+
self.block_number
61+
}
62+
5463
/// Get the amount of gas used by the block.
5564
pub const fn gas_used(&self) -> u64 {
5665
self.gas_used

crates/sim/src/env.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use trevm::{
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

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)