From 93b8c34a9f316acfdf258f3e08477056ee9cf52f Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 27 Mar 2019 14:16:11 +1000 Subject: [PATCH] Use header validation policy in DownloadHeaderSequenceTask so fast sync doesn't perform full validation on every block. --- .../eth/sync/tasks/DownloadHeaderSequenceTask.java | 12 ++++++++++-- .../eth/sync/tasks/ParallelDownloadHeadersTask.java | 5 +++++ .../sync/tasks/ParallelImportChainSegmentTask.java | 1 + .../sync/tasks/DownloadHeaderSequenceTaskTest.java | 7 +++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTask.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTask.java index e493003fab..7847977f51 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTask.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTask.java @@ -13,7 +13,6 @@ package tech.pegasys.pantheon.ethereum.eth.sync.tasks; import static java.util.Arrays.asList; -import static tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode.DETACHED_ONLY; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.core.BlockHeader; @@ -24,6 +23,7 @@ import tech.pegasys.pantheon.ethereum.eth.manager.task.AbstractPeerTask.PeerTaskResult; import tech.pegasys.pantheon.ethereum.eth.manager.task.AbstractRetryingPeerTask; import tech.pegasys.pantheon.ethereum.eth.manager.task.GetHeadersFromPeerByHashTask; +import tech.pegasys.pantheon.ethereum.eth.sync.ValidationPolicy; import tech.pegasys.pantheon.ethereum.eth.sync.tasks.exceptions.InvalidBlockException; import tech.pegasys.pantheon.ethereum.mainnet.BlockHeaderValidator; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; @@ -59,6 +59,7 @@ public class DownloadHeaderSequenceTask extends AbstractRetryingPeerTask DownloadHeaderSequenceTask endingAtHeader( final BlockHeader referenceHeader, final int segmentLength, final int maxRetries, + final ValidationPolicy validationPolicy, final MetricsSystem metricsSystem) { return new DownloadHeaderSequenceTask<>( protocolSchedule, @@ -99,6 +103,7 @@ public static DownloadHeaderSequenceTask endingAtHeader( referenceHeader, segmentLength, maxRetries, + validationPolicy, metricsSystem); } @@ -108,6 +113,7 @@ public static DownloadHeaderSequenceTask endingAtHeader( final EthContext ethContext, final BlockHeader referenceHeader, final int segmentLength, + final ValidationPolicy validationPolicy, final MetricsSystem metricsSystem) { return new DownloadHeaderSequenceTask<>( protocolSchedule, @@ -116,6 +122,7 @@ public static DownloadHeaderSequenceTask endingAtHeader( referenceHeader, segmentLength, DEFAULT_RETRIES, + validationPolicy, metricsSystem); } @@ -220,6 +227,7 @@ private boolean validateHeader(final BlockHeader child, final BlockHeader header final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(child.getNumber()); final BlockHeaderValidator blockHeaderValidator = protocolSpec.getBlockHeaderValidator(); - return blockHeaderValidator.validateHeader(child, header, protocolContext, DETACHED_ONLY); + return blockHeaderValidator.validateHeader( + child, header, protocolContext, validationPolicy.getValidationModeForNextBlock()); } } diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelDownloadHeadersTask.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelDownloadHeadersTask.java index 843ede18b2..46ceae089a 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelDownloadHeadersTask.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelDownloadHeadersTask.java @@ -16,6 +16,7 @@ import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.eth.manager.EthContext; import tech.pegasys.pantheon.ethereum.eth.manager.task.AbstractPipelinedTask; +import tech.pegasys.pantheon.ethereum.eth.sync.ValidationPolicy; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.metrics.MetricsSystem; @@ -36,6 +37,7 @@ public class ParallelDownloadHeadersTask private final ProtocolSchedule protocolSchedule; private final ProtocolContext protocolContext; private final EthContext ethContext; + private final ValidationPolicy validationPolicy; private final MetricsSystem metricsSystem; ParallelDownloadHeadersTask( @@ -44,12 +46,14 @@ public class ParallelDownloadHeadersTask final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext, final EthContext ethContext, + final ValidationPolicy validationPolicy, final MetricsSystem metricsSystem) { super(inboundQueue, outboundBacklogSize, metricsSystem); this.protocolSchedule = protocolSchedule; this.protocolContext = protocolContext; this.ethContext = ethContext; + this.validationPolicy = validationPolicy; this.metricsSystem = metricsSystem; } @@ -73,6 +77,7 @@ protected Optional> processStep( ethContext, nextCheckpointHeader, segmentLength, + validationPolicy, metricsSystem); final CompletableFuture> headerFuture = executeSubTask(downloadTask::run); diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelImportChainSegmentTask.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelImportChainSegmentTask.java index 557b7115f0..b54356f9fa 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelImportChainSegmentTask.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelImportChainSegmentTask.java @@ -112,6 +112,7 @@ protected void executeTask() { protocolSchedule, protocolContext, ethContext, + validationPolicy, metricsSystem); final ParallelValidateHeadersTask validateHeadersTask = new ParallelValidateHeadersTask<>( diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTaskTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTaskTest.java index 3c36797158..def94a2cbb 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTaskTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DownloadHeaderSequenceTaskTest.java @@ -24,6 +24,8 @@ import tech.pegasys.pantheon.ethereum.eth.manager.task.EthTask; import tech.pegasys.pantheon.ethereum.eth.messages.BlockHeadersMessage; import tech.pegasys.pantheon.ethereum.eth.messages.EthPV62; +import tech.pegasys.pantheon.ethereum.eth.sync.ValidationPolicy; +import tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import java.util.ArrayList; @@ -37,6 +39,8 @@ public class DownloadHeaderSequenceTaskTest extends RetryingMessageTaskTest> { + private final ValidationPolicy validationPolicy = () -> HeaderValidationMode.DETACHED_ONLY; + @Override protected List generateDataToBeRequested() { final List requestedHeaders = new ArrayList<>(); @@ -59,6 +63,7 @@ protected EthTask> createTask(final List requeste referenceHeader, requestedData.size(), maxRetries, + validationPolicy, metricsSystem); } @@ -77,6 +82,7 @@ public void failsWhenPeerReturnsOnlyReferenceHeader() { referenceHeader, 10, maxRetries, + validationPolicy, metricsSystem); final CompletableFuture> future = task.run(); @@ -106,6 +112,7 @@ public void failsWhenPeerReturnsOnlySubsetOfHeaders() { referenceHeader, 10, maxRetries, + validationPolicy, metricsSystem); final CompletableFuture> future = task.run();