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

[Draft] Add database reset on Ephemery Network #8626

Conversation

gconnect
Copy link
Contributor

@gconnect gconnect commented Sep 21, 2024

PR Description

  • Add deposit chainId to the generated network.yml file
  • Reset the db

Todo

  • Still need to ensure that the above only happens on Ephemery

Doing this tends not to work as expected

  final Eth2NetworkConfiguration networkConfig =
        Eth2NetworkConfiguration.builder(Eth2Network.EPHEMERY).build();

    if (networkConfig.getEth2Network().equals(Optional.of(Eth2Network.EPHEMERY))) {
      ....
    }

Still figuring how to handle the check.

Fixed Issue(s)

"fixes #8589"

Documentation

  • I thought about documentation and added the doc-change-required label to this PR if updates are required.

Changelog

  • I thought about adding a changelog entry, and added one if I deemed necessary.

@gconnect gconnect changed the title Add deposit chainId to the network.yml file and database reset [Draft] Add deposit chainId to the network.yml file and database reset Sep 21, 2024
@gconnect
Copy link
Contributor Author

@rolfyone please take a look when you have the time.

@gconnect gconnect changed the title [Draft] Add deposit chainId to the network.yml file and database reset [Draft] Add database reset on Ephemery Network Sep 21, 2024
@rolfyone
Copy link
Contributor

Thanks for raising, will review shortly. One initial comment -
We'll only want 'partially addresses' in the 'fixes, because this would otherwise close the issue...

if (source.exists()) {
final DatabaseNetwork databaseNetwork =
objectMapper.readerFor(DatabaseNetwork.class).readValue(source);

// Fork version validation
Copy link
Contributor

@rolfyone rolfyone Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove these comments, they don't really add to the readability of the code... (basically all of the comments in this file at least)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will remove it.

@gconnect
Copy link
Contributor Author

Thanks for raising, will review shortly. One initial comment - We'll only want 'partially addresses' in the 'fixes, because this would otherwise close the issue...

Not sure what you meant here.

Copy link
Contributor

@rolfyone rolfyone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially treat this as a 'tracer' PR and just create a new branch with what we want in the first one... Alternatively we need to remove all of the functionality other than the network yaml change
Basically, I need a PR that just changes the network yaml and doesn't break anything when reading the old configuration file that doesn't have the chain id in it... This is really critical, because thats basically ensuring its not a breaking change.

DatabaseNetwork databaseNetwork =
new DatabaseNetwork(forkVersionString, depositContractString);
// Handle Ephemery or other networks
String depositChainIdString = depositChainId.orElse(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final

Comment on lines +111 to +112
System.out.println(
"network " + NETWORK_CONFIG.getEth2Network().equals(Optional.of(Eth2Network.EPHEMERY)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want System.out in our code, we'd want to be using loggers... Is this just a test message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want System.out in our code, we'd want to be using loggers... Is this just a test message?

Yes it a test message, forgot to remove it before pushing

Comment on lines +113 to +118
if (NETWORK_CONFIG.getEth2Network().equals(Optional.of(Eth2Network.EPHEMERY))) {
databaseNetwork =
new DatabaseNetwork(forkVersionString, depositContractString, depositChainIdString);
} else {
databaseNetwork = new DatabaseNetwork(forkVersionString, depositContractString, null);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just always write deposit_chain_id to the file which makes this a lot more readable. The conditional check I'd be more worried about would be ensuring we don't crash if it's not there on startup once we're checking that.

@@ -95,11 +140,12 @@ 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); // Compare deposit_chain_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

this.forkVersion = forkVersion;
this.depositContract = depositContract;
this.depositChainId = depositChainId; // Can be null for non-Ephemery networks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment - will also be null for any old databases that ARE ephemery that you open... we can't assume all ephemery will have this data.
Also though it's less important, people just have to manually reset their db (it'll literally be a dev - you or me basically)

I do also think that we could have a constructor with just the old interface and default the new field to null, which would simplify testing and make it clearer...

Comment on lines +93 to +105
if (NETWORK_CONFIG.getEth2Network().equals(Optional.of(Eth2Network.EPHEMERY))) {

// Deposit chain ID validation
if (depositChainId.isPresent()
&& databaseNetwork.depositChainId != null
&& !databaseNetwork.depositChainId.equals(depositChainId.get())) {
throw DatabaseStorageException.unrecoverable(
formatMessage(
"deposit chain ID",
depositChainId.get(),
String.valueOf(databaseNetwork.depositChainId)));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer this check to another PR please. it'll make testing a lot easier to see.

@@ -37,11 +37,12 @@ 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))
assertThat(DatabaseNetwork.init(networkFile, fork, eth1Address, null))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd be surprised that any of these tests pass? we're pushing a null into an optional field which rarely ends well...
My suggestion above was to make the init not take an optional, so this will likely be a better answer after that...

Comment on lines +397 to +439
// This implementation is required for Ephemery Network
private void resetDatabaseDirectories() {
File networkFile = getNetworkFile();

try {
if (dbDirectory.exists()) {
deleteDirectoryRecursively(dbDirectory.toPath());
}
if (v5ArchiveDirectory.exists()) {
deleteDirectoryRecursively(v5ArchiveDirectory.toPath());
}
if (networkFile.exists()) {
try {
LOG.info("Resetting network file at: {}", networkFile.getAbsolutePath());
Files.delete(networkFile.toPath());
System.out.println("Database directories reset successfully.");
} catch (IOException e) {
throw DatabaseStorageException.unrecoverable(
String.format("Failed to reset network file at %s", networkFile.getAbsolutePath()),
e);
}
}
} catch (IOException e) {
throw DatabaseStorageException.unrecoverable("Failed to reset database directories", e);
}
}

private void deleteDirectoryRecursively(final Path path) throws IOException {
if (Files.isDirectory(path)) {
try (var stream = Files.walk(path)) {
stream
.sorted((o1, o2) -> o2.compareTo(o1))
.forEach(
p -> {
try {
Files.delete(p);
} catch (IOException e) {
throw new RuntimeException("Failed to delete file: " + p, e);
}
});
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll defer this to another PR please, lets get the new field in and not breaking anything.

getNetworkFile(),
spec.getGenesisSpecConfig().getGenesisForkVersion(),
eth1Address,
Optional.of(String.valueOf(spec.getGenesisSpecConfig().getDepositChainId())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like at this level we just want the depositChainId as a long going in...

Comment on lines +49 to +50
static final Eth2NetworkConfiguration NETWORK_CONFIG =
Eth2NetworkConfiguration.builder(Eth2Network.EPHEMERY).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally I think we avoid something like this... at the very least we just want to add our deposit chain id in this pr.

@gconnect
Copy link
Contributor Author

We could potentially treat this as a 'tracer' PR and just create a new branch with what we want in the first one... Alternatively we need to remove all of the functionality other than the network yaml change Basically, I need a PR that just changes the network yaml and doesn't break anything when reading the old configuration file that doesn't have the chain id in it... This is really critical, because thats basically ensuring its not a breaking change.

What changes will I apply to the network.yaml file if I am not adding the chainId?

@gconnect gconnect closed this Sep 23, 2024
@gconnect
Copy link
Contributor Author

opened a new PR here #8631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add automatic reset for Ephemery Network
2 participants