Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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,23 @@ public class DatabaseNetwork {
@VisibleForTesting
final String depositContract;

@JsonInclude(JsonInclude.Include.NON_NULL)
@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) {
this.forkVersion = forkVersion;
this.depositContract = depositContract;
this.depositChainId = depositChainId;
}

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

public static DatabaseNetwork init(
Expand Down Expand Up @@ -95,7 +107,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();
DatabaseNetwork databaseNetwork =
new DatabaseNetwork(fork.toHexString(), eth1Address.toHexString(), depositId);

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.writerFor(DatabaseNetwork.class).writeValue(networkFile, databaseNetwork);
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");

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

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

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