Skip to content

Commit

Permalink
Merge branch 'master' into 8527-1
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfyone authored Sep 24, 2024
2 parents d595017 + ae367d8 commit 8c2baae
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CapellaUpgradeAcceptanceTest extends AcceptanceTestBase {
@Test
void shouldUpgradeToCapella() throws Exception {
final UInt64 currentTime = timeProvider.getTimeInSeconds();
final int genesisTime = currentTime.plus(30).intValue(); // magic node startup time
final int genesisTime = currentTime.plus(60).intValue(); // magic node startup time
final int shanghaiTime = genesisTime + 4 * 2; // 4 slots, 2 seconds each
final Map<String, String> genesisOverrides =
Map.of("shanghaiTime", String.valueOf(shanghaiTime));
Expand All @@ -51,6 +51,15 @@ void shouldUpgradeToCapella() throws Exception {
genesisOverrides);
primaryEL.start();

TekuBeaconNode primaryNode =
createTekuBeaconNode(
beaconNodeConfigWithForks(genesisTime, primaryEL)
.withStartupTargetPeerCount(0)
.build());

primaryNode.start();
primaryNode.waitForMilestone(SpecMilestone.CAPELLA);

BesuNode secondaryEL =
createBesuNode(
BesuDockerVersion.STABLE,
Expand All @@ -64,15 +73,6 @@ void shouldUpgradeToCapella() throws Exception {
secondaryEL.start();
secondaryEL.addPeer(primaryEL);

TekuBeaconNode primaryNode =
createTekuBeaconNode(
beaconNodeConfigWithForks(genesisTime, primaryEL)
.withStartupTargetPeerCount(0)
.build());

primaryNode.start();
primaryNode.waitForMilestone(SpecMilestone.CAPELLA);

final int primaryNodeGenesisTime = primaryNode.getGenesisTime().intValue();

TekuBeaconNode lateJoiningNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DenebUpgradeAcceptanceTest extends AcceptanceTestBase {
@Test
void shouldUpgradeToDeneb() throws Exception {
final UInt64 currentTime = timeProvider.getTimeInSeconds();
final int genesisTime = currentTime.plus(30).intValue(); // magic node startup time
final int genesisTime = currentTime.plus(60).intValue(); // magic node startup time
final int epochDuration = 4 * 2; // 4 slots, 2 seconds each for swift
final int shanghaiTime = genesisTime + epochDuration;
final Map<String, String> genesisOverrides =
Expand All @@ -56,6 +56,15 @@ void shouldUpgradeToDeneb() throws Exception {
genesisOverrides);
primaryEL.start();

TekuBeaconNode primaryNode =
createTekuBeaconNode(
beaconNodeWithTrustedSetup(genesisTime, primaryEL)
.withStartupTargetPeerCount(0)
.build());

primaryNode.start();
primaryNode.waitForMilestone(SpecMilestone.DENEB);

BesuNode secondaryEL =
createBesuNode(
BesuDockerVersion.STABLE,
Expand All @@ -69,15 +78,6 @@ void shouldUpgradeToDeneb() throws Exception {
secondaryEL.start();
secondaryEL.addPeer(primaryEL);

TekuBeaconNode primaryNode =
createTekuBeaconNode(
beaconNodeWithTrustedSetup(genesisTime, primaryEL)
.withStartupTargetPeerCount(0)
.build());

primaryNode.start();
primaryNode.waitForMilestone(SpecMilestone.DENEB);

final int primaryNodeGenesisTime = primaryNode.getGenesisTime().intValue();

TekuBeaconNode lateJoiningNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@ public class Domain {

// Capella
public static final Bytes4 DOMAIN_BLS_TO_EXECUTION_CHANGE = Bytes4.fromHexString("0x0A000000");

// Electra
public static final Bytes4 DOMAIN_CONSOLIDATION = Bytes4.fromHexString("0x0B000000");
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public StateStorageMode getStateStorageMode() {
private Database createV4Database() {
try {
DatabaseNetwork.init(
getNetworkFile(), spec.getGenesisSpecConfig().getGenesisForkVersion(), eth1Address);
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
return RocksDbDatabaseFactory.createV4(
metricsSystem,
KvStoreConfiguration.v4Settings(dbDirectory.toPath()),
Expand All @@ -190,7 +193,10 @@ private Database createV5Database() {
final V5DatabaseMetadata metaData =
V5DatabaseMetadata.init(getMetadataFile(), V5DatabaseMetadata.v5Defaults());
DatabaseNetwork.init(
getNetworkFile(), spec.getGenesisSpecConfig().getGenesisForkVersion(), eth1Address);
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
return RocksDbDatabaseFactory.createV4(
metricsSystem,
metaData.getHotDbConfiguration().withDatabaseDir(dbDirectory.toPath()),
Expand Down Expand Up @@ -233,7 +239,10 @@ private Database createLevelDbV1Database() {
final V5DatabaseMetadata metaData =
V5DatabaseMetadata.init(getMetadataFile(), V5DatabaseMetadata.v5Defaults());
DatabaseNetwork.init(
getNetworkFile(), spec.getGenesisSpecConfig().getGenesisForkVersion(), eth1Address);
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());
return LevelDbDatabaseFactory.createLevelDb(
metricsSystem,
metaData.getHotDbConfiguration().withDatabaseDir(dbDirectory.toPath()),
Expand Down Expand Up @@ -284,7 +293,10 @@ private KvStoreConfiguration initV6Configuration() throws IOException {
V6DatabaseMetadata.init(getMetadataFile(), V6DatabaseMetadata.singleDBDefault());

DatabaseNetwork.init(
getNetworkFile(), spec.getGenesisSpecConfig().getGenesisForkVersion(), eth1Address);
getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
spec.getGenesisSpecConfig().getDepositChainId());

return metaData.getSingleDbConfiguration().getConfiguration();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
Expand All @@ -29,26 +30,41 @@
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.storage.server.DatabaseStorageException;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DatabaseNetwork {
@JsonProperty("fork_version")
@JsonProperty(value = "fork_version", required = true)
@VisibleForTesting
final String forkVersion;

@JsonProperty("deposit_contract")
@JsonProperty(value = "deposit_contract", required = true)
@VisibleForTesting
final String depositContract;

@JsonProperty("deposit_chain_id")
@VisibleForTesting
final Long depositChainId;

@JsonCreator
DatabaseNetwork(
@JsonProperty("fork_version") final String forkVersion,
@JsonProperty("deposit_contract") final String depositContract) {
@JsonProperty(value = "fork_version") final String forkVersion,
@JsonProperty(value = "deposit_contract") final String depositContract,
@JsonProperty("deposit_chain_id") final Long depositChainId) {
this.forkVersion = forkVersion;
this.depositContract = depositContract;
this.depositChainId = depositChainId;
}

@VisibleForTesting
DatabaseNetwork(final String forkVersion, final String depositContract) {
this(forkVersion, depositContract, null);
}

public static DatabaseNetwork init(
final File source, final Bytes4 forkVersion, final Eth1Address depositContract)
final File source,
final Bytes4 forkVersion,
final Eth1Address depositContract,
final Long depositChainId)
throws IOException {
final String forkVersionString = forkVersion.toHexString().toLowerCase(Locale.ROOT);
final String depositContractString = depositContract.toHexString().toLowerCase(Locale.ROOT);
Expand All @@ -71,7 +87,7 @@ public static DatabaseNetwork init(
return databaseNetwork;
} else {
DatabaseNetwork databaseNetwork =
new DatabaseNetwork(forkVersionString, depositContractString);
new DatabaseNetwork(forkVersionString, depositContractString, depositChainId);
objectMapper.writerFor(DatabaseNetwork.class).writeValue(source, databaseNetwork);
return databaseNetwork;
}
Expand All @@ -95,7 +111,8 @@ public boolean equals(final Object o) {
}
final DatabaseNetwork that = (DatabaseNetwork) o;
return Objects.equals(forkVersion, that.forkVersion)
&& Objects.equals(depositContract, that.depositContract);
&& Objects.equals(depositContract, that.depositContract)
&& Objects.equals(depositChainId, that.depositChainId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@

package tech.pegasys.teku.storage.server.network;

import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.WRITE_DOC_START_MARKER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
Expand All @@ -30,56 +38,136 @@

public class DatabaseNetworkTest {
DataStructureUtil dataStructureUtil = new DataStructureUtil(TestSpecFactory.createDefault());
private ObjectMapper objectMapper;
private static final String NETWORK_FILENAME = "network.yml";

@BeforeEach
void setUp() {
objectMapper = new ObjectMapper(new YAMLFactory().disable(WRITE_DOC_START_MARKER));
}

@Test
public void shouldCreateNetworkFile(@TempDir final File tempDir) throws IOException {
final File networkFile = new File(tempDir, "network.yml");
final File networkFile = new File(tempDir, NETWORK_FILENAME);
assertThat(networkFile).doesNotExist();
final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
assertThat(DatabaseNetwork.init(networkFile, fork, eth1Address))
final Long depositChainId = dataStructureUtil.randomLong();
assertThat(DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
.isEqualTo(
new DatabaseNetwork(
fork.toHexString().toLowerCase(Locale.ROOT),
eth1Address.toHexString().toLowerCase(Locale.ROOT)));
eth1Address.toHexString().toLowerCase(Locale.ROOT),
depositChainId));
assertThat(networkFile).exists();
}

@Test
public void shouldThrowIfForkDiffers(@TempDir final File tempDir) throws IOException {
final File networkFile = new File(tempDir, "network.yml");
final File networkFile = new File(tempDir, NETWORK_FILENAME);
assertThat(networkFile).doesNotExist();
final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
final Long depositChainId = dataStructureUtil.randomLong();
DatabaseNetwork.init(
networkFile, dataStructureUtil.randomFork().getCurrentVersion(), eth1Address);
networkFile,
dataStructureUtil.randomFork().getCurrentVersion(),
eth1Address,
depositChainId);

assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address))
assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
.isInstanceOf(DatabaseStorageException.class)
.hasMessageStartingWith("Supplied fork version");
}

@Test
public void shouldThrowIfDepositContractDiffers(@TempDir final File tempDir) throws IOException {
final File networkFile = new File(tempDir, "network.yml");
final File networkFile = new File(tempDir, NETWORK_FILENAME);
assertThat(networkFile).doesNotExist();
final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
DatabaseNetwork.init(networkFile, fork, dataStructureUtil.randomEth1Address());
final Long depositChainId = dataStructureUtil.randomLong();

assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address))
DatabaseNetwork.init(networkFile, fork, dataStructureUtil.randomEth1Address(), depositChainId);

assertThatThrownBy(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId))
.isInstanceOf(DatabaseStorageException.class)
.hasMessageStartingWith("Supplied deposit contract");
}

@Test
public void shouldNotThrowIfForkAndContractMatch(@TempDir final File tempDir) throws IOException {
final File networkFile = new File(tempDir, "network.yml");
final File networkFile = new File(tempDir, NETWORK_FILENAME);
assertThat(networkFile).doesNotExist();
final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
DatabaseNetwork.init(networkFile, fork, eth1Address);
final Long depositChainId = dataStructureUtil.randomLong();

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

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

@Test
void shouldWriteAndReadDatabaseNetworkWithDepositChainId(@TempDir final File tempDir)
throws IOException {
final File networkFile = new File(tempDir, NETWORK_FILENAME);

final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();
final Long depositChainId = dataStructureUtil.randomLong();
final DatabaseNetwork databaseNetwork =
new DatabaseNetwork(fork.toHexString(), eth1Address.toHexString(), depositChainId);

objectMapper.writerFor(DatabaseNetwork.class).writeValue(networkFile, databaseNetwork);
final DatabaseNetwork readDatabaseNetwork =
objectMapper.readerFor(DatabaseNetwork.class).readValue(networkFile);

assertEquals(fork.toHexString(), readDatabaseNetwork.forkVersion);
assertEquals(eth1Address.toHexString(), readDatabaseNetwork.depositContract);
assertEquals(depositChainId, readDatabaseNetwork.depositChainId);
}

@Test
void shouldWriteAndReadDatabaseNetworkWithoutDepositChainId(@TempDir final File tempDir)
throws IOException {
final File networkFile = new File(tempDir, NETWORK_FILENAME);

final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();

final DatabaseNetwork databaseNetwork =
new DatabaseNetwork(fork.toHexString(), eth1Address.toHexString());

objectMapper.writerFor(DatabaseNetwork.class).writeValue(networkFile, databaseNetwork);
String networkContent = Files.readString(networkFile.toPath());

final DatabaseNetwork readDatabaseNetwork =
objectMapper.readerFor(DatabaseNetwork.class).readValue(networkFile);

assertFalse(networkContent.contains("deposit_chain_id"));
assertEquals(fork.toHexString(), readDatabaseNetwork.forkVersion);
assertEquals(eth1Address.toHexString(), readDatabaseNetwork.depositContract);
assertNull(readDatabaseNetwork.depositChainId);
}

@Test
void shouldNotIncludeDepositChainIdWhenNull(@TempDir final File tempDir) throws IOException {
final File networkFile = new File(tempDir, NETWORK_FILENAME);

final Bytes4 fork = dataStructureUtil.randomFork().getCurrentVersion();
final Eth1Address eth1Address = dataStructureUtil.randomEth1Address();

final DatabaseNetwork databaseNetwork =
new DatabaseNetwork(fork.toHexString(), eth1Address.toHexString(), null);

objectMapper.writerFor(DatabaseNetwork.class).writeValue(networkFile, databaseNetwork);
String networkContent = Files.readString(networkFile.toPath());

final DatabaseNetwork readDatabaseNetwork =
objectMapper.readerFor(DatabaseNetwork.class).readValue(networkFile);

assertDoesNotThrow(() -> DatabaseNetwork.init(networkFile, fork, eth1Address));
assertFalse(networkContent.contains("deposit_chain_id"));
assertNull(readDatabaseNetwork.depositChainId);
}
}

0 comments on commit 8c2baae

Please sign in to comment.