Skip to content

Commit

Permalink
bugfix getStorageByRootHash to only return head worldstate if the sup…
Browse files Browse the repository at this point in the history
…plied optional rootHash is empty

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Oct 10, 2023
1 parent 3f1a31b commit 26fd65c
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<Hash, Hash> stateRootToBlockHashCache = Caffeine.newBuilder()
.maximumSize(512)
.expireAfterWrite(100, TimeUnit.MINUTES)
.build();
private final Cache<Hash, Hash> stateRootToBlockHashCache =
Caffeine.newBuilder().maximumSize(512).expireAfterWrite(100, TimeUnit.MINUTES).build();

CachedWorldStorageManager(
final BonsaiWorldStateProvider archive,
Expand Down Expand Up @@ -214,10 +212,15 @@ public Optional<BonsaiWorldState> getHeadWorldState(

@Override
public Optional<CachedBonsaiWorldView> getStorageByRootHash(final Optional<Hash> 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
Expand Down

0 comments on commit 26fd65c

Please sign in to comment.