Skip to content

Commit

Permalink
Refactor BackwardsSyncAlgorithm.pickNextStep for readability
Browse files Browse the repository at this point in the history
Mostly extracting methods
Signed-off-by: Simon Dudley <[email protected]>
  • Loading branch information
siladu committed Mar 25, 2024
1 parent b20713f commit 3b8ef52
Showing 1 changed file with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,7 @@ public CompletableFuture<Void> executeBackwardsSync(final Void unused) {
public CompletableFuture<Void> pickNextStep() {
final Optional<Hash> firstHash = context.getBackwardChain().getFirstHashToAppend();
if (firstHash.isPresent()) {
final CompletableFuture<Void> syncStep = new CompletableFuture<>();
executeSyncStep(firstHash.get())
.whenComplete(
(result, error) -> {
if (error != null) {
if (error instanceof CompletionException
&& error.getCause() instanceof MaxRetriesReachedException) {
context.getBackwardChain().removeFromHashToAppend(firstHash.get());
LOG.atWarn()
.setMessage(
"Unable to retrieve block {} from any peer, with {} peers available. Could be a reorged block. Waiting for the next block from the consensus client to try again.")
.addArgument(firstHash.get())
.addArgument(context.getEthContext().getEthPeers().peerCount())
.addArgument(context.getBackwardChain().getFirstHashToAppend())
.log();
LOG.atDebug()
.setMessage("Removing hash {} from hashesToAppend")
.addArgument(firstHash.get())
.log();
syncStep.complete(null);
} else {
syncStep.completeExceptionally(error);
}
} else {
LOG.atDebug()
.setMessage("Backward sync target block is {}")
.addArgument(result::toLogString)
.log();
context.getBackwardChain().removeFromHashToAppend(firstHash.get());
context.getStatus().updateTargetHeight(result.getHeader().getNumber());
syncStep.complete(null);
}
});
return syncStep;
return handleSyncStep(firstHash.get());
}
if (!context.isReady()) {
return waitForReady();
Expand Down Expand Up @@ -137,6 +104,54 @@ public CompletableFuture<Void> pickNextStep() {
return executeBackwardAsync(firstAncestorHeader);
}

private CompletableFuture<Void> handleSyncStep(final Hash firstHash) {
final CompletableFuture<Void> syncStep = new CompletableFuture<>();
executeSyncStep(firstHash)
.whenComplete(
(result, error) -> {
if (error != null) {
handleSyncStepError(error, firstHash, syncStep);
} else {
handleSyncStepSuccess(result, firstHash, syncStep);
}
});
return syncStep;
}

private void handleSyncStepSuccess(
final Block result, final Hash firstHash, final CompletableFuture<Void> syncStep) {
LOG.atDebug()
.setMessage("Backward sync target block is {}")
.addArgument(result::toLogString)
.log();
context.getBackwardChain().removeFromHashToAppend(firstHash);
context.getStatus().updateTargetHeight(result.getHeader().getNumber());
syncStep.complete(null);
}

private void handleSyncStepError(
final Throwable error, final Hash firstHash, final CompletableFuture<Void> syncStep) {
if (error instanceof CompletionException
&& error.getCause() instanceof MaxRetriesReachedException) {
handleMaxRetriesException(firstHash);
syncStep.complete(null);
} else {
syncStep.completeExceptionally(error);
}
}

private void handleMaxRetriesException(final Hash firstHash) {
context.getBackwardChain().removeFromHashToAppend(firstHash);
LOG.atWarn()
.setMessage(
"Unable to retrieve block {} from any peer, with {} peers available. Could be a reorged block. Waiting for the next block from the consensus client to try again.")
.addArgument(firstHash)
.addArgument(context.getEthContext().getEthPeers().peerCount())
.addArgument(context.getBackwardChain().getFirstHashToAppend())
.log();
LOG.atDebug().setMessage("Removing hash {} from hashesToAppend").addArgument(firstHash).log();
}

@VisibleForTesting
public CompletableFuture<Void> executeProcessKnownAncestors() {
return new ProcessKnownAncestorsStep(context, context.getBackwardChain()).executeAsync();
Expand Down

0 comments on commit 3b8ef52

Please sign in to comment.