Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Use header validation policy in DownloadHeaderSequenceTask #1172

Merged
merged 3 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -59,6 +59,7 @@ public class DownloadHeaderSequenceTask<C> extends AbstractRetryingPeerTask<List
private final BlockHeader referenceHeader;
private final int segmentLength;
private final long startingBlockNumber;
private final ValidationPolicy validationPolicy;
private final MetricsSystem metricsSystem;

private int lastFilledHeaderIndex;
Expand All @@ -70,13 +71,15 @@ private DownloadHeaderSequenceTask(
final BlockHeader referenceHeader,
final int segmentLength,
final int maxRetries,
final ValidationPolicy validationPolicy,
final MetricsSystem metricsSystem) {
super(ethContext, maxRetries, Collection::isEmpty, metricsSystem);
this.protocolSchedule = protocolSchedule;
this.protocolContext = protocolContext;
this.ethContext = ethContext;
this.referenceHeader = referenceHeader;
this.segmentLength = segmentLength;
this.validationPolicy = validationPolicy;
this.metricsSystem = metricsSystem;

startingBlockNumber = referenceHeader.getNumber() - segmentLength;
Expand All @@ -91,6 +94,7 @@ public static <C> DownloadHeaderSequenceTask<C> endingAtHeader(
final BlockHeader referenceHeader,
final int segmentLength,
final int maxRetries,
final ValidationPolicy validationPolicy,
final MetricsSystem metricsSystem) {
return new DownloadHeaderSequenceTask<>(
protocolSchedule,
Expand All @@ -99,6 +103,7 @@ public static <C> DownloadHeaderSequenceTask<C> endingAtHeader(
referenceHeader,
segmentLength,
maxRetries,
validationPolicy,
metricsSystem);
}

Expand All @@ -108,6 +113,7 @@ public static <C> DownloadHeaderSequenceTask<C> endingAtHeader(
final EthContext ethContext,
final BlockHeader referenceHeader,
final int segmentLength,
final ValidationPolicy validationPolicy,
final MetricsSystem metricsSystem) {
return new DownloadHeaderSequenceTask<>(
protocolSchedule,
Expand All @@ -116,6 +122,7 @@ public static <C> DownloadHeaderSequenceTask<C> endingAtHeader(
referenceHeader,
segmentLength,
DEFAULT_RETRIES,
validationPolicy,
metricsSystem);
}

Expand Down Expand Up @@ -220,6 +227,7 @@ private boolean validateHeader(final BlockHeader child, final BlockHeader header

final ProtocolSpec<C> protocolSpec = protocolSchedule.getByBlockNumber(child.getNumber());
final BlockHeaderValidator<C> blockHeaderValidator = protocolSpec.getBlockHeaderValidator();
return blockHeaderValidator.validateHeader(child, header, protocolContext, DETACHED_ONLY);
return blockHeaderValidator.validateHeader(
child, header, protocolContext, validationPolicy.getValidationModeForNextBlock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,6 +37,7 @@ public class ParallelDownloadHeadersTask<C>
private final ProtocolSchedule<C> protocolSchedule;
private final ProtocolContext<C> protocolContext;
private final EthContext ethContext;
private final ValidationPolicy validationPolicy;
private final MetricsSystem metricsSystem;

ParallelDownloadHeadersTask(
Expand All @@ -44,12 +46,14 @@ public class ParallelDownloadHeadersTask<C>
final ProtocolSchedule<C> protocolSchedule,
final ProtocolContext<C> 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;
}

Expand All @@ -73,6 +77,7 @@ protected Optional<List<BlockHeader>> processStep(
ethContext,
nextCheckpointHeader,
segmentLength,
validationPolicy,
metricsSystem);
final CompletableFuture<List<BlockHeader>> headerFuture = executeSubTask(downloadTask::run);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected void executeTask() {
protocolSchedule,
protocolContext,
ethContext,
validationPolicy,
metricsSystem);
final ParallelValidateHeadersTask<C> validateHeadersTask =
new ParallelValidateHeadersTask<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +39,8 @@

public class DownloadHeaderSequenceTaskTest extends RetryingMessageTaskTest<List<BlockHeader>> {

private final ValidationPolicy validationPolicy = () -> HeaderValidationMode.DETACHED_ONLY;

@Override
protected List<BlockHeader> generateDataToBeRequested() {
final List<BlockHeader> requestedHeaders = new ArrayList<>();
Expand All @@ -59,6 +63,7 @@ protected EthTask<List<BlockHeader>> createTask(final List<BlockHeader> requeste
referenceHeader,
requestedData.size(),
maxRetries,
validationPolicy,
metricsSystem);
}

Expand All @@ -77,6 +82,7 @@ public void failsWhenPeerReturnsOnlyReferenceHeader() {
referenceHeader,
10,
maxRetries,
validationPolicy,
metricsSystem);
final CompletableFuture<List<BlockHeader>> future = task.run();

Expand Down Expand Up @@ -106,6 +112,7 @@ public void failsWhenPeerReturnsOnlySubsetOfHeaders() {
referenceHeader,
10,
maxRetries,
validationPolicy,
metricsSystem);
final CompletableFuture<List<BlockHeader>> future = task.run();

Expand Down