Skip to content

Commit

Permalink
SpecConfigAndParent
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Nov 13, 2024
1 parent 19e01e9 commit e6efa54
Show file tree
Hide file tree
Showing 52 changed files with 346 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class DepositProcessingControllerTest {
private DepositProcessingController depositProcessingController;

private void createDepositProcessingController(final Consumer<SpecConfigBuilder> configModifier) {
final SpecConfig config = SpecConfigLoader.loadConfig("minimal", configModifier);
final SpecConfig config = SpecConfigLoader.loadConfig("minimal", configModifier).specConfig();
depositProcessingController =
new DepositProcessingController(
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Eth1DepositManagerTest {
SpecConfigLoader.loadConfig(
"minimal",
builder ->
builder.minGenesisTime(UInt64.valueOf(10_300)).genesisDelay(UInt64.valueOf(300)));
builder.minGenesisTime(UInt64.valueOf(10_300)).genesisDelay(UInt64.valueOf(300))).specConfig();
private final Spec spec = TestSpecFactory.createMinimalBellatrix();
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import tech.pegasys.teku.infrastructure.async.StubAsyncRunner;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigLoader;

public class MinimumGenesisTimeBlockFinderTest {

// Setup so genesis time for a block will be blockTime + 2
private final SpecConfig config =
SpecConfigLoader.loadConfig("minimal", builder -> builder.genesisDelay(UInt64.valueOf(2)));
SpecConfigLoader.loadConfig("minimal", builder -> builder.genesisDelay(UInt64.valueOf(2))).specConfig();
private final Eth1Provider eth1Provider = mock(Eth1Provider.class);
private final StubAsyncRunner asyncRunner = new StubAsyncRunner();

Expand Down Expand Up @@ -303,7 +304,7 @@ private void withMinGenesisTime(
final long minGenesisTime, final Optional<UInt64> eth1DepositContractDeployBlock) {
final SpecConfig config =
SpecConfigLoader.loadConfig(
"minimal", builder -> builder.minGenesisTime(UInt64.valueOf(minGenesisTime)));
"minimal", builder -> builder.minGenesisTime(UInt64.valueOf(minGenesisTime))).specConfig();
minimumGenesisTimeBlockFinder =
new MinimumGenesisTimeBlockFinder(config, eth1Provider, eth1DepositContractDeployBlock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import tech.pegasys.teku.spec.config.SpecConfigLoader;

class Eth1DataCachePeriodCalculatorTest {
private final SpecConfig config = SpecConfigLoader.loadConfig("minimal");
private final SpecConfig config = SpecConfigLoader.loadConfig("minimal").specConfig();

@Test
void shouldCalculateCachePeriodForMinimalConstantsFromFollowDistance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigLoader;

class Eth1VotingPeriodTest {

final SpecConfig specConfig =
final SpecConfigAndParent<?, SpecConfig> specConfig =
SpecConfigLoader.loadConfig(
"minimal",
b ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class ManualReferenceTestRunner extends Eth2ReferenceTestCase {
*
* <p>May be overridden by the ENV_DISPLAY_NAME environment variable.
*/
private static final String DISPLAY_NAME = "";
private static final String DISPLAY_NAME = "deneb - minimal - sanity/blocks - invalid_exceed_max_blobs_per_block";

@ParameterizedTest(name = "{0}")
@MethodSource("loadReferenceTests")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import tech.pegasys.teku.spec.SpecFactory;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigLoader;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
Expand All @@ -47,7 +48,7 @@ public void runTest(final TestDefinition testDefinition) throws Throwable {
private void processUpgrade(final TestDefinition testDefinition, final MetaData metadata) {
final SpecMilestone milestone = SpecMilestone.forName(metadata.postFork);
final UInt64 forkEpoch = UInt64.valueOf(metadata.forkEpoch);
final SpecConfig config =
final SpecConfigAndParent<?, SpecConfig> config =
SpecConfigLoader.loadConfig(
testDefinition.getConfigName(),
builder -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void updateConfig(final SpecConfigBuilder builder) {
}

static void updateConfig(final SpecConfigBuilder builder, final TimeProvider timeProvider) {
final SpecConfig config = SpecConfigLoader.loadConfig("ephemery");
final SpecConfig config = SpecConfigLoader.loadConfig("ephemery").specConfig();
final SpecConfigBuilder rawConfigBuilder = builder.rawConfig(config.getRawConfig());
final long periodsSinceInitialGenesis = getPeriodsSinceGenesis(timeProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecFactory;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigLoader;
import tech.pegasys.teku.spec.config.SpecConfigReader;
import tech.pegasys.teku.spec.config.builder.SpecConfigBuilder;
Expand All @@ -51,7 +52,7 @@ public class EphemeryNetworkTest {
private long expectedChainId;
private long periodSinceGenesis;
private final SpecConfigReader reader = new SpecConfigReader();
private SpecConfig configFile;
private SpecConfigAndParent<?, SpecConfig> configFile;
private SpecConfig config;

@BeforeEach
Expand All @@ -66,7 +67,7 @@ void setUp() {

@Test
public void testUpdateConfig() {
when(config.getRawConfig()).thenReturn(configFile.getRawConfig());
when(config.getRawConfig()).thenReturn(configFile.specConfig().getRawConfig());
when(builder.rawConfig(config.getRawConfig())).thenReturn(builder);
when(builder.depositChainId(expectedChainId)).thenReturn(builder);
when(builder.depositNetworkId(expectedChainId)).thenReturn(builder);
Expand Down Expand Up @@ -143,7 +144,7 @@ public void shouldUpdateConfigAfterFirstPeriod() {

final long expectedMinGenesisTime = MIN_GENESIS_TIME + (ONE_PERIOD * PERIOD_IN_SECONDS);

when(config.getRawConfig()).thenReturn(configFile.getRawConfig());
when(config.getRawConfig()).thenReturn(configFile.specConfig().getRawConfig());
when(builder.rawConfig(config.getRawConfig())).thenReturn(builder);
when(builder.depositChainId(genesisChainidAfterFirstPeriod)).thenReturn(builder);
when(builder.depositNetworkId(genesisChainidAfterFirstPeriod)).thenReturn(builder);
Expand All @@ -169,7 +170,7 @@ public void shouldUpdateConfigForManyPeriods() {

final long expectedMinGenesisTime = MIN_GENESIS_TIME + MANY_PERIOD * PERIOD_IN_SECONDS;

when(config.getRawConfig()).thenReturn(configFile.getRawConfig());
when(config.getRawConfig()).thenReturn(configFile.specConfig().getRawConfig());
when(builder.rawConfig(config.getRawConfig())).thenReturn(builder);
when(builder.depositChainId(genesisChainIdAfter1000Period)).thenReturn(builder);
when(builder.depositNetworkId(genesisChainIdAfter1000Period)).thenReturn(builder);
Expand Down Expand Up @@ -255,7 +256,7 @@ private void readConfig(final InputStream preset) throws IOException {
}

private Spec getSpec(final Consumer<SpecConfigBuilder> consumer) {
final SpecConfig config = SpecConfigLoader.loadConfig("ephemery", consumer);
final SpecConfigAndParent<?,SpecConfig> config = SpecConfigLoader.loadConfig("ephemery", consumer);
return SpecFactory.create(config);
}
}
8 changes: 6 additions & 2 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import tech.pegasys.teku.ethereum.performance.trackers.BlockProductionPerformance;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.collections.TekuPair;
import tech.pegasys.teku.infrastructure.ssz.Merkleizable;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand All @@ -52,6 +53,9 @@
import tech.pegasys.teku.spec.config.NetworkingSpecConfigElectra;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAltair;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigBellatrix;
import tech.pegasys.teku.spec.config.SpecConfigCapella;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.constants.Domain;
Expand Down Expand Up @@ -123,13 +127,13 @@ private Spec(
this.stateTransition = new StateTransition(this::atSlot);
}

static Spec create(final SpecConfig config, final SpecMilestone highestMilestoneSupported) {
static Spec create(final SpecConfigAndParent<?, SpecConfig> config, final SpecMilestone highestMilestoneSupported) {
final Map<SpecMilestone, SpecVersion> specVersions = new EnumMap<>(SpecMilestone.class);
final ForkSchedule.Builder forkScheduleBuilder = ForkSchedule.builder();
final SchemaRegistryBuilder schemaRegistryBuilder = SchemaRegistryBuilder.create();

for (SpecMilestone milestone : SpecMilestone.getMilestonesUpTo(highestMilestoneSupported)) {
SpecVersion.create(milestone, config, schemaRegistryBuilder)
SpecVersion.create(milestone, config.forMilestone(milestone), schemaRegistryBuilder)
.ifPresent(
milestoneSpec -> {
forkScheduleBuilder.addNextMilestone(milestoneSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;

import java.util.function.Consumer;

import tech.pegasys.teku.infrastructure.collections.TekuPair;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigAltair;
import tech.pegasys.teku.spec.config.SpecConfigAndParent;
import tech.pegasys.teku.spec.config.SpecConfigBellatrix;
import tech.pegasys.teku.spec.config.SpecConfigCapella;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
Expand All @@ -39,27 +42,27 @@ public static Spec create(final String configName) {
}

public static Spec create(final String configName, final Consumer<SpecConfigBuilder> modifier) {
final SpecConfig config = SpecConfigLoader.loadConfig(configName, modifier);
final SpecConfigAndParent<?, SpecConfig> config = SpecConfigLoader.loadConfig(configName, modifier);
return create(config);
}

public static Spec create(final SpecConfig config) {
public static Spec create(final SpecConfigAndParent<?, SpecConfig> config) {
final UInt64 altairForkEpoch =
config.toVersionAltair().map(SpecConfigAltair::getAltairForkEpoch).orElse(FAR_FUTURE_EPOCH);
config.specConfig().toVersionAltair().map(SpecConfigAltair::getAltairForkEpoch).orElse(FAR_FUTURE_EPOCH);
final UInt64 bellatrixForkEpoch =
config
config.specConfig()
.toVersionBellatrix()
.map(SpecConfigBellatrix::getBellatrixForkEpoch)
.orElse(FAR_FUTURE_EPOCH);
final UInt64 capellaForkEpoch =
config
config.specConfig()
.toVersionCapella()
.map(SpecConfigCapella::getCapellaForkEpoch)
.orElse(FAR_FUTURE_EPOCH);
final UInt64 denebForkEpoch =
config.toVersionDeneb().map(SpecConfigDeneb::getDenebForkEpoch).orElse(FAR_FUTURE_EPOCH);
config.specConfig().toVersionDeneb().map(SpecConfigDeneb::getDenebForkEpoch).orElse(FAR_FUTURE_EPOCH);
final UInt64 electraForkEpoch =
config
config.specConfig()
.toVersionElectra()
.map(SpecConfigElectra::getElectraForkEpoch)
.orElse(FAR_FUTURE_EPOCH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;

public class DelegatingSpecConfig implements SpecConfig {
protected final SpecConfig specConfig;
Expand Down Expand Up @@ -395,4 +396,9 @@ public int getAttestationSubnetPrefixBits() {
public int getProposerScoreBoost() {
return specConfig.getProposerScoreBoost();
}

@Override
public SpecMilestone getMilestone() {
return specConfig.getMilestone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.config.builder.SpecConfigBuilder;

public interface SpecConfig extends NetworkingSpecConfig {
Expand Down Expand Up @@ -188,4 +189,6 @@ default Optional<SpecConfigDeneb> toVersionDeneb() {
default Optional<SpecConfigElectra> toVersionElectra() {
return Optional.empty();
}

SpecMilestone getMilestone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;

public class SpecConfigAltairImpl extends DelegatingSpecConfig implements SpecConfigAltair {

Expand Down Expand Up @@ -156,6 +157,11 @@ public Optional<SpecConfigAltair> toVersionAltair() {
return Optional.of(this);
}

@Override
public SpecMilestone getMilestone() {
return SpecMilestone.ALTAIR;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package tech.pegasys.teku.spec.config;

import tech.pegasys.teku.spec.SpecMilestone;

import java.util.Optional;

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

public record SpecConfigAndParent<TConfig extends TParentConfig,
TParentConfig extends SpecConfig>(TConfig specConfig, Optional<SpecConfigAndParent<TParentConfig, SpecConfig>> parentSpecConfig) {

public static <TConfig extends TParentConfig, TParentConfig extends SpecConfig> SpecConfigAndParent<TConfig, TParentConfig> of(final TConfig spec, final SpecConfigAndParent<TParentConfig, SpecConfig> parentSpec) {
return new SpecConfigAndParent<>(spec, Optional.of(parentSpec));
}

public static <TConfig extends TParentConfig, TParentConfig extends SpecConfig> SpecConfigAndParent<TConfig, TParentConfig> of(final TConfig spec) {
return new SpecConfigAndParent<>(spec, Optional.empty());
}

public SpecConfig forMilestone(final SpecMilestone milestone) {
if(specConfig.getMilestone() == milestone) {
return specConfig;
}
if(parentSpecConfig.isEmpty()) {
throw new IllegalArgumentException("No config available for milestone " + milestone);
}
return parentSpecConfig.get().forMilestone(milestone);
}


@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigPhase0, SpecConfig> requirePhase0(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.PHASE0);
return (SpecConfigAndParent<SpecConfigPhase0, SpecConfig>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigAltair, SpecConfig> requireAltair(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.ALTAIR);
return (SpecConfigAndParent<SpecConfigAltair, SpecConfig>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigBellatrix, SpecConfig> requireBellatrix(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.BELLATRIX);
return (SpecConfigAndParent<SpecConfigBellatrix, SpecConfig>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigCapella, SpecConfig> requireCapella(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.CAPELLA);
return (SpecConfigAndParent<SpecConfigCapella, SpecConfig>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigDeneb, SpecConfig> requireDeneb(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.DENEB);
return (SpecConfigAndParent<SpecConfigDeneb, SpecConfig>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigElectra, SpecConfig> requireElectra(final SpecConfigAndParent<?, SpecConfig> specConfigAndParent) {
checkArgument(
specConfigAndParent.specConfig.getMilestone() == SpecMilestone.ELECTRA);
return (SpecConfigAndParent<SpecConfigElectra, SpecConfig>) specConfigAndParent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.tuweni.units.bigints.UInt256;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;

public class SpecConfigBellatrixImpl extends DelegatingSpecConfigAltair
implements SpecConfigBellatrix {
Expand Down Expand Up @@ -153,6 +154,11 @@ public Optional<SpecConfigBellatrix> toVersionBellatrix() {
return Optional.of(this);
}

@Override
public SpecMilestone getMilestone() {
return SpecMilestone.BELLATRIX;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;

public class SpecConfigCapellaImpl extends DelegatingSpecConfigBellatrix
implements SpecConfigCapella {
Expand Down Expand Up @@ -100,4 +101,9 @@ public int getMaxValidatorsPerWithdrawalSweep() {
public Optional<SpecConfigCapella> toVersionCapella() {
return Optional.of(this);
}

@Override
public SpecMilestone getMilestone() {
return SpecMilestone.CAPELLA;
}
}
Loading

0 comments on commit e6efa54

Please sign in to comment.