Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for fast sync regression on post-merge networks #4224

Merged
merged 2 commits into from
Aug 9, 2022
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 @@ -14,9 +14,11 @@
*/
package org.hyperledger.besu.ethereum.mainnet;

import org.hyperledger.besu.config.MergeConfigOptions;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.AncestryValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.AttachedProofOfWorkValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.BaseFeeMarketBlockHeaderGasPriceValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.CalculatedDifficultyValidationRule;
import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.ConstantFieldValidationRule;
Expand Down Expand Up @@ -122,22 +124,35 @@ public static BlockHeaderValidator.Builder createPgaBlockHeaderValidator(

public static BlockHeaderValidator.Builder createBaseFeeMarketValidator(
final BaseFeeMarket baseFeeMarket) {
return new BlockHeaderValidator.Builder()
.addRule(CalculatedDifficultyValidationRule::new)
.addRule(new AncestryValidationRule())
.addRule(new GasUsageValidationRule())
.addRule(
new GasLimitRangeAndDeltaValidationRule(
MIN_GAS_LIMIT, Long.MAX_VALUE, Optional.of(baseFeeMarket)))
.addRule(new TimestampMoreRecentThanParent(MINIMUM_SECONDS_SINCE_PARENT))
.addRule(new TimestampBoundedByFutureParameter(TIMESTAMP_TOLERANCE_S))
.addRule(new ExtraDataMaxLengthValidationRule(BlockHeader.MAX_EXTRA_DATA_BYTES))
.addRule(
new ProofOfWorkValidationRule(
new EpochCalculator.DefaultEpochCalculator(),
PoWHasher.ETHASH_LIGHT,
Optional.of(baseFeeMarket)))
.addRule((new BaseFeeMarketBlockHeaderGasPriceValidationRule(baseFeeMarket)));
var builder =
new BlockHeaderValidator.Builder()
.addRule(CalculatedDifficultyValidationRule::new)
.addRule(new AncestryValidationRule())
.addRule(new GasUsageValidationRule())
.addRule(
new GasLimitRangeAndDeltaValidationRule(
MIN_GAS_LIMIT, Long.MAX_VALUE, Optional.of(baseFeeMarket)))
.addRule(new TimestampMoreRecentThanParent(MINIMUM_SECONDS_SINCE_PARENT))
.addRule(new TimestampBoundedByFutureParameter(TIMESTAMP_TOLERANCE_S))
.addRule(new ExtraDataMaxLengthValidationRule(BlockHeader.MAX_EXTRA_DATA_BYTES))
.addRule((new BaseFeeMarketBlockHeaderGasPriceValidationRule(baseFeeMarket)));

// if merge is enabled, use the attached version of the proof of work validation rule
if (MergeConfigOptions.isMergeEnabled()) {
builder.addRule(
new AttachedProofOfWorkValidationRule(
new EpochCalculator.DefaultEpochCalculator(),
PoWHasher.ETHASH_LIGHT,
Optional.of(baseFeeMarket)));

} else {
builder.addRule(
new ProofOfWorkValidationRule(
new EpochCalculator.DefaultEpochCalculator(),
PoWHasher.ETHASH_LIGHT,
Optional.of(baseFeeMarket)));
}
return builder;
}

static BlockHeaderValidator.Builder createBaseFeeMarketOmmerValidator(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet.headervalidationrules;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.AttachedBlockHeaderValidationRule;
import org.hyperledger.besu.ethereum.mainnet.EpochCalculator;
import org.hyperledger.besu.ethereum.mainnet.PoWHasher;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;

import java.util.Optional;

/**
* An attached proof of work validation rule that wraps the detached version of the same. Suitable
* for use in block validator stacks supporting the merge.
*/
public class AttachedProofOfWorkValidationRule implements AttachedBlockHeaderValidationRule {

private final ProofOfWorkValidationRule detachedRule;

public AttachedProofOfWorkValidationRule(
final EpochCalculator epochCalculator,
final PoWHasher hasher,
final Optional<FeeMarket> feeMarket) {
this.detachedRule = new ProofOfWorkValidationRule(epochCalculator, hasher, feeMarket);
}

@Override
public boolean validate(
final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) {
return detachedRule.validate(header, parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.ethereum.mainnet.headervalidationrules;

import org.hyperledger.besu.config.MergeConfigOptions;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.AttachedBlockHeaderValidationRule;
Expand All @@ -37,9 +36,7 @@ public CalculatedDifficultyValidationRule(final DifficultyCalculator difficultyC
@Override
public boolean validate(
final BlockHeader header, final BlockHeader parent, final ProtocolContext context) {
if (MergeConfigOptions.isMergeEnabled()) {
return true;
}

final BigInteger actualDifficulty = new BigInteger(1, header.getDifficulty().toArray());
final BigInteger expectedDifficulty =
difficultyCalculator.nextDifficulty(header.getTimestamp(), parent, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static java.lang.Boolean.FALSE;

import org.hyperledger.besu.config.MergeConfigOptions;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.DetachedBlockHeaderValidationRule;
Expand Down Expand Up @@ -70,12 +69,6 @@ public boolean validate(final BlockHeader header, final BlockHeader parent) {
return false;
}

// TODO: remove this rule bypass, use post-merge headervalidation rules
// https://github.com/hyperledger/besu/issues/2898
if (MergeConfigOptions.isMergeEnabled()) {
return true;
}

final Hash headerHash = hashHeader(header);
PoWSolution solution =
hasher.hash(header.getNonce(), header.getNumber(), epochCalculator, headerHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.base.Preconditions.checkArgument;

import org.hyperledger.besu.config.MergeConfigOptions;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.DetachedBlockHeaderValidationRule;

Expand All @@ -36,9 +35,6 @@ public TimestampMoreRecentThanParent(final long minimumSecondsSinceParent) {

@Override
public boolean validate(final BlockHeader header, final BlockHeader parent) {
if (MergeConfigOptions.isMergeEnabled()) {
return true;
}
return validateTimestamp(header.getTimestamp(), parent.getTimestamp());
}

Expand Down