Skip to content

Commit ada0cd2

Browse files
committed
simplify block loop
1 parent 72f389c commit ada0cd2

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/tasks/block.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloy::{
55
use alloy_primitives::{keccak256, Bytes, B256};
66
use alloy_rlp::Buf;
77
use std::{sync::OnceLock, time::Duration};
8-
use tokio::{select, sync::mpsc, task::JoinHandle};
8+
use tokio::{sync::mpsc, task::JoinHandle};
99
use tracing::Instrument;
1010
use zenith_types::{encode_txns, Alloy2718Coder};
1111

@@ -158,33 +158,25 @@ impl BlockBuilder {
158158
/// Spawn the block builder task, returning the inbound channel to it, and
159159
/// a handle to the running task.
160160
pub fn spawn(mut self, outbound: mpsc::UnboundedSender<InProgressBlock>) -> JoinHandle<()> {
161-
let mut sleep =
162-
Box::pin(tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer)));
163-
164161
tokio::spawn(
165162
async move {
166163
loop {
167-
168-
select! {
169-
biased;
170-
_ = &mut sleep => {
171-
// Build a block
172-
let mut in_progress = InProgressBlock::default();
173-
self.get_transactions(&mut in_progress).await;
174-
self.get_bundles(&mut in_progress).await;
175-
176-
if !in_progress.is_empty() {
177-
tracing::debug!(txns = in_progress.len(), "sending block to submit task");
178-
let in_progress_block = std::mem::take(&mut in_progress);
179-
if outbound.send(in_progress_block).is_err() {
180-
tracing::debug!("downstream task gone");
181-
break
182-
}
183-
}
184-
185-
// Reset the sleep timer, as we want to do so when (and only when) our sleep future has elapsed,
186-
// irrespective of whether we have any blocks to build.
187-
sleep.as_mut().reset(tokio::time::Instant::now() + Duration::from_secs(self.incoming_transactions_buffer));
164+
// sleep the buffer time
165+
tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer))
166+
.await;
167+
168+
// Build a block
169+
let mut in_progress = InProgressBlock::default();
170+
self.get_transactions(&mut in_progress).await;
171+
self.get_bundles(&mut in_progress).await;
172+
173+
// submit the block if it has transactions
174+
if !in_progress.is_empty() {
175+
tracing::debug!(txns = in_progress.len(), "sending block to submit task");
176+
let in_progress_block = std::mem::take(&mut in_progress);
177+
if outbound.send(in_progress_block).is_err() {
178+
tracing::debug!("downstream task gone");
179+
break;
188180
}
189181
}
190182
}

0 commit comments

Comments
 (0)