diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index c6eeb8b3a04e..894291393d45 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -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; @@ -1092,13 +1091,7 @@ private Optional 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( diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/BonsaiWorldStateProvider.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/BonsaiWorldStateProvider.java index fb545df0594d..fd8e8dd274da 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/BonsaiWorldStateProvider.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/BonsaiWorldStateProvider.java @@ -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; @@ -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 Optional getAccountProof( final BlockHeader blockHeader, diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/trielog/TrieLogManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/trielog/TrieLogManager.java index 7ebcfd932e87..5102b7a0b90e 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/trielog/TrieLogManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/bonsai/trielog/TrieLogManager.java @@ -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 diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/FlatWorldStateArchive.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/FlatWorldStateArchive.java index dcbf0d5c76b8..40227e0eec3d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/FlatWorldStateArchive.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/FlatWorldStateArchive.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.ethereum.worldstate; - import org.hyperledger.besu.ethereum.trie.bonsai.cache.CachedWorldStorageManager; import java.util.Optional; diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateArchive.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateArchive.java index 8c334939bc95..edab8184eb5f 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateArchive.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateArchive.java @@ -68,7 +68,9 @@ Optional getAccountProof( final List accountStorageKeys, final Function, ? extends Optional> mapper); - default boolean isFlatArchive() { - return this instanceof FlatWorldStateArchive; + default Optional isFlatArchive() { + return Optional.of(this) + .filter(FlatWorldStateArchive.class::isInstance) + .map(FlatWorldStateArchive.class::cast); } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapProtocolManager.java index da138c782a35..3a63d3626efe 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapProtocolManager.java @@ -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; @@ -53,12 +52,11 @@ public SnapProtocolManager( final List peerValidators, final EthPeers ethPeers, final EthMessages snapMessages, - final ProtocolContext protocolContext, - final Supplier 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 calculateCapabilities() { diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServer.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServer.java index 6d5afa5ec10c..22118166913c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServer.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServer.java @@ -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; @@ -84,36 +85,34 @@ class SnapServer implements BesuEvents.InitialSyncCompletionListener { private final EthMessages snapMessages; private final Function, Optional> 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")); + }); } /** diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServerTest.java index a3eef1894a5d..462a93acd9e8 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/snap/SnapServerTest.java @@ -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; @@ -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; @@ -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;