finish diffbased refactor

Signed-off-by: Karim Taam <karim.t2am@gmail.com>
pull/6920/head
Karim Taam 11 months ago
parent 2ce37aea5e
commit 06cda00141
  1. 286
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/BonsaiWorldStateProvider.java
  2. 2
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/cache/BonsaiCachedWorldStorageManager.java
  3. 4
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/trielog/TrieLogFactoryImpl.java
  4. 15
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/worldview/BonsaiWorldState.java
  5. 5
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/worldview/BonsaiWorldStateUpdateAccumulator.java
  6. 310
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/DiffBasedWorldStateProvider.java
  7. 10
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/cache/DiffBasedCachedWorldStorageManager.java
  8. 2
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/trielog/TrieLogManager.java
  9. 2
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/trielog/TrieLogPruner.java
  10. 21
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/worldview/DiffBasedWorldState.java
  11. 12
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/common/worldview/accumulator/DiffBasedWorldStateUpdateAccumulator.java
  12. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java
  13. 2
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/BonsaiSnapshotIsolationTests.java
  14. 5
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/BonsaiWorldStateProviderTest.java
  15. 2
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/CachedMerkleTrieLoaderTest.java
  16. 5
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/LogRollingTests.java
  17. 5
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/RollingImport.java
  18. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/storage/BonsaiWorldStateKeyValueStorageTest.java
  19. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/trielog/TrieLogFactoryTests.java
  20. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/common/trielog/TrieLogLayerTests.java
  21. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/common/trielog/TrieLogManagerTests.java
  22. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/common/trielog/TrieLogPrunerTest.java
  23. 18
      ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BonsaiReferenceTestWorldState.java

