Skip to content

Commit

Permalink
changes for state-root-less retrieval of code by hash in snapserver
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Oct 2, 2023
1 parent d27bdb6 commit 487095a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ public Optional<BonsaiWorldState> getHeadWorldState(
}

@Override
public Optional<CachedBonsaiWorldView> getStorageByRootHash(final Hash rootHash) {
return Optional.ofNullable(cachedWorldStatesByHash.get(rootHash));
public Optional<CachedBonsaiWorldView> getStorageByRootHash(final Optional<Hash> rootHash) {
return rootHash
.map(Optional::of)
.orElseGet(rootWorldStateStorage::getWorldStateBlockHash)
.map(cachedWorldStatesByHash::get);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode;
import org.hyperledger.besu.ethereum.worldstate.FlatWorldStateStorage;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
Expand All @@ -55,7 +54,7 @@
import org.slf4j.LoggerFactory;

@SuppressWarnings("unused")
public class BonsaiWorldStateKeyValueStorage implements FlatWorldStateStorage, AutoCloseable {
public class BonsaiWorldStateKeyValueStorage implements WorldStateStorage, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(BonsaiWorldStateKeyValueStorage.class);

// 0x776f726c64526f6f74
Expand Down Expand Up @@ -133,7 +132,6 @@ public FlatDbMode deriveFlatDbStrategy() {
return flatDbMode;
}

@Override
public FlatDbStrategy getFlatDbStrategy() {
if (flatDbStrategy == null) {
loadFlatDbStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void addCachedLayer(
Optional<BonsaiWorldState> getHeadWorldState(
final Function<Hash, Optional<BlockHeader>> hashBlockHeaderFunction);

Optional<CachedBonsaiWorldView> getStorageByRootHash(Hash rootHash);
Optional<CachedBonsaiWorldView> getStorageByRootHash(Optional<Hash> rootHash);

long getMaxLayersToLoad();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.bonsai.cache.CachedBonsaiWorldView;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.eth.manager.EthMessages;
import org.hyperledger.besu.ethereum.eth.messages.snap.AccountRangeMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.ByteCodesMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.GetAccountRangeMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.GetByteCodesMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.GetStorageRangeMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.GetTrieNodesMessage;
import org.hyperledger.besu.ethereum.eth.messages.snap.SnapV1;
Expand All @@ -29,13 +31,13 @@
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.worldstate.FlatWorldStateStorage;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.NavigableMap;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -64,9 +66,12 @@ class SnapServer {
StorageRangeMessage.create(new ArrayDeque<>(), Collections.emptyList());
private static final TrieNodesMessage EMPTY_TRIE_NODES_MESSAGE =
TrieNodesMessage.create(new ArrayList<>());
private static final ByteCodesMessage EMPTY_BYTE_CODES_MESSAGE =
ByteCodesMessage.create(new ArrayDeque<>());

private final EthMessages snapMessages;
private final Function<Hash, Optional<FlatWorldStateStorage>> worldStateStorageProvider;
private final Function<Optional<Hash>, Optional<BonsaiWorldStateKeyValueStorage>>
worldStateStorageProvider;

SnapServer(final EthMessages snapMessages, final WorldStateArchive archive) {
this.snapMessages = snapMessages;
Expand All @@ -81,7 +86,8 @@ class SnapServer {

SnapServer(
final EthMessages snapMessages,
final Function<Hash, Optional<FlatWorldStateStorage>> worldStateStorageProvider) {
final Function<Optional<Hash>, Optional<BonsaiWorldStateKeyValueStorage>>
worldStateStorageProvider) {
this.snapMessages = snapMessages;
this.worldStateStorageProvider = worldStateStorageProvider;
}
Expand Down Expand Up @@ -111,7 +117,7 @@ MessageData constructGetAccountRangeResponse(final MessageData message) {
var worldStateHash = getAccountRangeMessage.range(true).worldStateRootHash();

return worldStateStorageProvider
.apply(worldStateHash)
.apply(Optional.of(worldStateHash))
.map(
storage -> {
NavigableMap<Bytes32, Bytes> accounts =
Expand Down Expand Up @@ -159,7 +165,7 @@ MessageData constructGetStorageRangeResponse(final MessageData message) {
.log();

return worldStateStorageProvider
.apply(range.worldStateRootHash())
.apply(Optional.of(range.worldStateRootHash()))
.map(
storage -> {
// reusable predicate to limit by rec count and bytes:
Expand Down Expand Up @@ -207,7 +213,24 @@ MessageData constructGetStorageRangeResponse(final MessageData message) {

private MessageData constructGetBytecodesResponse(final MessageData message) {
// TODO implement once code is stored by hash
return ByteCodesMessage.create(new ArrayDeque<>());
final GetByteCodesMessage getByteCodesMessage = GetByteCodesMessage.readFrom(message);
final GetByteCodesMessage.CodeHashes codeHashes = getByteCodesMessage.codeHashes(true);

// there is no worldstate root or block header for us to use, so default to head. This
// can cause problems for self-destructed contracts pre-shanghai. for now since this impl
// is deferring to #5889, we can just get any flat code storage and know we are not deleting
// code for now.
return worldStateStorageProvider
.apply(Optional.empty())
.map(
storage -> {
List<Bytes> codeBytes = new ArrayDeque<>();
for (Bytes32 codeHash : codeHashes.hashes()) {
storage.getCode(codeHash, null).ifPresent(codeBytes::add);
}
return ByteCodesMessage.create(codeBytes);
})
.orElse(EMPTY_BYTE_CODES_MESSAGE);
}

MessageData constructGetTrieNodesResponse(final MessageData message) {
Expand All @@ -220,14 +243,14 @@ MessageData constructGetTrieNodesResponse(final MessageData message) {
.addArgument(() -> triePaths.paths().size())
.log();

//TODO: implement limits
// TODO: implement limits
return worldStateStorageProvider
.apply(triePaths.worldStateRootHash())
.apply(Optional.of(triePaths.worldStateRootHash()))
.map(
storage -> {
ArrayList<Bytes> trieNodes = new ArrayList<>();
for (var triePath : triePaths.paths()) {
// first element is paths is account
// first element in paths is account
if (triePath.size() == 1) {
// if there is only one path, presume it should be compact encoded account path
storage.getTrieNodeUnsafe(triePath.get(0)).ifPresent(trieNodes::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Optional<BonsaiWorldState> getHeadWorldState(
}

@Override
public Optional<CachedBonsaiWorldView> getStorageByRootHash(final Hash rootHash) {
public Optional<CachedBonsaiWorldView> getStorageByRootHash(final Optional<Hash> rootHash) {
return Optional.empty();
}

Expand Down

0 comments on commit 487095a

Please sign in to comment.