From 1b68e2b6bac7f4a5391e0eae8a41a4714bb9d36f Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Fri, 6 Dec 2024 16:46:36 +0000 Subject: [PATCH] Fix `is_syncing` API reporting when head is synced optimistically (#8889) --- CHANGELOG.md | 1 + .../beaconrestapi/handlers/v1/node/GetSyncing.java | 10 +++++----- .../handlers/v1/node/GetSyncingTest.java | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d868630e8f9..6e045d3d29c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,3 +15,4 @@ ### Bug Fixes - Added a startup script for unix systems to ensure that when jemalloc is installed the script sets the LD_PRELOAD environment variable to the use the jemalloc library +- Set `is_syncing` to `false` instead of `true` for the `/eth/v1/node/syncing` API endpoint when the head is optimistic and the sync distance is 0 diff --git a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncing.java b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncing.java index 749346dd4de..ba1851c670d 100644 --- a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncing.java +++ b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncing.java @@ -97,12 +97,11 @@ public SyncStatusData( final ExecutionClientDataProvider executionClientDataProvider) { final SyncingStatus status = syncProvider.getSyncingStatus(); final SyncState syncState = syncProvider.getCurrentSyncState(); - this.isSyncing = !syncState.isInSync(); + this.slotsBehind = calculateSlotsBehind(status, syncState); + this.isSyncing = !slotsBehind.isZero(); this.elOffline = Optional.of(!executionClientDataProvider.isExecutionClientAvailable()); this.isOptimistic = Optional.of(syncState.isOptimistic()); this.currentSlot = status.getCurrentSlot(); - // do this last, after isSyncing is calculated - this.slotsBehind = calculateSlotsBehind(status); } SyncStatusData( @@ -138,8 +137,9 @@ public UInt64 getSlotsBehind() { return slotsBehind; } - private UInt64 calculateSlotsBehind(final SyncingStatus syncingStatus) { - if (isSyncing && syncingStatus.getHighestSlot().isPresent()) { + private UInt64 calculateSlotsBehind( + final SyncingStatus syncingStatus, final SyncState syncState) { + if (!syncState.isInSync() && syncingStatus.getHighestSlot().isPresent()) { final UInt64 highestSlot = syncingStatus.getHighestSlot().get(); return highestSlot.minusMinZero(syncingStatus.getCurrentSlot()); } diff --git a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncingTest.java b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncingTest.java index d3d99f657b5..40d8ffe7432 100644 --- a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncingTest.java +++ b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetSyncingTest.java @@ -57,6 +57,17 @@ public void shouldGetSyncStatusInSync() throws Exception { .isEqualTo(new GetSyncing.SyncStatusData(false, false, false, 10, 0)); } + @Test + public void shouldGetSyncStatusInSyncWhenHeadIsOptimistic() throws Exception { + when(syncService.getSyncStatus()).thenReturn(getSyncStatus(false, 1, 10, 10)); + when(syncService.getCurrentSyncState()).thenReturn(SyncState.OPTIMISTIC_SYNCING); + + handler.handleRequest(request); + assertThat(request.getResponseCode()).isEqualTo(SC_OK); + assertThat(request.getResponseBody()) + .isEqualTo(new GetSyncing.SyncStatusData(false, true, false, 10, 0)); + } + @Test public void shouldGetElOffline() throws Exception { when(syncService.getSyncStatus()).thenReturn(getSyncStatus(false, 1, 10, 11));