@ -19,52 +19,33 @@ package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.services.trielogs.TrieLog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import com.google.common.annotations.VisibleForTesting;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BonsaiWorldStateProvider implements WorldStateArchive {
public class BonsaiWorldStateProvider extends DiffBasedWorldStateProvider {
private static final Logger LOG = LoggerFactory.getLogger(BonsaiWorldStateProvider.class);
private final Blockchain blockchain;
private final BonsaiCachedWorldStorageManager cachedWorldStorageManager;
private final TrieLogManager trieLogManager;
private final BonsaiWorldState persistedState;
private final BonsaiWorldStateKeyValueStorage worldStateKeyValueStorage;
private final BonsaiCachedMerkleTrieLoader cachedMerkleTrieLoader;
public BonsaiWorldStateProvider(
@ -76,27 +57,11 @@ public class BonsaiWorldStateProvider implements WorldStateArchive {
final BesuContext pluginContext,
final EvmConfiguration evmConfiguration,
final TrieLogPruner trieLogPruner) {
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
this.cachedWorldStorageManager =
new BonsaiCachedWorldStorageManager(this, worldStateKeyValueStorage, metricsSystem);
// TODO: de-dup constructors
this.trieLogManager =
new TrieLogManager(
blockchain,
worldStateKeyValueStorage,
maxLayersToLoad.orElse(BonsaiCachedWorldStorageManager.RETAINED_LAYERS),
pluginContext,
trieLogPruner);
this.blockchain = blockchain;
super(worldStateKeyValueStorage, blockchain, maxLayersToLoad, pluginContext, trieLogPruner);
this.cachedMerkleTrieLoader = cachedMerkleTrieLoader;
this.persistedState = new BonsaiWorldState(this, worldStateKeyValueStorage, evmConfiguration);
blockchain
.getBlockHeader(persistedState.getWorldStateBlockHash())
.ifPresent(
blockHeader ->
this.cachedWorldStorageManager.addCachedLayer(
blockHeader, persistedState.getWorldStateRootHash(), persistedState));
provideCachedWorldStorageManager(
new BonsaiCachedWorldStorageManager(this, worldStateKeyValueStorage, metricsSystem));
loadPersistedState(new BonsaiWorldState(this, worldStateKeyValueStorage, evmConfiguration));
}
@VisibleForTesting
@ -107,181 +72,19 @@ public class BonsaiWorldStateProvider implements WorldStateArchive {
final Blockchain blockchain,
final BonsaiCachedMerkleTrieLoader cachedMerkleTrieLoader,
final EvmConfiguration evmConfiguration) {
this.cachedWorldStorageManager = cachedWorldStorageManager;
this.trieLogManager = trieLogManager;
this.blockchain = blockchain;
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
this.persistedState = new BonsaiWorldState(this, worldStateKeyValueStorage, evmConfiguration);
super(worldStateKeyValueStorage, blockchain, trieLogManager);
this.cachedMerkleTrieLoader = cachedMerkleTrieLoader;
blockchain
.getBlockHeader(persistedState.getWorldStateBlockHash())
.ifPresent(
blockHeader ->
this.cachedWorldStorageManager.addCachedLayer(
blockHeader, persistedState.getWorldStateRootHash(), persistedState));
}
@Override
public Optional<WorldState> get(final Hash rootHash, final Hash blockHash) {
return cachedWorldStorageManager
.getWorldState(blockHash)
.or(
() -> {
if (blockHash.equals(persistedState.blockHash())) {
return Optional.of(persistedState);
} else {
return Optional.empty();
}
})
.map(WorldState.class::cast);
}
@Override
public boolean isWorldStateAvailable(final Hash rootHash, final Hash blockHash) {
return cachedWorldStorageManager.containWorldStateStorage(blockHash)
|| persistedState.blockHash().equals(blockHash)
|| worldStateKeyValueStorage.isWorldStateAvailable(rootHash, blockHash);
}
@Override
public Optional<MutableWorldState> getMutable(
final BlockHeader blockHeader, final boolean shouldPersistState) {
if (shouldPersistState) {
return getMutable(blockHeader.getStateRoot(), blockHeader.getHash());
} else {
final BlockHeader chainHeadBlockHeader = blockchain.getChainHeadHeader();
if (chainHeadBlockHeader.getNumber() - blockHeader.getNumber()
>= trieLogManager.getMaxLayersToLoad()) {
LOG.warn(
"Exceeded the limit of back layers that can be loaded ({})",
trieLogManager.getMaxLayersToLoad());
return Optional.empty();
}
return cachedWorldStorageManager
.getWorldState(blockHeader.getHash())
.or(() -> cachedWorldStorageManager.getNearestWorldState(blockHeader))
.or(() -> cachedWorldStorageManager.getHeadWorldState(blockchain::getBlockHeader))
.flatMap(
bonsaiWorldState ->
rollMutableStateToBlockHash(
(BonsaiWorldState) bonsaiWorldState, blockHeader.getHash()))
.map(MutableWorldState::freeze);
}
}
@Override
public synchronized Optional<MutableWorldState> getMutable(
final Hash rootHash, final Hash blockHash) {
return rollMutableStateToBlockHash(persistedState, blockHash);
}
Optional<MutableWorldState> rollMutableStateToBlockHash(
final BonsaiWorldState mutableState, final Hash blockHash) {
if (blockHash.equals(mutableState.blockHash())) {
return Optional.of(mutableState);
} else {
try {
final Optional<BlockHeader> maybePersistedHeader =
blockchain.getBlockHeader(mutableState.blockHash()).map(BlockHeader.class::cast);
final List<TrieLog> rollBacks = new ArrayList<>();
final List<TrieLog> rollForwards = new ArrayList<>();
if (maybePersistedHeader.isEmpty()) {
trieLogManager.getTrieLogLayer(mutableState.blockHash()).ifPresent(rollBacks::add);
} else {
BlockHeader targetHeader = blockchain.getBlockHeader(blockHash).get();
BlockHeader persistedHeader = maybePersistedHeader.get();
// roll back from persisted to even with target
Hash persistedBlockHash = persistedHeader.getBlockHash();
while (persistedHeader.getNumber() > targetHeader.getNumber()) {
LOG.debug("Rollback {}", persistedBlockHash);
rollBacks.add(trieLogManager.getTrieLogLayer(persistedBlockHash).get());
persistedHeader = blockchain.getBlockHeader(persistedHeader.getParentHash()).get();
persistedBlockHash = persistedHeader.getBlockHash();
}
// roll forward to target
Hash targetBlockHash = targetHeader.getBlockHash();
while (persistedHeader.getNumber() < targetHeader.getNumber()) {
LOG.debug("Rollforward {}", targetBlockHash);
rollForwards.add(trieLogManager.getTrieLogLayer(targetBlockHash).get());
targetHeader = blockchain.getBlockHeader(targetHeader.getParentHash()).get();
targetBlockHash = targetHeader.getBlockHash();
}
// roll back in tandem until we hit a shared state
while (!persistedBlockHash.equals(targetBlockHash)) {
LOG.debug("Paired Rollback {}", persistedBlockHash);
LOG.debug("Paired Rollforward {}", targetBlockHash);
rollForwards.add(trieLogManager.getTrieLogLayer(targetBlockHash).get());
targetHeader = blockchain.getBlockHeader(targetHeader.getParentHash()).get();
rollBacks.add(trieLogManager.getTrieLogLayer(persistedBlockHash).get());
persistedHeader = blockchain.getBlockHeader(persistedHeader.getParentHash()).get();
targetBlockHash = targetHeader.getBlockHash();
persistedBlockHash = persistedHeader.getBlockHash();
}
}
// attempt the state rolling
final BonsaiWorldStateUpdateAccumulator bonsaiUpdater =
(BonsaiWorldStateUpdateAccumulator) mutableState.updater();
try {
for (final TrieLog rollBack : rollBacks) {
LOG.debug("Attempting Rollback of {}", rollBack.getBlockHash());
bonsaiUpdater.rollBack(rollBack);
}
for (int i = rollForwards.size() - 1; i >= 0; i--) {
final var forward = rollForwards.get(i);
LOG.debug("Attempting Rollforward of {}", rollForwards.get(i).getBlockHash());
bonsaiUpdater.rollForward(forward);
}
bonsaiUpdater.commit();
mutableState.persist(blockchain.getBlockHeader(blockHash).get());
LOG.debug(
"Archive rolling finished, {} now at {}",
mutableState.getWorldStateStorage().getClass().getSimpleName(),
blockHash);
return Optional.of(mutableState);
} catch (final MerkleTrieException re) {
// need to throw to trigger the heal
throw re;
} catch (final Exception e) {
// if we fail we must clean up the updater
bonsaiUpdater.reset();
LOG.debug(
"State rolling failed on "
+ mutableState.getWorldStateStorage().getClass().getSimpleName()
+ " for block hash "
+ blockHash,
e);
return Optional.empty();
}
} catch (final RuntimeException re) {
LOG.info("Archive rolling failed for block hash " + blockHash, re);
if (re instanceof MerkleTrieException) {
// need to throw to trigger the heal
throw re;
}
throw new MerkleTrieException(
"invalid", Optional.of(Address.ZERO), Hash.EMPTY, Bytes.EMPTY);
}
}
provideCachedWorldStorageManager(cachedWorldStorageManager);
loadPersistedState(new BonsaiWorldState(this, worldStateKeyValueStorage, evmConfiguration));
}
public BonsaiCachedMerkleTrieLoader getCachedMerkleTrieLoader() {
return cachedMerkleTrieLoader;
}
@Override
public MutableWorldState getMutable() {
return persistedState;
private BonsaiWorldStateKeyValueStorage getWorldStateKeyValueStorage() {
return (BonsaiWorldStateKeyValueStorage) worldStateKeyValueStorage;
}
/**
* Prepares the state healing process for a given address and location. It prepares the state
* healing, including retrieving data from storage, identifying invalid slots or nodes, removing
@ -290,12 +93,14 @@ public class BonsaiWorldStateProvider implements WorldStateArchive {
*/
public void prepareStateHealing(final Address address, final Bytes location) {
final Set<Bytes> keysToDelete = new HashSet<>();
final BonsaiWorldStateKeyValueStorage.Updater updater = worldStateKeyValueStorage.updater();
final BonsaiWorldStateKeyValueStorage.Updater updater =
getWorldStateKeyValueStorage().updater();
final Hash accountHash = address.addressHash();
final StoredMerklePatriciaTrie<Bytes, Bytes> accountTrie =
new StoredMerklePatriciaTrie<>(
(l, h) -> {
final Optional<Bytes> node = worldStateKeyValueStorage.getAccountStateTrieNode(l, h);
final Optional<Bytes> node =
getWorldStateKeyValueStorage().getAccountStateTrieNode(l, h);
if (node.isPresent()) {
keysToDelete.add(l);
}
@ -315,8 +120,8 @@ public class BonsaiWorldStateProvider implements WorldStateArchive {
new StoredMerklePatriciaTrie<>(
(l, h) -> {
Optional<Bytes> node =
worldStateKeyValueStorage.getAccountStorageTrieNode(
accountHash, l, h);
getWorldStateKeyValueStorage()
.getAccountStorageTrieNode(accountHash, l, h);
if (node.isPresent()) {
keysToDelete.add(Bytes.concatenate(accountHash, l));
}
@ -336,60 +141,9 @@ public class BonsaiWorldStateProvider implements WorldStateArchive {
LOG.warn("Invalid node for account {} at location {}", address, location);
// ignore
}
keysToDelete.forEach(bytes -> updater.removeAccountStateTrieNode(bytes));
keysToDelete.forEach(updater::removeAccountStateTrieNode);
updater.commit();
worldStateKeyValueStorage.downgradeToPartialFlatDbMode();
}
public TrieLogManager getTrieLogManager() {
return trieLogManager;
}
public BonsaiCachedWorldStorageManager getCachedWorldStorageManager() {
return cachedWorldStorageManager;
}
@Override
public void resetArchiveStateTo(final BlockHeader blockHeader) {
persistedState.resetWorldStateTo(blockHeader);
this.cachedWorldStorageManager.reset();
this.cachedWorldStorageManager.addCachedLayer(
blockHeader, persistedState.getWorldStateRootHash(), persistedState);
}
@Override
public <U> Optional<U> getAccountProof(
final BlockHeader blockHeader,
final Address accountAddress,
final List<UInt256> accountStorageKeys,
final Function<Optional<WorldStateProof>, ? extends Optional<U>> mapper) {
try (BonsaiWorldState ws = (BonsaiWorldState) getMutable(blockHeader, false).orElse(null)) {
if (ws != null) {
final WorldStateProofProvider worldStateProofProvider =
new WorldStateProofProvider(
new WorldStateStorageCoordinator(ws.getWorldStateStorage()));
return mapper.apply(
worldStateProofProvider.getAccountProof(
ws.getWorldStateRootHash(), accountAddress, accountStorageKeys));
}
} catch (Exception ex) {
LOG.error("failed proof query for " + blockHeader.getBlockHash().toShortHexString(), ex);
}
return Optional.empty();
}
@Override
public Optional<Bytes> getNodeData(final Hash hash) {
return Optional.empty();
}
@Override
public void close() {
try {
worldStateKeyValueStorage.close();
} catch (Exception e) {
// no op
}
getWorldStateKeyValueStorage().downgradeToPartialFlatDbMode();
}
}

@ -35,7 +35,7 @@ public class BonsaiCachedWorldStorageManager extends DiffBasedCachedWorldStorage
}
@Override
public DiffBasedWorldState<?> createWorldState(
public DiffBasedWorldState createWorldState(
final BonsaiWorldStateProvider archive,
final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage,
final EvmConfiguration evmConfiguration) {

@ -165,7 +165,9 @@ public class TrieLogFactoryImpl implements TrieLogFactory {
final StateTrieAccountValue newValue = nullOrValue(input, StateTrieAccountValue::readFrom);
final boolean isCleared = getOptionalIsCleared(input);
input.leaveList();
newLayer.getAccountChanges().put(address, new DiffBasedValue<>(oldValue, newValue, isCleared));
newLayer
.getAccountChanges()
.put(address, new DiffBasedValue<>(oldValue, newValue, isCleared));
}
if (input.nextIsNull()) {

@ -29,12 +29,12 @@ import org.hyperledger.besu.ethereum.trie.NodeLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiAccount;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateLayerStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedValue;
import org.hyperledger.besu.ethereum.trie.diffbased.common.cache.DiffBasedCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.DiffBasedWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.preload.StorageConsumingMap;
@ -55,7 +55,7 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
public class BonsaiWorldState extends DiffBasedWorldState<BonsaiWorldStateKeyValueStorage> {
public class BonsaiWorldState extends DiffBasedWorldState {
protected final BonsaiCachedMerkleTrieLoader cachedMerkleTrieLoader;
@ -74,7 +74,7 @@ public class BonsaiWorldState extends DiffBasedWorldState<BonsaiWorldStateKeyVal
protected BonsaiWorldState(
final BonsaiWorldStateKeyValueStorage worldStateKeyValueStorage,
final BonsaiCachedMerkleTrieLoader cachedMerkleTrieLoader,
final BonsaiCachedWorldStorageManager cachedWorldStorageManager,
final DiffBasedCachedWorldStorageManager cachedWorldStorageManager,
final TrieLogManager trieLogManager,
final EvmConfiguration evmConfiguration) {
super(worldStateKeyValueStorage, cachedWorldStorageManager, trieLogManager);
@ -90,6 +90,11 @@ public class BonsaiWorldState extends DiffBasedWorldState<BonsaiWorldStateKeyVal
evmConfiguration));
}
@Override
public BonsaiWorldStateKeyValueStorage getWorldStateStorage() {
return (BonsaiWorldStateKeyValueStorage) worldStateKeyValueStorage;
}
@Override
protected Hash calculateRootHash(
final Optional<DiffBasedWorldStateKeyValueStorage.Updater> maybeStateUpdater,
@ -328,7 +333,7 @@ public class BonsaiWorldState extends DiffBasedWorldState<BonsaiWorldStateKeyVal
@Override
public Optional<Bytes> getCode(@Nonnull final Address address, final Hash codeHash) {
return worldStateKeyValueStorage.getCode(codeHash, address.addressHash());
return getWorldStateStorage().getCode(codeHash, address.addressHash());
}
protected Optional<Bytes> getAccountStateTrieNode(final Bytes location, final Bytes32 nodeHash) {

@ -92,7 +92,8 @@ public class BonsaiWorldStateUpdateAccumulator
}
@Override
protected void assertCloseEnoughForDiffing(final BonsaiAccount source, final AccountValue account, final String context) {
BonsaiAccount.assertCloseEnoughForDiffing(source,account,context);
protected void assertCloseEnoughForDiffing(
final BonsaiAccount source, final AccountValue account, final String context) {
BonsaiAccount.assertCloseEnoughForDiffing(source, account, context);
}
}

@ -0,0 +1,310 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.diffbased.common;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.diffbased.common.cache.DiffBasedCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.DiffBasedWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.services.trielogs.TrieLog;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DiffBasedWorldStateProvider implements WorldStateArchive {
private static final Logger LOG = LoggerFactory.getLogger(DiffBasedWorldStateProvider.class);
protected final Blockchain blockchain;
protected final TrieLogManager trieLogManager;
protected DiffBasedCachedWorldStorageManager cachedWorldStorageManager;
protected DiffBasedWorldState persistedState;
protected final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage;
public DiffBasedWorldStateProvider(
final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage,
final Blockchain blockchain,
final Optional<Long> maxLayersToLoad,
final BesuContext pluginContext,
final TrieLogPruner trieLogPruner) {
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
// TODO: de-dup constructors
this.trieLogManager =
new TrieLogManager(
blockchain,
worldStateKeyValueStorage,
maxLayersToLoad.orElse(DiffBasedCachedWorldStorageManager.RETAINED_LAYERS),
pluginContext,
trieLogPruner);
this.blockchain = blockchain;
}
public DiffBasedWorldStateProvider(
final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage,
final Blockchain blockchain,
final TrieLogManager trieLogManager) {
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
// TODO: de-dup constructors
this.trieLogManager = trieLogManager;
this.blockchain = blockchain;
}
protected void provideCachedWorldStorageManager(
final DiffBasedCachedWorldStorageManager cachedWorldStorageManager) {
this.cachedWorldStorageManager = cachedWorldStorageManager;
}
protected void loadPersistedState(final DiffBasedWorldState persistedState) {
this.persistedState = persistedState;
blockchain
.getBlockHeader(persistedState.getWorldStateBlockHash())
.ifPresent(
blockHeader ->
this.cachedWorldStorageManager.addCachedLayer(
blockHeader, persistedState.getWorldStateRootHash(), persistedState));
}
@Override
public Optional<WorldState> get(final Hash rootHash, final Hash blockHash) {
return cachedWorldStorageManager
.getWorldState(blockHash)
.or(
() -> {
if (blockHash.equals(persistedState.blockHash())) {
return Optional.of(persistedState);
} else {
return Optional.empty();
}
})
.map(WorldState.class::cast);
}
@Override
public boolean isWorldStateAvailable(final Hash rootHash, final Hash blockHash) {
return cachedWorldStorageManager.containWorldStateStorage(blockHash)
|| persistedState.blockHash().equals(blockHash)
|| worldStateKeyValueStorage.isWorldStateAvailable(rootHash, blockHash);
}
@Override
public Optional<MutableWorldState> getMutable(
final BlockHeader blockHeader, final boolean shouldPersistState) {
if (shouldPersistState) {
return getMutable(blockHeader.getStateRoot(), blockHeader.getHash());
} else {
final BlockHeader chainHeadBlockHeader = blockchain.getChainHeadHeader();
if (chainHeadBlockHeader.getNumber() - blockHeader.getNumber()
>= trieLogManager.getMaxLayersToLoad()) {
LOG.warn(
"Exceeded the limit of back layers that can be loaded ({})",
trieLogManager.getMaxLayersToLoad());
return Optional.empty();
}
return cachedWorldStorageManager
.getWorldState(blockHeader.getHash())
.or(() -> cachedWorldStorageManager.getNearestWorldState(blockHeader))
.or(() -> cachedWorldStorageManager.getHeadWorldState(blockchain::getBlockHeader))
.flatMap(worldState -> rollMutableStateToBlockHash(worldState, blockHeader.getHash()))
.map(MutableWorldState::freeze);
}
}
@Override
public synchronized Optional<MutableWorldState> getMutable(
final Hash rootHash, final Hash blockHash) {
return rollMutableStateToBlockHash(persistedState, blockHash);
}
Optional<MutableWorldState> rollMutableStateToBlockHash(
final DiffBasedWorldState mutableState, final Hash blockHash) {
if (blockHash.equals(mutableState.blockHash())) {
return Optional.of(mutableState);
} else {
try {
final Optional<BlockHeader> maybePersistedHeader =
blockchain.getBlockHeader(mutableState.blockHash()).map(BlockHeader.class::cast);
final List<TrieLog> rollBacks = new ArrayList<>();
final List<TrieLog> rollForwards = new ArrayList<>();
if (maybePersistedHeader.isEmpty()) {
trieLogManager.getTrieLogLayer(mutableState.blockHash()).ifPresent(rollBacks::add);
} else {
BlockHeader targetHeader = blockchain.getBlockHeader(blockHash).get();
BlockHeader persistedHeader = maybePersistedHeader.get();
// roll back from persisted to even with target
Hash persistedBlockHash = persistedHeader.getBlockHash();
while (persistedHeader.getNumber() > targetHeader.getNumber()) {
LOG.debug("Rollback {}", persistedBlockHash);
rollBacks.add(trieLogManager.getTrieLogLayer(persistedBlockHash).get());
persistedHeader = blockchain.getBlockHeader(persistedHeader.getParentHash()).get();
persistedBlockHash = persistedHeader.getBlockHash();
}
// roll forward to target
Hash targetBlockHash = targetHeader.getBlockHash();
while (persistedHeader.getNumber() < targetHeader.getNumber()) {
LOG.debug("Rollforward {}", targetBlockHash);
rollForwards.add(trieLogManager.getTrieLogLayer(targetBlockHash).get());
targetHeader = blockchain.getBlockHeader(targetHeader.getParentHash()).get();
targetBlockHash = targetHeader.getBlockHash();
}
// roll back in tandem until we hit a shared state
while (!persistedBlockHash.equals(targetBlockHash)) {
LOG.debug("Paired Rollback {}", persistedBlockHash);
LOG.debug("Paired Rollforward {}", targetBlockHash);
rollForwards.add(trieLogManager.getTrieLogLayer(targetBlockHash).get());
targetHeader = blockchain.getBlockHeader(targetHeader.getParentHash()).get();
rollBacks.add(trieLogManager.getTrieLogLayer(persistedBlockHash).get());
persistedHeader = blockchain.getBlockHeader(persistedHeader.getParentHash()).get();
targetBlockHash = targetHeader.getBlockHash();
persistedBlockHash = persistedHeader.getBlockHash();
}
}
// attempt the state rolling
final DiffBasedWorldStateUpdateAccumulator<?> diffBasedUpdater =
(DiffBasedWorldStateUpdateAccumulator<?>) mutableState.updater();
try {
for (final TrieLog rollBack : rollBacks) {
LOG.debug("Attempting Rollback of {}", rollBack.getBlockHash());
diffBasedUpdater.rollBack(rollBack);
}
for (int i = rollForwards.size() - 1; i >= 0; i--) {
final var forward = rollForwards.get(i);
LOG.debug("Attempting Rollforward of {}", rollForwards.get(i).getBlockHash());
diffBasedUpdater.rollForward(forward);
}
diffBasedUpdater.commit();
mutableState.persist(blockchain.getBlockHeader(blockHash).get());
LOG.debug(
"Archive rolling finished, {} now at {}",
mutableState.getWorldStateStorage().getClass().getSimpleName(),
blockHash);
return Optional.of(mutableState);
} catch (final MerkleTrieException re) {
// need to throw to trigger the heal
throw re;
} catch (final Exception e) {
// if we fail we must clean up the updater
diffBasedUpdater.reset();
LOG.debug(
"State rolling failed on "
+ mutableState.getWorldStateStorage().getClass().getSimpleName()
+ " for block hash "
+ blockHash,
e);
return Optional.empty();
}
} catch (final RuntimeException re) {
LOG.info("Archive rolling failed for block hash " + blockHash, re);
if (re instanceof MerkleTrieException) {
// need to throw to trigger the heal
throw re;
}
throw new MerkleTrieException(
"invalid", Optional.of(Address.ZERO), Hash.EMPTY, Bytes.EMPTY);
}
}
}
@Override
public MutableWorldState getMutable() {
return persistedState;
}
public TrieLogManager getTrieLogManager() {
return trieLogManager;
}
public DiffBasedCachedWorldStorageManager getCachedWorldStorageManager() {
return cachedWorldStorageManager;
}
@Override
public void resetArchiveStateTo(final BlockHeader blockHeader) {
persistedState.resetWorldStateTo(blockHeader);
this.cachedWorldStorageManager.reset();
this.cachedWorldStorageManager.addCachedLayer(
blockHeader, persistedState.getWorldStateRootHash(), persistedState);
}
@Override
public <U> Optional<U> getAccountProof(
final BlockHeader blockHeader,
final Address accountAddress,
final List<UInt256> accountStorageKeys,
final Function<Optional<WorldStateProof>, ? extends Optional<U>> mapper) {
try (DiffBasedWorldState ws =
(DiffBasedWorldState) getMutable(blockHeader, false).orElse(null)) {
if (ws != null) {
final WorldStateProofProvider worldStateProofProvider =
new WorldStateProofProvider(
new WorldStateStorageCoordinator(ws.getWorldStateStorage()));
return mapper.apply(
worldStateProofProvider.getAccountProof(
ws.getWorldStateRootHash(), accountAddress, accountStorageKeys));
}
} catch (Exception ex) {
LOG.error("failed proof query for " + blockHeader.getBlockHash().toShortHexString(), ex);
}
return Optional.empty();
}
@Override
public Optional<Bytes> getNodeData(final Hash hash) {
return Optional.empty();
}
@Override
public void close() {
try {
worldStateKeyValueStorage.close();
} catch (Exception e) {
// no op
}
}
}

@ -77,7 +77,7 @@ public abstract class DiffBasedCachedWorldStorageManager implements StorageSubsc
public synchronized void addCachedLayer(
final BlockHeader blockHeader,
final Hash worldStateRootHash,
final DiffBasedWorldState<?> forWorldState) {
final DiffBasedWorldState forWorldState) {
final Optional<DiffBasedCachedWorldView> cachedDiffBasedWorldView =
Optional.ofNullable(this.cachedWorldStatesByHash.get(blockHeader.getBlockHash()));
if (cachedDiffBasedWorldView.isPresent()) {
@ -135,7 +135,7 @@ public abstract class DiffBasedCachedWorldStorageManager implements StorageSubsc
}
}
public Optional<DiffBasedWorldState<?>> getWorldState(final Hash blockHash) {
public Optional<DiffBasedWorldState> getWorldState(final Hash blockHash) {
if (cachedWorldStatesByHash.containsKey(blockHash)) {
// return a new worldstate using worldstate storage and an isolated copy of the updater
return Optional.ofNullable(cachedWorldStatesByHash.get(blockHash))
@ -154,7 +154,7 @@ public abstract class DiffBasedCachedWorldStorageManager implements StorageSubsc
return Optional.empty();
}
public Optional<DiffBasedWorldState<?>> getNearestWorldState(final BlockHeader blockHeader) {
public Optional<DiffBasedWorldState> getNearestWorldState(final BlockHeader blockHeader) {
LOG.atDebug()
.setMessage("getting nearest worldstate for {}")
.addArgument(blockHeader.toLogString())
@ -186,7 +186,7 @@ public abstract class DiffBasedCachedWorldStorageManager implements StorageSubsc
archive, createLayeredKeyValueStorage(storage), evmConfiguration));
}
public Optional<DiffBasedWorldState<?>> getHeadWorldState(
public Optional<DiffBasedWorldState> getHeadWorldState(
final Function<Hash, Optional<BlockHeader>> hashBlockHeaderFunction) {
LOG.atDebug().setMessage("getting head worldstate").log();
@ -233,7 +233,7 @@ public abstract class DiffBasedCachedWorldStorageManager implements StorageSubsc
this.cachedWorldStatesByHash.clear();
}
public abstract DiffBasedWorldState<?> createWorldState(
public abstract DiffBasedWorldState createWorldState(
final BonsaiWorldStateProvider archive,
final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage,
final EvmConfiguration evmConfiguration);

@ -67,7 +67,7 @@ public class TrieLogManager {
final DiffBasedWorldStateUpdateAccumulator<?> localUpdater,
final Hash forWorldStateRootHash,
final BlockHeader forBlockHeader,
final DiffBasedWorldState<?> forWorldState) {
final DiffBasedWorldState forWorldState) {
// do not overwrite a trielog layer that already exists in the database.
// if it's only in memory we need to save it
// for example, in case of reorg we don't replace a trielog layer

@ -19,6 +19,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import java.util.Comparator;
import java.util.Optional;
@ -29,7 +30,6 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ -23,15 +23,15 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StorageSlotKey;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiSnapshotWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateLayerStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.trie.diffbased.common.StorageSubscriber;
import org.hyperledger.besu.ethereum.trie.diffbased.common.cache.DiffBasedCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.DiffBasedWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateKeyValueStorage;
import org.hyperledger.besu.evm.account.Account;
@ -51,24 +51,23 @@ import org.apache.tuweni.units.bigints.UInt256;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class DiffBasedWorldState<
WORLDSTATE_STORAGE extends DiffBasedWorldStateKeyValueStorage>
public abstract class DiffBasedWorldState
implements MutableWorldState, DiffBasedWorldView, StorageSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(DiffBasedWorldState.class);
protected WORLDSTATE_STORAGE worldStateKeyValueStorage;
protected final BonsaiCachedWorldStorageManager cachedWorldStorageManager;
protected DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage;
protected final DiffBasedCachedWorldStorageManager cachedWorldStorageManager;
protected final TrieLogManager trieLogManager;
protected BonsaiWorldStateUpdateAccumulator accumulator;
protected DiffBasedWorldStateUpdateAccumulator<?> accumulator;
protected Hash worldStateRootHash;
protected Hash worldStateBlockHash;
protected boolean isFrozen;
protected DiffBasedWorldState(
final WORLDSTATE_STORAGE worldStateKeyValueStorage,
final BonsaiCachedWorldStorageManager cachedWorldStorageManager,
final DiffBasedWorldStateKeyValueStorage worldStateKeyValueStorage,
final DiffBasedCachedWorldStorageManager cachedWorldStorageManager,
final TrieLogManager trieLogManager) {
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
this.worldStateRootHash =
@ -130,11 +129,11 @@ public abstract class DiffBasedWorldState<
}
@Override
public WORLDSTATE_STORAGE getWorldStateStorage() {
public DiffBasedWorldStateKeyValueStorage getWorldStateStorage() {
return worldStateKeyValueStorage;
}
public BonsaiWorldStateUpdateAccumulator getAccumulator() {
public DiffBasedWorldStateUpdateAccumulator<?> getAccumulator() {
return accumulator;
}

@ -23,7 +23,6 @@ import org.hyperledger.besu.datatypes.StorageSlotKey;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiAccount;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedAccount;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedValue;
@ -582,9 +581,10 @@ public abstract class DiffBasedWorldStateUpdateAccumulator<ACCOUNT extends DiffB
"Expected to create account, but the account exists. Address=%s", address));
}
} else {
assertCloseEnoughForDiffing(accountValue.getUpdated(),
expectedValue,
"Address=" + address + " Prior Value in Rolling Change");
assertCloseEnoughForDiffing(
accountValue.getUpdated(),
expectedValue,
"Address=" + address + " Prior Value in Rolling Change");
}
if (replacementValue == null) {
if (accountValue.getPrior() == null) {
@ -807,6 +807,6 @@ public abstract class DiffBasedWorldStateUpdateAccumulator<ACCOUNT extends DiffB
protected abstract ACCOUNT createAccount(
final DiffBasedWorldView context, final UpdateTrackingAccount<ACCOUNT> tracked);
protected abstract void assertCloseEnoughForDiffing(final ACCOUNT source, final AccountValue account, final String context);
protected abstract void assertCloseEnoughForDiffing(
final ACCOUNT source, final AccountValue account, final String context);
}

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain;
import static org.mockito.ArgumentMatchers.any;
@ -65,7 +65,6 @@ import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;

@ -14,7 +14,7 @@
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.BLOCKCHAIN;
@ -36,15 +36,14 @@ import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.trielog.TrieLogFactoryImpl;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static org.assertj.core.api.Assertions.assertThat;

@ -14,7 +14,7 @@
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@ -29,12 +29,11 @@ import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.trielog.TrieLogFactoryImpl;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.log.LogsBloomFilter;

@ -14,7 +14,7 @@
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai;
import static com.google.common.base.Preconditions.checkArgument;
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.ACCOUNT_INFO_STATE;
@ -25,12 +25,11 @@ import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIden
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.trielog.TrieLogFactoryImpl;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;

@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie.bonsai.storage;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE;
@ -35,7 +35,6 @@ import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.StorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai.trielog;
package org.hyperledger.besu.ethereum.trie.diffbased.bonsai.trielog;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@ -24,7 +24,6 @@ import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.trielog.TrieLogFactoryImpl;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;

@ -13,14 +13,13 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai.trielog;
package org.hyperledger.besu.ethereum.trie.diffbased.common.trielog;
import org.hyperledger.besu.datatypes.AccountValue;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StorageSlotKey;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import java.util.Optional;

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.bonsai.trielog;
package org.hyperledger.besu.ethereum.trie.diffbased.common.trielog;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner.noOpTrieLogPruner;
@ -24,7 +24,6 @@ import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;

@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie.bonsai.trielog;
package org.hyperledger.besu.ethereum.trie.diffbased.common.trielog;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@ -27,7 +27,6 @@ import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;
import java.util.Optional;
import java.util.stream.Stream;

@ -22,10 +22,11 @@ import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMer
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiPreImageProxy;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.cache.DiffBasedCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogAddedEvent;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogPruner;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.DiffBasedWorldStateUpdateAccumulator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
@ -53,7 +54,7 @@ public class BonsaiReferenceTestWorldState extends BonsaiWorldState
protected BonsaiReferenceTestWorldState(
final BonsaiReferenceTestWorldStateStorage worldStateKeyValueStorage,
final BonsaiCachedMerkleTrieLoader cachedMerkleTrieLoader,
final BonsaiCachedWorldStorageManager cachedWorldStorageManager,
final DiffBasedCachedWorldStorageManager cachedWorldStorageManager,
final TrieLogManager trieLogManager,
final BonsaiPreImageProxy preImageProxy,
final EvmConfiguration evmConfiguration) {
@ -80,8 +81,7 @@ public class BonsaiReferenceTestWorldState extends BonsaiWorldState
@Override
public ReferenceTestWorldState copy() {
var layerCopy =
new BonsaiReferenceTestWorldStateStorage(worldStateKeyValueStorage, preImageProxy);
var layerCopy = new BonsaiReferenceTestWorldStateStorage(getWorldStateStorage(), preImageProxy);
return new BonsaiReferenceTestWorldState(
layerCopy,
cachedMerkleTrieLoader,
@ -167,7 +167,7 @@ public class BonsaiReferenceTestWorldState extends BonsaiWorldState
public void addCachedLayer(
final BlockHeader blockHeader,
final Hash worldStateRootHash,
final DiffBasedWorldState<?> forWorldState) {
final DiffBasedWorldState forWorldState) {
// reference test world states are not cached
}
@ -177,17 +177,17 @@ public class BonsaiReferenceTestWorldState extends BonsaiWorldState
}
@Override
public Optional<DiffBasedWorldState<?>> getWorldState(final Hash blockHash) {
public Optional<DiffBasedWorldState> getWorldState(final Hash blockHash) {
return Optional.empty();
}
@Override
public Optional<DiffBasedWorldState<?>> getNearestWorldState(final BlockHeader blockHeader) {
public Optional<DiffBasedWorldState> getNearestWorldState(final BlockHeader blockHeader) {
return Optional.empty();
}
@Override
public Optional<DiffBasedWorldState<?>> getHeadWorldState(
public Optional<DiffBasedWorldState> getHeadWorldState(
final Function<Hash, Optional<BlockHeader>> hashBlockHeaderFunction) {
return Optional.empty();
}
@ -210,7 +210,7 @@ public class BonsaiReferenceTestWorldState extends BonsaiWorldState
final DiffBasedWorldStateUpdateAccumulator<?> localUpdater,
final Hash forWorldStateRootHash,
final BlockHeader forBlockHeader,
final DiffBasedWorldState<?> forWorldState) {
final DiffBasedWorldState forWorldState) {
// notify trie log added observers, synchronously
TrieLog trieLog = trieLogFactory.create(localUpdater, forBlockHeader);
trieLogObservers.forEach(o -> o.onTrieLogAdded(new TrieLogAddedEvent(trieLog)));

Loading…
Cancel
Save