Skip to content

Commit

Permalink
Add deposit chainId to network file on start (#8639)
Browse files Browse the repository at this point in the history
  • Loading branch information
gconnect authored Sep 24, 2024
1 parent cef41e3 commit ae367d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
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 @@ -61,7 +61,10 @@ public class DatabaseNetwork {
}

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 @@ -84,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public void shouldCreateNetworkFile(@TempDir final File tempDir) throws IOExcept
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();
}

Expand All @@ -66,10 +68,14 @@ public void shouldThrowIfForkDiffers(@TempDir final File tempDir) throws IOExcep
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");
}
Expand All @@ -80,9 +86,11 @@ public void shouldThrowIfDepositContractDiffers(@TempDir final File tempDir) thr
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");
}
Expand All @@ -93,9 +101,11 @@ public void shouldNotThrowIfForkAndContractMatch(@TempDir final File tempDir) th
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));
assertDoesNotThrow(() -> DatabaseNetwork.init(networkFile, fork, eth1Address, depositChainId));
}

@Test
Expand Down

0 comments on commit ae367d8

Please sign in to comment.