Extract tests for Schedule out of the ScheduleBuilder tests (#5239)

Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
pull/5248/head
Simon Dudley 2 years ago committed by GitHub
parent bf2e6876f6
commit 1f3d5ea2bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 118
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/DefaultTimestampScheduleTest.java
  2. 51
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java
  3. 20
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java
  4. 59
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/TimestampScheduleBuilderTest.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<ProtocolSpecBuilder, ProtocolSpecBuilder> 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();
}
}

@ -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<BigInteger> CHAIN_ID = Optional.of(BigInteger.ONE);
private static final BigInteger DEFAULT_CHAIN_ID = BigInteger.ONE;
private ProtocolScheduleBuilder builder;
private StubGenesisConfigOptions config;
private final Function<ProtocolSpecBuilder, ProtocolSpecBuilder> 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();
}
}

@ -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();
}
}

@ -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();
}
}

Loading…
Cancel
Save