Skip to content

Commit 1ee991f

Browse files
committed
fixup clippy and fmt
1 parent dd4b377 commit 1ee991f

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

bin/builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ async fn main() -> eyre::Result<()> {
5252
let (submit_channel, submit_jh) = submit.spawn();
5353

5454
let sim_items = SimCache::new();
55-
56-
let slot_calculator = SlotCalculator::pecorino();
55+
let slot_calculator = SlotCalculator::new(config.start_timestamp, config.chain_offset, config.target_slot_time);
5756
let builder = Arc::new(BlockBuilder::new(&config, ru_provider.clone(), slot_calculator));
5857

5958
let sim_cache_jh =

src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl BuilderConfig {
259259
}
260260

261261
/// Loads the Signet system constants for Pecorino.
262-
pub fn load_pecorino_constants(&self) -> SignetSystemConstants {
262+
pub const fn load_pecorino_constants(&self) -> SignetSystemConstants {
263263
let host = HostConfig::new(
264264
self.host_chain_id,
265265
149984,

src/tasks/block.rs

+57-18
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@ use trevm::{
2424
},
2525
};
2626

27-
/// Pecorino Chain ID
27+
/// Pecorino Chain ID used for the Pecorino network.
2828
pub const PECORINO_CHAIN_ID: u64 = 14174;
2929

30-
/// BlockBuilder is a task that periodically builds a block then sends it for
31-
/// signing and submission.
30+
/// `BlockBuilder` is responsible for periodically building blocks and submitting them for signing and inclusion in the blockchain.
3231
#[derive(Debug)]
3332
pub struct BlockBuilder {
34-
/// Configuration.
33+
/// Configuration for the builder.
3534
pub config: BuilderConfig,
36-
/// A provider that cannot sign transactions.
35+
/// A provider that cannot sign transactions, used for interacting with the rollup.
3736
pub ru_provider: WalletlessProvider,
38-
/// The slot calculator for waking up and sleeping the builder correctly
37+
/// The slot calculator for determining when to wake up and build blocks.
3938
pub slot_calculator: SlotCalculator,
4039
}
4140

4241
impl BlockBuilder {
43-
/// Creates a new block builder that builds blocks based on the given provider.
42+
/// Creates a new `BlockBuilder` instance.
43+
///
44+
/// # Arguments
45+
/// - `config`: The configuration for the builder.
46+
/// - `ru_provider`: A provider for interacting with the rollup.
47+
/// - `slot_calculator`: A slot calculator for managing block timing.
48+
///
49+
/// # Returns
50+
/// A new `BlockBuilder` instance.
4451
pub fn new(
4552
config: &BuilderConfig,
4653
ru_provider: WalletlessProvider,
@@ -50,6 +57,14 @@ impl BlockBuilder {
5057
}
5158

5259
/// Handles building a single block.
60+
///
61+
/// # Arguments
62+
/// - `constants`: The system constants for the rollup.
63+
/// - `sim_items`: The simulation cache containing transactions and bundles.
64+
/// - `finish_by`: The deadline by which the block must be built.
65+
///
66+
/// # Returns
67+
/// A `Result` containing the built block or an error.
5368
pub async fn handle_build(
5469
&self,
5570
constants: SignetSystemConstants,
@@ -74,14 +89,22 @@ impl BlockBuilder {
7489
Ok(block)
7590
}
7691

77-
/// Scans the tx and bundle receivers for new items and adds them to the cache.
92+
/// Spawns a task to handle incoming transactions and bundles, adding them to the simulation cache.
93+
///
94+
/// # Arguments
95+
/// - `tx_receiver`: A channel receiver for incoming transactions.
96+
/// - `bundle_receiver`: A channel receiver for incoming bundles.
97+
/// - `cache`: The simulation cache to store the received items.
98+
///
99+
/// # Returns
100+
/// A `JoinHandle` for the spawned task.
78101
pub fn spawn_cache_handler(
79102
self: Arc<Self>,
80103
mut tx_receiver: mpsc::UnboundedReceiver<TxEnvelope>,
81104
mut bundle_receiver: mpsc::UnboundedReceiver<Bundle>,
82105
cache: SimCache,
83106
) -> JoinHandle<()> {
84-
let jh = tokio::spawn(async move {
107+
tokio::spawn(async move {
85108
loop {
86109
select! {
87110
maybe_tx = tx_receiver.recv() => {
@@ -96,18 +119,25 @@ impl BlockBuilder {
96119
}
97120
}
98121
}
99-
});
100-
jh
122+
})
101123
}
102124

103125
/// Spawns the block building task.
126+
///
127+
/// # Arguments
128+
/// - `constants`: The system constants for the rollup.
129+
/// - `cache`: The simulation cache containing transactions and bundles.
130+
/// - `submit_sender`: A channel sender for submitting built blocks.
131+
///
132+
/// # Returns
133+
/// A `JoinHandle` for the spawned task.
104134
pub fn spawn_builder_task(
105135
self: Arc<Self>,
106136
constants: SignetSystemConstants,
107137
cache: SimCache,
108138
submit_sender: mpsc::UnboundedSender<BuiltBlock>,
109139
) -> JoinHandle<()> {
110-
let jh = tokio::spawn(async move {
140+
tokio::spawn(async move {
111141
loop {
112142
let sim_cache = cache.clone();
113143

@@ -126,19 +156,24 @@ impl BlockBuilder {
126156
}
127157
}
128158
}
129-
});
130-
jh
159+
})
131160
}
132161

133-
/// Returns the instant at which simulation must stop.
162+
/// Calculates the deadline for the current block simulation.
163+
///
164+
/// # Returns
165+
/// An `Instant` representing the deadline.
134166
pub fn calculate_deadline(&self) -> Instant {
135167
let now = SystemTime::now();
136168
let unix_seconds = now.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs();
137169

138170
Instant::now().checked_add(Duration::from_secs(unix_seconds)).unwrap()
139171
}
140172

141-
/// Creates an AlloyDB from a rollup provider
173+
/// Creates an `AlloyDB` instance from the rollup provider.
174+
///
175+
/// # Returns
176+
/// An `Option` containing the wrapped database or `None` if an error occurs.
142177
async fn create_db(&self) -> Option<WrapAlloyDatabaseAsync> {
143178
let latest = match self.ru_provider.get_block_number().await {
144179
Ok(block_number) => block_number,
@@ -155,7 +190,7 @@ impl BlockBuilder {
155190
}
156191
}
157192

158-
/// The wrapped alloy database type that is compatible with Db + DatabaseRef
193+
/// The wrapped alloy database type that is compatible with `Db` and `DatabaseRef`.
159194
type WrapAlloyDatabaseAsync = WrapDatabaseAsync<
160195
AlloyDB<
161196
alloy::network::Ethereum,
@@ -178,13 +213,17 @@ type WrapAlloyDatabaseAsync = WrapDatabaseAsync<
178213
>,
179214
>;
180215

181-
/// Configuration struct for Pecorino network values
216+
/// Configuration struct for Pecorino network values.
182217
#[derive(Debug, Clone)]
183218
pub struct PecorinoCfg {}
184219

185220
impl Copy for PecorinoCfg {}
186221

187222
impl trevm::Cfg for PecorinoCfg {
223+
/// Fills the configuration environment with Pecorino-specific values.
224+
///
225+
/// # Arguments
226+
/// - `cfg_env`: The configuration environment to be filled.
188227
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
189228
let CfgEnv { chain_id, spec, .. } = cfg_env;
190229

tests/block_builder_test.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Tests for the block building task.
12
#[cfg(test)]
23
mod tests {
34
use alloy::{
@@ -17,6 +18,11 @@ mod tests {
1718
};
1819
use tokio::{sync::mpsc::unbounded_channel, time::timeout};
1920

21+
/// Tests the `handle_build` method of the `BlockBuilder`.
22+
///
23+
/// This test sets up a simulated environment using Anvil, creates a block builder,
24+
/// and verifies that the block builder can successfully build a block containing
25+
/// transactions from multiple senders.
2026
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2127
async fn test_handle_build() {
2228
setup_logging();
@@ -65,7 +71,11 @@ mod tests {
6571
assert!(got.unwrap().tx_count() == 2);
6672
}
6773

68-
/// Tests the full block builder loop
74+
/// Tests the full block builder loop, including transaction ingestion and block simulation.
75+
///
76+
/// This test sets up a simulated environment using Anvil, creates a block builder,
77+
/// and verifies that the builder can process incoming transactions and produce a block
78+
/// within a specified timeout.
6979
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
7080
async fn test_spawn() {
7181
setup_logging();
@@ -96,16 +106,18 @@ mod tests {
96106
config.chain_offset,
97107
config.target_slot_time,
98108
);
99-
let sim_cache = SimCache::new();
100109
let builder = Arc::new(BlockBuilder::new(&config, ru_provider.clone(), slot_calculator));
101110

111+
// Create a shared sim cache
112+
let sim_cache = SimCache::new();
113+
102114
// Create a sim cache and start filling it with items
103-
let _ =
104-
builder.clone().spawn_cache_handler(tx_receiver, bundle_receiver, sim_cache.clone());
115+
builder.clone().spawn_cache_handler(tx_receiver, bundle_receiver, sim_cache.clone());
105116

106117
// Finally, Kick off the block builder task.
107-
let _ = builder.clone().spawn_builder_task(constants, sim_cache.clone(), block_sender);
118+
builder.clone().spawn_builder_task(constants, sim_cache.clone(), block_sender);
108119

120+
// Feed in transactions to the tx_sender and wait for the block to be simulated
109121
let tx_1 = new_signed_tx(&test_key_0, 0, U256::from(1_f64), 11_000).unwrap();
110122
let tx_2 = new_signed_tx(&test_key_1, 0, U256::from(2_f64), 10_000).unwrap();
111123
tx_sender.send(tx_1).unwrap();
@@ -114,8 +126,9 @@ mod tests {
114126
// Wait for a block with timeout
115127
let result = timeout(Duration::from_secs(5), block_receiver.recv()).await;
116128
assert!(result.is_ok(), "Did not receive block within 5 seconds");
129+
130+
// Assert on the block
117131
let block = result.unwrap();
118-
dbg!(&block);
119132
assert!(block.is_some(), "Block channel closed without receiving a block");
120133
assert!(block.unwrap().tx_count() == 2); // TODO: Why is this failing? I'm seeing EVM errors but haven't tracked them down yet.
121134
}

0 commit comments

Comments
 (0)