Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup EphemeryNetwork and add max slot constant #8778

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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