Skip to content

Commit

Permalink
fix(builder): only reset sleep future when it has elapsed (#63)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Evalir authored Oct 28, 2024
1 parent 8eb4227 commit c2d83b5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/builder/src/tasks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ impl BlockBuilder {

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

let mut sleep =
Box::pin(tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer)));

let handle = tokio::spawn(
async move {
loop {
let sleep: tokio::time::Sleep = tokio::time::sleep(Duration::from_secs(self.incoming_transactions_buffer));
tokio::pin!(sleep);

select! {
biased;
Expand All @@ -118,6 +119,10 @@ impl BlockBuilder {
break
}
}

// Reset the sleep timer, as we want to do so when (and only when) our sleep future has elapsed,
// irrespective of whether we have any blocks to build.
sleep.as_mut().reset(tokio::time::Instant::now() + Duration::from_secs(self.incoming_transactions_buffer));
}
item_res = inbound.recv() => {
match item_res {
Expand Down

0 comments on commit c2d83b5

Please sign in to comment.