Skip to content

feat: add block number property to BuiltBlock #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions crates/sim/src/built.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct BuiltBlock {
pub(crate) host_fills: Vec<SignedFill>,
/// Transactions in the block.
pub(crate) transactions: Vec<TxEnvelope>,
/// The block number for the block.
pub(crate) block_number: u64,

/// The amount of gas used by the block so far
pub(crate) gas_used: u64,
Expand All @@ -35,22 +37,29 @@ impl fmt::Debug for BuiltBlock {
.field("host_fills", &self.host_fills.len())
.field("transactions", &self.transactions.len())
.field("gas_used", &self.gas_used)
.finish()
.field("block_number", &self.block_number)
.finish_non_exhaustive()
}
}

impl BuiltBlock {
/// Create a new `BuiltBlock`
pub const fn new() -> Self {
pub const fn new(block_number: u64) -> Self {
Self {
host_fills: Vec::new(),
transactions: Vec::new(),
block_number,
gas_used: 0,
raw_encoding: OnceLock::new(),
hash: OnceLock::new(),
}
}

/// Gets the block number for the block.
pub const fn block_number(&self) -> u64 {
self.block_number
}

/// Get the amount of gas used by the block.
pub const fn gas_used(&self) -> u64 {
self.gas_used
Expand Down
42 changes: 29 additions & 13 deletions crates/sim/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use trevm::{
helpers::Ctx,
inspectors::{Layered, TimeLimit},
revm::{
context::result::{EVMError, ExecutionResult},
context::{
result::{EVMError, ExecutionResult},
BlockEnv, CfgEnv,
},
database::{Cache, CacheDB},
inspector::NoOpInspector,
DatabaseRef, Inspector,
Expand Down Expand Up @@ -63,15 +66,19 @@ where
Insp: Inspector<Ctx<SimDb<Db>>> + Default + Sync + 'static,
{
/// Creates a new `SimEnv` instance.
pub fn new(
pub fn new<C, B>(
db: Db,
constants: SignetSystemConstants,
cfg: Box<dyn Cfg>,
block: Box<dyn Block>,
cfg: C,
block: B,
finish_by: std::time::Instant,
concurrency_limit: usize,
sim_items: SimCache,
) -> Self {
) -> Self
where
C: Cfg,
B: Block,
{
SimEnv::new(db, constants, cfg, block, finish_by, concurrency_limit, sim_items).into()
}

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

/// Chain cfg to use for the simulation.
cfg: Box<dyn Cfg>,
cfg: CfgEnv,

/// Block to use for the simulation.
block: Box<dyn Block>,
block: BlockEnv,

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

impl<Db, Insp> SimEnv<Db, Insp> {
/// Creates a new `SimFactory` instance.
pub fn new(
pub fn new<C, B>(
db: Db,
constants: SignetSystemConstants,
cfg: Box<dyn Cfg>,
block: Box<dyn Block>,
cfg_ref: C,
block_ref: B,
finish_by: std::time::Instant,
concurrency_limit: usize,
sim_items: SimCache,
) -> Self {
) -> Self
where
C: Cfg,
B: Block,
{
let mut cfg = CfgEnv::default();
cfg_ref.fill_cfg_env(&mut cfg);
let mut block = BlockEnv::default();
block_ref.fill_block_env(&mut block);

Self {
db: Arc::new(CacheDB::new(db)),
constants,
Expand Down Expand Up @@ -188,12 +204,12 @@ impl<Db, Insp> SimEnv<Db, Insp> {
}

/// Get a reference to the chain cfg.
pub fn cfg(&self) -> &dyn Cfg {
pub const fn cfg(&self) -> &CfgEnv {
&self.cfg
}

/// Get a reference to the block.
pub fn block(&self) -> &dyn Block {
pub const fn block(&self) -> &BlockEnv {
&self.block
}

Expand Down
10 changes: 4 additions & 6 deletions crates/sim/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ where
max_gas: u64,
) -> Self
where
C: Cfg + 'static,
B: Block + 'static,
C: Cfg,
B: Block,
{
let cfg: Box<dyn Cfg> = Box::new(cfg);
let block: Box<dyn Block> = Box::new(block);

let env = SimEnv::<Db, Insp>::new(
db,
constants,
Expand All @@ -58,7 +55,8 @@ where
sim_items,
);
let finish_by = env.finish_by();
Self { env: env.into(), block: BuiltBlock::new(), finish_by, max_gas }
let number = env.block().number;
Self { env: env.into(), block: BuiltBlock::new(number), finish_by, max_gas }
}

/// Run a simulation round, and accumulate the results into the block.
Expand Down
Loading