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 6c730f6
Show file tree
Hide file tree
Showing 54 changed files with 568 additions and 317 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 @@ -56,9 +56,10 @@ class Eth1DepositManagerTest {
private static final int MIN_GENESIS_BLOCK_TIMESTAMP = 10_000;
private final SpecConfig config =
SpecConfigLoader.loadConfig(
"minimal",
builder ->
builder.minGenesisTime(UInt64.valueOf(10_300)).genesisDelay(UInt64.valueOf(300)));
"minimal",
builder ->
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 @@ -38,7 +38,8 @@ 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,8 @@ 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<? extends SpecConfig> specConfig =
SpecConfigLoader.loadConfig(
"minimal",
b ->
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<? extends 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<? extends 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,8 @@ private void readConfig(final InputStream preset) throws IOException {
}

private Spec getSpec(final Consumer<SpecConfigBuilder> consumer) {
final SpecConfig config = SpecConfigLoader.loadConfig("ephemery", consumer);
final SpecConfigAndParent<? extends SpecConfig> config =
SpecConfigLoader.loadConfig("ephemery", consumer);
return SpecFactory.create(config);
}
}
20 changes: 16 additions & 4 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.constants.Domain;
Expand Down Expand Up @@ -111,25 +112,32 @@ public class Spec {
private final Map<SpecMilestone, SpecVersion> specVersions;
private final ForkSchedule forkSchedule;
private final StateTransition stateTransition;
private final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent;

private Spec(
final Map<SpecMilestone, SpecVersion> specVersions, final ForkSchedule forkSchedule) {
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent,
final Map<SpecMilestone, SpecVersion> specVersions,
final ForkSchedule forkSchedule) {
Preconditions.checkArgument(specVersions != null && !specVersions.isEmpty());
Preconditions.checkArgument(forkSchedule != null);
this.specConfigAndParent = specConfigAndParent;
this.specVersions = specVersions;
this.forkSchedule = forkSchedule;

// Setup state transition
this.stateTransition = new StateTransition(this::atSlot);
}

static Spec create(final SpecConfig config, final SpecMilestone highestMilestoneSupported) {
static Spec create(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent,
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, specConfigAndParent.forMilestone(milestone), schemaRegistryBuilder)
.ifPresent(
milestoneSpec -> {
forkScheduleBuilder.addNextMilestone(milestoneSpec);
Expand All @@ -139,7 +147,7 @@ static Spec create(final SpecConfig config, final SpecMilestone highestMilestone

final ForkSchedule forkSchedule = forkScheduleBuilder.build();

return new Spec(specVersions, forkSchedule);
return new Spec(specConfigAndParent, specVersions, forkSchedule);
}

public SpecVersion forMilestone(final SpecMilestone milestone) {
Expand All @@ -162,6 +170,10 @@ private SpecVersion atTimeMillis(final UInt64 genesisTimeMillis, final UInt64 cu
return atTime(millisToSeconds(genesisTimeMillis), millisToSeconds(currentTimeMillis));
}

public SpecConfigAndParent<? extends SpecConfig> getSpecConfigAndParent() {
return specConfigAndParent;
}

public SpecConfig getSpecConfig(final UInt64 epoch) {
return atEpoch(epoch).getConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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 +40,39 @@ 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<? extends SpecConfig> config =
SpecConfigLoader.loadConfig(configName, modifier);
return create(config);
}

public static Spec create(final SpecConfig config) {
public static Spec create(final SpecConfigAndParent<? extends 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
.specConfig()
.toVersionBellatrix()
.map(SpecConfigBellatrix::getBellatrixForkEpoch)
.orElse(FAR_FUTURE_EPOCH);
final UInt64 capellaForkEpoch =
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
.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,84 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.
*/

package tech.pegasys.teku.spec.config;

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

import java.util.Optional;
import tech.pegasys.teku.spec.SpecMilestone;

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

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

public static <TConfig extends SpecConfig> SpecConfigAndParent<TConfig> 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> requirePhase0(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
return (SpecConfigAndParent<SpecConfigPhase0>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigAltair> requireAltair(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
checkArgument(specConfigAndParent.specConfig.toVersionAltair().isPresent());
return (SpecConfigAndParent<SpecConfigAltair>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigBellatrix> requireBellatrix(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
checkArgument(specConfigAndParent.specConfig.toVersionBellatrix().isPresent());
return (SpecConfigAndParent<SpecConfigBellatrix>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigCapella> requireCapella(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
checkArgument(specConfigAndParent.specConfig.toVersionCapella().isPresent());
return (SpecConfigAndParent<SpecConfigCapella>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigDeneb> requireDeneb(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
checkArgument(specConfigAndParent.specConfig.toVersionDeneb().isPresent());
return (SpecConfigAndParent<SpecConfigDeneb>) specConfigAndParent;
}

@SuppressWarnings("unchecked")
public static SpecConfigAndParent<SpecConfigElectra> requireElectra(
final SpecConfigAndParent<? extends SpecConfig> specConfigAndParent) {
checkArgument(specConfigAndParent.specConfig.toVersionElectra().isPresent());
return (SpecConfigAndParent<SpecConfigElectra>) specConfigAndParent;
}
}
Loading

0 comments on commit 6c730f6

Please sign in to comment.