Skip to content

Commit

Permalink
Moved initial state and genesis state config into StateBootstrapConfig (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha authored Jan 16, 2024
1 parent 74d7c8a commit b71a58e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class Eth2NetworkConfiguration {

public static final boolean DEFAULT_FORK_CHOICE_UPDATED_ALWAYS_SEND_PAYLOAD_ATTRIBUTES = false;

public static final boolean DEFAULT_ALLOW_SYNC_OUTSIDE_WEAK_SUBJECTIVITY_PERIOD = false;

public static final int DEFAULT_ASYNC_P2P_MAX_THREADS = 10;

public static final int DEFAULT_ASYNC_P2P_MAX_QUEUE = DEFAULT_MAX_QUEUE_SIZE;
Expand All @@ -76,8 +78,6 @@ public class Eth2NetworkConfiguration {

private final Spec spec;
private final String constants;
private final Optional<String> initialState;
private final Optional<String> genesisState;
private final StateBoostrapConfig stateBoostrapConfig;
private final int startupTargetPeerCount;
private final int startupTimeoutSeconds;
Expand Down Expand Up @@ -105,8 +105,6 @@ public class Eth2NetworkConfiguration {
private Eth2NetworkConfiguration(
final Spec spec,
final String constants,
final Optional<String> initialState,
final Optional<String> genesisState,
final StateBoostrapConfig stateBoostrapConfig,
final int startupTargetPeerCount,
final int startupTimeoutSeconds,
Expand All @@ -132,8 +130,6 @@ private Eth2NetworkConfiguration(
final boolean forkChoiceUpdatedAlwaysSendPayloadAttributes) {
this.spec = spec;
this.constants = constants;
this.initialState = initialState;
this.genesisState = genesisState;
this.stateBoostrapConfig = stateBoostrapConfig;
this.startupTargetPeerCount = startupTargetPeerCount;
this.startupTimeoutSeconds = startupTimeoutSeconds;
Expand Down Expand Up @@ -188,14 +184,6 @@ public String getConstants() {
return constants;
}

public Optional<String> getInitialState() {
return initialState;
}

public Optional<String> getGenesisState() {
return genesisState;
}

public StateBoostrapConfig getNetworkBoostrapConfig() {
return stateBoostrapConfig;
}
Expand Down Expand Up @@ -290,9 +278,12 @@ public String toString() {
public static class Builder {
private static final String EPOCHS_STORE_BLOBS_MAX_KEYWORD = "MAX";
private String constants;
private Optional<String> initialState = Optional.empty();
private final StateBoostrapConfig stateBoostrapConfig = new StateBoostrapConfig();
private Optional<String> genesisState = Optional.empty();
private Optional<String> initialState = Optional.empty();
private Optional<String> checkpointSyncUrl = Optional.empty();
private boolean isUsingCustomInitialState = false;
private boolean allowSyncOutsideWeakSubjectivityPeriod =
DEFAULT_ALLOW_SYNC_OUTSIDE_WEAK_SUBJECTIVITY_PERIOD;
private int startupTargetPeerCount = DEFAULT_STARTUP_TARGET_PEER_COUNT;
private int startupTimeoutSeconds = DEFAULT_STARTUP_TIMEOUT_SECONDS;
private int asyncP2pMaxThreads = DEFAULT_ASYNC_P2P_MAX_THREADS;
Expand Down Expand Up @@ -374,9 +365,12 @@ public Eth2NetworkConfiguration build() {
return new Eth2NetworkConfiguration(
spec,
constants,
initialState,
genesisState,
stateBoostrapConfig,
new StateBoostrapConfig(
genesisState,
initialState,
checkpointSyncUrl,
isUsingCustomInitialState,
allowSyncOutsideWeakSubjectivityPeriod),
startupTargetPeerCount,
startupTimeoutSeconds,
discoveryBootnodes,
Expand Down Expand Up @@ -432,30 +426,41 @@ public Builder constants(final String constants) {
}

public Builder checkpointSyncUrl(final String checkpointSyncUrl) {
this.checkpointSyncUrl = Optional.of(checkpointSyncUrl);
this.genesisState =
Optional.of(UrlSanitizer.appendPath(checkpointSyncUrl, GENESIS_STATE_URL_PATH));
this.initialState =
Optional.of(UrlSanitizer.appendPath(checkpointSyncUrl, FINALIZED_STATE_URL_PATH));
this.stateBoostrapConfig.setUsingCheckpointSync(true);
return this;
}

/** Used when the user specifies the --initial-state option in the CLI. */
public Builder customInitialState(final String initialState) {
this.initialState = Optional.of(initialState);
this.stateBoostrapConfig.setUsingCustomInitialState(true);
this.isUsingCustomInitialState = true;
return this;
}

public Builder defaultInitialState(final String initialState) {
/**
* Used to load initial states from a URL.
*
* @param initialState The URL pointing to a initial state resource (e.g. a file on GitHub or an
* Beacon API debug state endpoint.
*/
public Builder defaultInitialStateFromUrl(final String initialState) {
this.initialState = Optional.of(initialState);
this.stateBoostrapConfig.setUsingCustomInitialState(false);
return this;
}

public Builder initialStateFromClasspath(final String filename) {
this.initialState =
Optional.ofNullable(Eth2NetworkConfiguration.class.getResource(filename))
.map(URL::toExternalForm);
/**
* Used to load initial states from SSZ files within our distributed jar.
*
* @param filename the name of the ssz file (e.g. "mainnet-genesis.ssz")
*/
public Builder defaultInitialStateFromClasspath(final String filename) {
Optional.ofNullable(Eth2NetworkConfiguration.class.getResource(filename))
.map(URL::toExternalForm)
.ifPresent(path -> this.initialState = Optional.of(path));
return this;
}

Expand All @@ -466,8 +471,7 @@ public Builder customGenesisState(final String genesisState) {

public Builder ignoreWeakSubjectivityPeriodEnabled(
boolean ignoreWeakSubjectivityPeriodEnabled) {
this.stateBoostrapConfig.setAllowSyncOutsideWeakSubjectivityPeriod(
ignoreWeakSubjectivityPeriodEnabled);
this.allowSyncOutsideWeakSubjectivityPeriod = ignoreWeakSubjectivityPeriodEnabled;
return this;
}

Expand All @@ -492,9 +496,9 @@ public Builder asyncBeaconChainMaxQueue(final int asyncBeaconChainMaxQueue) {
}

public Builder genesisStateFromClasspath(final String filename) {
this.genesisState =
Optional.ofNullable(Eth2NetworkConfiguration.class.getResource(filename))
.map(URL::toExternalForm);
Optional.ofNullable(Eth2NetworkConfiguration.class.getResource(filename))
.map(URL::toExternalForm)
.ifPresent(path -> this.genesisState = Optional.of(path));
return this;
}

Expand Down Expand Up @@ -636,8 +640,11 @@ public Builder applyNetworkDefaults(final Eth2Network network) {

private Builder reset() {
constants = null;
initialState = Optional.empty();
genesisState = Optional.empty();
initialState = Optional.empty();
checkpointSyncUrl = Optional.empty();
isUsingCustomInitialState = false;
allowSyncOutsideWeakSubjectivityPeriod = DEFAULT_ALLOW_SYNC_OUTSIDE_WEAK_SUBJECTIVITY_PERIOD;
startupTargetPeerCount = DEFAULT_STARTUP_TARGET_PEER_COUNT;
startupTimeoutSeconds = DEFAULT_STARTUP_TIMEOUT_SECONDS;
discoveryBootnodes = new ArrayList<>();
Expand Down Expand Up @@ -669,7 +676,7 @@ public Builder applyLessSwiftNetworkDefaults() {
public Builder applyMainnetNetworkDefaults() {
return reset()
.constants(MAINNET.configName())
.initialStateFromClasspath("mainnet-genesis.ssz")
.defaultInitialStateFromClasspath("mainnet-genesis.ssz")
.genesisStateFromClasspath("mainnet-genesis.ssz")
.trustedSetupFromClasspath(MAINNET_TRUSTED_SETUP_FILENAME)
.startupTimeoutSeconds(120)
Expand Down Expand Up @@ -704,7 +711,7 @@ public Builder applyPraterNetworkDefaults() {
.trustedSetupFromClasspath(MAINNET_TRUSTED_SETUP_FILENAME)
.startupTimeoutSeconds(120)
.eth1DepositContractDeployBlock(4367322)
.defaultInitialState(
.defaultInitialStateFromUrl(
"https://github.com/eth2-clients/eth2-testnets/raw/192c1b48ea5ff4adb4e6ef7d2a9e5f82fb5ffd72/shared/prater/genesis.ssz")
.customGenesisState(
"https://github.com/eth2-clients/eth2-testnets/raw/192c1b48ea5ff4adb4e6ef7d2a9e5f82fb5ffd72/shared/prater/genesis.ssz")
Expand Down Expand Up @@ -732,7 +739,7 @@ private Builder applySepoliaNetworkDefaults() {
.startupTimeoutSeconds(120)
.trustedSetupFromClasspath(MAINNET_TRUSTED_SETUP_FILENAME)
.eth1DepositContractDeployBlock(1273020)
.defaultInitialState(
.defaultInitialStateFromUrl(
"https://github.com/eth-clients/merge-testnets/raw/9c873ab67b902aa676370a549129e5e91013afa3/sepolia/genesis.ssz")
.customGenesisState(
"https://github.com/eth-clients/merge-testnets/raw/9c873ab67b902aa676370a549129e5e91013afa3/sepolia/genesis.ssz")
Expand All @@ -751,7 +758,7 @@ private Builder applyLuksoNetworkDefaults() {
.constants(LUKSO.configName())
.startupTimeoutSeconds(120)
.eth1DepositContractDeployBlock(0)
.initialStateFromClasspath("lukso-genesis.ssz")
.defaultInitialStateFromClasspath("lukso-genesis.ssz")
.genesisStateFromClasspath("lukso-genesis.ssz")
.discoveryBootnodes(
// Consensus layer bootnodes
Expand All @@ -762,7 +769,7 @@ private Builder applyLuksoNetworkDefaults() {
public Builder applyGnosisNetworkDefaults() {
return reset()
.constants(GNOSIS.configName())
.initialStateFromClasspath("gnosis-genesis.ssz")
.defaultInitialStateFromClasspath("gnosis-genesis.ssz")
.genesisStateFromClasspath("gnosis-genesis.ssz")
.startupTimeoutSeconds(120)
.eth1DepositContractDeployBlock(19469077)
Expand All @@ -787,7 +794,7 @@ public Builder applyGnosisNetworkDefaults() {
public Builder applyChiadoNetworkDefaults() {
return reset()
.constants(CHIADO.configName())
.initialStateFromClasspath("chiado-genesis.ssz")
.defaultInitialStateFromClasspath("chiado-genesis.ssz")
.genesisStateFromClasspath("chiado-genesis.ssz")
.startupTimeoutSeconds(120)
.eth1DepositContractDeployBlock(155435)
Expand Down Expand Up @@ -816,7 +823,7 @@ private Builder applyHoleskyNetworkDefaults() {
.startupTimeoutSeconds(120)
.trustedSetupFromClasspath(MAINNET_TRUSTED_SETUP_FILENAME)
.eth1DepositContractDeployBlock(0)
.defaultInitialState(
.defaultInitialStateFromUrl(
"https://checkpoint-sync.holesky.ethpandaops.io/eth/v2/debug/beacon/states/finalized")
.customGenesisState(
"https://github.com/eth-clients/holesky/raw/59cb4fcbc8b39e431c1d737937ae8188f4a19a98/custom_config_data/genesis.ssz")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,46 @@

package tech.pegasys.teku.networks;

public class StateBoostrapConfig {
private boolean isUsingCustomInitialState;
private boolean isUsingCheckpointSync;
private boolean allowSyncOutsideWeakSubjectivityPeriod;
import java.util.Optional;

StateBoostrapConfig() {}
public class StateBoostrapConfig {
private final Optional<String> genesisState;
private final Optional<String> initialState;
private final Optional<String> checkpointSyncUrl;
private final boolean isUsingCustomInitialState;
private final boolean allowSyncOutsideWeakSubjectivityPeriod;

public StateBoostrapConfig(
final Optional<String> genesisState,
final Optional<String> initialState,
final Optional<String> checkpointSyncUrl,
final boolean isUsingCustomInitialState,
final boolean allowSyncOutsideWeakSubjectivityPeriod) {
this.checkpointSyncUrl = checkpointSyncUrl;
this.genesisState = genesisState;
this.initialState = initialState;
this.isUsingCustomInitialState = isUsingCustomInitialState;
this.allowSyncOutsideWeakSubjectivityPeriod = allowSyncOutsideWeakSubjectivityPeriod;
}

public void setUsingCustomInitialState(final boolean usingCustomInitialState) {
isUsingCustomInitialState = usingCustomInitialState;
public Optional<String> getGenesisState() {
return genesisState;
}

public void setUsingCheckpointSync(final boolean usingCheckpointSync) {
isUsingCheckpointSync = usingCheckpointSync;
public Optional<String> getInitialState() {
return initialState;
}

public void setAllowSyncOutsideWeakSubjectivityPeriod(
final boolean allowSyncOutsideWeakSubjectivityPeriod) {
this.allowSyncOutsideWeakSubjectivityPeriod = allowSyncOutsideWeakSubjectivityPeriod;
public Optional<String> getCheckpointSyncUrl() {
return checkpointSyncUrl;
}

public boolean isUsingCustomInitialState() {
return isUsingCustomInitialState;
}

public boolean isUsingCheckpointSync() {
return isUsingCheckpointSync;
return checkpointSyncUrl.isPresent();
}

public boolean isAllowSyncOutsideWeakSubjectivityPeriod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package tech.pegasys.teku.networks;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static tech.pegasys.teku.networks.Eth2NetworkConfiguration.FINALIZED_STATE_URL_PATH;
import static tech.pegasys.teku.networks.Eth2NetworkConfiguration.GENESIS_STATE_URL_PATH;

import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -41,6 +43,7 @@ public void build_shouldBuildKnownNetworks(

assertThat(networkConfig.getConstants()).isEqualTo(network.configName());
assertThat(networkConfigBuilder.build()).usingRecursiveComparison().isEqualTo(networkConfig);
assertThat(networkConfig.getNetworkBoostrapConfig().isUsingCustomInitialState()).isFalse();
}

@Test
Expand Down Expand Up @@ -160,4 +163,39 @@ private List<NodeRecord> parseBootnodes(List<String> bootnodes) {
private interface NetworkDefinition {
Eth2NetworkConfiguration.Builder configure(Eth2NetworkConfiguration.Builder builder);
}

@Test
public void shouldNotHaveCustomInitialStateFlagWhenUsingPreConfiguredNetworks() {
final Eth2NetworkConfiguration eth2NetworkConfig =
new Eth2NetworkConfiguration.Builder().applyNetworkDefaults(Eth2Network.MAINNET).build();
assertThat(eth2NetworkConfig.getNetworkBoostrapConfig().isUsingCustomInitialState()).isFalse();
}

@Test
public void shouldHaveCustomInitialStateFlagSetWhenSpecifyingInitialState() {
final Eth2NetworkConfiguration eth2NetworkConfig =
new Eth2NetworkConfiguration.Builder()
.applyNetworkDefaults(Eth2Network.MAINNET)
.customInitialState("/foo/bar")
.build();
assertThat(eth2NetworkConfig.getNetworkBoostrapConfig().getInitialState()).hasValue("/foo/bar");
assertThat(eth2NetworkConfig.getNetworkBoostrapConfig().isUsingCustomInitialState()).isTrue();
}

@Test
public void shouldSetInitialStateAndGenesisStateWhenUsingCheckpointSyncUrl() {
final String checkpointSyncUrl = "http://foo.com";
final Eth2NetworkConfiguration eth2NetworkConfig =
new Eth2NetworkConfiguration.Builder()
.applyNetworkDefaults(Eth2Network.MAINNET)
.checkpointSyncUrl(checkpointSyncUrl)
.build();

final StateBoostrapConfig networkBoostrapConfig = eth2NetworkConfig.getNetworkBoostrapConfig();
assertThat(networkBoostrapConfig.getInitialState())
.contains(checkpointSyncUrl + "/" + FINALIZED_STATE_URL_PATH);
assertThat(networkBoostrapConfig.getGenesisState())
.contains(checkpointSyncUrl + "/" + GENESIS_STATE_URL_PATH);
assertThat(networkBoostrapConfig.isUsingCustomInitialState()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ public void initBlockManager() {
protected SyncServiceFactory createSyncServiceFactory() {
return new DefaultSyncServiceFactory(
beaconConfig.syncConfig(),
beaconConfig.eth2NetworkConfig().getGenesisState(),
beaconConfig.eth2NetworkConfig().getNetworkBoostrapConfig().getGenesisState(),
metricsSystem,
asyncRunnerFactory,
beaconAsyncRunner,
Expand Down Expand Up @@ -1318,7 +1318,10 @@ protected void setupInitialState(final RecentChainData client) {

final Optional<AnchorPoint> initialAnchor =
tryLoadingAnchorPointFromInitialState(networkConfiguration)
.or(() -> attemptToLoadAnchorPoint(networkConfiguration.getGenesisState()));
.or(
() ->
attemptToLoadAnchorPoint(
networkConfiguration.getNetworkBoostrapConfig().getGenesisState()));

/*
If flag to allow sync outside of weak subjectivity period has been set, we pass an instance of
Expand Down Expand Up @@ -1362,7 +1365,9 @@ private Optional<AnchorPoint> tryLoadingAnchorPointFromInitialState(
Optional<AnchorPoint> initialAnchor = Optional.empty();

try {
initialAnchor = attemptToLoadAnchorPoint(networkConfiguration.getInitialState());
initialAnchor =
attemptToLoadAnchorPoint(
networkConfiguration.getNetworkBoostrapConfig().getInitialState());
} catch (final InvalidConfigurationException e) {
final StateBoostrapConfig stateBoostrapConfig =
networkConfiguration.getNetworkBoostrapConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void run() {
final Spec spec = networkConfiguration.getSpec();

final Optional<AnchorPoint> initialAnchor =
wsInitializer.loadInitialAnchorPoint(spec, networkConfiguration.getInitialState());
wsInitializer.loadInitialAnchorPoint(
spec, networkConfiguration.getNetworkBoostrapConfig().getInitialState());

final UInt64 computedSlot = getComputedSlot(initialAnchor, spec);
final UInt64 computedEpoch =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public TekuConfiguration build() {

// Check for invalid config settings
if (syncConfig.isReconstructHistoricStatesEnabled()
&& eth2NetworkConfiguration.getGenesisState().isEmpty()) {
&& eth2NetworkConfiguration.getNetworkBoostrapConfig().getGenesisState().isEmpty()) {
throw new InvalidConfigurationException(
"Genesis state required when reconstructing historic states");
}
Expand Down
Loading

0 comments on commit b71a58e

Please sign in to comment.