Skip to content

Commit 355998a

Browse files
committed
add logs and optimise block handling in fork choice logic
1 parent 6351f28 commit 355998a

File tree

1 file changed

+10
-8
lines changed
  • crates/chain-orchestrator/src

1 file changed

+10
-8
lines changed

crates/chain-orchestrator/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const L1_MESSAGE_QUEUE_HASH_MASK: B256 =
8181
b256!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000");
8282

8383
/// The number of headers to fetch in each request when fetching headers from peers.
84-
const HEADER_FETCH_COUNT: u64 = 4000;
84+
const HEADER_FETCH_COUNT: u64 = 100;
8585

8686
/// The size of the event channel used to broadcast events to listeners.
8787
const EVENT_CHANNEL_SIZE: usize = 5000;
@@ -834,7 +834,7 @@ impl<
834834
// If the received block number has a block number greater than the current head by more
835835
// than the optimistic sync threshold, we optimistically sync the chain.
836836
if received_block_number > current_head_number + self.config.optimistic_sync_threshold() {
837-
tracing::debug!(target: "scroll::chain_orchestrator", ?received_block_number, ?current_head_number, "Received new block from peer with block number greater than current head by more than the optimistic sync threshold");
837+
tracing::trace!(target: "scroll::chain_orchestrator", ?received_block_number, ?current_head_number, "Received new block from peer with block number greater than current head by more than the optimistic sync threshold");
838838
let block_info = BlockInfo {
839839
number: received_block_number,
840840
hash: block_with_peer.block.header.hash_slow(),
@@ -856,10 +856,11 @@ impl<
856856
{
857857
// Fetch the headers for the received block until we can reconcile it with the current
858858
// chain head.
859-
let block_number_diff = received_block_number - current_head_number;
859+
let fetch_count = received_block_number - current_head_number;
860860
let new_headers = if received_block_number > current_head_number + 1 {
861+
tracing::trace!(target: "scroll::chain_orchestrator", ?received_block_hash, ?received_block_number, ?current_head_number, fetch_count, "Fetching headers to extend chain");
861862
self.block_client
862-
.get_full_block_range(received_block_hash, block_number_diff)
863+
.get_full_block_range(received_block_hash, fetch_count)
863864
.await
864865
.into_iter()
865866
.rev()
@@ -874,6 +875,7 @@ impl<
874875
if new_headers.first().expect("at least one header exists").parent_hash ==
875876
current_head_hash
876877
{
878+
tracing::trace!(target: "scroll::chain_orchestrator", ?received_block_hash, ?received_block_number, "Received block from peer that extends the current head");
877879
let chain_import = self.import_chain(new_headers, block_with_peer).await?;
878880
return Ok(Some(ChainOrchestratorEvent::ChainExtended(chain_import)));
879881
}
@@ -948,17 +950,17 @@ impl<
948950
let parent_hash = new_headers.front().expect("at least one header exists").parent_hash;
949951
let parent_number = new_headers.front().expect("at least one header exists").number - 1;
950952
let fetch_count = HEADER_FETCH_COUNT.min(parent_number - current_safe_head.number);
953+
tracing::trace!(target: "scroll::chain_orchestrator", ?received_block_hash, ?received_block_number, ?parent_hash, ?parent_number, %current_safe_head, fetch_count, "Fetching headers to find common ancestor for fork");
951954
let headers: Vec<ScrollBlock> = self
952955
.block_client
953956
.get_full_block_range(parent_hash, fetch_count)
954957
.await
955958
.into_iter()
956-
.rev()
957959
.map(|b| b.into_block())
958960
.collect();
959961

960962
let mut index = None;
961-
for (i, header) in headers.iter().enumerate().rev() {
963+
for (i, header) in headers.iter().enumerate() {
962964
let current_block = self
963965
.l2_client
964966
.get_block_by_number(header.number.into())
@@ -976,7 +978,7 @@ impl<
976978

977979
if let Some(index) = index {
978980
tracing::trace!(target: "scroll::chain_orchestrator", ?received_block_hash, ?received_block_number, common_ancestor = ?headers[index].hash_slow(), common_ancestor_number = headers[index].number, "Found common ancestor for fork - reorging to new chain");
979-
for header in headers.into_iter().skip(index + 1).rev() {
981+
for header in headers.into_iter().take(index) {
980982
new_headers.push_front(header);
981983
}
982984
let chain_import = self.import_chain(new_headers.into(), block_with_peer).await?;
@@ -985,7 +987,7 @@ impl<
985987

986988
// If we did not find a common ancestor, we add all the fetched headers to the front of
987989
// the deque and continue fetching.
988-
for header in headers.into_iter().rev() {
990+
for header in headers.into_iter() {
989991
new_headers.push_front(header);
990992
}
991993
}

0 commit comments

Comments
 (0)