diff --git a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java index 512d2dc21fd..e0b0559795d 100644 --- a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java +++ b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java @@ -501,10 +501,6 @@ public Optional getOrSyncHeadByHash(final Hash headHash, final Hash .addArgument(maybeHeadHeader.get()::toLogString) .log(); } else { - LOG.atDebug() - .setMessage("Appending new head block hash {} to backward sync") - .addArgument(headHash::toHexString) - .log(); backwardSyncContext.maybeUpdateTargetHeight(headHash); backwardSyncContext .syncBackwardsUntil(headHash) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java index 1c1382e06b9..273dd56c78d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java @@ -125,11 +125,15 @@ public synchronized void maybeUpdateTargetHeight(final Hash headHash) { } public synchronized CompletableFuture syncBackwardsUntil(final Hash newBlockHash) { - if (!isTrusted(newBlockHash)) { - backwardChain.addNewHash(newBlockHash); - } - if (isReady()) { + if (!isTrusted(newBlockHash)) { + LOG.atDebug() + .setMessage("Appending new head block hash {} to backward sync") + .addArgument(newBlockHash::toHexString) + .log(); + backwardChain.addNewHash(newBlockHash); + } + final Status status = getOrStartSyncSession(); backwardChain .getBlock(newBlockHash) diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java index 532400c20d2..ce4640ae93f 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java @@ -231,6 +231,20 @@ public void shouldSyncUntilHash() throws Exception { assertThat(localBlockchain.getChainHeadBlock()).isEqualTo(remoteBlockchain.getChainHeadBlock()); } + @Test + public void shouldNotSyncUntilHashWhenNotInSync() { + doReturn(false).when(context).isReady(); + final Hash hash = getBlockByNumber(REMOTE_HEIGHT).getHash(); + final CompletableFuture future = context.syncBackwardsUntil(hash); + + respondUntilFutureIsDone(future); + + assertThatThrownBy(future::get) + .isInstanceOf(ExecutionException.class) + .hasMessageContaining("Backward sync is not ready"); + assertThat(backwardChain.getFirstHashToAppend()).isEmpty(); + } + @Test public void shouldSyncUntilRemoteBranch() throws Exception {