Skip to content

Commit a58d4e3

Browse files
committed
Fix: Refactor checkpoint_url sync
1 parent f2b7a77 commit a58d4e3

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

bin/ethlambda/src/checkpoint_sync.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum CheckpointSyncError {
5858
BlockHeaderJustifiedRootMismatch,
5959
#[error("anchor block does not match anchor state")]
6060
AnchorPairingMismatch,
61+
#[error("all checkpoint peers failed")]
62+
AllPeersFailed,
6163
}
6264

6365
/// Build the HTTP client used for checkpoint sync fetches.

bin/ethlambda/src/main.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ async fn fetch_initial_state(
626626
) -> Result<Store, checkpoint_sync::CheckpointSyncError> {
627627
let validators = genesis.validators();
628628

629-
let Some((first_url, rest_urls)) = checkpoint_urls.split_first() else {
629+
if checkpoint_urls.is_empty() {
630630
info!("No checkpoint sync URL provided, initializing from genesis state");
631631
let genesis_state = State::from_genesis(genesis.genesis_time, validators);
632632
return Ok(Store::from_anchor_state(backend, genesis_state));
@@ -639,31 +639,26 @@ async fn fetch_initial_state(
639639
url_count = checkpoint_urls.len(),
640640
"Starting checkpoint sync"
641641
);
642-
let mut result = try_checkpoint_url(first_url, genesis.genesis_time, &validators).await;
643-
if let Err(err) = &result {
644-
if rest_urls.is_empty() {
645-
warn!(%first_url, %err, "Checkpoint sync failed for this peer; no more URLs to try");
646-
} else {
647-
warn!(%first_url, %err, "Checkpoint sync failed for this peer; trying next URL");
648-
}
649-
}
650642

651-
for (idx, url) in rest_urls.iter().enumerate() {
652-
if result.is_ok() {
653-
break;
654-
}
655-
result = try_checkpoint_url(url, genesis.genesis_time, &validators).await;
656-
if let Err(err) = &result {
657-
let has_more = idx + 1 < rest_urls.len();
658-
if has_more {
659-
warn!(%url, %err, "Checkpoint sync failed for this peer; trying next URL");
660-
} else {
661-
warn!(%url, %err, "Checkpoint sync failed for this peer; no more URLs to try");
643+
let mut iter = checkpoint_urls.iter().peekable();
644+
let (state, signed_block) = loop {
645+
let Some(url) = iter.next() else {
646+
return Err(checkpoint_sync::CheckpointSyncError::AllPeersFailed);
647+
};
648+
match try_checkpoint_url(url, genesis.genesis_time, &validators).await {
649+
Ok(pair) => {
650+
info!(%url, "Checkpoint sync successful with this peer");
651+
break pair;
652+
}
653+
Err(err) => {
654+
if iter.peek().is_some() {
655+
warn!(%url, %err, "Checkpoint sync failed for this peer; trying next URL");
656+
} else {
657+
warn!(%url, %err, "Checkpoint sync failed for this peer; no more URLs to try");
658+
}
662659
}
663660
}
664-
}
665-
666-
let (state, signed_block) = result?;
661+
};
667662

668663
info!(
669664
slot = state.slot,

0 commit comments

Comments
 (0)