Skip to content

Commit 554bee4

Browse files
dylanlottprestwich
authored andcommitted
adds block module
1 parent 5c564fb commit 554bee4

File tree

7 files changed

+118
-105
lines changed

7 files changed

+118
-105
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;
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 = signet_constants::pecorino::RU_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: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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-
tasks::bundler::Bundle,
7+
tasks::{block::cfg::PecorinoCfg, bundler::Bundle},
78
};
89
use alloy::{
910
consensus::TxEnvelope,
1011
eips::{BlockId, BlockNumberOrTag::Latest},
1112
network::Ethereum,
12-
primitives::{Address, B256, FixedBytes, U256},
1313
providers::Provider,
1414
};
1515
use chrono::{DateTime, Utc};
@@ -33,15 +33,9 @@ use tokio::{
3333
task::JoinHandle,
3434
time::sleep,
3535
};
36-
use trevm::{
37-
Block,
38-
revm::{
39-
context::{BlockEnv, CfgEnv},
40-
context_interface::block::BlobExcessGasAndPrice,
41-
database::{AlloyDB, WrapDatabaseAsync},
42-
inspector::NoOpInspector,
43-
primitives::hardfork::SpecId::{self},
44-
},
36+
use trevm::revm::{
37+
database::{AlloyDB, WrapDatabaseAsync},
38+
inspector::NoOpInspector,
4539
};
4640

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

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,5 +1,5 @@
11
//! Test utilities for testing builder tasks
2-
use crate::{config::BuilderConfig, tasks::block::PecorinoBlockEnv};
2+
use crate::{config::BuilderConfig, tasks::block::cfg::PecorinoBlockEnv};
33
use alloy::{
44
consensus::{SignableTransaction, TxEip1559, TxEnvelope},
55
primitives::{Address, FixedBytes, TxKind, U256},
@@ -32,7 +32,6 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
3232
sequencer_key: None,
3333
builder_key: "0000000000000000000000000000000000000000000000000000000000000000".into(),
3434
block_confirmation_buffer: 1,
35-
3635
builder_rewards_address: Address::default(),
3736
rollup_block_gas_limit: 3_000_000_000,
3837
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
@@ -9,7 +9,7 @@ mod tests {
99
signers::local::PrivateKeySigner,
1010
};
1111
use builder::{
12-
tasks::block::Simulator,
12+
tasks::block::sim::Simulator,
1313
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
1414
};
1515
use init4_bin_base::utils::calc::SlotCalculator;

0 commit comments

Comments
 (0)