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

Check network on start and reset db #8642

Merged
merged 31 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
62100c1
Add check for Ephemery and reset db
gconnect Sep 24, 2024
d5dc6c4
Remove unused comment
gconnect Sep 24, 2024
a8f6afa
Add a shouldResetForEphemery method and test
gconnect Sep 24, 2024
a33707b
Merge branch 'master' into check-network-on-start
gconnect Sep 24, 2024
c1149ac
Merge branch 'master' into check-network-on-start
gconnect Sep 25, 2024
34ecc2d
update null check
gconnect Sep 25, 2024
572d91b
Merge branch 'master' into check-network-on-start
gconnect Sep 26, 2024
7b6584b
Merge branch 'master' into check-network-on-start
gconnect Sep 26, 2024
e935764
Merge branch 'master' into check-network-on-start
gconnect Sep 27, 2024
d496675
Merge branch 'master' into check-network-on-start
gconnect Sep 30, 2024
1bbc3c4
optimized the reset code and add a custom EphemeryException
gconnect Sep 30, 2024
8a3d40c
Merge branch 'master' into check-network-on-start
gconnect Sep 30, 2024
d23b3c4
Update the reset method and remove migrate
gconnect Sep 30, 2024
6a5d9fc
Remove private modifier
gconnect Sep 30, 2024
c2aced4
Apply spotlessApply
gconnect Sep 30, 2024
6d2cf43
Apply spotlessApply
gconnect Oct 1, 2024
7b2871c
Create custom Ephemery reset class and test
gconnect Oct 1, 2024
d9d1951
Add fnal and move global variable to local variable
gconnect Oct 1, 2024
0d47959
Remove repeated code
gconnect Oct 1, 2024
4306df8
Merge branch 'master' into check-network-on-start
gconnect Oct 2, 2024
ee57df8
Merge branch 'master' into check-network-on-start
gconnect Oct 2, 2024
f73e491
Ensure reset method delete specific files and update test and change …
gconnect Oct 2, 2024
f95a61a
Remove print statement and add final
gconnect Oct 2, 2024
b2bcefd
Update the recursive delete method and test
gconnect Oct 2, 2024
769c6f8
Merge branch 'master' into check-network-on-start
gconnect Oct 2, 2024
6cdc994
Remove print statement
gconnect Oct 2, 2024
5588f16
Merge branch 'master' into check-network-on-start
gconnect Oct 3, 2024
e96e6e3
Merge branch 'master' into check-network-on-start
gconnect Oct 5, 2024
92f6d5b
Merge branch 'master' into check-network-on-start
rolfyone Oct 7, 2024
c1ad18d
Merge branch 'master' into check-network-on-start
rolfyone Oct 8, 2024
b20eedc
Merge branch 'master' into check-network-on-start
rolfyone Oct 8, 2024
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 @@ -27,15 +27,19 @@ public class EphemeryDatabaseReset {
Database resetDatabaseAndCreate(
final ServiceConfig serviceConfig, final VersionedDatabaseFactory dbFactory) {
try {
final Path beaconDataDir = serviceConfig.getDataDirLayout().getBeaconDataDirectory();
final Path beaconDataDir =
serviceConfig.getDataDirLayout().getBeaconDataDirectory().getParent();
final Path dbDataDir = beaconDataDir.resolve("db");
final Path networkFile = beaconDataDir.resolve("network.yml");
final Path validatorDataDir = serviceConfig.getDataDirLayout().getValidatorDataDirectory();
final Path slashProtectionDir;
if (validatorDataDir.endsWith("slashprotection")) {
slashProtectionDir = validatorDataDir;
} else {
slashProtectionDir = validatorDataDir.resolve("slashprotection");
}
deleteDirectoryRecursively(beaconDataDir);
deleteDirectoryRecursively(dbDataDir);
deleteFile(networkFile);
deleteDirectoryRecursively(slashProtectionDir);
return dbFactory.createDatabase();
} catch (final Exception ex) {
Expand All @@ -44,6 +48,12 @@ Database resetDatabaseAndCreate(
}
}

void deleteFile(final Path path) throws IOException {
if (Files.exists(path)) {
Files.delete(path);
}
}

void deleteDirectoryRecursively(final Path path) throws IOException {
if (Files.isDirectory(path)) {
try (var stream = Files.walk(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class StorageService extends Service implements StorageServiceFacade {
private final boolean depositSnapshotStorageEnabled;
private final boolean blobSidecarsStorageCountersEnabled;
private static final Logger LOG = LogManager.getLogger();
private final EphemeryDatabaseReset ephemeryDatabaseReset;

public StorageService(
final ServiceConfig serviceConfig,
Expand All @@ -70,7 +69,6 @@ public StorageService(
this.config = storageConfiguration;
this.depositSnapshotStorageEnabled = depositSnapshotStorageEnabled;
this.blobSidecarsStorageCountersEnabled = blobSidecarsStorageCountersEnabled;
this.ephemeryDatabaseReset = new EphemeryDatabaseReset();
}

@Override
Expand All @@ -92,7 +90,10 @@ protected SafeFuture<?> doStart() {
try {
database = dbFactory.createDatabase();
} catch (EphemeryException e) {
final EphemeryDatabaseReset ephemeryDatabaseReset = new EphemeryDatabaseReset();
database = ephemeryDatabaseReset.resetDatabaseAndCreate(serviceConfig, dbFactory);
gconnect marked this conversation as resolved.
Show resolved Hide resolved
LOG.warn(
"Ephemery network deposit contract id has updated, resetting the stored database and slashing protection data.");
gconnect marked this conversation as resolved.
Show resolved Hide resolved
}
final SettableLabelledGauge pruningTimingsLabelledGauge =
SettableLabelledGauge.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import static java.nio.file.Files.createTempDirectory;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -44,8 +47,9 @@ class EphemeryDatabaseResetTest {

@Mock private DataDirLayout dataDirLayout;
private Path beaconDataDir;
private Path dbDataDir;
private Path resolvedSlashProtectionDir;

private File networkFile;
@Mock private Database database;

@InjectMocks private EphemeryDatabaseReset ephemeryDatabaseReset;
Expand All @@ -55,30 +59,44 @@ void setUp() throws IOException {
MockitoAnnotations.openMocks(this);
ephemeryDatabaseReset = spy(new EphemeryDatabaseReset());
beaconDataDir = createTempDirectory("beaconDataDir");
dbDataDir = beaconDataDir.resolve("db");
networkFile = new File(beaconDataDir.toFile(), "network.yml");
final Path validatorDataDir = createTempDirectory("validatorDataDir");
resolvedSlashProtectionDir = validatorDataDir.resolve("slashprotection");

when(serviceConfig.getDataDirLayout()).thenReturn(dataDirLayout);
when(dataDirLayout.getBeaconDataDirectory()).thenReturn(beaconDataDir);
when(dataDirLayout.getBeaconDataDirectory().resolve("db")).thenReturn(dbDataDir);
when(dataDirLayout.getBeaconDataDirectory().resolve("network.yml"))
.thenReturn(networkFile.toPath());
when(dataDirLayout.getValidatorDataDirectory()).thenReturn(validatorDataDir);
when(dataDirLayout.getValidatorDataDirectory().resolve("slashprotection"))
.thenReturn(resolvedSlashProtectionDir);
}

@Test
void shouldResetDirectoriesAndCreateDatabase() throws IOException {
void shouldResetSpecificDirectoriesAndCreateDatabase() throws IOException {
final Path kvStoreDir = beaconDataDir.resolve("kvstore");
Files.createDirectory(kvStoreDir);
final Path dbVersion = beaconDataDir.resolve("db.version");
Files.createFile(dbVersion);

when(dbFactory.createDatabase()).thenReturn(database);

final Database result = ephemeryDatabaseReset.resetDatabaseAndCreate(serviceConfig, dbFactory);
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(beaconDataDir);
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir);
verify(ephemeryDatabaseReset).deleteFile(networkFile.toPath());
gconnect marked this conversation as resolved.
Show resolved Hide resolved
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir);
verify(dbFactory).createDatabase();
assertTrue(Files.exists(kvStoreDir));
assertTrue(Files.exists(dbVersion));
assertEquals(database, result);
}

@Test
void shouldThrowInvalidConfigurationExceptionWhenDirectoryDeletionFails() throws IOException {
doThrow(new IOException("Failed to delete directory"))
.when(ephemeryDatabaseReset)
.deleteDirectoryRecursively(beaconDataDir);
.deleteDirectoryRecursively(dbDataDir);
final InvalidConfigurationException exception =
assertThrows(
InvalidConfigurationException.class,
Expand All @@ -94,7 +112,7 @@ void shouldThrowInvalidConfigurationExceptionWhenDirectoryDeletionFails() throws

@Test
void shouldThrowInvalidConfigurationExceptionWhenDatabaseCreationFails() throws IOException {
doNothing().when(ephemeryDatabaseReset).deleteDirectoryRecursively(beaconDataDir);
doNothing().when(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir);
doNothing().when(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir);
when(dbFactory.createDatabase()).thenThrow(new RuntimeException("Database creation failed"));
final InvalidConfigurationException exception =
Expand All @@ -106,7 +124,7 @@ void shouldThrowInvalidConfigurationExceptionWhenDatabaseCreationFails() throws
assertEquals(
"The existing ephemery database was old, and was unable to reset it.",
exception.getMessage());
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(beaconDataDir);
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir);
verify(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir);
verify(dbFactory).createDatabase();
}
Expand Down