Skip to content

Commit

Permalink
Fetch leaves in serial in commit task
Browse files Browse the repository at this point in the history
This should prevent the CPU from spiking (in both the commit task
and query server) when the commit task falls behind and has to fetch
many leaves at once
  • Loading branch information
jbearer committed Mar 5, 2024
1 parent 4c2357c commit 83cceb5
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions sequencer/src/hotshot_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use async_trait::async_trait;
use clap::Parser;
use contract_bindings::hot_shot::{HotShot, Qc};
use ethers::prelude::*;
use futures::{future::join_all, stream::StreamExt};
use futures::{
future,
stream::{self, StreamExt},
};
use hotshot_query_service::availability::LeafQueryData;
use sequencer_utils::{commitment_to_u256, connect_rpc, contract_send, Signer};
use std::error::Error;
Expand Down Expand Up @@ -186,28 +189,26 @@ async fn sync_with_l1(
};

// Download leaves between `contract_block_height` and `hotshot_block_height`.
let leaves = join_all(
(contract_block_height..hotshot_block_height)
.take(max_blocks)
.map(|height| hotshot.get_leaf(height)),
)
.await;
// It is possible that we failed to fetch some leaves. But as long as we successfully fetched a
// prefix of the desired list (since leaves must be sent to the contract in order) we can make
// some progress.
let leaves = leaves
.into_iter()
.scan(contract_block_height, |height, leaf| match leaf {
Ok(leaf) => {
*height += 1;
Some(leaf)
}
Err(err) => {
tracing::error!("error fetching leaf {height}: {err}");
None
}
let leaves = stream::iter(contract_block_height..hotshot_block_height)
.take(max_blocks)
.then(|height| hotshot.get_leaf(height))
// It is possible that we failed to fetch some leaves. But as long as we successfully
// fetched a prefix of the desired list (since leaves must be sent to the contract in order)
// we can make some progress.
.scan(contract_block_height, |height, leaf| {
future::ready(match leaf {
Ok(leaf) => {
*height += 1;
Some(leaf)
}
Err(err) => {
tracing::error!("error fetching leaf {height}: {err}");
None
}
})
})
.collect::<Vec<_>>();
.collect::<Vec<_>>()
.await;
if leaves.is_empty() {
return Err(SyncError::Other(anyhow!("failed to fetch any leaves")));
}
Expand Down

0 comments on commit 83cceb5

Please sign in to comment.