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 @@ -39,12 +40,24 @@ public class DatabaseNetwork {
@VisibleForTesting
final String depositContract;

@JsonInclude(JsonInclude.Include.NON_NULL)
gconnect marked this conversation as resolved.
Show resolved Hide resolved
@JsonProperty("deposit_chainId")
@VisibleForTesting
final Long depositChainId;

@JsonCreator
DatabaseNetwork(
@JsonProperty("fork_version") final String forkVersion,
@JsonProperty("deposit_contract") final String depositContract) {
@JsonProperty("deposit_contract") final String depositContract,
@JsonProperty("deposit_chainId") final Long depositChainId) {
gconnect marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -16,7 +16,11 @@
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.assertNull;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
Expand Down Expand Up @@ -82,4 +86,45 @@ public void shouldNotThrowIfForkAndContractMatch(@TempDir final File tempDir) th

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

@Test
void shouldWriteAndReadDatabaseNetworkWithChainId(@TempDir final File tempDir)
throws IOException {
final File networkFile = new File(tempDir, "network.yml");

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

final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
gconnect marked this conversation as resolved.
Show resolved Hide resolved
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(depositId, readDatabaseNetwork.depositChainId);
}

@Test
void shouldWriteAndReadDatabaseNetworkWithoutChainId(@TempDir final File tempDir)
throws IOException {
final File networkFile = new File(tempDir, "network.yml");
gconnect marked this conversation as resolved.
Show resolved Hide resolved

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

final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.writerFor(DatabaseNetwork.class).writeValue(networkFile, databaseNetwork);
gconnect marked this conversation as resolved.
Show resolved Hide resolved
final DatabaseNetwork readDatabaseNetwork =
objectMapper.readerFor(DatabaseNetwork.class).readValue(networkFile);

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