Skip to content

Commit

Permalink
refactor check for full flat
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Jan 25, 2024
1 parent e66d673 commit 8407b9e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.cli.options.unstable.SynchronizerOptions;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.config.CheckpointConfigOptions;
import org.hyperledger.besu.config.GenesisConfigFile;
Expand Down Expand Up @@ -1092,13 +1091,7 @@ private Optional<SnapProtocolManager> createSnapProtocolManager(
final EthPeers ethPeers,
final EthMessages snapMessages) {
return Optional.of(
new SnapProtocolManager(
peerValidators,
ethPeers,
snapMessages,
protocolContext,
// TODO: revisit this as part of #6157
() -> SynchronizerOptions.create().isSnapsyncFlatDbHealingEnabled()));
new SnapProtocolManager(peerValidators, ethPeers, snapMessages, protocolContext));
}

WorldStateArchive createWorldStateArchive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hyperledger.besu.ethereum.trie.bonsai.cache.CachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.bonsai.cache.CachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.bonsai.storage.flat.FullFlatDbStrategy;
import org.hyperledger.besu.ethereum.trie.bonsai.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
Expand Down Expand Up @@ -349,6 +350,11 @@ public void resetArchiveStateTo(final BlockHeader blockHeader) {
blockHeader, persistedState.getWorldStateRootHash(), persistedState);
}

@Override
public boolean isFullFlat() {
return worldStateStorage.getFlatDbStrategy() instanceof FullFlatDbStrategy;
}

@Override
public <U> Optional<U> getAccountProof(
final BlockHeader blockHeader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import org.hyperledger.besu.plugin.services.trielogs.TrieLogFactory;
import org.hyperledger.besu.plugin.services.trielogs.TrieLogProvider;
import org.hyperledger.besu.util.Subscribers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TrieLogManager {
private static final Logger LOG = LoggerFactory.getLogger(TrieLogManager.class);
public static final long LOG_RANGE_LIMIT = 1000; // restrict trielog range queries to 1k logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.ethereum.worldstate;


import org.hyperledger.besu.ethereum.trie.bonsai.cache.CachedWorldStorageManager;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ <U> Optional<U> getAccountProof(
final List<UInt256> accountStorageKeys,
final Function<Optional<WorldStateProof>, ? extends Optional<U>> mapper);

default boolean isFlatArchive() {
return this instanceof FlatWorldStateArchive;
default Optional<FlatWorldStateArchive> isFlatArchive() {
return Optional.of(this)
.filter(FlatWorldStateArchive.class::isInstance)
.map(FlatWorldStateArchive.class::cast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableList;
import org.slf4j.Logger;
Expand All @@ -53,12 +52,11 @@ public SnapProtocolManager(
final List<PeerValidator> peerValidators,
final EthPeers ethPeers,
final EthMessages snapMessages,
final ProtocolContext protocolContext,
final Supplier<Boolean> isEnabled) {
final ProtocolContext protocolContext) {
this.ethPeers = ethPeers;
this.snapMessages = snapMessages;
this.supportedCapabilities = calculateCapabilities();
new SnapServer(snapMessages, protocolContext, isEnabled.get());
new SnapServer(snapMessages, protocolContext);
}

private List<Capability> calculateCapabilities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.bonsai.cache.CachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.worldstate.FlatWorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.plugin.services.BesuEvents;

Expand Down Expand Up @@ -84,36 +85,34 @@ class SnapServer implements BesuEvents.InitialSyncCompletionListener {
private final EthMessages snapMessages;
private final Function<Optional<Hash>, Optional<BonsaiWorldStateKeyValueStorage>>
worldStateStorageProvider;
private boolean isEnabled = true;

SnapServer(
final EthMessages snapMessages,
final ProtocolContext protocolContext,
final boolean isEnabled) {
SnapServer(final EthMessages snapMessages, final ProtocolContext protocolContext) {
this(
snapMessages,
rootHash ->
((BonsaiWorldStateProvider) protocolContext.getWorldStateArchive())
.getCachedWorldStorageManager()
.flatMap(storageManager -> storageManager.getStorageByRootHash(rootHash)));
this.isEnabled = isEnabled;
var archive = protocolContext.getWorldStateArchive();
if (isEnabled && archive.isFlatArchive()) {
var cachedStorageManager = ((FlatWorldStateArchive) archive).getCachedWorldStorageManager();
var blockchain = protocolContext.getBlockchain();

// prime state-root-to-blockhash cache
primeWorldStateArchive(cachedStorageManager, blockchain);

// subscribe to initial sync completed events to start/stop snap server:
protocolContext
.getSynchronizer()
.filter(z -> z instanceof DefaultSynchronizer)
.map(DefaultSynchronizer.class::cast)
.ifPresentOrElse(
z -> this.listenerId.set(z.subscribeInitialSync(this)),
() -> LOGGER.warn("SnapServer created without reference to sync status"));
}
Optional.of(protocolContext.getWorldStateArchive())
.flatMap(WorldStateArchive::isFlatArchive)
.filter(FlatWorldStateArchive::isFullFlat)
.ifPresent(
flatArchive -> {
var cachedStorageManager = flatArchive.getCachedWorldStorageManager();
var blockchain = protocolContext.getBlockchain();

// prime state-root-to-blockhash cache
primeWorldStateArchive(cachedStorageManager, blockchain);

// subscribe to initial sync completed events to start/stop snap server:
protocolContext
.getSynchronizer()
.filter(z -> z instanceof DefaultSynchronizer)
.map(DefaultSynchronizer.class::cast)
.ifPresentOrElse(
z -> this.listenerId.set(z.subscribeInitialSync(this)),
() -> LOGGER.warn("SnapServer created without reference to sync status"));
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hyperledger.besu.ethereum.eth.manager.snap.SnapServer.HASH_LAST;
import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.DEFAULT_CONFIG;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
Expand All @@ -37,9 +38,9 @@
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProvider;
import org.hyperledger.besu.ethereum.trie.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
Expand All @@ -56,7 +57,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.DEFAULT_CONFIG;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit 8407b9e

Please sign in to comment.