Skip to content

Commit 402db8c

Browse files
committed
adds block module
1 parent edc64fa commit 402db8c

File tree

7 files changed

+119
-106
lines changed

7 files changed

+119
-106
lines changed

bin/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use builder::{
22
config::BuilderConfig,
33
service::serve_builder,
4-
tasks::{block::Simulator, bundler, metrics::MetricsTask, submit::SubmitTask, tx_poller},
4+
tasks::{block::sim::Simulator, bundler, metrics::MetricsTask, submit::SubmitTask, tx_poller},
55
};
66
use init4_bin_base::{deps::tracing, utils::from_env::FromEnv};
77
use signet_sim::SimCache;

src/tasks/block/cfg.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//! This file implements the [`trevm::Cfg`] and [`trevm::Block`] traits for Pecorino blocks.
2+
use alloy::primitives::{Address, B256, FixedBytes, U256};
3+
use trevm::{
4+
Block,
5+
revm::{
6+
context::{BlockEnv, CfgEnv},
7+
context_interface::block::BlobExcessGasAndPrice,
8+
primitives::hardfork::SpecId,
9+
},
10+
};
11+
12+
use crate::{config::BuilderConfig, constants::PECORINO_CHAIN_ID};
13+
14+
/// PecorinoCfg holds network-level configuration values.
15+
#[derive(Debug, Clone, Copy)]
16+
pub struct PecorinoCfg {}
17+
18+
impl trevm::Cfg for PecorinoCfg {
19+
/// Fills the configuration environment with Pecorino-specific values.
20+
///
21+
/// # Arguments
22+
///
23+
/// - `cfg_env`: The configuration environment to be filled.
24+
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
25+
let CfgEnv { chain_id, spec, .. } = cfg_env;
26+
27+
*chain_id = PECORINO_CHAIN_ID;
28+
*spec = SpecId::default();
29+
}
30+
}
31+
32+
/// PecorinoBlockEnv holds block-level configurations for Pecorino blocks.
33+
#[derive(Debug, Clone, Copy)]
34+
pub struct PecorinoBlockEnv {
35+
/// The block number for this block.
36+
pub number: u64,
37+
/// The address the block reward should be sent to.
38+
pub beneficiary: Address,
39+
/// Timestamp for the block.
40+
pub timestamp: u64,
41+
/// The gas limit for this block environment.
42+
pub gas_limit: u64,
43+
/// The basefee to use for calculating gas usage.
44+
pub basefee: u64,
45+
/// The prevrandao to use for this block.
46+
pub prevrandao: Option<FixedBytes<32>>,
47+
}
48+
49+
/// Implements [`trevm::Block`] for the Pecorino block.
50+
impl Block for PecorinoBlockEnv {
51+
/// Fills the block environment with the Pecorino specific values
52+
fn fill_block_env(&self, block_env: &mut BlockEnv) {
53+
// Destructure the fields off of the block_env and modify them
54+
let BlockEnv {
55+
number,
56+
beneficiary,
57+
timestamp,
58+
gas_limit,
59+
basefee,
60+
difficulty,
61+
prevrandao,
62+
blob_excess_gas_and_price,
63+
} = block_env;
64+
*number = self.number;
65+
*beneficiary = self.beneficiary;
66+
*timestamp = self.timestamp;
67+
*gas_limit = self.gas_limit;
68+
*basefee = self.basefee;
69+
*prevrandao = self.prevrandao;
70+
71+
// NB: The following fields are set to sane defaults because they
72+
// are not supported by the rollup
73+
*difficulty = U256::ZERO;
74+
*blob_excess_gas_and_price =
75+
Some(BlobExcessGasAndPrice { excess_blob_gas: 0, blob_gasprice: 0 });
76+
}
77+
}
78+
79+
impl PecorinoBlockEnv {
80+
/// Returns a new PecorinoBlockEnv with the specified values.
81+
///
82+
/// # Arguments
83+
///
84+
/// - config: The BuilderConfig for the builder.
85+
/// - number: The block number of this block, usually the latest block number plus 1,
86+
/// unless simulating blocks in the past.
87+
/// - timestamp: The timestamp of the block, typically set to the deadline of the
88+
/// block building task.
89+
pub fn new(config: BuilderConfig, number: u64, timestamp: u64, basefee: u64) -> Self {
90+
PecorinoBlockEnv {
91+
number,
92+
beneficiary: config.builder_rewards_address,
93+
timestamp,
94+
gas_limit: config.rollup_block_gas_limit,
95+
basefee,
96+
prevrandao: Some(B256::random()),
97+
}
98+
}
99+
}

src/tasks/block/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! block holds the tasks responsible for block environment configuration,
2+
//! preparation, and simulation.
3+
4+
/// Block simulation task for building blocks.
5+
pub mod sim;
6+
7+
/// Block environment configuration for the builder.
8+
pub mod cfg;

