From 26fd65cc48dc8a438a4a519f31bd49edba796c87 Mon Sep 17 00:00:00 2001 From: garyschulte Date: Fri, 6 Oct 2023 09:10:26 -0700 Subject: [PATCH] bugfix getStorageByRootHash to only return head worldstate if the supplied optional rootHash is empty Signed-off-by: garyschulte --- .../cache/CachedWorldStorageManager.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java index b04cbc85da4..371173beabb 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java @@ -14,8 +14,6 @@ */ package org.hyperledger.besu.ethereum.bonsai.cache; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateProvider; import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiSnapshotWorldStateKeyValueStorage; @@ -45,6 +43,8 @@ import java.util.stream.LongStream; import java.util.stream.Stream; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.google.common.annotations.VisibleForTesting; import org.apache.tuweni.bytes.Bytes32; import org.slf4j.Logger; @@ -55,10 +55,8 @@ public class CachedWorldStorageManager extends AbstractTrieLogManager private static final Logger LOG = LoggerFactory.getLogger(CachedWorldStorageManager.class); private final BonsaiWorldStateProvider archive; private final ObservableMetricsSystem metricsSystem; - private final Cache stateRootToBlockHashCache = Caffeine.newBuilder() - .maximumSize(512) - .expireAfterWrite(100, TimeUnit.MINUTES) - .build(); + private final Cache stateRootToBlockHashCache = + Caffeine.newBuilder().maximumSize(512).expireAfterWrite(100, TimeUnit.MINUTES).build(); CachedWorldStorageManager( final BonsaiWorldStateProvider archive, @@ -214,10 +212,15 @@ public Optional getHeadWorldState( @Override public Optional getStorageByRootHash(final Optional rootHash) { - return rootHash.map(stateRootToBlockHashCache::getIfPresent) - .map(Optional::of) - .orElseGet(rootWorldStateStorage::getWorldStateBlockHash) - .map(cachedWorldStatesByHash::get); + if (rootHash.isPresent()) { + // if we supplied a hash, return the worldstate for that hash if it is available: + return rootHash + .map(stateRootToBlockHashCache::getIfPresent) + .map(cachedWorldStatesByHash::get); + } else { + // if we did not supply a hash, return the head worldstate from cachedWorldStates + return rootWorldStateStorage.getWorldStateBlockHash().map(cachedWorldStatesByHash::get); + } } @Override