Skip to content

Commit ec54c66

Browse files
committed
Add tests
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
1 parent 43ee50e commit ec54c66

File tree

3 files changed

+31
-80
lines changed

3 files changed

+31
-80
lines changed

ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/bonsai/AbstractIsolationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ protected StorageProvider createKeyValueStorageProvider() {
187187
8388608 /*CACHE_CAPACITY*/,
188188
false),
189189
Arrays.asList(KeyValueSegmentIdentifier.values()),
190-
DataStorageFormat.BONSAI,
191190
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS))
192191
.withCommonConfiguration(
193192
new BesuConfiguration() {

plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.java

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hyperledger.besu.plugin.services.BesuConfiguration;
2121
import org.hyperledger.besu.plugin.services.MetricsSystem;
2222
import org.hyperledger.besu.plugin.services.exception.StorageException;
23-
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
2423
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
2524
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageFactory;
2625
import org.hyperledger.besu.plugin.services.storage.SegmentIdentifier;
@@ -55,7 +54,6 @@
5554
public class RocksDBKeyValueStorageFactory implements KeyValueStorageFactory {
5655

5756
private static final Logger LOG = LoggerFactory.getLogger(RocksDBKeyValueStorageFactory.class);
58-
private static final VersionedStorageFormat DEFAULT_VERSIONED_FORMAT = FOREST_WITH_VARIABLES;
5957
private static final EnumSet<BaseVersionedStorageFormat> SUPPORTED_VERSIONED_FORMATS =
6058
EnumSet.of(FOREST_WITH_VARIABLES, BONSAI_WITH_VARIABLES);
6159
private static final String NAME = "rocksdb";
@@ -74,76 +72,31 @@ public class RocksDBKeyValueStorageFactory implements KeyValueStorageFactory {
7472
* @param configuration the configuration
7573
* @param configuredSegments the segments
7674
* @param ignorableSegments the ignorable segments
77-
* @param format the storage format
7875
* @param rocksDBMetricsFactory the rocks db metrics factory
7976
*/
8077
public RocksDBKeyValueStorageFactory(
8178
final Supplier<RocksDBFactoryConfiguration> configuration,
8279
final List<SegmentIdentifier> configuredSegments,
8380
final List<SegmentIdentifier> ignorableSegments,
84-
final DataStorageFormat format,
8581
final RocksDBMetricsFactory rocksDBMetricsFactory) {
8682
this.configuration = configuration;
8783
this.configuredSegments = configuredSegments;
8884
this.ignorableSegments = ignorableSegments;
8985
this.rocksDBMetricsFactory = rocksDBMetricsFactory;
90-
this.databaseMetadata = DatabaseMetadata.defaultForNewDb(format);
9186
}
9287

9388
/**
9489
* Instantiates a new RocksDb key value storage factory.
9590
*
9691
* @param configuration the configuration
9792
* @param configuredSegments the segments
98-
* @param format the storage format
9993
* @param rocksDBMetricsFactory the rocks db metrics factory
10094
*/
10195
public RocksDBKeyValueStorageFactory(
10296
final Supplier<RocksDBFactoryConfiguration> configuration,
10397
final List<SegmentIdentifier> configuredSegments,
104-
final DataStorageFormat format,
10598
final RocksDBMetricsFactory rocksDBMetricsFactory) {
106-
this(configuration, configuredSegments, List.of(), format, rocksDBMetricsFactory);
107-
}
108-
109-
/**
110-
* Instantiates a new Rocks db key value storage factory.
111-
*
112-
* @param configuration the configuration
113-
* @param configuredSegments the segments
114-
* @param ignorableSegments the ignorable segments
115-
* @param rocksDBMetricsFactory the rocks db metrics factory
116-
*/
117-
public RocksDBKeyValueStorageFactory(
118-
final Supplier<RocksDBFactoryConfiguration> configuration,
119-
final List<SegmentIdentifier> configuredSegments,
120-
final List<SegmentIdentifier> ignorableSegments,
121-
final RocksDBMetricsFactory rocksDBMetricsFactory) {
122-
this(
123-
configuration,
124-
configuredSegments,
125-
ignorableSegments,
126-
DEFAULT_VERSIONED_FORMAT.getFormat(),
127-
rocksDBMetricsFactory);
128-
}
129-
130-
/**
131-
* Instantiates a new Rocks db key value storage factory.
132-
*
133-
* @param configuration the configuration
134-
* @param configuredSegments the segments
135-
* @param rocksDBMetricsFactory the rocks db metrics factory
136-
*/
137-
public RocksDBKeyValueStorageFactory(
138-
final Supplier<RocksDBFactoryConfiguration> configuration,
139-
final List<SegmentIdentifier> configuredSegments,
140-
final RocksDBMetricsFactory rocksDBMetricsFactory) {
141-
this(
142-
configuration,
143-
configuredSegments,
144-
List.of(),
145-
DEFAULT_VERSIONED_FORMAT.getFormat(),
146-
rocksDBMetricsFactory);
99+
this(configuration, configuredSegments, List.of(), rocksDBMetricsFactory);
147100
}
148101

149102
@Override

plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ public void shouldCreateCorrectMetadataFileForLatestVersionForNewDb() throws Exc
7373
}
7474
}
7575

76-
private void mockCommonConfiguration(
77-
final Path tempDataDir, final Path tempDatabaseDir, final DataStorageFormat format) {
78-
when(commonConfiguration.getStoragePath()).thenReturn(tempDatabaseDir);
79-
when(commonConfiguration.getDataPath()).thenReturn(tempDataDir);
80-
lenient().when(commonConfiguration.getDatabaseFormat()).thenReturn(format);
81-
}
82-
8376
@Test
8477
public void shouldFailIfDbExistsAndNoMetadataFileFound() throws Exception {
8578
final Path tempDataDir = temporaryFolder.resolve("data");
@@ -122,7 +115,7 @@ public void shouldDetectCorrectMetadataV1() throws Exception {
122115
}
123116

124117
@Test
125-
public void shouldDetectCorrectVersionInCaseOfRollback() throws Exception {
118+
public void shouldFailInCaseOfUnmanagedRollback() throws Exception {
126119
final Path tempDataDir = temporaryFolder.resolve("data");
127120
final Path tempDatabaseDir = temporaryFolder.resolve("db");
128121
Files.createDirectories(tempDatabaseDir);
@@ -133,22 +126,20 @@ public void shouldDetectCorrectVersionInCaseOfRollback() throws Exception {
133126

134127
final RocksDBKeyValueStorageFactory storageFactory =
135128
new RocksDBKeyValueStorageFactory(
136-
() -> rocksDbConfiguration,
137-
segments,
138-
BONSAI,
139-
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS);
129+
() -> rocksDbConfiguration, segments, RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS);
140130

141131
storageFactory.create(segment, commonConfiguration, metricsSystem);
142132
storageFactory.close();
143133

134+
Utils.createDatabaseMetadataV2(tempDataDir, BONSAI, 1);
135+
144136
final RocksDBKeyValueStorageFactory rolledbackStorageFactory =
145137
new RocksDBKeyValueStorageFactory(
146-
() -> rocksDbConfiguration,
147-
segments,
148-
FOREST,
149-
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS);
150-
rolledbackStorageFactory.create(segment, commonConfiguration, metricsSystem);
151-
rolledbackStorageFactory.close();
138+
() -> rocksDbConfiguration, segments, RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS);
139+
assertThatThrownBy(
140+
() -> rolledbackStorageFactory.create(segment, commonConfiguration, metricsSystem))
141+
.isInstanceOf(StorageException.class)
142+
.hasMessageStartingWith("Database unsafe downgrade detect");
152143
}
153144

154145
@Test
@@ -167,12 +158,12 @@ public void shouldThrowExceptionWhenVersionNumberIsInvalid() throws Exception {
167158
segments,
168159
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS)
169160
.create(segment, commonConfiguration, metricsSystem))
170-
.isInstanceOf(StorageException.class);
161+
.isInstanceOf(StorageException.class)
162+
.hasMessageStartingWith("Unsupported db version");
171163
}
172164

173165
@Test
174-
public void shouldThrowExceptionWhenExistingDatabaseVersionDifferentFromConfig()
175-
throws Exception {
166+
public void shouldThrowExceptionWhenExistingDatabaseFormatDiffersFromConfig() throws Exception {
176167

177168
final DataStorageFormat actualDatabaseFormat = FOREST;
178169
final DataStorageFormat expectedDatabaseFormat = BONSAI;
@@ -185,12 +176,6 @@ public void shouldThrowExceptionWhenExistingDatabaseVersionDifferentFromConfig()
185176

186177
Utils.createDatabaseMetadataV2(tempDataDir, FOREST, 2);
187178

188-
String exceptionMessage =
189-
String.format(
190-
"Database format mismatch: DB at %s is %s but config expects %s. "
191-
+ "Please check your config.",
192-
tempDataDir.toAbsolutePath(), actualDatabaseFormat, expectedDatabaseFormat);
193-
194179
assertThatThrownBy(
195180
() ->
196181
new RocksDBKeyValueStorageFactory(
@@ -199,7 +184,10 @@ public void shouldThrowExceptionWhenExistingDatabaseVersionDifferentFromConfig()
199184
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS)
200185
.create(segment, commonConfiguration, metricsSystem))
201186
.isInstanceOf(StorageException.class)
202-
.hasMessage(exceptionMessage);
187+
.hasMessage(
188+
"Database format mismatch: DB at %s is %s but config expects %s. "
189+
+ "Please check your config.",
190+
tempDataDir.toAbsolutePath(), actualDatabaseFormat, expectedDatabaseFormat);
203191
}
204192

205193
@Test
@@ -240,9 +228,11 @@ public void shouldThrowExceptionWhenMetaDataFileIsCorrupted() throws Exception {
240228
segments,
241229
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS)
242230
.create(segment, commonConfiguration, metricsSystem))
243-
.isInstanceOf(IllegalStateException.class);
231+
.isInstanceOf(IllegalStateException.class)
232+
.hasMessageStartingWith("Invalid metadata file");
233+
;
244234

245-
Utils.createDatabaseMetadataRaw(tempDataDir, "{\"version\":\"iomedae\"}");
235+
Utils.createDatabaseMetadataRaw(tempDataDir, "{\"version\"=1}");
246236

247237
assertThatThrownBy(
248238
() ->
@@ -251,7 +241,9 @@ public void shouldThrowExceptionWhenMetaDataFileIsCorrupted() throws Exception {
251241
segments,
252242
RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS)
253243
.create(segment, commonConfiguration, metricsSystem))
254-
.isInstanceOf(IllegalStateException.class);
244+
.isInstanceOf(IllegalStateException.class)
245+
.hasMessageStartingWith("Invalid metadata file");
246+
;
255247
}
256248

257249
@Test
@@ -276,4 +268,11 @@ public void shouldCreateDBCorrectlyIfSymlink() throws Exception {
276268
.isEqualTo(BaseVersionedStorageFormat.FOREST_WITH_VARIABLES);
277269
}
278270
}
271+
272+
private void mockCommonConfiguration(
273+
final Path tempDataDir, final Path tempDatabaseDir, final DataStorageFormat format) {
274+
when(commonConfiguration.getStoragePath()).thenReturn(tempDatabaseDir);
275+
when(commonConfiguration.getDataPath()).thenReturn(tempDataDir);
276+
lenient().when(commonConfiguration.getDatabaseFormat()).thenReturn(format);
277+
}
279278
}

0 commit comments

Comments
 (0)