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

Update network file #8631

Merged
merged 14 commits into from
Sep 24, 2024
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,22 +30,34 @@
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) {
gconnect marked this conversation as resolved.
Show resolved Hide resolved
this(forkVersion, depositContract, null);
}

public static DatabaseNetwork init(
Expand Down Expand Up @@ -95,7 +108,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,10 +38,17 @@

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();
Expand All @@ -47,7 +62,7 @@ public void shouldCreateNetworkFile(@TempDir final File tempDir) throws IOExcept

@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();
Expand All @@ -61,7 +76,7 @@ public void shouldThrowIfForkDiffers(@TempDir final File tempDir) throws IOExcep

@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();
Expand All @@ -74,12 +89,75 @@ public void shouldThrowIfDepositContractDiffers(@TempDir final File tempDir) thr

@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);

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

@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);
gconnect marked this conversation as resolved.
Show resolved Hide resolved
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);

assertFalse(networkContent.contains("deposit_chain_id"));
assertNull(readDatabaseNetwork.depositChainId);
}
}