Refactor BackwardsSyncAlgorithm.pickNextStep for readability (#6775)

* Refactor BackwardsSyncAlgorithm.pickNextStep for readability

...mostly extracting methods

- Result null check
- Remove redundant BannedMethod suppressions
- Add sonar config back in that was removed by mistake

Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
pull/6807/head
Simon Dudley 8 months ago committed by GitHub
parent 2eca4d5a4e
commit bb9ba13cf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 88
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgorithm.java
  2. 2
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java
  3. 1
      gradle.properties

@ -63,40 +63,7 @@ public class BackwardSyncAlgorithm implements BesuEvents.InitialSyncCompletionLi
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();
@ -137,6 +104,59 @@ public class BackwardSyncAlgorithm implements BesuEvents.InitialSyncCompletionLi
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) {
if (result == null) {
LOG.atWarn().setMessage("Unexpected null result in for hash {}").addArgument(firstHash).log();
syncStep.completeExceptionally(new BackwardSyncException("Unexpected null result", true));
} else {
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) {
handleEthPeerMaxRetriesException(firstHash);
syncStep.complete(null);
} else {
syncStep.completeExceptionally(error);
}
}
private void handleEthPeerMaxRetriesException(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();

@ -442,7 +442,6 @@ public class BackwardSyncContextTest {
}
}
@SuppressWarnings("BannedMethod")
@Test
public void whenBlockNotFoundInPeers_shouldRemoveBlockFromQueueAndProgressInNextSession() {
// This scenario can happen due to a reorg
@ -466,7 +465,6 @@ public class BackwardSyncContextTest {
.isEqualTo(remoteBlockchain.getBlockByNumber(reorgBlockHeight).orElseThrow());
}
@SuppressWarnings("BannedMethod")
@Test
public void
whenBlockNotFoundInPeers_shouldRemoveBlockFromQueueAndProgressWithQueueInSameSession() {

@ -13,3 +13,4 @@ org.gradle.jvmargs=-Xmx4g \
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
--add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
# Could be moved to sonar properties after https://sonarsource.atlassian.net/browse/SONARGRADL-134
systemProp.sonar.gradle.skipCompile=true

Loading…
Cancel
Save