src/tasks/block.rs renamed to src/tasks/block/sim.rs

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! `block.rs` contains the Simulator and everything that wires it into an
22
//! actor that handles the simulation of a stream of bundles and transactions
33
//! and turns them into valid Pecorino blocks for network submission.
4+
use super::cfg::PecorinoBlockEnv;
45
use crate::{
56
config::{BuilderConfig, RuProvider},
6-
constants::{BASEFEE_DEFAULT, PECORINO_CHAIN_ID},
7-
tasks::bundler::Bundle,
7+
constants::BASEFEE_DEFAULT,
8+
tasks::{block::cfg::PecorinoCfg, bundler::Bundle},
89
};
910
use alloy::{
1011
consensus::TxEnvelope,
1112
eips::{BlockId, BlockNumberOrTag::Latest},
1213
network::Ethereum,
13-
primitives::{Address, B256, FixedBytes, U256},
1414
providers::Provider,
1515
};
1616
use chrono::{DateTime, Utc};
@@ -34,15 +34,9 @@ use tokio::{
3434
task::JoinHandle,
3535
time::sleep,
3636
};
37-
use trevm::{
38-
Block,
39-
revm::{
40-
context::{BlockEnv, CfgEnv},
41-
context_interface::block::BlobExcessGasAndPrice,
42-
database::{AlloyDB, WrapDatabaseAsync},
43-
inspector::NoOpInspector,
44-
primitives::hardfork::SpecId::{self},
45-
},
37+
use trevm::revm::{
38+
database::{AlloyDB, WrapDatabaseAsync},
39+
inspector::NoOpInspector,
4640
};
4741

4842
/// `Simulator` is responsible for periodically building blocks and submitting them for
@@ -419,90 +413,3 @@ async fn cache_updater(
419413
}
420414
}
421415
}
422-
423-
/// PecorinoCfg holds network-level configuration values.
424-
#[derive(Debug, Clone, Copy)]
425-
pub struct PecorinoCfg {}
426-
427-
impl trevm::Cfg for PecorinoCfg {
428-
/// Fills the configuration environment with Pecorino-specific values.
429-
///
430-
/// # Arguments
431-
///
432-
/// - `cfg_env`: The configuration environment to be filled.
433-
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
434-
let CfgEnv { chain_id, spec, .. } = cfg_env;
435-
436-
*chain_id = PECORINO_CHAIN_ID;
437-
*spec = SpecId::default();
438-
}
439-
}
440-
441-
/// PecorinoBlockEnv holds block-level configurations for Pecorino blocks.
442-
#[derive(Debug, Clone, Copy)]
443-
pub struct PecorinoBlockEnv {
444-
/// The block number for this block.
445-
pub number: u64,
446-
/// The address the block reward should be sent to.
447-
pub beneficiary: Address,
448-
/// Timestamp for the block.
449-
pub timestamp: u64,
450-
/// The gas limit for this block environment.
451-
pub gas_limit: u64,
452-
/// The basefee to use for calculating gas usage.
453-
pub basefee: u64,
454-
/// The prevrandao to use for this block.
455-
pub prevrandao: Option<FixedBytes<32>>,
456-
}
457-
458-
/// Implements [`trevm::Block`] for the Pecorino block.
459-
impl Block for PecorinoBlockEnv {
460-
/// Fills the block environment with the Pecorino specific values
461-
fn fill_block_env(&self, block_env: &mut trevm::revm::context::BlockEnv) {
462-
// Destructure the fields off of the block_env and modify them
463-
let BlockEnv {
464-
number,
465-
beneficiary,
466-
timestamp,
467-
gas_limit,
468-
basefee,
469-
difficulty,
470-
prevrandao,
471-
blob_excess_gas_and_price,
472-
} = block_env;
473-
*number = self.number;
474-
*beneficiary = self.beneficiary;
475-
*timestamp = self.timestamp;
476-
*gas_limit = self.gas_limit;
477-
*basefee = self.basefee;
478-
*prevrandao = self.prevrandao;
479-
480-
// NB: The following fields are set to sane defaults because they
481-
// are not supported by the rollup
482-
*difficulty = U256::ZERO;
483-
*blob_excess_gas_and_price =
484-
Some(BlobExcessGasAndPrice { excess_blob_gas: 0, blob_gasprice: 0 });
485-
}
486-
}
487-
488-
impl PecorinoBlockEnv {
489-
/// Returns a new PecorinoBlockEnv with the specified values.
490-
///
491-
/// # Arguments
492-
///
493-
/// - config: The BuilderConfig for the builder.
494-
/// - number: The block number of this block, usually the latest block number plus 1,
495-
/// unless simulating blocks in the past.
496-
/// - timestamp: The timestamp of the block, typically set to the deadline of the
497-
/// block building task.
498-
fn new(config: BuilderConfig, number: u64, timestamp: u64, basefee: u64) -> Self {
499-
PecorinoBlockEnv {
500-
number,
501-
beneficiary: config.builder_rewards_address,
502-
timestamp,
503-
gas_limit: config.rollup_block_gas_limit,
504-
basefee,
505-
prevrandao: Some(B256::random()),
506-
}
507-
}
508-
}

src/tasks/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/// Block creation task
2-
pub mod block;
3-
41
/// Bundle poller task
52
pub mod bundler;
63

@@ -15,3 +12,6 @@ pub mod submit;
1512

1613
/// Tx polling task
1714
pub mod tx_poller;
15+
16+
/// Block simulation and environment
17+
pub mod block;

src/test_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Test utilities for testing builder tasks
22
use crate::{
33
config::BuilderConfig,
4+
tasks::block::cfg::PecorinoBlockEnv,
45
constants::{PECORINO_CHAIN_ID, PECORINO_HOST_CHAIN_ID},
5-
tasks::block::PecorinoBlockEnv,
66
};
77
use alloy::{
88
consensus::{SignableTransaction, TxEip1559, TxEnvelope},
@@ -36,7 +36,6 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
3636
sequencer_key: None,
3737
builder_key: "0000000000000000000000000000000000000000000000000000000000000000".into(),
3838
block_confirmation_buffer: 1,
39-
4039
builder_rewards_address: Address::default(),
4140
rollup_block_gas_limit: 3_000_000_000,
4241
tx_pool_url: "http://localhost:9000/".into(),

tests/block_builder_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod tests {
1010
};
1111
use builder::{
1212
constants::PECORINO_CHAIN_ID,
13-
tasks::block::Simulator,
13+
tasks::block::sim::Simulator,
1414
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
1515
};
1616
use init4_bin_base::utils::calc::SlotCalculator;

0 commit comments

Comments
 (0)