Skip to content

Commit

Permalink
cleanup EphemeryNetwork and add max slot constant
Browse files Browse the repository at this point in the history
fixes Consensys#8777

Signed-off-by: Paul Harris <paul.harris@consensys.net>
  • Loading branch information
rolfyone committed Oct 24, 2024
1 parent 7751981 commit 186fbb2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigLoader;
import tech.pegasys.teku.spec.config.builder.SpecConfigBuilder;
import tech.pegasys.teku.spec.networks.Eth2Network;

public class EphemeryNetwork {
private static final long GENESIS_CHAINID = 39438135;
private static final long GENESIS_TIMESTAMP = 1720119600;
private static final long INITIAL_GENESIS_TIMESTAMP = 1720119600;
private static final int PERIOD = 28;
private static final long PERIOD_IN_SECONDS = (PERIOD * 24 * 60 * 60);
public static final long MAX_EPHEMERY_SLOT = (PERIOD_IN_SECONDS / 12) - 1;

static long getPeriodsSinceGenesis(final TimeProvider timeProvider) {
return ChronoUnit.DAYS.between(
Instant.ofEpochSecond(GENESIS_TIMESTAMP),
Instant.ofEpochSecond(INITIAL_GENESIS_TIMESTAMP),
Instant.ofEpochMilli(timeProvider.getTimeInMillis().longValue()))
/ PERIOD;
}
Expand All @@ -43,23 +43,19 @@ public static void updateConfig(final SpecConfigBuilder builder) {
static void updateConfig(final SpecConfigBuilder builder, final TimeProvider timeProvider) {
final SpecConfig config = SpecConfigLoader.loadConfig("ephemery");
final SpecConfigBuilder rawConfigBuilder = builder.rawConfig(config.getRawConfig());

if (Eth2Network.EPHEMERY.configName().equals("ephemery")) {
final long currentTimestamp = timeProvider.getTimeInMillis().longValue() / 1000;

final long updatedTimestamp =
GENESIS_TIMESTAMP + (getPeriodsSinceGenesis(timeProvider) * PERIOD_IN_SECONDS);
final long updatedChainId = GENESIS_CHAINID + getPeriodsSinceGenesis(timeProvider);

try {
if (currentTimestamp > (GENESIS_TIMESTAMP + PERIOD_IN_SECONDS)) {
rawConfigBuilder.depositNetworkId(updatedChainId);
rawConfigBuilder.depositChainId(updatedChainId);
rawConfigBuilder.minGenesisTime(UInt64.valueOf(updatedTimestamp));
}
} catch (RuntimeException e) {
throw new RuntimeException("Error updating genesis file: " + e.getMessage(), e);
final long periodsSinceInitialGenesis = getPeriodsSinceGenesis(timeProvider);

try {
if (periodsSinceInitialGenesis > 0L) {
final long updatedChainId = GENESIS_CHAINID + periodsSinceInitialGenesis;
final long currentPeriodGenesis =
INITIAL_GENESIS_TIMESTAMP + (periodsSinceInitialGenesis * PERIOD_IN_SECONDS);
rawConfigBuilder.depositNetworkId(updatedChainId);
rawConfigBuilder.depositChainId(updatedChainId);
rawConfigBuilder.minGenesisTime(UInt64.valueOf(currentPeriodGenesis));
}
} catch (RuntimeException e) {
throw new RuntimeException("Error updating genesis file: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import tech.pegasys.teku.spec.config.SpecConfigLoader;
import tech.pegasys.teku.spec.config.SpecConfigReader;
import tech.pegasys.teku.spec.config.builder.SpecConfigBuilder;
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;

public class EphemeryNetworkTest {
private static final long GENESIS_CHAINID = 39438135;
Expand Down Expand Up @@ -187,6 +188,20 @@ public void shouldUpdateConfigForManyPeriods() {
.isGreaterThan(genesisChainIdAfter1000Period + PERIOD_IN_SECONDS);
}

@Test
void checkEphemeryMaxSlot() {
final Spec spec =
getSpec(phase0Builder -> phase0Builder.minGenesisTime(UInt64.valueOf(MIN_GENESIS_TIME)));
final long timeBeforeNextPeriod = MIN_GENESIS_TIME + PERIOD_IN_SECONDS - 1;
final MiscHelpers miscHelpers = new MiscHelpers(spec.getGenesisSpecConfig());
assertThat(
miscHelpers
.computeSlotAtTime(
UInt64.valueOf(MIN_GENESIS_TIME), UInt64.valueOf(timeBeforeNextPeriod))
.longValue())
.isEqualTo(EphemeryNetwork.MAX_EPHEMERY_SLOT);
}

@Test
void shouldNotUpdateConfigBeforeNextPeriod() {
final Spec spec =
Expand Down

0 comments on commit 186fbb2

Please sign in to comment.