Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit c2d83b5

Browse files
authored
fix(builder): only reset sleep future when it has elapsed (#63)
* fix(builder): only reset sleep future when it has elapsed Right now, when spawning the block builder task, we're spawning the sleep future inside the loop. This means that any of the two `select!` branches will re-set the sleep timer. This is particularly bad on the branch that receives txs: It means that if we constantly received txs in between the incoming transactions buffer, the first branch would never trigger, as it requires the sleep future to elapse, therefore never building a block. This would effectively DDoS the builder. Fixes ENG-609 * fix: moved pin to heap from stack to avoid lifetime issues * chore: docs
1 parent 8eb4227 commit c2d83b5

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

crates/builder/src/tasks/block.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ impl BlockBuilder {
101101

102102
let (sender, mut inbound) = mpsc::unbounded_channel();
103103

104+
let mut sleep =
105+
Box::pin(tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer)));
106+
104107
let handle = tokio::spawn(
105108
async move {
106109
loop {
107-
let sleep: tokio::time::Sleep = tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer));
108-
tokio::pin!(sleep);
109110

110111
select! {
111112
biased;
@@ -118,6 +119,10 @@ impl BlockBuilder {
118119
break
119120
}
120121
}
122+
123+
// Reset the sleep timer, as we want to do so when (and only when) our sleep future has elapsed,
124+
// irrespective of whether we have any blocks to build.
125+
sleep.as_mut().reset(tokio::time::Instant::now() + Duration::from_secs(self.incoming_transactions_buffer));
121126
}
122127
item_res = inbound.recv() => {
123128
match item_res {

0 commit comments

Comments
 (0)