Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork/stem based flat db #7778

Draft
wants to merge 11 commits into
base: verkle
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,6 @@ private String generateConfigurationOverview() {
if (rocksDBPlugin.isHighSpecEnabled()) {
builder.setHighSpecEnabled();
}

if (DataStorageFormat.BONSAI.equals(getDataStorageConfiguration().getDataStorageFormat())) {
final DiffBasedSubStorageConfiguration subStorageConfiguration =
getDataStorageConfiguration().getDiffBasedSubStorageConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class DataStorageOptions implements CLIOptions<DataStorageConfiguration>
private DiffBasedSubStorageOptions diffBasedSubStorageOptions =
DiffBasedSubStorageOptions.create();

/** Options specific to verkle storage modes. */
@Mixin private VerkleSubStorageOptions verkleSubStorageOptions = VerkleSubStorageOptions.create();

/** Default Constructor. */
DataStorageOptions() {}

Expand All @@ -76,6 +79,7 @@ public static DataStorageOptions create() {
*/
public void validate(final CommandLine commandLine) {
diffBasedSubStorageOptions.validate(commandLine, dataStorageFormat);
verkleSubStorageOptions.validate(commandLine, dataStorageFormat);
}

/**
Expand All @@ -90,6 +94,8 @@ public static DataStorageOptions fromConfig(final DataStorageConfiguration domai
dataStorageOptions.receiptCompactionEnabled = domainObject.getReceiptCompactionEnabled();
dataStorageOptions.diffBasedSubStorageOptions =
DiffBasedSubStorageOptions.fromConfig(domainObject.getDiffBasedSubStorageConfiguration());
dataStorageOptions.verkleSubStorageOptions =
VerkleSubStorageOptions.fromConfig(domainObject.getVerkleSubStorageConfiguration());
return dataStorageOptions;
}

Expand All @@ -99,14 +105,16 @@ public DataStorageConfiguration toDomainObject() {
ImmutableDataStorageConfiguration.builder()
.dataStorageFormat(dataStorageFormat)
.receiptCompactionEnabled(receiptCompactionEnabled)
.diffBasedSubStorageConfiguration(diffBasedSubStorageOptions.toDomainObject());
.diffBasedSubStorageConfiguration(diffBasedSubStorageOptions.toDomainObject())
.verkleSubStorageConfiguration(verkleSubStorageOptions.toDomainObject());
return builder.build();
}

@Override
public List<String> getCLIOptions() {
final List<String> cliOptions = CommandLineUtils.getCLIOptions(this, new DataStorageOptions());
cliOptions.addAll(diffBasedSubStorageOptions.getCLIOptions());
cliOptions.addAll(verkleSubStorageOptions.getCLIOptions());
return cliOptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public static DiffBasedSubStorageOptions create() {
* to apply.
*/
public void validate(final CommandLine commandLine, final DataStorageFormat dataStorageFormat) {
if (DataStorageFormat.BONSAI == dataStorageFormat) {
if (DataStorageFormat.BONSAI == dataStorageFormat
|| DataStorageFormat.VERKLE == dataStorageFormat) {
if (limitTrieLogsEnabled) {
if (maxLayersToLoad < MINIMUM_TRIE_LOG_RETENTION_LIMIT) {
throw new CommandLine.ParameterException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright ConsenSys AG.
*
* 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.cli.options.storage;

import static org.hyperledger.besu.ethereum.worldstate.DiffBasedSubStorageConfiguration.DiffBasedUnstable.DEFAULT_STEM_FLAT_DB_ENABLED;

import org.hyperledger.besu.cli.options.CLIOptions;
import org.hyperledger.besu.cli.util.CommandLineUtils;
import org.hyperledger.besu.ethereum.worldstate.DiffBasedSubStorageConfiguration;
import org.hyperledger.besu.ethereum.worldstate.VerkleSubStorageConfiguration;
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;

import java.util.List;

import picocli.CommandLine;
import picocli.CommandLine.Option;

/** The Data storage CLI options. */
public class VerkleSubStorageOptions implements CLIOptions<DiffBasedSubStorageConfiguration> {

@CommandLine.ArgGroup(validate = false)
private final VerkleSubStorageOptions.Unstable unstableOptions = new Unstable();

/** Default Constructor. */
VerkleSubStorageOptions() {}

/** The unstable options for data storage. */
public static class Unstable {

@Option(
hidden = true,
names = {
"--Xverkle-stem-flat-db-enabled",
},
arity = "1",
description = "Enables stem flat database strategy for verkle. (default: ${DEFAULT-VALUE})")
private Boolean stemFlatDbEnabled = DEFAULT_STEM_FLAT_DB_ENABLED;

/** Default Constructor. */
Unstable() {}
}

/**
* Create data storage options.
*
* @return the data storage options
*/
public static VerkleSubStorageOptions create() {
return new VerkleSubStorageOptions();
}

/**
* Validates the data storage options
*
* @param commandLine the full commandLine to check all the options specified by the user
* @param dataStorageFormat the selected data storage format which determines the validation rules
* to apply.
*/
public void validate(final CommandLine commandLine, final DataStorageFormat dataStorageFormat) {
// no op
}

/**
* Converts to options from the configuration
*
* @param domainObject to be reversed
* @return the options that correspond to the configuration
*/
public static VerkleSubStorageOptions fromConfig(
final VerkleSubStorageConfiguration domainObject) {
final VerkleSubStorageOptions dataStorageOptions = VerkleSubStorageOptions.create();
dataStorageOptions.unstableOptions.stemFlatDbEnabled =
domainObject.getUnstable().getStemFlatDbEnabled();
return dataStorageOptions;
}

@Override
public final DiffBasedSubStorageConfiguration toDomainObject() {
return ImmutableVerkleSubStorageConfiguration.builder()
.unstable(
ImmutableVerkleSubStorageConfiguration.VerkleUnstable.builder()
.stemFlatDbEnabled(unstableOptions.stemFlatDbEnabled)
.build())
.build();
}

@Override
public List<String> getCLIOptions() {
return CommandLineUtils.getCLIOptions(this, new VerkleSubStorageOptions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.PersistVisitor;
import org.hyperledger.besu.ethereum.trie.RestoreVisitor;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.forest.ForestWorldStateArchive;
import org.hyperledger.besu.ethereum.trie.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.util.io.RollingFileReader;

import java.io.IOException;
Expand Down Expand Up @@ -192,8 +192,8 @@ private void restoreAccounts() throws IOException {
final Bytes accountRlp = accountInput.readBytes();
final Bytes code = accountInput.readBytes();

final StateTrieAccountValue trieAccount =
StateTrieAccountValue.readFrom(new BytesValueRLPInput(accountRlp, false, true));
final PmtStateTrieAccountValue trieAccount =
PmtStateTrieAccountValue.readFrom(new BytesValueRLPInput(accountRlp, false, true));
if (!trieAccount.getCodeHash().equals(Hash.hash(code))) {
throw new RuntimeException("Code hash doesn't match");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ public BlockProcessingResult validateBlock(final Block block) {
HeaderValidationMode.FULL,
HeaderValidationMode.NONE,
false);

return validationResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import org.hyperledger.besu.ethereum.rlp.RLPOutput;

import java.util.Optional;

/** The values of an account in the world state trie. */
public interface AccountValue {
/**
Expand Down Expand Up @@ -46,6 +48,13 @@ public interface AccountValue {
*/
Hash getCodeHash();

/**
* The size of the EVM bytecode associated with this account.
*
* @return the size of the account code (which may be {@link Optional#empty()}).
*/
Optional<Long> getCodeSize();

/**
* Writes the account value to the provided {@link RLPOutput}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public final JsonRpcResponse response(final JsonRpcRequestContext request) {
.setCause(t)
.log();
} else {
t.printStackTrace(System.out);
LOG.atError()
.setMessage("failed to exec consensus method {}, error: {}")
.addArgument(this.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
Optional.of(forkChoice.getHeadBlockHash() + " is an invalid block")));
}

System.out.println(forkChoice.getHeadBlockHash());
final Optional<BlockHeader> maybeNewHead =
mergeCoordinator.getOrSyncHeadByHash(
forkChoice.getHeadBlockHash(), forkChoice.getFinalizedBlockHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
newBlockHeader, new BlockBody(transactions, Collections.emptyList(), maybeWithdrawals));

if (maybeParentHeader.isEmpty()) {
LOG.atDebug()
LOG.atInfo()
.setMessage("Parent of block {} is not present, append it to backward sync")
.addArgument(block::toLogString)
.log();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -64,7 +64,8 @@ public GetProofResult(
public static GetProofResult buildGetProofResult(
final Address address, final WorldStateProof worldStateProof) {

final StateTrieAccountValue stateTrieAccountValue = worldStateProof.getStateTrieAccountValue();
final PmtStateTrieAccountValue stateTrieAccountValue =
worldStateProof.getStateTrieAccountValue();

final List<StorageEntryProof> storageEntries = new ArrayList<>();
worldStateProof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.trie.TrieIterator.State;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.util.io.RollingFileWriter;

import java.io.IOException;
Expand Down Expand Up @@ -241,8 +241,8 @@ private TrieIterator.State visitAccount(final Bytes32 nodeKey, final Node<Bytes>

backupStatus.currentAccount = nodeKey;
final Bytes nodeValue = node.getValue().orElse(Hash.EMPTY);
final StateTrieAccountValue account =
StateTrieAccountValue.readFrom(new BytesValueRLPInput(nodeValue, false));
final PmtStateTrieAccountValue account =
PmtStateTrieAccountValue.readFrom(new BytesValueRLPInput(nodeValue, false));

final Bytes code = worldStateKeyValueStorage.getCode(account.getCodeHash()).orElse(Bytes.EMPTY);
backupStatus.codeSize.addAndGet(code.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;

import java.util.Collections;
Expand Down Expand Up @@ -202,7 +202,7 @@ private GetProofResult generateWorldState() {

when(blockchainQueries.getWorldStateArchive()).thenReturn(archive);

final StateTrieAccountValue stateTrieAccountValue = mock(StateTrieAccountValue.class);
final PmtStateTrieAccountValue stateTrieAccountValue = mock(PmtStateTrieAccountValue.class);
when(stateTrieAccountValue.getBalance()).thenReturn(balance);
when(stateTrieAccountValue.getCodeHash()).thenReturn(codeHash);
when(stateTrieAccountValue.getNonce()).thenReturn(nonce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.Proof;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,22 +29,22 @@

public class WorldStateProof {

private final StateTrieAccountValue stateTrieAccountValue;
private final PmtStateTrieAccountValue stateTrieAccountValue;

private final Proof<Bytes> accountProof;

private final Map<UInt256, Proof<Bytes>> storageProofs;

public WorldStateProof(
final StateTrieAccountValue stateTrieAccountValue,
final PmtStateTrieAccountValue stateTrieAccountValue,
final Proof<Bytes> accountProof,
final SortedMap<UInt256, Proof<Bytes>> storageProofs) {
this.stateTrieAccountValue = stateTrieAccountValue;
this.accountProof = accountProof;
this.storageProofs = storageProofs;
}

public StateTrieAccountValue getStateTrieAccountValue() {
public PmtStateTrieAccountValue getStateTrieAccountValue() {
return stateTrieAccountValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.Proof;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.patricia.RemoveVisitor;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;

import java.util.Comparator;
Expand Down Expand Up @@ -72,7 +72,7 @@ public Optional<WorldStateProof> getAccountProof(
return accountProof
.getValue()
.map(RLP::input)
.map(StateTrieAccountValue::readFrom)
.map(PmtStateTrieAccountValue::readFrom)
.map(
account -> {
final SortedMap<UInt256, Proof<Bytes>> storageProofs =
Expand All @@ -84,7 +84,7 @@ public Optional<WorldStateProof> getAccountProof(

private SortedMap<UInt256, Proof<Bytes>> getStorageProofs(
final Hash accountHash,
final StateTrieAccountValue account,
final PmtStateTrieAccountValue account,
final List<UInt256> accountStorageKeys) {
final MerkleTrie<Bytes32, Bytes> storageTrie =
newAccountStorageTrie(accountHash, account.getStorageRoot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.verkle.cache.preloader.StemPreloader;
import org.hyperledger.besu.ethereum.trie.diffbased.verkle.storage.VerkleWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
Expand Down Expand Up @@ -86,7 +87,8 @@ public WorldStateKeyValueStorage createWorldStateStorage(
if (dataStorageConfiguration.getDataStorageFormat().equals(DataStorageFormat.BONSAI)) {
return new BonsaiWorldStateKeyValueStorage(this, metricsSystem, dataStorageConfiguration);
} else if (dataStorageConfiguration.getDataStorageFormat().equals(DataStorageFormat.VERKLE)) {
return new VerkleWorldStateKeyValueStorage(this, metricsSystem);
return new VerkleWorldStateKeyValueStorage(
this, new StemPreloader(), dataStorageConfiguration, metricsSystem);
} else {
return new ForestWorldStateKeyValueStorage(
getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.WORLD_STATE));
Expand Down
Loading
Loading