From 1f3d5ea2bc95b6983c579900d0b35b32bc30f91f Mon Sep 17 00:00:00 2001 From: Simon Dudley Date: Mon, 20 Mar 2023 14:52:20 +1000 Subject: [PATCH] Extract tests for Schedule out of the ScheduleBuilder tests (#5239) Signed-off-by: Simon Dudley --- .../mainnet/DefaultTimestampScheduleTest.java | 118 ++++++++++++++++++ ....java => MutableProtocolScheduleTest.java} | 51 +++++++- .../mainnet/ProtocolScheduleBuilderTest.java | 20 --- .../mainnet/TimestampScheduleBuilderTest.java | 59 --------- 4 files changed, 168 insertions(+), 80 deletions(-) create mode 100644 ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/DefaultTimestampScheduleTest.java rename ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/{ProtocolScheduleTest.java => MutableProtocolScheduleTest.java} (62%) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/DefaultTimestampScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/DefaultTimestampScheduleTest.java new file mode 100644 index 0000000000..7be32fc63b --- /dev/null +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/DefaultTimestampScheduleTest.java @@ -0,0 +1,118 @@ +/* + * 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; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.hyperledger.besu.config.StubGenesisConfigOptions; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.evm.internal.EvmConfiguration; + +import java.math.BigInteger; +import java.util.function.Function; + +import org.junit.Before; +import org.junit.Test; + +public class DefaultTimestampScheduleTest { + + private static final BigInteger chainId = BigInteger.ONE; + private static final BigInteger defaultChainId = BigInteger.ONE; + private static final PrivacyParameters privacyParameters = new PrivacyParameters(); + private static final EvmConfiguration evmConfiguration = EvmConfiguration.DEFAULT; + private static final BlockHeader BLOCK_HEADER = + new BlockHeaderTestFixture().timestamp(1L).buildHeader(); + private TimestampScheduleBuilder builder; + private StubGenesisConfigOptions config; + + private final Function modifier = Function.identity(); + + private final long FIRST_TIMESTAMP_FORK = 1L; + + @Before + public void setup() { + config = new StubGenesisConfigOptions(); + config.chainId(chainId); + boolean isRevertReasonEnabled = false; + boolean quorumCompatibilityMode = false; + builder = + new TimestampScheduleBuilder( + config, + defaultChainId, + ProtocolSpecAdapters.create(FIRST_TIMESTAMP_FORK, modifier), + privacyParameters, + isRevertReasonEnabled, + quorumCompatibilityMode, + evmConfiguration); + } + + @Test + public void getByBlockHeader_whenSpecFound() { + config.shanghaiTime(FIRST_TIMESTAMP_FORK); + final TimestampSchedule schedule = builder.createTimestampSchedule(); + + assertThat(schedule.getByBlockHeader(BLOCK_HEADER)).isNotNull(); + } + + @Test + public void getByBlockHeader_whenSpecNotFoundReturnsNull() { + config.shanghaiTime(2L); + builder = + new TimestampScheduleBuilder( + config, + defaultChainId, + ProtocolSpecAdapters.create(2L, modifier), + privacyParameters, + false, + false, + evmConfiguration); + final TimestampSchedule schedule = builder.createTimestampSchedule(); + + assertThat(schedule.getByBlockHeader(BLOCK_HEADER)).isNull(); + } + + @Test + public void streamMilestoneBlocksReturnTimestampsInOrder() { + config.shanghaiTime(FIRST_TIMESTAMP_FORK); + config.cancunTime(2L); + config.experimentalEipsTime(5L); + config.futureEipsTime(3L); + final TimestampSchedule schedule = builder.createTimestampSchedule(); + + assertThat(schedule.streamMilestoneBlocks()).containsExactly(FIRST_TIMESTAMP_FORK, 2L, 3L, 5L); + } + + @Test + public void isOnMilestoneBoundary() { + config.shanghaiTime(FIRST_TIMESTAMP_FORK); + config.cancunTime(2L); + config.experimentalEipsTime(4L); + final HeaderBasedProtocolSchedule protocolSchedule = builder.createTimestampSchedule(); + + assertThat(protocolSchedule.isOnMilestoneBoundary(header(0))).isEqualTo(false); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(FIRST_TIMESTAMP_FORK))) + .isEqualTo(true); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(2))).isEqualTo(true); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(3))).isEqualTo(false); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(4))).isEqualTo(true); + } + + private BlockHeader header(final long timestamp) { + return new BlockHeaderTestFixture().timestamp(timestamp).buildHeader(); + } +} diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java similarity index 62% rename from ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleTest.java rename to ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java index b37235e2ce..c29a155d4c 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java @@ -18,20 +18,51 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.hyperledger.besu.config.StubGenesisConfigOptions; import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.evm.internal.EvmConfiguration; import java.math.BigInteger; import java.util.Optional; +import java.util.function.Function; import org.assertj.core.api.Assertions; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) -public class ProtocolScheduleTest { +public class MutableProtocolScheduleTest { private static final Optional CHAIN_ID = Optional.of(BigInteger.ONE); + private static final BigInteger DEFAULT_CHAIN_ID = BigInteger.ONE; + + private ProtocolScheduleBuilder builder; + private StubGenesisConfigOptions config; + + private final Function modifier = Function.identity(); + + private final long FIRST_TIMESTAMP_FORK = 1L; + + @Before + public void setup() { + config = new StubGenesisConfigOptions(); + config.chainId(DEFAULT_CHAIN_ID); + boolean isRevertReasonEnabled = false; + boolean quorumCompatibilityMode = false; + builder = + new ProtocolScheduleBuilder( + config, + DEFAULT_CHAIN_ID, + ProtocolSpecAdapters.create(FIRST_TIMESTAMP_FORK, modifier), + new PrivacyParameters(), + isRevertReasonEnabled, + quorumCompatibilityMode, + EvmConfiguration.DEFAULT); + } @SuppressWarnings("unchecked") @Test @@ -87,4 +118,22 @@ public class ProtocolScheduleTest { assertThat(spec).isEqualTo(spec2); } + + @Test + public void isOnMilestoneBoundary() { + config.berlinBlock(1L); + config.londonBlock(2L); + config.mergeNetSplitBlock(4L); + final HeaderBasedProtocolSchedule protocolSchedule = builder.createProtocolSchedule(); + + assertThat(protocolSchedule.isOnMilestoneBoundary(header(0))).isEqualTo(true); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(1))).isEqualTo(true); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(2))).isEqualTo(true); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(3))).isEqualTo(false); + assertThat(protocolSchedule.isOnMilestoneBoundary(header(4))).isEqualTo(true); + } + + private BlockHeader header(final long blockNumber) { + return new BlockHeaderTestFixture().number(blockNumber).buildHeader(); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java index c223bbbe11..6d1ceba63f 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java @@ -22,8 +22,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.config.GenesisConfigOptions; -import org.hyperledger.besu.ethereum.core.BlockHeader; -import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -189,22 +187,4 @@ public class ProtocolScheduleBuilderTest { verify(modifier, times(1)).apply(any()); } - - @Test - public void isOnMilestoneBoundary() { - when(configOptions.getBerlinBlockNumber()).thenReturn(OptionalLong.of(1L)); - when(configOptions.getLondonBlockNumber()).thenReturn(OptionalLong.of(2L)); - when(configOptions.getMergeNetSplitBlockNumber()).thenReturn(OptionalLong.of(4L)); - final HeaderBasedProtocolSchedule protocolSchedule = builder.createProtocolSchedule(); - - assertThat(protocolSchedule.isOnMilestoneBoundary(header(0))).isEqualTo(true); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(1))).isEqualTo(true); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(2))).isEqualTo(true); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(3))).isEqualTo(false); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(4))).isEqualTo(true); - } - - private BlockHeader header(final long blockNumber) { - return new BlockHeaderTestFixture().number(blockNumber).buildHeader(); - } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/TimestampScheduleBuilderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/TimestampScheduleBuilderTest.java index b1489e88ff..8e367ac4dc 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/TimestampScheduleBuilderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/TimestampScheduleBuilderTest.java @@ -18,8 +18,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.hyperledger.besu.config.StubGenesisConfigOptions; -import org.hyperledger.besu.ethereum.core.BlockHeader; -import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -35,8 +33,6 @@ public class TimestampScheduleBuilderTest { private static final BigInteger defaultChainId = BigInteger.ONE; private static final PrivacyParameters privacyParameters = new PrivacyParameters(); private static final EvmConfiguration evmConfiguration = EvmConfiguration.DEFAULT; - private static final BlockHeader BLOCK_HEADER = - new BlockHeaderTestFixture().timestamp(1L).buildHeader(); private TimestampScheduleBuilder builder; private StubGenesisConfigOptions config; @@ -113,59 +109,4 @@ public class TimestampScheduleBuilderTest { .hasMessage( "Genesis Config Error: 'Cancun' is scheduled for milestone 2 but it must be on or after milestone 3."); } - - @Test - public void getByBlockHeader_whenSpecFound() { - config.shanghaiTime(FIRST_TIMESTAMP_FORK); - final TimestampSchedule schedule = builder.createTimestampSchedule(); - - assertThat(schedule.getByBlockHeader(BLOCK_HEADER)).isNotNull(); - } - - @Test - public void getByBlockHeader_whenSpecNotFoundReturnsNull() { - config.shanghaiTime(2L); - builder = - new TimestampScheduleBuilder( - config, - defaultChainId, - ProtocolSpecAdapters.create(2L, modifier), - privacyParameters, - false, - false, - evmConfiguration); - final TimestampSchedule schedule = builder.createTimestampSchedule(); - - assertThat(schedule.getByBlockHeader(BLOCK_HEADER)).isNull(); - } - - @Test - public void streamMilestoneBlocksReturnTimestampsInOrder() { - config.shanghaiTime(FIRST_TIMESTAMP_FORK); - config.cancunTime(2L); - config.experimentalEipsTime(5L); - config.futureEipsTime(3L); - final TimestampSchedule schedule = builder.createTimestampSchedule(); - - assertThat(schedule.streamMilestoneBlocks()).containsExactly(FIRST_TIMESTAMP_FORK, 2L, 3L, 5L); - } - - @Test - public void isOnMilestoneBoundary() { - config.shanghaiTime(FIRST_TIMESTAMP_FORK); - config.cancunTime(2L); - config.experimentalEipsTime(4L); - final HeaderBasedProtocolSchedule protocolSchedule = builder.createTimestampSchedule(); - - assertThat(protocolSchedule.isOnMilestoneBoundary(header(0))).isEqualTo(false); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(FIRST_TIMESTAMP_FORK))) - .isEqualTo(true); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(2))).isEqualTo(true); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(3))).isEqualTo(false); - assertThat(protocolSchedule.isOnMilestoneBoundary(header(4))).isEqualTo(true); - } - - private BlockHeader header(final long timestamp) { - return new BlockHeaderTestFixture().timestamp(timestamp).buildHeader(); - } }