Skip to content

fix(l1): fcu not triggering sync if snap is enabled + re-enable snap sync hive test #2605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/pr-main_l1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,10 @@ jobs:
simulation: ethereum/sync
test_pattern: ""
ethrex_flags: "--syncmode full"
# Flaky test, reenable when fixed
# - name: "Sync snap"
# simulation: ethereum/sync
# test_pattern: ""
# ethrex_flags: "--syncmode snap"
- name: "Sync snap"
simulation: ethereum/sync
test_pattern: ""
ethrex_flags: "--syncmode snap"
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand Down
50 changes: 24 additions & 26 deletions crates/networking/p2p/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{

use ethrex_blockchain::Blockchain;
use ethrex_common::H256;
use ethrex_storage::{error::StoreError, Store};
use ethrex_storage::Store;
use tokio::{
sync::Mutex,
time::{sleep, Duration},
Expand All @@ -18,12 +18,6 @@ use crate::{
sync::{SyncMode, Syncer},
};

#[derive(Debug)]
pub enum SyncStatus {
Active(SyncMode),
Inactive,
}

/// Abstraction to interact with the active sync process without disturbing it
#[derive(Debug)]
pub struct SyncManager {
Expand Down Expand Up @@ -80,35 +74,48 @@ impl SyncManager {
}
}

/// Sets the latest fcu head and starts the next sync cycle if the syncer is currently inactive
pub fn sync_to_head(&self, fcu_head: H256) {
self.set_head(fcu_head);
if !self.is_active() {
self.start_sync();
}
}

/// Returns the syncer's current syncmode (either snap or full)
pub fn sync_mode(&self) -> SyncMode {
if self.snap_enabled.load(Ordering::Relaxed) {
SyncMode::Snap
} else {
SyncMode::Full
}
}

/// Updates the last fcu head. This may be used on the next sync cycle if needed
pub fn set_head(&self, fcu_head: H256) {
fn set_head(&self, fcu_head: H256) {
if let Ok(mut latest_fcu_head) = self.last_fcu_head.try_lock() {
*latest_fcu_head = fcu_head;
} else {
warn!("Failed to update latest fcu head for syncing")
}
}

/// Returns the current sync status, either active or inactive and what the current syncmode is in the case of active
pub fn status(&self) -> Result<SyncStatus, StoreError> {
Ok(if self.syncer.try_lock().is_err() {
SyncStatus::Active(self.sync_mode())
} else {
SyncStatus::Inactive
})
/// Returns true is the syncer is active
fn is_active(&self) -> bool {
self.syncer.try_lock().is_err()
}

/// Attempts to sync to the last received fcu head
/// Will do nothing if the syncer is already involved in a sync process
/// If the sync process would require multiple sync cycles (such as snap sync), starts all required sync cycles until the sync is complete
pub fn start_sync(&self) {
fn start_sync(&self) {
let syncer = self.syncer.clone();
let store = self.store.clone();
let sync_head = self.last_fcu_head.clone();

tokio::spawn(async move {
let Ok(Some(current_head)) = store.get_latest_canonical_block_hash().await else {
tracing::error!("Failed to fecth latest canonical block, unable to sync");
tracing::error!("Failed to fetch latest canonical block, unable to sync");
return;
};

Expand Down Expand Up @@ -148,13 +155,4 @@ impl SyncManager {
}
});
}

/// Returns the syncer's current syncmode (either snap or full)
pub fn sync_mode(&self) -> SyncMode {
if self.snap_enabled.load(Ordering::Relaxed) {
SyncMode::Snap
} else {
SyncMode::Full
}
}
}
9 changes: 6 additions & 3 deletions crates/networking/rpc/engine/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ async fn handle_forkchoice(
}

if context.syncer.sync_mode() == SyncMode::Snap {
context.syncer.set_head(fork_choice_state.head_block_hash);
context
.syncer
.sync_to_head(fork_choice_state.head_block_hash);
return Ok((None, PayloadStatus::syncing().into()));
}

Expand Down Expand Up @@ -312,8 +314,9 @@ async fn handle_forkchoice(
.update_sync_status(false)
.await
.map_err(|e| RpcErr::Internal(e.to_string()))?;
context.syncer.set_head(fork_choice_state.head_block_hash);
context.syncer.start_sync();
context
.syncer
.sync_to_head(fork_choice_state.head_block_hash);
Comment on lines +317 to +319
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that now the syncer decides to start the sync!

ForkChoiceResponse::from(PayloadStatus::syncing())
}
InvalidForkChoice::Disconnected(_, _) | InvalidForkChoice::ElementNotFound(_) => {
Expand Down
4 changes: 1 addition & 3 deletions crates/networking/rpc/engine/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,7 @@ async fn try_execute_payload(
.update_sync_status(false)
.await
.map_err(|e| RpcErr::Internal(e.to_string()))?;
context.syncer.set_head(block_hash);
context.syncer.start_sync();

context.syncer.sync_to_head(block_hash);
Ok(PayloadStatus::syncing())
}
// Under the current implementation this is not possible: we always calculate the state
Expand Down
Loading