diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index a8f6981d896..e1549b2357d 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -12,7 +12,7 @@ concurrency: env: GRADLE_OPTS: "-Xmx7g" - total-runners: 12 + total-runners: 14 jobs: acceptanceTestEthereum: @@ -24,7 +24,7 @@ jobs: strategy: fail-fast: true matrix: - runner_index: [0,1,2,3,4,5,6,7,8,9,10,11] + runner_index: [0,1,2,3,4,5,6,7,8,9,10,11,12,13] steps: - name: Checkout Repo uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 diff --git a/.github/workflows/pre-review.yml b/.github/workflows/pre-review.yml index cba13f1ebda..93bfcfa9f50 100644 --- a/.github/workflows/pre-review.yml +++ b/.github/workflows/pre-review.yml @@ -12,7 +12,7 @@ concurrency: env: GRADLE_OPTS: "-Xmx6g -Dorg.gradle.parallel=true" - total-runners: 8 + total-runners: 10 jobs: repolint: @@ -83,7 +83,7 @@ jobs: strategy: fail-fast: true matrix: - runner_index: [0,1,2,3,4,5,6,7] + runner_index: [0,1,2,3,4,5,6,7,8,9] steps: - name: Checkout Repo uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 diff --git a/.github/workflows/reference-tests.yml b/.github/workflows/reference-tests.yml index 4e458e057e3..d27114ebc1e 100644 --- a/.github/workflows/reference-tests.yml +++ b/.github/workflows/reference-tests.yml @@ -8,7 +8,7 @@ on: env: GRADLE_OPTS: "-Xmx6g -Dorg.gradle.daemon=false -Dorg.gradle.parallel=true -Dorg.gradle.caching=true" - total-runners: 10 + total-runners: 8 concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} @@ -24,7 +24,7 @@ jobs: strategy: fail-fast: true matrix: - runner_index: [1,2,3,4,5,6,7,8,9,10] + runner_index: [1,2,3,4,5,6,7,8] steps: - name: Checkout Repo uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eabc3f6961..5ce29955d12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,8 @@ - Fix mounted data path directory permissions for besu user [#7575](https://github.com/hyperledger/besu/pull/7575) - Fix for `debug_traceCall` to handle transactions without specified gas price. [#7510](https://github.com/hyperledger/besu/pull/7510) - Corrects a regression where custom plugin services are not initialized correctly. [#7625](https://github.com/hyperledger/besu/pull/7625) - +- Fix for IBFT2 chains using the BONSAI DB format [#7631](https://github.com/hyperledger/besu/pull/7631) +- Fix reading `tx-pool-min-score` option from configuration file [#7623](https://github.com/hyperledger/besu/pull/7623) ## 24.9.1 diff --git a/besu/src/main/java/org/hyperledger/besu/cli/util/TomlConfigurationDefaultProvider.java b/besu/src/main/java/org/hyperledger/besu/cli/util/TomlConfigurationDefaultProvider.java index ca76b40b686..636be329d43 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/util/TomlConfigurationDefaultProvider.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/util/TomlConfigurationDefaultProvider.java @@ -120,7 +120,11 @@ private String getConfigurationValue(final OptionSpec optionSpec) { } private boolean isNumericType(final Class type) { - return type.equals(Integer.class) + return type.equals(Byte.class) + || type.equals(byte.class) + || type.equals(Short.class) + || type.equals(short.class) + || type.equals(Integer.class) || type.equals(int.class) || type.equals(Long.class) || type.equals(long.class) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java index b8d4d2645e0..58412029fc3 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java @@ -74,6 +74,7 @@ import org.hyperledger.besu.plugin.services.BesuEvents; import org.hyperledger.besu.util.Subscribers; +import java.time.Duration; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -182,7 +183,10 @@ protected MiningCoordinator createMiningCoordinator( Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, uniqueMessageMulticaster, - new RoundTimer(bftEventQueue, bftConfig.getRequestTimeoutSeconds(), bftExecutors), + new RoundTimer( + bftEventQueue, + Duration.ofSeconds(bftConfig.getRequestTimeoutSeconds()), + bftExecutors), new BlockTimer(bftEventQueue, forksSchedule, bftExecutors, clock), blockCreatorFactory, clock); diff --git a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java index 7961305c48e..3d3412d4860 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java @@ -84,6 +84,7 @@ import org.hyperledger.besu.plugin.services.BesuEvents; import org.hyperledger.besu.util.Subscribers; +import java.time.Duration; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -222,7 +223,10 @@ protected MiningCoordinator createMiningCoordinator( Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, uniqueMessageMulticaster, - new RoundTimer(bftEventQueue, qbftConfig.getRequestTimeoutSeconds(), bftExecutors), + new RoundTimer( + bftEventQueue, + Duration.ofSeconds(qbftConfig.getRequestTimeoutSeconds()), + bftExecutors), new BlockTimer(bftEventQueue, qbftForksSchedule, bftExecutors, clock), blockCreatorFactory, clock); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java index eb48a3f2a5c..37acbe167ff 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java @@ -427,6 +427,24 @@ public void minScoreWorks() { Byte.toString(minScore)); } + @Test + public void minScoreWorksConfigFile() throws IOException { + final byte minScore = -10; + final Path tempConfigFilePath = + createTempFile( + "config", + String.format( + """ + tx-pool-min-score=%s + """, + minScore)); + + internalTestSuccess( + config -> assertThat(config.getMinScore()).isEqualTo(minScore), + "--config-file", + tempConfigFilePath.toString()); + } + @Test public void minScoreNonByteValueReturnError() { final var overflowMinScore = Integer.toString(-300); diff --git a/config/src/main/java/org/hyperledger/besu/config/BftConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/BftConfigOptions.java index c94752b598d..58df7be8490 100644 --- a/config/src/main/java/org/hyperledger/besu/config/BftConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/BftConfigOptions.java @@ -37,6 +37,13 @@ public interface BftConfigOptions { */ int getBlockPeriodSeconds(); + /** + * Gets block period milliseconds. For TESTING only. If set then blockperiodseconds is ignored. + * + * @return the block period milliseconds + */ + long getBlockPeriodMilliseconds(); + /** * Gets request timeout seconds. * diff --git a/config/src/main/java/org/hyperledger/besu/config/BftFork.java b/config/src/main/java/org/hyperledger/besu/config/BftFork.java index 30f8e1c5d5b..fdaa8f2fa9d 100644 --- a/config/src/main/java/org/hyperledger/besu/config/BftFork.java +++ b/config/src/main/java/org/hyperledger/besu/config/BftFork.java @@ -21,6 +21,7 @@ import java.util.Locale; import java.util.Optional; import java.util.OptionalInt; +import java.util.OptionalLong; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -40,6 +41,9 @@ public class BftFork implements Fork { /** The constant BLOCK_PERIOD_SECONDS_KEY. */ public static final String BLOCK_PERIOD_SECONDS_KEY = "blockperiodseconds"; + /** The constant BLOCK_PERIOD_MILLISECONDS_KEY. */ + public static final String BLOCK_PERIOD_MILLISECONDS_KEY = "xblockperiodmilliseconds"; + /** The constant BLOCK_REWARD_KEY. */ public static final String BLOCK_REWARD_KEY = "blockreward"; @@ -82,6 +86,15 @@ public OptionalInt getBlockPeriodSeconds() { return JsonUtil.getPositiveInt(forkConfigRoot, BLOCK_PERIOD_SECONDS_KEY); } + /** + * Gets block period milliseconds. Experimental for test scenarios only. + * + * @return the block period milliseconds + */ + public OptionalLong getBlockPeriodMilliseconds() { + return JsonUtil.getLong(forkConfigRoot, BLOCK_PERIOD_MILLISECONDS_KEY); + } + /** * Gets block reward wei. * diff --git a/config/src/main/java/org/hyperledger/besu/config/JsonBftConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/JsonBftConfigOptions.java index 95f2d9f7ce7..b1c8630b399 100644 --- a/config/src/main/java/org/hyperledger/besu/config/JsonBftConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/JsonBftConfigOptions.java @@ -34,6 +34,7 @@ public class JsonBftConfigOptions implements BftConfigOptions { private static final long DEFAULT_EPOCH_LENGTH = 30_000; private static final int DEFAULT_BLOCK_PERIOD_SECONDS = 1; + private static final int DEFAULT_BLOCK_PERIOD_MILLISECONDS = 0; // Experimental for test only private static final int DEFAULT_ROUND_EXPIRY_SECONDS = 1; // In a healthy network this can be very small. This default limit will allow for suitable // protection for on a typical 20 node validator network with multiple rounds @@ -66,6 +67,12 @@ public int getBlockPeriodSeconds() { bftConfigRoot, "blockperiodseconds", DEFAULT_BLOCK_PERIOD_SECONDS); } + @Override + public long getBlockPeriodMilliseconds() { + return JsonUtil.getLong( + bftConfigRoot, "xblockperiodmilliseconds", DEFAULT_BLOCK_PERIOD_MILLISECONDS); + } + @Override public int getRequestTimeoutSeconds() { return JsonUtil.getInt(bftConfigRoot, "requesttimeoutseconds", DEFAULT_ROUND_EXPIRY_SECONDS); @@ -133,6 +140,9 @@ public Map asMap() { if (bftConfigRoot.has("blockperiodseconds")) { builder.put("blockPeriodSeconds", getBlockPeriodSeconds()); } + if (bftConfigRoot.has("xblockperiodmilliseconds")) { + builder.put("xBlockPeriodMilliSeconds", getBlockPeriodMilliseconds()); + } if (bftConfigRoot.has("requesttimeoutseconds")) { builder.put("requestTimeoutSeconds", getRequestTimeoutSeconds()); } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BlockTimer.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BlockTimer.java index 5649b69e8f1..49ef94e008c 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BlockTimer.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BlockTimer.java @@ -24,9 +24,14 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** Class for starting and keeping organised block timers */ public class BlockTimer { + private static final Logger LOG = LoggerFactory.getLogger(BlockTimer.class); + private final ForksSchedule forksSchedule; private final BftExecutors bftExecutors; private Optional> currentTimerTask; @@ -79,12 +84,26 @@ public synchronized void startTimer( cancelTimer(); final long now = clock.millis(); + final long expiryTime; + + // Experimental option for test scenarios only. Not for production use. + final long blockPeriodMilliseconds = + forksSchedule.getFork(round.getSequenceNumber()).getValue().getBlockPeriodMilliseconds(); - // absolute time when the timer is supposed to expire - final int blockPeriodSeconds = - forksSchedule.getFork(round.getSequenceNumber()).getValue().getBlockPeriodSeconds(); - final long minimumTimeBetweenBlocksMillis = blockPeriodSeconds * 1000L; - final long expiryTime = chainHeadHeader.getTimestamp() * 1_000 + minimumTimeBetweenBlocksMillis; + if (blockPeriodMilliseconds > 0) { + // Experimental mode for setting < 1 second block periods e.g. for CI/CD pipelines + // running tests against Besu + expiryTime = clock.millis() + blockPeriodMilliseconds; + LOG.warn( + "Test-mode only xblockperiodmilliseconds has been set to {} millisecond blocks. Do not use in a production system.", + blockPeriodMilliseconds); + } else { + // absolute time when the timer is supposed to expire + final int blockPeriodSeconds = + forksSchedule.getFork(round.getSequenceNumber()).getValue().getBlockPeriodSeconds(); + final long minimumTimeBetweenBlocksMillis = blockPeriodSeconds * 1000L; + expiryTime = chainHeadHeader.getTimestamp() * 1_000 + minimumTimeBetweenBlocksMillis; + } if (expiryTime > now) { final long delay = expiryTime - now; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/MutableBftConfigOptions.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/MutableBftConfigOptions.java index fd406ea5e12..7b27c7b4c44 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/MutableBftConfigOptions.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/MutableBftConfigOptions.java @@ -31,6 +31,7 @@ public class MutableBftConfigOptions implements BftConfigOptions { private long epochLength; private int blockPeriodSeconds; + private long blockPeriodMilliseconds; private int requestTimeoutSeconds; private int gossipedHistoryLimit; private int messageQueueLimit; @@ -48,6 +49,7 @@ public class MutableBftConfigOptions implements BftConfigOptions { public MutableBftConfigOptions(final BftConfigOptions bftConfigOptions) { this.epochLength = bftConfigOptions.getEpochLength(); this.blockPeriodSeconds = bftConfigOptions.getBlockPeriodSeconds(); + this.blockPeriodMilliseconds = bftConfigOptions.getBlockPeriodMilliseconds(); this.requestTimeoutSeconds = bftConfigOptions.getRequestTimeoutSeconds(); this.gossipedHistoryLimit = bftConfigOptions.getGossipedHistoryLimit(); this.messageQueueLimit = bftConfigOptions.getMessageQueueLimit(); @@ -68,6 +70,11 @@ public int getBlockPeriodSeconds() { return blockPeriodSeconds; } + @Override + public long getBlockPeriodMilliseconds() { + return blockPeriodMilliseconds; + } + @Override public int getRequestTimeoutSeconds() { return requestTimeoutSeconds; @@ -131,6 +138,16 @@ public void setBlockPeriodSeconds(final int blockPeriodSeconds) { this.blockPeriodSeconds = blockPeriodSeconds; } + /** + * Sets block period milliseconds. Experimental for test scenarios. Not for use on production + * systems. + * + * @param blockPeriodMilliseconds the block period milliseconds + */ + public void setBlockPeriodMilliseconds(final long blockPeriodMilliseconds) { + this.blockPeriodMilliseconds = blockPeriodMilliseconds; + } + /** * Sets request timeout seconds. * diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/RoundTimer.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/RoundTimer.java index 6a8b02991d6..0302943fc12 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/RoundTimer.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/RoundTimer.java @@ -16,6 +16,7 @@ import org.hyperledger.besu.consensus.common.bft.events.RoundExpiry; +import java.time.Duration; import java.util.Optional; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -31,21 +32,21 @@ public class RoundTimer { private final BftExecutors bftExecutors; private Optional> currentTimerTask; private final BftEventQueue queue; - private final long baseExpiryMillis; + private final Duration baseExpiryPeriod; /** * Construct a RoundTimer with primed executor service ready to start timers * * @param queue The queue in which to put round expiry events - * @param baseExpirySeconds The initial round length for round 0 + * @param baseExpiryPeriod The initial round length for round 0 * @param bftExecutors executor service that timers can be scheduled with */ public RoundTimer( - final BftEventQueue queue, final long baseExpirySeconds, final BftExecutors bftExecutors) { + final BftEventQueue queue, final Duration baseExpiryPeriod, final BftExecutors bftExecutors) { this.queue = queue; this.bftExecutors = bftExecutors; this.currentTimerTask = Optional.empty(); - this.baseExpiryMillis = baseExpirySeconds * 1000; + this.baseExpiryPeriod = baseExpiryPeriod; } /** Cancels the current running round timer if there is one */ @@ -71,7 +72,8 @@ public synchronized boolean isRunning() { public synchronized void startTimer(final ConsensusRoundIdentifier round) { cancelTimer(); - final long expiryTime = baseExpiryMillis * (long) Math.pow(2, round.getRoundNumber()); + final long expiryTime = + baseExpiryPeriod.toMillis() * (long) Math.pow(2, round.getRoundNumber()); final Runnable newTimerRunnable = () -> queue.add(new RoundExpiry(round)); @@ -81,7 +83,7 @@ public synchronized void startTimer(final ConsensusRoundIdentifier round) { // Once we are up to round 2 start logging round expiries if (round.getRoundNumber() >= 2) { LOG.info( - "QBFT round {} expired. Moved to round {} which will expire in {} seconds", + "BFT round {} expired. Moved to round {} which will expire in {} seconds", round.getRoundNumber() - 1, round.getRoundNumber(), (expiryTime / 1000)); diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/RoundTimerTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/RoundTimerTest.java index 0ebca51c9e2..8f437395471 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/RoundTimerTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/RoundTimerTest.java @@ -25,6 +25,7 @@ import org.hyperledger.besu.consensus.common.bft.events.RoundExpiry; +import java.time.Duration; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -46,7 +47,7 @@ public void initialise() { bftExecutors = mock(BftExecutors.class); queue = new BftEventQueue(1000); queue.start(); - timer = new RoundTimer(queue, 1, bftExecutors); + timer = new RoundTimer(queue, Duration.ofSeconds(1), bftExecutors); } @Test diff --git a/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java b/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java index 5d2b02b1a7c..8896733548d 100644 --- a/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java +++ b/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java @@ -100,6 +100,7 @@ import org.hyperledger.besu.util.Subscribers; import java.time.Clock; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.util.ArrayList; @@ -403,7 +404,7 @@ private static ControllerAndState createControllerAndFinalState( Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, multicaster, - new RoundTimer(bftEventQueue, ROUND_TIMER_SEC, bftExecutors), + new RoundTimer(bftEventQueue, Duration.ofSeconds(ROUND_TIMER_SEC), bftExecutors), new BlockTimer(bftEventQueue, forksSchedule, bftExecutors, TestClock.fixed()), blockCreatorFactory, clock); diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactory.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactory.java index 0e71fe81216..2367f90e2be 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactory.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactory.java @@ -32,6 +32,7 @@ import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampBoundedByFutureParameter; import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampMoreRecentThanParent; +import java.time.Duration; import java.util.Optional; import org.apache.tuweni.units.bigints.UInt256; @@ -45,32 +46,43 @@ private IbftBlockHeaderValidationRulesetFactory() {} * Produces a BlockHeaderValidator configured for assessing bft block headers which are to form * part of the BlockChain (i.e. not proposed blocks, which do not contain commit seals) * - * @param secondsBetweenBlocks the minimum number of seconds which must elapse between blocks. + * @param minimumTimeBetweenBlocks the minimum time which must elapse between blocks. * @param baseFeeMarket an {@link Optional} wrapping {@link BaseFeeMarket} class if appropriate. * @return BlockHeaderValidator configured for assessing bft block headers */ public static BlockHeaderValidator.Builder blockHeaderValidator( - final long secondsBetweenBlocks, final Optional baseFeeMarket) { - return new BlockHeaderValidator.Builder() - .addRule(new AncestryValidationRule()) - .addRule(new GasUsageValidationRule()) - .addRule( - new GasLimitRangeAndDeltaValidationRule( - DEFAULT_MIN_GAS_LIMIT, DEFAULT_MAX_GAS_LIMIT, baseFeeMarket)) - .addRule(new TimestampBoundedByFutureParameter(1)) - .addRule(new TimestampMoreRecentThanParent(secondsBetweenBlocks)) - .addRule( - new ConstantFieldValidationRule<>( - "MixHash", BlockHeader::getMixHash, BftHelpers.EXPECTED_MIX_HASH)) - .addRule( - new ConstantFieldValidationRule<>( - "OmmersHash", BlockHeader::getOmmersHash, Hash.EMPTY_LIST_HASH)) - .addRule( - new ConstantFieldValidationRule<>( - "Difficulty", BlockHeader::getDifficulty, UInt256.ONE)) - .addRule(new ConstantFieldValidationRule<>("Nonce", BlockHeader::getNonce, 0L)) - .addRule(new BftValidatorsValidationRule()) - .addRule(new BftCoinbaseValidationRule()) - .addRule(new BftCommitSealsValidationRule()); + final Duration minimumTimeBetweenBlocks, final Optional baseFeeMarket) { + final BlockHeaderValidator.Builder ruleBuilder = + new BlockHeaderValidator.Builder() + .addRule(new AncestryValidationRule()) + .addRule(new GasUsageValidationRule()) + .addRule( + new GasLimitRangeAndDeltaValidationRule( + DEFAULT_MIN_GAS_LIMIT, DEFAULT_MAX_GAS_LIMIT, baseFeeMarket)) + .addRule(new TimestampBoundedByFutureParameter(1)) + .addRule( + new ConstantFieldValidationRule<>( + "MixHash", BlockHeader::getMixHash, BftHelpers.EXPECTED_MIX_HASH)) + .addRule( + new ConstantFieldValidationRule<>( + "OmmersHash", BlockHeader::getOmmersHash, Hash.EMPTY_LIST_HASH)) + .addRule( + new ConstantFieldValidationRule<>( + "Difficulty", BlockHeader::getDifficulty, UInt256.ONE)) + .addRule(new ConstantFieldValidationRule<>("Nonce", BlockHeader::getNonce, 0L)) + .addRule(new BftValidatorsValidationRule()) + .addRule(new BftCoinbaseValidationRule()) + .addRule(new BftCommitSealsValidationRule()); + + // Currently the minimum acceptable time between blocks is 1 second. The timestamp of an + // Ethereum header is stored as seconds since Unix epoch so blocks being produced more + // frequently than once a second cannot pass this validator. For non-production scenarios + // (e.g. for testing block production much more frequently than once a second) Besu has + // an experimental 'xblockperiodmilliseconds' option for BFT chains. If this is enabled + // we cannot apply the TimestampMoreRecentThanParent validation rule so we do not add it + if (minimumTimeBetweenBlocks.compareTo(Duration.ofSeconds(1)) >= 0) { + ruleBuilder.addRule(new TimestampMoreRecentThanParent(minimumTimeBetweenBlocks.getSeconds())); + } + return ruleBuilder; } } diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleBuilder.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleBuilder.java index 0789f2e8981..3adf5718955 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleBuilder.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleBuilder.java @@ -29,6 +29,7 @@ import org.hyperledger.besu.evm.internal.EvmConfiguration; import org.hyperledger.besu.plugin.services.MetricsSystem; +import java.time.Duration; import java.util.Optional; /** Defines the protocol behaviours for a blockchain using a BFT consensus mechanism. */ @@ -120,6 +121,9 @@ protected BlockHeaderValidator.Builder createBlockHeaderRuleset( Optional.of(feeMarket).filter(FeeMarket::implementsBaseFee).map(BaseFeeMarket.class::cast); return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - config.getBlockPeriodSeconds(), baseFeeMarket); + config.getBlockPeriodMilliseconds() > 0 + ? Duration.ofMillis(config.getBlockPeriodMilliseconds()) + : Duration.ofSeconds(config.getBlockPeriodSeconds()), + baseFeeMarket); } } diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidator.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidator.java index 783861cdfef..1d7b3da9d61 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidator.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidator.java @@ -78,7 +78,9 @@ public boolean validateProposal(final Proposal msg) { return false; } - if (!validateBlock(msg.getBlock())) { + // We want to validate the block but not persist it yet as it's just a proposal. If it turns + // out to be an accepted block it will be persisted at block import time + if (!validateBlockWithoutPersisting(msg.getBlock())) { return false; } @@ -93,14 +95,14 @@ public boolean validateProposal(final Proposal msg) { msg.getSignedPayload(), msg.getBlock(), blockInterface); } - private boolean validateBlock(final Block block) { + private boolean validateBlockWithoutPersisting(final Block block) { final BlockValidator blockValidator = protocolSchedule.getByBlockHeader(block.getHeader()).getBlockValidator(); final var validationResult = blockValidator.validateAndProcessBlock( - protocolContext, block, HeaderValidationMode.LIGHT, HeaderValidationMode.FULL); + protocolContext, block, HeaderValidationMode.LIGHT, HeaderValidationMode.FULL, false); if (validationResult.isFailed()) { LOG.info( diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java index a7deb9e1973..160fc2a2f39 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java @@ -38,6 +38,7 @@ import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket; +import java.time.Duration; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -93,7 +94,7 @@ public void bftValidateHeaderPassesWithBaseFee() { final BlockHeaderValidator validator = IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - 5, Optional.of(FeeMarket.london(1))) + Duration.ofSeconds(5), Optional.of(FeeMarket.london(1))) .build(); assertThat( @@ -372,7 +373,8 @@ private BlockHeaderTestFixture getPresetHeaderBuilder( } public BlockHeaderValidator getBlockHeaderValidator() { - return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(5, Optional.empty()) + return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( + Duration.ofSeconds(5), Optional.empty()) .build(); } } diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/blockcreation/BftBlockCreatorTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/blockcreation/BftBlockCreatorTest.java index 8b8406b6385..5469717b13f 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/blockcreation/BftBlockCreatorTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/blockcreation/BftBlockCreatorTest.java @@ -64,6 +64,7 @@ import org.hyperledger.besu.testutil.DeterministicEthScheduler; import org.hyperledger.besu.testutil.TestClock; +import java.time.Duration; import java.time.ZoneId; import java.util.Collections; import java.util.List; @@ -105,7 +106,7 @@ public void createdBlockPassesValidationRulesAndHasAppropriateHashAndMixHash() { public BlockHeaderValidator.Builder createBlockHeaderRuleset( final BftConfigOptions config, final FeeMarket feeMarket) { return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - 5, Optional.empty()); + Duration.ofSeconds(5), Optional.empty()); } }; final GenesisConfigOptions configOptions = @@ -200,7 +201,7 @@ public BlockHeaderValidator.Builder createBlockHeaderRuleset( final BlockHeaderValidator rules = IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - secondsBetweenBlocks, Optional.empty()) + Duration.ofSeconds(secondsBetweenBlocks), Optional.empty()) .build(); // NOTE: The header will not contain commit seals, so can only do light validation on header. diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java index f7fb7af3e94..2352642b87b 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java @@ -113,7 +113,7 @@ public void setup() { when(protocolSpec.getBlockValidator()).thenReturn(blockValidator); when(protocolSchedule.getByBlockHeader(any())).thenReturn(protocolSpec); - when(blockValidator.validateAndProcessBlock(any(), any(), any(), any())) + when(blockValidator.validateAndProcessBlock(any(), any(), any(), any(), eq(false))) .thenReturn(new BlockProcessingResult(Optional.empty())); when(roundChangeCertificateValidator.validateProposalMessageMatchesLatestPrepareCertificate( @@ -168,7 +168,7 @@ public void ifProposalConsistencyChecksFailProposalIsIllegal() { @Test public void blockValidationFailureFailsValidation() { - when(blockValidator.validateAndProcessBlock(any(), any(), any(), any())) + when(blockValidator.validateAndProcessBlock(any(), any(), any(), any(), eq(false))) .thenReturn(new BlockProcessingResult("Failed")); final Proposal proposalMsg = diff --git a/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java b/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java index 8906f0de7f6..d90d5a15277 100644 --- a/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java +++ b/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java @@ -118,6 +118,7 @@ import java.io.IOException; import java.nio.file.Path; import java.time.Clock; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.util.ArrayList; @@ -512,7 +513,7 @@ private static ControllerAndState createControllerAndFinalState( Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, multicaster, - new RoundTimer(bftEventQueue, ROUND_TIMER_SEC, bftExecutors), + new RoundTimer(bftEventQueue, Duration.ofSeconds(ROUND_TIMER_SEC), bftExecutors), new BlockTimer(bftEventQueue, forksSchedule, bftExecutors, TestClock.fixed()), blockCreatorFactory, clock); diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactory.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactory.java index 7320dfaaf7a..ac5ba3ac23a 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactory.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactory.java @@ -31,6 +31,7 @@ import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampBoundedByFutureParameter; import org.hyperledger.besu.ethereum.mainnet.headervalidationrules.TimestampMoreRecentThanParent; +import java.time.Duration; import java.util.Optional; import org.apache.tuweni.units.bigints.UInt256; @@ -44,31 +45,42 @@ private QbftBlockHeaderValidationRulesetFactory() {} * Produces a BlockHeaderValidator configured for assessing bft block headers which are to form * part of the BlockChain (i.e. not proposed blocks, which do not contain commit seals) * - * @param secondsBetweenBlocks the minimum number of seconds which must elapse between blocks. + * @param minimumTimeBetweenBlocks the minimum amount of time that must elapse between blocks. * @param useValidatorContract whether validator selection is using a validator contract * @param baseFeeMarket an {@link Optional} wrapping {@link BaseFeeMarket} class if appropriate. * @return BlockHeaderValidator configured for assessing bft block headers */ public static BlockHeaderValidator.Builder blockHeaderValidator( - final long secondsBetweenBlocks, + final Duration minimumTimeBetweenBlocks, final boolean useValidatorContract, final Optional baseFeeMarket) { - return new BlockHeaderValidator.Builder() - .addRule(new AncestryValidationRule()) - .addRule(new GasUsageValidationRule()) - .addRule( - new GasLimitRangeAndDeltaValidationRule( - DEFAULT_MIN_GAS_LIMIT, DEFAULT_MAX_GAS_LIMIT, baseFeeMarket)) - .addRule(new TimestampBoundedByFutureParameter(1)) - .addRule(new TimestampMoreRecentThanParent(secondsBetweenBlocks)) - .addRule( - new ConstantFieldValidationRule<>( - "MixHash", BlockHeader::getMixHash, BftHelpers.EXPECTED_MIX_HASH)) - .addRule( - new ConstantFieldValidationRule<>( - "Difficulty", BlockHeader::getDifficulty, UInt256.ONE)) - .addRule(new QbftValidatorsValidationRule(useValidatorContract)) - .addRule(new BftCoinbaseValidationRule()) - .addRule(new BftCommitSealsValidationRule()); + BlockHeaderValidator.Builder ruleBuilder = + new BlockHeaderValidator.Builder() + .addRule(new AncestryValidationRule()) + .addRule(new GasUsageValidationRule()) + .addRule( + new GasLimitRangeAndDeltaValidationRule( + DEFAULT_MIN_GAS_LIMIT, DEFAULT_MAX_GAS_LIMIT, baseFeeMarket)) + .addRule(new TimestampBoundedByFutureParameter(1)) + .addRule( + new ConstantFieldValidationRule<>( + "MixHash", BlockHeader::getMixHash, BftHelpers.EXPECTED_MIX_HASH)) + .addRule( + new ConstantFieldValidationRule<>( + "Difficulty", BlockHeader::getDifficulty, UInt256.ONE)) + .addRule(new QbftValidatorsValidationRule(useValidatorContract)) + .addRule(new BftCoinbaseValidationRule()) + .addRule(new BftCommitSealsValidationRule()); + + // Currently the minimum acceptable time between blocks is 1 second. The timestamp of an + // Ethereum header is stored as seconds since Unix epoch so blocks being produced more + // frequently than once a second cannot pass this validator. For non-production scenarios + // (e.g. for testing block production much more frequently than once a second) Besu has + // an experimental 'xblockperiodmilliseconds' option for BFT chains. If this is enabled + // we cannot apply the TimestampMoreRecentThanParent validation rule so we do not add it + if (minimumTimeBetweenBlocks.compareTo(Duration.ofSeconds(1)) >= 0) { + ruleBuilder.addRule(new TimestampMoreRecentThanParent(minimumTimeBetweenBlocks.getSeconds())); + } + return ruleBuilder; } } diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftForksSchedulesFactory.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftForksSchedulesFactory.java index 90448f62015..0f0d467de37 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftForksSchedulesFactory.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftForksSchedulesFactory.java @@ -49,6 +49,7 @@ private static QbftConfigOptions createQbftConfigOptions( new MutableQbftConfigOptions(lastSpec.getValue()); fork.getBlockPeriodSeconds().ifPresent(bftConfigOptions::setBlockPeriodSeconds); + fork.getBlockPeriodMilliseconds().ifPresent(bftConfigOptions::setBlockPeriodMilliseconds); fork.getBlockRewardWei().ifPresent(bftConfigOptions::setBlockRewardWei); if (fork.isMiningBeneficiaryConfigured()) { diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleBuilder.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleBuilder.java index 44c7ddfba8c..e1cbc134b6f 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleBuilder.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleBuilder.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.evm.internal.EvmConfiguration; import org.hyperledger.besu.plugin.services.MetricsSystem; +import java.time.Duration; import java.util.Optional; /** Defines the protocol behaviours for a blockchain using a QBFT consensus mechanism. */ @@ -164,7 +165,9 @@ protected BlockHeaderValidator.Builder createBlockHeaderRuleset( Optional.of(feeMarket).filter(FeeMarket::implementsBaseFee).map(BaseFeeMarket.class::cast); return QbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - qbftConfigOptions.getBlockPeriodSeconds(), + qbftConfigOptions.getBlockPeriodMilliseconds() > 0 + ? Duration.ofMillis(qbftConfigOptions.getBlockPeriodMilliseconds()) + : Duration.ofSeconds(qbftConfigOptions.getBlockPeriodSeconds()), qbftConfigOptions.isValidatorContractMode(), baseFeeMarket); } diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactoryTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactoryTest.java index 4771cf91cbd..ae1fbc0f19b 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactoryTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactoryTest.java @@ -34,6 +34,7 @@ import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket; +import java.time.Duration; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -92,7 +93,7 @@ public void bftValidateHeaderPassesWithBaseFee() { final BlockHeaderValidator validator = QbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( - 5, false, Optional.of(FeeMarket.london(1))) + Duration.ofSeconds(5), false, Optional.of(FeeMarket.london(1))) .build(); assertThat( @@ -366,7 +367,8 @@ blockHeader, parentHeader, protocolContext(validators), HeaderValidationMode.FUL } public BlockHeaderValidator getBlockHeaderValidator() { - return QbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(5, false, Optional.empty()) + return QbftBlockHeaderValidationRulesetFactory.blockHeaderValidator( + Duration.ofSeconds(5), false, Optional.empty()) .build(); } } diff --git a/docker/tests/02/goss_wait.yaml b/docker/tests/02/goss_wait.yaml new file mode 100644 index 00000000000..f6b397c6188 --- /dev/null +++ b/docker/tests/02/goss_wait.yaml @@ -0,0 +1,7 @@ +--- +# runtime docker tests for interfaces & ports +port: + tcp:30303: + listening: true + ip: + - 0.0.0.0 diff --git a/docs/tracing/README.md b/docs/tracing/README.md index 77deadd16aa..3a3f9fa435f 100644 --- a/docs/tracing/README.md +++ b/docs/tracing/README.md @@ -1,6 +1,6 @@ # Tracing -Hyperledger Besu integrates with the [open-telemetry](https://open-telemetry.io) project to integrate tracing reporting. +Hyperledger Besu integrates with the [open-telemetry](https://opentelemetry.io/) project to integrate tracing reporting. This allows to report all JSON-RPC traffic as traces. diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/BlockValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/BlockValidator.java index e8136062bbd..8cfafee730b 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/BlockValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/BlockValidator.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.Request; import org.hyperledger.besu.ethereum.core.TransactionReceipt; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import java.util.List; @@ -83,8 +84,8 @@ BlockProcessingResult validateAndProcessBlock( final boolean shouldRecordBadBlock); /** - * Performs fast block validation with the given context, block, transaction receipts, requests, - * header validation mode, and ommer validation mode. + * Performs fast block validation appropriate for use during syncing skipping transaction receipt + * roots and receipts roots as these are done during the download of the blocks. * * @param context the protocol context * @param block the block to validate @@ -92,13 +93,15 @@ BlockProcessingResult validateAndProcessBlock( * @param requests the requests * @param headerValidationMode the header validation mode * @param ommerValidationMode the ommer validation mode + * @param bodyValidationMode the body validation mode * @return true if the block is valid, false otherwise */ - boolean fastBlockValidation( + boolean validateBlockForSyncing( final ProtocolContext context, final Block block, final List receipts, final Optional> requests, final HeaderValidationMode headerValidationMode, - final HeaderValidationMode ommerValidationMode); + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/MainnetBlockValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/MainnetBlockValidator.java index 5d80a951505..0c56a419e35 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/MainnetBlockValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/MainnetBlockValidator.java @@ -25,6 +25,7 @@ import org.hyperledger.besu.ethereum.mainnet.BlockBodyValidator; import org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator; import org.hyperledger.besu.ethereum.mainnet.BlockProcessor; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import org.hyperledger.besu.ethereum.trie.MerkleTrieException; import org.hyperledger.besu.plugin.services.exception.StorageException; @@ -247,13 +248,14 @@ protected BlockProcessingResult processBlock( } @Override - public boolean fastBlockValidation( + public boolean validateBlockForSyncing( final ProtocolContext context, final Block block, final List receipts, final Optional> requests, final HeaderValidationMode headerValidationMode, - final HeaderValidationMode ommerValidationMode) { + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode) { final BlockHeader header = block.getHeader(); if (!blockHeaderValidator.validateHeader(header, context, headerValidationMode)) { String description = String.format("Failed header validation (%s)", headerValidationMode); @@ -262,7 +264,7 @@ public boolean fastBlockValidation( } if (!blockBodyValidator.validateBodyLight( - context, block, receipts, requests, ommerValidationMode)) { + context, block, receipts, requests, ommerValidationMode, bodyValidationMode)) { badBlockManager.addBadBlock( block, BadBlockCause.fromValidationFailure("Failed body validation (light)")); return false; diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockImporter.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockImporter.java index ce5c849e3a2..468485b9fde 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockImporter.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockImporter.java @@ -16,6 +16,7 @@ import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.mainnet.BlockImportResult; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import java.util.List; @@ -73,10 +74,11 @@ BlockImportResult importBlock( * @return {@code BlockImportResult} * @see BlockImportResult */ - BlockImportResult fastImportBlock( + BlockImportResult importBlockForSyncing( ProtocolContext context, Block block, List receipts, HeaderValidationMode headerValidationMode, - HeaderValidationMode ommerValidationMode); + HeaderValidationMode ommerValidationMode, + BodyValidationMode bodyValidationMode); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BaseFeeBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BaseFeeBlockBodyValidator.java index 0e288185cfc..aabc4ee5d09 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BaseFeeBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BaseFeeBlockBodyValidator.java @@ -43,9 +43,11 @@ public boolean validateBodyLight( final Block block, final List receipts, final Optional> requests, - final HeaderValidationMode ommerValidationMode) { + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode) { - return super.validateBodyLight(context, block, receipts, requests, ommerValidationMode) + return super.validateBodyLight( + context, block, receipts, requests, ommerValidationMode, bodyValidationMode) && validateTransactionGasPrice(block); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BlockBodyValidator.java index 62b4985f5ca..c84be3771b9 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BlockBodyValidator.java @@ -59,5 +59,6 @@ boolean validateBodyLight( Block block, List receipts, final Optional> requests, - final HeaderValidationMode ommerValidationMode); + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidationMode.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidationMode.java new file mode 100644 index 00000000000..344a950e365 --- /dev/null +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidationMode.java @@ -0,0 +1,26 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * 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; + +public enum BodyValidationMode { + /** No Validation. data must be pre-validated */ + NONE, + + /** Skip receipts and transactions root validation */ + LIGHT, + + /** Fully validate the body */ + FULL; +} diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index 3202440dd89..68951f9f2a3 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -29,6 +29,7 @@ import java.util.Optional; import java.util.Set; +import com.google.common.annotations.VisibleForTesting; import org.apache.tuweni.bytes.Bytes32; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +56,8 @@ public boolean validateBody( final Hash worldStateRootHash, final HeaderValidationMode ommerValidationMode) { - if (!validateBodyLight(context, block, receipts, requests, ommerValidationMode)) { + if (!validateBodyLight( + context, block, receipts, requests, ommerValidationMode, BodyValidationMode.FULL)) { return false; } @@ -77,18 +79,26 @@ public boolean validateBodyLight( final Block block, final List receipts, final Optional> requests, - final HeaderValidationMode ommerValidationMode) { + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode) { + if (bodyValidationMode == BodyValidationMode.NONE) { + return true; + } + final BlockHeader header = block.getHeader(); final BlockBody body = block.getBody(); - final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions()); - if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) { - return false; - } + // these checks are only needed for full validation and can be skipped for light validation + if (bodyValidationMode == BodyValidationMode.FULL) { + final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions()); + if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) { + return false; + } - final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); - if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { - return false; + final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); + if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { + return false; + } } final long gasUsed = @@ -115,7 +125,8 @@ public boolean validateBodyLight( return true; } - private static boolean validateTransactionsRoot( + @VisibleForTesting + protected boolean validateTransactionsRoot( final BlockHeader header, final Bytes32 expected, final Bytes32 actual) { if (!expected.equals(actual)) { LOG.info( @@ -157,7 +168,8 @@ private static boolean validateGasUsed( return true; } - private static boolean validateReceiptsRoot( + @VisibleForTesting + protected boolean validateReceiptsRoot( final BlockHeader header, final Bytes32 expected, final Bytes32 actual) { if (!expected.equals(actual)) { LOG.warn( diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporter.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporter.java index 62b708d33e6..8646467a282 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporter.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporter.java @@ -57,20 +57,22 @@ public synchronized BlockImportResult importBlock( } @Override - public BlockImportResult fastImportBlock( + public BlockImportResult importBlockForSyncing( final ProtocolContext context, final Block block, final List receipts, final HeaderValidationMode headerValidationMode, - final HeaderValidationMode ommerValidationMode) { + final HeaderValidationMode ommerValidationMode, + final BodyValidationMode bodyValidationMode) { - if (blockValidator.fastBlockValidation( + if (blockValidator.validateBlockForSyncing( context, block, receipts, block.getBody().getRequests(), headerValidationMode, - ommerValidationMode)) { + ommerValidationMode, + bodyValidationMode)) { context.getBlockchain().appendBlock(block, receipts); return new BlockImportResult(true); } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/MainnetBlockValidatorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/MainnetBlockValidatorTest.java index 361353ee2b2..40573ee8d6e 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/MainnetBlockValidatorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/MainnetBlockValidatorTest.java @@ -34,6 +34,7 @@ import org.hyperledger.besu.ethereum.mainnet.BlockBodyValidator; import org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator; import org.hyperledger.besu.ethereum.mainnet.BlockProcessor; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import org.hyperledger.besu.ethereum.trie.MerkleTrieException; import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; @@ -98,7 +99,8 @@ public void setup() { when(blockHeaderValidator.validateHeader(any(), any(), any(), any())).thenReturn(true); when(blockBodyValidator.validateBody(any(), any(), any(), any(), any(), any())) .thenReturn(true); - when(blockBodyValidator.validateBodyLight(any(), any(), any(), any(), any())).thenReturn(true); + when(blockBodyValidator.validateBodyLight(any(), any(), any(), any(), any(), any())) + .thenReturn(true); when(blockProcessor.processBlock(any(), any(), any())).thenReturn(successfulProcessingResult); when(blockProcessor.processBlock(any(), any(), any(), any())) .thenReturn(successfulProcessingResult); @@ -343,55 +345,65 @@ public void validateAndProcessBlock_withShouldRecordBadBlockNotSet() { } @Test - public void fastBlockValidation_onSuccess() { + public void validateBlockForSyncing_onSuccess() { final boolean isValid = - mainnetBlockValidator.fastBlockValidation( + mainnetBlockValidator.validateBlockForSyncing( protocolContext, block, Collections.emptyList(), block.getBody().getRequests(), HeaderValidationMode.FULL, - HeaderValidationMode.FULL); + HeaderValidationMode.FULL, + BodyValidationMode.FULL); assertThat(isValid).isTrue(); assertNoBadBlocks(); } @Test - public void fastBlockValidation_onFailedHeaderValidation() { - final HeaderValidationMode validationMode = HeaderValidationMode.FULL; + public void validateBlockValidation_onFailedHeaderForSyncing() { + final HeaderValidationMode headerValidationMode = HeaderValidationMode.FULL; when(blockHeaderValidator.validateHeader( - any(BlockHeader.class), eq(protocolContext), eq(validationMode))) + any(BlockHeader.class), eq(protocolContext), eq(headerValidationMode))) .thenReturn(false); + final BodyValidationMode bodyValidationMode = BodyValidationMode.FULL; final boolean isValid = - mainnetBlockValidator.fastBlockValidation( + mainnetBlockValidator.validateBlockForSyncing( protocolContext, block, Collections.emptyList(), block.getBody().getRequests(), - validationMode, - validationMode); + headerValidationMode, + headerValidationMode, + bodyValidationMode); assertThat(isValid).isFalse(); assertBadBlockIsTracked(block); } @Test - public void fastBlockValidation_onFailedBodyValidation() { - final HeaderValidationMode validationMode = HeaderValidationMode.FULL; + public void validateBlockValidation_onFailedBodyForSyncing() { + final HeaderValidationMode headerValidationMode = HeaderValidationMode.FULL; + final BodyValidationMode bodyValidationMode = BodyValidationMode.FULL; when(blockBodyValidator.validateBodyLight( - eq(protocolContext), eq(block), any(), any(), eq(validationMode))) + eq(protocolContext), + eq(block), + any(), + any(), + eq(headerValidationMode), + eq(bodyValidationMode))) .thenReturn(false); final boolean isValid = - mainnetBlockValidator.fastBlockValidation( + mainnetBlockValidator.validateBlockForSyncing( protocolContext, block, Collections.emptyList(), block.getBody().getRequests(), - validationMode, - validationMode); + headerValidationMode, + headerValidationMode, + bodyValidationMode); assertThat(isValid).isFalse(); assertBadBlockIsTracked(block); diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidatorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidatorTest.java index c116d545d80..c412a30756b 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidatorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidatorTest.java @@ -19,14 +19,22 @@ import static org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.NONE; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.GWei; +import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; import org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions; import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil; +import org.hyperledger.besu.ethereum.core.TransactionReceipt; import org.hyperledger.besu.ethereum.core.Withdrawal; import org.hyperledger.besu.ethereum.mainnet.requests.DepositRequestValidator; import org.hyperledger.besu.ethereum.mainnet.requests.RequestsValidatorCoordinator; @@ -93,7 +101,12 @@ void validatesWithdrawals() { assertThat( new MainnetBlockBodyValidator(protocolSchedule) .validateBodyLight( - blockchainSetupUtil.getProtocolContext(), block, emptyList(), any(), NONE)) + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + any(), + NONE, + BodyValidationMode.FULL)) .isTrue(); } @@ -117,7 +130,12 @@ void validationFailsIfWithdrawalsValidationFails() { assertThat( new MainnetBlockBodyValidator(protocolSchedule) .validateBodyLight( - blockchainSetupUtil.getProtocolContext(), block, emptyList(), any(), NONE)) + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + any(), + NONE, + BodyValidationMode.FULL)) .isFalse(); } @@ -141,7 +159,12 @@ void validationFailsIfWithdrawalsRootValidationFails() { assertThat( new MainnetBlockBodyValidator(protocolSchedule) .validateBodyLight( - blockchainSetupUtil.getProtocolContext(), block, emptyList(), any(), NONE)) + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + any(), + NONE, + BodyValidationMode.FULL)) .isFalse(); } @@ -165,7 +188,99 @@ public void validationFailsIfWithdrawalRequestsValidationFails() { assertThat( new MainnetBlockBodyValidator(protocolSchedule) .validateBodyLight( - blockchainSetupUtil.getProtocolContext(), block, emptyList(), any(), NONE)) + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + any(), + NONE, + BodyValidationMode.FULL)) .isFalse(); } + + @Test + @SuppressWarnings("unchecked") + public void noneValidationModeDoesNothing() { + final Block block = mock(Block.class); + final List receipts = mock(List.class); + + final MainnetBlockBodyValidator bodyValidator = new MainnetBlockBodyValidator(protocolSchedule); + + assertThat( + bodyValidator.validateBodyLight( + blockchainSetupUtil.getProtocolContext(), + block, + receipts, + Optional.empty(), + NONE, + BodyValidationMode.NONE)) + .isTrue(); + verifyNoInteractions(block); + verifyNoInteractions(receipts); + } + + @Test + public void lightValidationDoesNotCheckTransactionRootOrReceiptRoot() { + final Block block = + blockDataGenerator.block( + new BlockOptions() + .setBlockNumber(1) + .setGasUsed(0) + .hasTransactions(false) + .hasOmmers(false) + .setReceiptsRoot(BodyValidation.receiptsRoot(emptyList())) + .setLogsBloom(LogsBloomFilter.empty()) + .setParentHash(blockchainSetupUtil.getBlockchain().getChainHeadHash()) + .setWithdrawals(Optional.of(withdrawals))); + blockchainSetupUtil.getBlockchain().appendBlock(block, Collections.emptyList()); + + final MainnetBlockBodyValidator bodyValidator = new MainnetBlockBodyValidator(protocolSchedule); + final MainnetBlockBodyValidator bodyValidatorSpy = spy(bodyValidator); + + assertThat( + bodyValidatorSpy.validateBodyLight( + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + Optional.empty(), + NONE, + BodyValidationMode.LIGHT)) + .isTrue(); + verify(bodyValidatorSpy, never()).validateReceiptsRoot(any(), any(), any()); + verify(bodyValidatorSpy, never()).validateTransactionsRoot(any(), any(), any()); + } + + @Test + public void fullValidationChecksTransactionRootAndReceiptRoot() { + final Block block = + blockDataGenerator.block( + new BlockOptions() + .setBlockNumber(1) + .setGasUsed(0) + .hasTransactions(false) + .hasOmmers(false) + .setReceiptsRoot(BodyValidation.receiptsRoot(emptyList())) + .setLogsBloom(LogsBloomFilter.empty()) + .setParentHash(blockchainSetupUtil.getBlockchain().getChainHeadHash()) + .setWithdrawals(Optional.of(withdrawals))); + blockchainSetupUtil.getBlockchain().appendBlock(block, Collections.emptyList()); + + final MainnetBlockBodyValidator bodyValidator = new MainnetBlockBodyValidator(protocolSchedule); + final MainnetBlockBodyValidator bodyValidatorSpy = spy(bodyValidator); + + assertThat( + bodyValidatorSpy.validateBodyLight( + blockchainSetupUtil.getProtocolContext(), + block, + emptyList(), + Optional.empty(), + NONE, + BodyValidationMode.FULL)) + .isTrue(); + final Hash receiptsRoot = BodyValidation.receiptsRoot(emptyList()); + final Hash transactionsRoot = BodyValidation.transactionsRoot(emptyList()); + verify(bodyValidatorSpy, times(1)) + .validateReceiptsRoot(block.getHeader(), receiptsRoot, receiptsRoot); + verify(bodyValidatorSpy, times(1)) + .validateTransactionsRoot(block.getHeader(), transactionsRoot, transactionsRoot); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PragueRequestsValidatorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PragueRequestsValidatorTest.java index 833e692d7f4..6c325b70a86 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PragueRequestsValidatorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PragueRequestsValidatorTest.java @@ -106,7 +106,8 @@ void shouldValidateRequestsTest() { block, emptyList(), expectedRequests, - NONE)) + NONE, + BodyValidationMode.FULL)) .isFalse(); } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncDownloadPipelineFactory.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncDownloadPipelineFactory.java index 07e964426cc..87032b76e57 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncDownloadPipelineFactory.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncDownloadPipelineFactory.java @@ -37,6 +37,7 @@ import org.hyperledger.besu.ethereum.eth.sync.range.SyncTargetRangeSource; import org.hyperledger.besu.ethereum.eth.sync.state.SyncState; import org.hyperledger.besu.ethereum.eth.sync.state.SyncTarget; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.metrics.BesuMetricCategory; import org.hyperledger.besu.plugin.services.MetricsSystem; @@ -117,6 +118,10 @@ public Pipeline createDownloadPipelineForSyncTarget(final SyncT final int downloaderParallelism = syncConfig.getDownloaderParallelism(); final int headerRequestSize = syncConfig.getDownloaderHeaderRequestSize(); final int singleHeaderBufferSize = headerRequestSize * downloaderParallelism; + final BodyValidationMode bodyValidationMode = + protocolSchedule.anyMatch(scheduledProtocolSpec -> scheduledProtocolSpec.spec().isPoS()) + ? BodyValidationMode.NONE + : BodyValidationMode.LIGHT; final SyncTargetRangeSource checkpointRangeSource = new SyncTargetRangeSource( new RangeHeadersFetcher( @@ -148,7 +153,8 @@ public Pipeline createDownloadPipelineForSyncTarget(final SyncT attachedValidationPolicy, ommerValidationPolicy, ethContext, - fastSyncState.getPivotBlockHeader().get()); + fastSyncState.getPivotBlockHeader().get(), + bodyValidationMode); return PipelineBuilder.createPipelineFrom( "fetchCheckpoints", diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStep.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStep.java index c6945964cf0..20b9d84916a 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStep.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStep.java @@ -22,6 +22,7 @@ import org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy; import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException; import org.hyperledger.besu.ethereum.mainnet.BlockImportResult; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import java.util.List; @@ -45,6 +46,7 @@ public class ImportBlocksStep implements Consumer> { private long accumulatedTime = 0L; private OptionalLong logStartBlock = OptionalLong.empty(); private final BlockHeader pivotHeader; + private final BodyValidationMode bodyValidationMode; public ImportBlocksStep( final ProtocolSchedule protocolSchedule, @@ -52,13 +54,15 @@ public ImportBlocksStep( final ValidationPolicy headerValidationPolicy, final ValidationPolicy ommerValidationPolicy, final EthContext ethContext, - final BlockHeader pivotHeader) { + final BlockHeader pivotHeader, + final BodyValidationMode bodyValidationMode) { this.protocolSchedule = protocolSchedule; this.protocolContext = protocolContext; this.headerValidationPolicy = headerValidationPolicy; this.ommerValidationPolicy = ommerValidationPolicy; this.ethContext = ethContext; this.pivotHeader = pivotHeader; + this.bodyValidationMode = bodyValidationMode; } @Override @@ -106,20 +110,20 @@ protected static long getBlocksPercent(final long lastBlock, final long totalBlo if (totalBlocks == 0) { return 0; } - final long blocksPercent = (100 * lastBlock / totalBlocks); - return blocksPercent; + return (100 * lastBlock / totalBlocks); } protected boolean importBlock(final BlockWithReceipts blockWithReceipts) { final BlockImporter importer = protocolSchedule.getByBlockHeader(blockWithReceipts.getHeader()).getBlockImporter(); final BlockImportResult blockImportResult = - importer.fastImportBlock( + importer.importBlockForSyncing( protocolContext, blockWithReceipts.getBlock(), blockWithReceipts.getReceipts(), headerValidationPolicy.getValidationModeForNextBlock(), - ommerValidationPolicy.getValidationModeForNextBlock()); + ommerValidationPolicy.getValidationModeForNextBlock(), + bodyValidationMode); return blockImportResult.isImported(); } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStepTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStepTest.java index 70c9e10eba6..af4f45f6901 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStepTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/ImportBlocksStepTest.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy; import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException; import org.hyperledger.besu.ethereum.mainnet.BlockImportResult; +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; @@ -72,7 +73,8 @@ public void setUp() { validationPolicy, ommerValidationPolicy, null, - pivotHeader); + pivotHeader, + BodyValidationMode.FULL); } @Test @@ -84,12 +86,13 @@ public void shouldImportBlocks() { .collect(toList()); for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) { - when(blockImporter.fastImportBlock( + when(blockImporter.importBlockForSyncing( protocolContext, blockWithReceipts.getBlock(), blockWithReceipts.getReceipts(), FULL, - LIGHT)) + LIGHT, + BodyValidationMode.FULL)) .thenReturn(new BlockImportResult(true)); } importBlocksStep.accept(blocksWithReceipts); @@ -105,8 +108,13 @@ public void shouldThrowExceptionWhenValidationFails() { final Block block = gen.block(); final BlockWithReceipts blockWithReceipts = new BlockWithReceipts(block, gen.receipts(block)); - when(blockImporter.fastImportBlock( - protocolContext, block, blockWithReceipts.getReceipts(), FULL, LIGHT)) + when(blockImporter.importBlockForSyncing( + protocolContext, + block, + blockWithReceipts.getReceipts(), + FULL, + LIGHT, + BodyValidationMode.FULL)) .thenReturn(new BlockImportResult(false)); assertThatThrownBy(() -> importBlocksStep.accept(singletonList(blockWithReceipts))) .isInstanceOf(InvalidBlockException.class);