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

Ephemery startup network determination #8781

Merged
merged 5 commits into from
Nov 5, 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 @@ -33,6 +33,7 @@
import tech.pegasys.teku.service.serviceutils.Service;
import tech.pegasys.teku.service.serviceutils.ServiceConfig;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.storage.api.CombinedStorageChannel;
import tech.pegasys.teku.storage.api.Eth1DepositStorageChannel;
import tech.pegasys.teku.storage.api.VoteUpdateChannel;
Expand Down Expand Up @@ -66,16 +67,19 @@ public class StorageService extends Service implements StorageServiceFacade {
private final boolean depositSnapshotStorageEnabled;
private final boolean blobSidecarsStorageCountersEnabled;
private static final Logger LOG = LogManager.getLogger();
private final Optional<Eth2Network> maybeNetwork;

public StorageService(
final ServiceConfig serviceConfig,
final StorageConfiguration storageConfiguration,
final boolean depositSnapshotStorageEnabled,
final boolean blobSidecarsStorageCountersEnabled) {
final boolean blobSidecarsStorageCountersEnabled,
final Optional<Eth2Network> eth2Network) {
this.serviceConfig = serviceConfig;
this.config = storageConfiguration;
this.depositSnapshotStorageEnabled = depositSnapshotStorageEnabled;
this.blobSidecarsStorageCountersEnabled = blobSidecarsStorageCountersEnabled;
this.maybeNetwork = eth2Network;
}

@Override
Expand All @@ -92,7 +96,8 @@ protected SafeFuture<?> doStart() {
new VersionedDatabaseFactory(
serviceConfig.getMetricsSystem(),
serviceConfig.getDataDirLayout().getBeaconDataDirectory(),
config);
config,
maybeNetwork);
try {
database = dbFactory.createDatabase();
} catch (EphemeryException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ void setUp(@TempDir final Path tempDir) {
when(serviceConfig.createAsyncRunner(any(), anyInt(), anyInt(), anyInt()))
.thenReturn(stubAsyncRunner);

storageService = new StorageService(serviceConfig, storageConfiguration, false, false);
storageService =
new StorageService(serviceConfig, storageConfiguration, false, false, Optional.empty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
import tech.pegasys.teku.infrastructure.io.SyncDataAccessor;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.storage.server.kvstore.KvStoreConfiguration;
import tech.pegasys.teku.storage.server.kvstore.schema.V6SchemaCombinedSnapshot;
import tech.pegasys.teku.storage.server.leveldb.LevelDbDatabaseFactory;
Expand Down Expand Up @@ -60,12 +62,17 @@ public class VersionedDatabaseFactory implements DatabaseFactory {
private final Spec spec;
private final boolean storeNonCanonicalBlocks;
private final SyncDataAccessor dbSettingFileSyncDataAccessor;
private final Optional<Eth2Network> maybeNetwork;

public VersionedDatabaseFactory(
final MetricsSystem metricsSystem, final Path dataPath, final StorageConfiguration config) {
final MetricsSystem metricsSystem,
final Path dataPath,
final StorageConfiguration config,
final Optional<Eth2Network> maybeNetwork) {

this.metricsSystem = metricsSystem;
this.dataDirectory = dataPath.toFile();
this.maybeNetwork = maybeNetwork;

this.createDatabaseVersion = config.getDataStorageCreateDbVersion();
this.maxKnownNodeCacheSize = config.getMaxKnownNodeCacheSize();
Expand Down Expand Up @@ -168,7 +175,8 @@ private Database createV4Database() {
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
spec.getGenesisSpecConfig().getDepositChainId(),
maybeNetwork);
return RocksDbDatabaseFactory.createV4(
metricsSystem,
KvStoreConfiguration.v4Settings(dbDirectory.toPath()),
Expand All @@ -195,7 +203,8 @@ private Database createV5Database() {
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
spec.getGenesisSpecConfig().getDepositChainId(),
maybeNetwork);
return RocksDbDatabaseFactory.createV4(
metricsSystem,
metaData.getHotDbConfiguration().withDatabaseDir(dbDirectory.toPath()),
Expand Down Expand Up @@ -241,7 +250,8 @@ private Database createLevelDbV1Database() {
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
spec.getGenesisSpecConfig().getDepositChainId(),
maybeNetwork);
return LevelDbDatabaseFactory.createLevelDb(
metricsSystem,
metaData.getHotDbConfiguration().withDatabaseDir(dbDirectory.toPath()),
Expand Down Expand Up @@ -295,7 +305,8 @@ private KvStoreConfiguration initV6Configuration() throws IOException {
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
spec.getGenesisSpecConfig().getDepositChainId(),
maybeNetwork);

return metaData.getSingleDbConfiguration().getConfiguration();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import java.io.IOException;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.storage.server.DatabaseStorageException;

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -45,9 +47,6 @@ public class DatabaseNetwork {
@VisibleForTesting
final Long depositChainId;

private static final String EPHEMERY_DEPOSIT_CONTRACT_ADDRESS =
"0x4242424242424242424242424242424242424242";

@JsonCreator
DatabaseNetwork(
@JsonProperty(value = "fork_version") final String forkVersion,
Expand All @@ -67,7 +66,8 @@ public static DatabaseNetwork init(
final File source,
final Bytes4 forkVersion,
final Eth1Address depositContract,
final Long depositChainId)
final Long depositChainId,
final Optional<Eth2Network> maybeNetwork)
throws IOException {
final String forkVersionString = forkVersion.toHexString().toLowerCase(Locale.ROOT);
final String depositContractString = depositContract.toHexString().toLowerCase(Locale.ROOT);
Expand All @@ -88,7 +88,7 @@ public static DatabaseNetwork init(
"deposit contract", depositContractString, databaseNetwork.depositContract));
}
if (databaseNetwork.depositChainId != null
&& !depositContractString.equals(EPHEMERY_DEPOSIT_CONTRACT_ADDRESS)
&& maybeNetwork.map(n -> !n.equals(Eth2Network.EPHEMERY)).orElse(true)
&& !databaseNetwork.depositChainId.equals(depositChainId)) {
throw DatabaseStorageException.unrecoverable(
formatMessage(
Expand All @@ -97,7 +97,7 @@ public static DatabaseNetwork init(
String.valueOf(databaseNetwork.depositChainId)));
}
if (databaseNetwork.depositChainId != null
&& depositContractString.equals(EPHEMERY_DEPOSIT_CONTRACT_ADDRESS)
&& maybeNetwork.map(n -> n.equals(Eth2Network.EPHEMERY)).orElse(false)
&& !databaseNetwork.depositChainId.equals(depositChainId)) {
throw new EphemeryException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -50,7 +51,8 @@ public void createDatabase_fromEmptyDataDir() throws Exception {
StorageConfiguration.builder()
.specProvider(spec)
.eth1DepositContract(eth1Address)
.build());
.build(),
Optional.empty());
try (final Database db = dbFactory.createDatabase()) {
assertThat(db).isNotNull();

Expand All @@ -76,7 +78,8 @@ public void createDatabase_fromExistingDataDir() throws Exception {
StorageConfiguration.builder()
.specProvider(spec)
.eth1DepositContract(eth1Address)
.build());
.build(),
Optional.empty());

try (final Database db = dbFactory.createDatabase()) {
assertThat(db).isNotNull();
Expand All @@ -97,7 +100,8 @@ public void createDatabase_invalidVersionFile() throws Exception {
StorageConfiguration.builder()
.specProvider(spec)
.eth1DepositContract(eth1Address)
.build());
.build(),
Optional.empty());

assertThatThrownBy(dbFactory::createDatabase)
.isInstanceOf(DatabaseStorageException.class)
Expand All @@ -115,7 +119,8 @@ public void createDatabase_dbExistsButNoVersionIsSaved() {
StorageConfiguration.builder()
.specProvider(spec)
.eth1DepositContract(eth1Address)
.build());
.build(),
Optional.empty());

assertThatThrownBy(dbFactory::createDatabase)
.isInstanceOf(DatabaseStorageException.class)
Expand All @@ -135,7 +140,8 @@ public void createDatabase_shouldAllowAllSupportedDatabases(final DatabaseVersio
.eth1DepositContract(eth1Address)
.dataStorageMode(DATA_STORAGE_MODE)
.dataStorageCreateDbVersion(version)
.build());
.build(),
Optional.empty());
assertThat(dbFactory.getDatabaseVersion()).isEqualTo(version);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -53,7 +54,8 @@ public void shouldCreateNetworkFile(@TempDir final File tempDir) throws IOExcept
final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
final Long depositChainId = dataStructureUtil.randomLong();
assertThat(DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
assertThat(
DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId, Optional.empty()))
.isEqualTo(
new DatabaseNetwork(
fork.toHexString().toLowerCase(Locale.ROOT),
Expand All @@ -73,9 +75,13 @@ public void shouldThrowIfForkDiffers(@TempDir final File tempDir) throws IOExcep
networkFile,
dataStructureUtil.randomFork().getCurrentVersion(),
eth1Address,
depositChainId);
depositChainId,
Optional.empty());

assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
assertThatThrownBy(
() ->
DatabaseNetwork.init(
networkFile, fork, eth1Address, depositChainId, Optional.empty()))
.isInstanceOf(DatabaseStorageException.class)
.hasMessageStartingWith("Supplied fork version");
}
Expand All @@ -88,9 +94,13 @@ public void shouldThrowIfDepositContractDiffers(@TempDir final File tempDir) thr
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
final Long depositChainId = dataStructureUtil.randomLong();

DatabaseNetwork.init(networkFile, fork, dataStructureUtil.randomEth1Address(), depositChainId);
DatabaseNetwork.init(
networkFile, fork, dataStructureUtil.randomEth1Address(), depositChainId, Optional.empty());

assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
assertThatThrownBy(
() ->
DatabaseNetwork.init(
networkFile, fork, eth1Address, depositChainId, Optional.empty()))
.isInstanceOf(DatabaseStorageException.class)
.hasMessageStartingWith("Supplied deposit contract");
}
Expand All @@ -103,9 +113,11 @@ public void shouldNotThrowIfForkAndContractMatch(@TempDir final File tempDir) th
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
final Long depositChainId = dataStructureUtil.randomLong();

DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId);
DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId, Optional.empty());

assertDoesNotThrow(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId));
assertDoesNotThrow(
() ->
DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId, Optional.empty()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static tech.pegasys.teku.infrastructure.logging.SubCommandLogger.SUB_COMMAND_LOG;

import java.util.Optional;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import picocli.CommandLine;
import tech.pegasys.teku.cli.converter.PicoCliVersionProvider;
Expand Down Expand Up @@ -115,7 +116,8 @@ private Database createDatabase(
StorageConfiguration.builder()
.eth1DepositContract(networkConfiguration.getEth1DepositContractAddress())
.specProvider(spec)
.build());
.build(),
Optional.empty());
return databaseFactory.createDatabase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,8 @@ private Database createDatabase(
StorageConfiguration.builder()
.eth1DepositContract(networkConfiguration.getEth1DepositContractAddress())
.specProvider(spec)
.build());
.build(),
Optional.empty());
return databaseFactory.createDatabase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import org.apache.commons.io.FileUtils;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -189,7 +190,8 @@ KvStoreDatabase createDatabase(final Path databasePath, final DatabaseVersion da
.storeNonCanonicalBlocks(true)
.eth1DepositContract(config.getEth1DepositContractAddress())
.dataStorageCreateDbVersion(databaseVersion)
.build());
.build(),
Optional.empty());
final Database database = databaseFactory.createDatabase();
if (!(database instanceof KvStoreDatabase)) {
throw new DatabaseMigraterError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public BeaconNodeServiceController(
.powchain()
.getDepositTreeSnapshotConfiguration()
.isBundledDepositSnapshotEnabled(),
tekuConfig.metricsConfig().isBlobSidecarsStorageCountersEnabled()));
tekuConfig.metricsConfig().isBlobSidecarsStorageCountersEnabled(),
tekuConfig.beaconChain().eth2NetworkConfig().getEth2Network()));
Optional<ExecutionWeb3jClientProvider> maybeExecutionWeb3jClientProvider = Optional.empty();
if (tekuConfig.executionLayer().isEnabled()) {
// Need to make sure the execution engine is listening before starting the beacon chain
Expand Down