Skip to content

Commit

Permalink
Make sure genesis world state is created in archive mode
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <[email protected]>
  • Loading branch information
matthew1001 committed Sep 20, 2024
1 parent e914f22 commit fa3b9fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
import org.hyperledger.besu.services.kvstore.SegmentedInMemoryKeyValueStorage;

Expand All @@ -43,8 +44,13 @@ public class GenesisWorldStateProvider {
*/
public static MutableWorldState createGenesisWorldState(
final DataStorageConfiguration dataStorageConfiguration) {
if (Objects.requireNonNull(dataStorageConfiguration).getDataStorageFormat().isBonsaiFormat()) {
return createGenesisBonsaiWorldState();

if (Objects.requireNonNull(dataStorageConfiguration).getDataStorageFormat()
== DataStorageFormat.BONSAI) {
return createGenesisBonsaiWorldState(false);
} else if (Objects.requireNonNull(dataStorageConfiguration).getDataStorageFormat()
== DataStorageFormat.BONSAI_ARCHIVE) {
return createGenesisBonsaiWorldState(true);
} else {
return createGenesisForestWorldState();
}
Expand All @@ -55,7 +61,7 @@ public static MutableWorldState createGenesisWorldState(
*
* @return a mutable world state for the Genesis block
*/
private static MutableWorldState createGenesisBonsaiWorldState() {
private static MutableWorldState createGenesisBonsaiWorldState(final boolean archiveMode) {
final BonsaiCachedMerkleTrieLoader bonsaiCachedMerkleTrieLoader =
new BonsaiCachedMerkleTrieLoader(new NoOpMetricsSystem());
final BonsaiWorldStateKeyValueStorage bonsaiWorldStateKeyValueStorage =
Expand All @@ -65,7 +71,9 @@ private static MutableWorldState createGenesisBonsaiWorldState() {
new InMemoryKeyValueStorage(),
new NoOpMetricsSystem()),
new NoOpMetricsSystem(),
DataStorageConfiguration.DEFAULT_BONSAI_CONFIG);
archiveMode
? DataStorageConfiguration.DEFAULT_BONSAI_ARCHIVE_CONFIG
: DataStorageConfiguration.DEFAULT_BONSAI_CONFIG);
return new BonsaiWorldState(
bonsaiWorldStateKeyValueStorage,
bonsaiCachedMerkleTrieLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.CODE_STORAGE;
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage.WORLD_ROOT_HASH_KEY;

import org.hyperledger.besu.ethereum.bonsai.BonsaiContext;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.flat.ArchiveCodeStorageStrategy;
Expand Down Expand Up @@ -78,15 +79,20 @@ public void loadFlatDbStrategy(final SegmentedKeyValueStorage composedWorldState
}

@VisibleForTesting
FlatDbMode deriveFlatDbStrategy(final SegmentedKeyValueStorage composedWorldStateStorage) {
// final FlatDbMode requestedFlatDbMode =
// dataStorageConfiguration.getUnstable().getBonsaiFullFlatDbEnabled()
// ? FlatDbMode.FULL
// : FlatDbMode.PARTIAL;
synchronized FlatDbMode deriveFlatDbStrategy(
final SegmentedKeyValueStorage composedWorldStateStorage) {
final FlatDbMode requestedFlatDbMode =
dataStorageConfiguration.getUnstable().getBonsaiFullFlatDbEnabled()
? (dataStorageConfiguration
.getDataStorageFormat()
.equals(DataStorageFormat.BONSAI_ARCHIVE)
? FlatDbMode.ARCHIVE
: FlatDbMode.FULL)
: FlatDbMode.PARTIAL;

// TODO: commented out for archive testing
// final var existingTrieData =
// composedWorldStateStorage.get(TRIE_BRANCH_STORAGE, WORLD_ROOT_HASH_KEY).isPresent();
final var existingTrieData =
composedWorldStateStorage.get(TRIE_BRANCH_STORAGE, WORLD_ROOT_HASH_KEY).isPresent();

var flatDbMode =
FlatDbMode.fromVersion(
Expand All @@ -100,15 +106,20 @@ FlatDbMode deriveFlatDbStrategy(final SegmentedKeyValueStorage composedWorldStat
// and default to the storage config otherwise

// TODO: temporarily hard code ARCHIVE mode for testing
/*var flatDbModeVal =
dataStorageConfiguration
.getDataStorageFormat()
.equals(DataStorageFormat.BONSAI_ARCHIVE)
? FlatDbMode.ARCHIVE.getVersion()
: FlatDbMode.FULL.getVersion();*/
var flatDbModeVal =
dataStorageConfiguration
.getDataStorageFormat()
.equals(DataStorageFormat.BONSAI_ARCHIVE)
existingTrieData
? FlatDbMode.ARCHIVE.getVersion()
: FlatDbMode.FULL.getVersion();
// existingTrieData
// ? FlatDbMode.ARCHIVE.getVersion()
// : requestedFlatDbMode.getVersion();
: requestedFlatDbMode.getVersion();

// MRW TODO - If there is archive data in the freezer segment, we can assume
// archive mode

// persist this config in the db
var setDbModeTx = composedWorldStateStorage.startTransaction();
setDbModeTx.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public interface DataStorageConfiguration {
.bonsaiMaxLayersToLoad(DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD)
.build();

DataStorageConfiguration DEFAULT_BONSAI_ARCHIVE_CONFIG =
ImmutableDataStorageConfiguration.builder()
.dataStorageFormat(DataStorageFormat.BONSAI_ARCHIVE)
.bonsaiMaxLayersToLoad(DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD)
.build();

DataStorageConfiguration DEFAULT_BONSAI_PARTIAL_DB_CONFIG =
ImmutableDataStorageConfiguration.builder()
.dataStorageFormat(DataStorageFormat.BONSAI)
Expand Down

0 comments on commit fa3b9fb

Please sign in to comment.