Skip to content

Commit

Permalink
renaming state trie account for verkle integration (#7907)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim Taam <[email protected]>
  • Loading branch information
matkt authored Nov 25, 2024
1 parent 788b29f commit 4620477
Show file tree
Hide file tree
Showing 31 changed files with 218 additions and 157 deletions.
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 @@ -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.MiningConfiguration;
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 @@ -73,7 +73,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 @@ -85,7 +85,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
@@ -0,0 +1,81 @@
/*
* 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.ethereum.trie.common;

import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.datatypes.AccountValue;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;

/** Represents the raw values associated with an account in the world state trie. */
public abstract class AbstractStateTrieAccountValue implements AccountValue {

protected final long nonce;
protected final Wei balance;
protected final Hash codeHash;

public AbstractStateTrieAccountValue(final long nonce, final Wei balance, final Hash codeHash) {
checkNotNull(balance, "balance cannot be null");
checkNotNull(codeHash, "codeHash cannot be null");
this.nonce = nonce;
this.balance = balance;
this.codeHash = codeHash;
}

/**
* The account nonce, that is the number of transactions sent from that account.
*
* @return the account nonce.
*/
@Override
public long getNonce() {
return nonce;
}

/**
* The available balance of that account.
*
* @return the balance, in Wei, of the account.
*/
@Override
public Wei getBalance() {
return balance;
}

/**
* The hash of the EVM bytecode associated with this account.
*
* @return the hash of the account code (which may be {@link Hash#EMPTY}).
*/
@Override
public Hash getCodeHash() {
return codeHash;
}

/**
* The hash of the root of the storage trie associated with this account.
*
* @return the hash of the root node of the storage trie.
*/
@Override
public Hash getStorageRoot() {
return Hash.EMPTY_TRIE_HASH;
}

@Override
public abstract void writeTo(final RLPOutput out);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.worldstate;
package org.hyperledger.besu.ethereum.trie.common;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -26,43 +26,17 @@

import org.apache.tuweni.bytes.Bytes32;

/** Represents the raw values associated with an account in the world state trie. */
public class StateTrieAccountValue implements AccountValue {
/** Represents the raw values associated with an account in the world state patricia merkle trie. */
public class PmtStateTrieAccountValue extends AbstractStateTrieAccountValue
implements AccountValue {

protected final long nonce;
protected final Wei balance;
protected final Hash storageRoot;
protected final Hash codeHash;

public StateTrieAccountValue(
public PmtStateTrieAccountValue(
final long nonce, final Wei balance, final Hash storageRoot, final Hash codeHash) {
checkNotNull(balance, "balance cannot be null");
super(nonce, balance, codeHash);
checkNotNull(storageRoot, "storageRoot cannot be null");
checkNotNull(codeHash, "codeHash cannot be null");
this.nonce = nonce;
this.balance = balance;
this.storageRoot = storageRoot;
this.codeHash = codeHash;
}

/**
* The account nonce, that is the number of transactions sent from that account.
*
* @return the account nonce.
*/
@Override
public long getNonce() {
return nonce;
}

/**
* The available balance of that account.
*
* @return the balance, in Wei, of the account.
*/
@Override
public Wei getBalance() {
return balance;
}

/**
Expand All @@ -75,25 +49,15 @@ public Hash getStorageRoot() {
return storageRoot;
}

/**
* The hash of the EVM bytecode associated with this account.
*
* @return the hash of the account code (which may be {@link Hash#EMPTY}).
*/
@Override
public Hash getCodeHash() {
return codeHash;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final StateTrieAccountValue that = (StateTrieAccountValue) o;
PmtStateTrieAccountValue that = (PmtStateTrieAccountValue) o;
return nonce == that.nonce
&& balance.equals(that.balance)
&& storageRoot.equals(that.storageRoot)
&& codeHash.equals(that.codeHash);
&& Objects.equals(balance, that.balance)
&& Objects.equals(storageRoot, that.storageRoot)
&& Objects.equals(codeHash, that.codeHash);
}

@Override
Expand All @@ -109,11 +73,10 @@ public void writeTo(final RLPOutput out) {
out.writeUInt256Scalar(balance);
out.writeBytes(storageRoot);
out.writeBytes(codeHash);

out.endList();
}

public static StateTrieAccountValue readFrom(final RLPInput in) {
public static PmtStateTrieAccountValue readFrom(final RLPInput in) {
in.enterList();

final long nonce = in.readLongScalar();
Expand All @@ -132,9 +95,9 @@ public static StateTrieAccountValue readFrom(final RLPInput in) {
} else {
codeHash = in.readBytes32();
}

in.leaveList();

return new StateTrieAccountValue(nonce, balance, Hash.wrap(storageRoot), Hash.wrap(codeHash));
return new PmtStateTrieAccountValue(
nonce, balance, Hash.wrap(storageRoot), Hash.wrap(codeHash));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
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;
Expand All @@ -26,7 +27,6 @@
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldStateConfig;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.plugin.ServiceManager;

Expand Down Expand Up @@ -120,7 +120,7 @@ public void prepareStateHealing(final Address address, final Bytes location) {
accountTrie
.get(accountHash)
.map(RLP::input)
.map(StateTrieAccountValue::readFrom)
.map(PmtStateTrieAccountValue::readFrom)
.ifPresent(
account -> {
final StoredMerklePatriciaTrie<Bytes, Bytes> storageTrie =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
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.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.flat.BonsaiFlatDbStrategy;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.flat.BonsaiFlatDbStrategyProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.flat.FlatDbStrategy;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateKeyValueStorage;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
import org.hyperledger.besu.plugin.services.MetricsSystem;
Expand Down Expand Up @@ -135,7 +135,7 @@ public Optional<Bytes> getStorageValueByStorageSlotKey(
getAccount(accountHash)
.map(
b ->
StateTrieAccountValue.readFrom(
PmtStateTrieAccountValue.readFrom(
org.hyperledger.besu.ethereum.rlp.RLP.input(b))
.getStorageRoot()),
accountHash,
Expand Down
Loading

0 comments on commit 4620477

Please sign in to comment.