Skip to content

Commit 27f35cb

Browse files
committed
fix: continue block integration
1 parent d3d3f0c commit 27f35cb

File tree

7 files changed

+71
-217
lines changed

7 files changed

+71
-217
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,4 @@ tokio = { version = "1.36.0", features = ["full", "macros", "rt-multi-thread"] }
6464

6565
async-trait = "0.1.80"
6666
oauth2 = "4.4.2"
67-
chrono = "0.4.41"
6867
tokio-stream = "0.1.17"

bin/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn main() -> eyre::Result<()> {
2626
let (block_env, env_jh) = env_task.spawn();
2727

2828
// Spawn the cache system
29-
let cache_system = config.spawn_cache_system(block_env);
29+
let cache_system = config.spawn_cache_system(block_env.clone());
3030

3131
// Prep providers and contracts
3232
let (host_provider, quincey) =
@@ -46,7 +46,7 @@ async fn main() -> eyre::Result<()> {
4646
let (submit_channel, submit_jh) = submit.spawn();
4747

4848
// Set up the simulator
49-
let sim = Simulator::new(&config, ru_provider.clone());
49+
let sim = Simulator::new(&config, ru_provider.clone(), block_env);
5050
let build_jh = sim.spawn_simulator_task(constants, cache_system.sim_cache, submit_channel);
5151

5252
// Start the healthcheck server

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
quincey::Quincey,
33
signer::{LocalOrAws, SignerError},
44
tasks::{
5+
block::cfg::SignetCfgEnv,
56
cache::{BundlePoller, CacheSystem, CacheTask, TxPoller},
67
env::EnvTask,
78
oauth::{Authenticator, SharedToken},
@@ -278,4 +279,9 @@ impl BuilderConfig {
278279

279280
CacheSystem { cache_task, tx_poller, bundle_poller, sim_cache }
280281
}
282+
283+
/// Create a [`SignetCfgEnv`] using this config.
284+
pub const fn cfg_env(&self) -> SignetCfgEnv {
285+
SignetCfgEnv { chain_id: self.ru_chain_id }
286+
}
281287
}

src/tasks/block/cfg.rs

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
//! 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;
2+
use trevm::revm::{context::CfgEnv, primitives::hardfork::SpecId};
133

144
/// PecorinoCfg holds network-level configuration values.
155
#[derive(Debug, Clone, Copy)]
16-
pub struct PecorinoCfg {}
6+
pub struct SignetCfgEnv {
7+
/// The chain ID.
8+
pub chain_id: u64,
9+
}
1710

18-
impl trevm::Cfg for PecorinoCfg {
11+
impl trevm::Cfg for SignetCfgEnv {
1912
/// Fills the configuration environment with Pecorino-specific values.
2013
///
2114
/// # Arguments
@@ -24,76 +17,7 @@ impl trevm::Cfg for PecorinoCfg {
2417
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
2518
let CfgEnv { chain_id, spec, .. } = cfg_env;
2619

27-
*chain_id = signet_constants::pecorino::RU_CHAIN_ID;
20+
*chain_id = self.chain_id;
2821
*spec = SpecId::default();
2922
}
3023
}
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/sim.rs

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
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;
5-
use crate::{
6-
config::{BuilderConfig, RuProvider},
7-
tasks::block::cfg::PecorinoCfg,
8-
};
9-
use alloy::{
10-
eips::{BlockId, BlockNumberOrTag::Latest},
11-
network::Ethereum,
12-
providers::Provider,
13-
};
14-
use chrono::{DateTime, Utc};
15-
use eyre::{Context, bail};
4+
use crate::config::{BuilderConfig, RuProvider};
5+
use alloy::{eips::BlockId, network::Ethereum, providers::Provider};
166
use init4_bin_base::{
17-
deps::tracing::{debug, error, info, warn},
7+
deps::tracing::{debug, error},
188
utils::calc::SlotCalculator,
199
};
2010
use signet_sim::{BlockBuild, BuiltBlock, SimCache};
2111
use signet_types::constants::SignetSystemConstants;
2212
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
2313
use tokio::{
24-
sync::mpsc::{self},
14+
sync::{
15+
mpsc::{self},
16+
watch,
17+
},
2518
task::JoinHandle,
2619
};
2720
use trevm::revm::{
21+
context::BlockEnv,
2822
database::{AlloyDB, WrapDatabaseAsync},
2923
inspector::NoOpInspector,
3024
};
@@ -38,6 +32,9 @@ pub struct Simulator {
3832
pub config: BuilderConfig,
3933
/// A provider that cannot sign transactions, used for interacting with the rollup.
4034
pub ru_provider: RuProvider,
35+
36+
/// The block configuration environment on which to simulate
37+
pub block_env: watch::Receiver<Option<BlockEnv>>,
4138
}
4239

4340
type AlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;
@@ -53,8 +50,12 @@ impl Simulator {
5350
/// # Returns
5451
///
5552
/// A new `Simulator` instance.
56-
pub fn new(config: &BuilderConfig, ru_provider: RuProvider) -> Self {
57-
Self { config: config.clone(), ru_provider }
53+
pub fn new(
54+
config: &BuilderConfig,
55+
ru_provider: RuProvider,
56+
block_env: watch::Receiver<Option<BlockEnv>>,
57+
) -> Self {
58+
Self { config: config.clone(), ru_provider, block_env }
5859
}
5960

6061
/// Get the slot calculator.
@@ -78,14 +79,14 @@ impl Simulator {
7879
constants: SignetSystemConstants,
7980
sim_items: SimCache,
8081
finish_by: Instant,
81-
block: PecorinoBlockEnv,
82+
block: BlockEnv,
8283
) -> eyre::Result<BuiltBlock> {
8384
let db = self.create_db().await.unwrap();
8485

8586
let block_build: BlockBuild<_, NoOpInspector> = BlockBuild::new(
8687
db,
8788
constants,
88-
PecorinoCfg {},
89+
self.config.cfg_env(),
8990
block,
9091
finish_by,
9192
self.config.concurrency_limit,
@@ -146,14 +147,10 @@ impl Simulator {
146147
let sim_cache = cache.clone();
147148
let finish_by = self.calculate_deadline();
148149

149-
let block_env = match self.next_block_env(finish_by).await {
150-
Ok(block) => block,
151-
Err(err) => {
152-
error!(err = %err, "failed to configure next block");
153-
break;
154-
}
155-
};
156-
info!(block_env = ?block_env, "created block");
150+
// If no env, skip this run
151+
let Some(block_env) = self.block_env.borrow().clone() else { return };
152+
153+
debug!(block_env = ?block_env, "building on block");
157154

158155
match self.handle_build(constants, sim_cache, finish_by, block_env).await {
159156
Ok(block) => {
@@ -210,68 +207,4 @@ impl Simulator {
210207
let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync::new(alloy_db).unwrap();
211208
Some(wrapped_db)
212209
}
213-
214-
/// Prepares the next block environment.
215-
///
216-
/// Prepares the next block environment to load into the simulator by fetching the latest block number,
217-
/// assigning the correct next block number, checking the basefee, and setting the timestamp,
218-
/// reward address, and gas configuration for the block environment based on builder configuration.
219-
///
220-
/// # Arguments
221-
///
222-
/// - finish_by: The deadline at which block simulation will end.
223-
async fn next_block_env(&self, finish_by: Instant) -> eyre::Result<PecorinoBlockEnv> {
224-
let remaining = finish_by.duration_since(Instant::now());
225-
let finish_time = SystemTime::now() + remaining;
226-
let deadline: DateTime<Utc> = finish_time.into();
227-
debug!(deadline = %deadline, "preparing block env");
228-
229-
// Fetch the latest block number and increment it by 1
230-
let latest_block_number = match self.ru_provider.get_block_number().await {
231-
Ok(num) => num,
232-
Err(err) => {
233-
error!(%err, "RPC error during block build");
234-
bail!(err)
235-
}
236-
};
237-
debug!(next_block_num = latest_block_number + 1, "preparing block env");
238-
239-
// Fetch the basefee from previous block to calculate gas for this block
240-
let basefee = match self.get_basefee().await? {
241-
Some(basefee) => basefee,
242-
None => {
243-
warn!("get basefee failed - RPC error likely occurred");
244-
todo!()
245-
}
246-
};
247-
debug!(basefee = basefee, "setting basefee");
248-
249-
// Craft the Block environment to pass to the simulator
250-
let block_env = PecorinoBlockEnv::new(
251-
self.config.clone(),
252-
latest_block_number + 1,
253-
deadline.timestamp() as u64,
254-
basefee,
255-
);
256-
debug!(block_env = ?block_env, "prepared block env");
257-
258-
Ok(block_env)
259-
}
260-
261-
/// Returns the basefee of the latest block.
262-
///
263-
/// # Returns
264-
///
265-
/// The basefee of the previous (latest) block if the request was successful,
266-
/// or a sane default if the RPC failed.
267-
async fn get_basefee(&self) -> eyre::Result<Option<u64>> {
268-
let Some(block) =
269-
self.ru_provider.get_block_by_number(Latest).await.wrap_err("basefee error")?
270-
else {
271-
return Ok(None);
272-
};
273-
274-
debug!(basefee = ?block.header.base_fee_per_gas, "basefee found");
275-
Ok(block.header.base_fee_per_gas)
276-
}
277210
}

src/test_utils.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
//! Test utilities for testing builder tasks
2-
use crate::{config::BuilderConfig, tasks::block::cfg::PecorinoBlockEnv};
2+
use crate::config::BuilderConfig;
33
use alloy::{
44
consensus::{SignableTransaction, TxEip1559, TxEnvelope},
5-
primitives::{Address, FixedBytes, TxKind, U256},
5+
primitives::{Address, B256, TxKind, U256},
66
signers::{SignerSync, local::PrivateKeySigner},
77
};
8-
use chrono::{DateTime, Utc};
98
use eyre::Result;
109
use init4_bin_base::{
1110
deps::tracing_subscriber::{
1211
EnvFilter, Layer, fmt, layer::SubscriberExt, registry, util::SubscriberInitExt,
1312
},
1413
utils::calc::SlotCalculator,
1514
};
16-
use std::{
17-
str::FromStr,
18-
time::{Instant, SystemTime},
19-
};
15+
use std::str::FromStr;
16+
use trevm::revm::{context::BlockEnv, context_interface::block::BlobExcessGasAndPrice};
2017

2118
/// Sets up a block builder with test values
2219
pub fn setup_test_config() -> Result<BuilderConfig> {
@@ -87,18 +84,19 @@ pub fn test_block_env(
8784
config: BuilderConfig,
8885
number: u64,
8986
basefee: u64,
90-
finish_by: Instant,
91-
) -> PecorinoBlockEnv {
92-
let remaining = finish_by.duration_since(Instant::now());
93-
let finish_time = SystemTime::now() + remaining;
94-
let deadline: DateTime<Utc> = finish_time.into();
95-
96-
PecorinoBlockEnv {
87+
timestamp: u64,
88+
) -> BlockEnv {
89+
BlockEnv {
9790
number,
98-
beneficiary: Address::repeat_byte(0),
99-
timestamp: deadline.timestamp() as u64,
91+
beneficiary: Address::repeat_byte(1),
92+
timestamp,
10093
gas_limit: config.rollup_block_gas_limit,
10194
basefee,
102-
prevrandao: Some(FixedBytes::random()),
95+
difficulty: U256::ZERO,
96+
prevrandao: Some(B256::random()),
97+
blob_excess_gas_and_price: Some(BlobExcessGasAndPrice {
98+
excess_blob_gas: 0,
99+
blob_gasprice: 0,
100+
}),
103101
}
104102
}

0 commit comments

Comments
 (0)