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

get code refactor for stateless #8298

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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 @@ -20,11 +20,12 @@
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiAccount;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.AccountState;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;

import java.util.Optional;

import graphql.schema.DataFetchingEnvironment;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;

Expand Down Expand Up @@ -115,7 +116,7 @@ public Long getTransactionCount() {
* @param environment the DataFetchingEnvironment
* @return the code of the account
*/
public Bytes getCode(final DataFetchingEnvironment environment) {
public Bytecode getCode(final DataFetchingEnvironment environment) {

if (account.get() instanceof BonsaiAccount) {
final BlockchainQueries query = getBlockchainQueries(environment);
Expand All @@ -125,7 +126,7 @@ public Bytes getCode(final DataFetchingEnvironment environment) {
ws -> Optional.of(ws.get(account.get().getAddress()).getCode()))
.get();
} else {
return account.map(AccountState::getCode).orElse(Bytes.EMPTY);
return account.map(AccountState::getCode).orElse(FullBytecode.EMPTY);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;

import graphql.schema.DataFetchingEnvironment;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/**
Expand Down Expand Up @@ -58,8 +59,8 @@ public Long getTransactionCount() {
}

@Override
public Bytes getCode(final DataFetchingEnvironment environment) {
return Bytes.EMPTY;
public Bytecode getCode(final DataFetchingEnvironment environment) {
return FullBytecode.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.util.io.RollingFileWriter;

import java.io.IOException;
Expand Down Expand Up @@ -243,7 +245,8 @@ private TrieIterator.State visitAccount(final Bytes32 nodeKey, final Node<Bytes>
final PmtStateTrieAccountValue account =
PmtStateTrieAccountValue.readFrom(new BytesValueRLPInput(nodeValue, false));

final Bytes code = worldStateKeyValueStorage.getCode(account.getCodeHash()).orElse(Bytes.EMPTY);
final Bytecode code =
worldStateKeyValueStorage.getCode(account.getCodeHash()).orElse(FullBytecode.EMPTY);
backupStatus.codeSize.addAndGet(code.size());

final BytesValueRLPOutput accountOutput = new BytesValueRLPOutput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.debug.TraceFrame;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Collections;
import java.util.Optional;
import java.util.function.Function;

import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -211,7 +212,7 @@ void shouldBeSuccessfulWhenTransactionsAndAccountArePresent() {

final String codeString =
"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063b27b880414610030575b";
final Bytes code = Bytes.fromHexString(codeString);
final Bytecode code = FullBytecode.fromHexString(codeString);
final long nonce = MAX_NONCE - 1;
final String balanceString = "0xffff";
final Wei balance = Wei.fromHexString(balanceString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.log.LogsBloomFilter;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

Expand Down Expand Up @@ -170,7 +171,8 @@ private static void writeAccountsTo(
final MutableAccount account = updater.createAccount(genesisAccount.address());
account.setNonce(genesisAccount.nonce());
account.setBalance(genesisAccount.balance());
account.setCode(genesisAccount.code());
account.setCode(
genesisAccount.code() == null ? null : FullBytecode.wrap(genesisAccount.code()));
genesisAccount.storage().forEach(account::setStorageValue);
});
updater.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.code.CodeInvalid;
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
Expand Down Expand Up @@ -411,7 +413,7 @@ public TransactionProcessingResult processTransaction(
final Address contractAddress =
Address.contractAddress(senderAddress, sender.getNonce() - 1L);

final Bytes initCodeBytes = transaction.getPayload();
final Bytecode initCodeBytes = FullBytecode.wrap(transaction.getPayload());
Code code = contractCreationProcessor.getCodeFromEVMForCreation(initCodeBytes);
initialFrame =
commonMessageFrameBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.privacy.group.FlexibleGroupManagement;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.data.PrivacyGenesis;
import org.hyperledger.besu.plugin.services.privacy.PrivacyGroupGenesisProvider;
Expand Down Expand Up @@ -80,7 +81,10 @@ public void applyGenesisToPrivateWorldState(

account.setNonce(genesisAccount.getNonce());
account.setBalance(Wei.fromQuantity(genesisAccount.getBalance()));
account.setCode(genesisAccount.getCode());
account.setCode(
genesisAccount.getCode() == null
? null
: FullBytecode.wrap(genesisAccount.getCode()));

genesisAccount.getStorage().forEach(account::setStorageValue);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.processor.AbstractMessageProcessor;
import org.hyperledger.besu.evm.tracing.OperationTracer;
Expand Down Expand Up @@ -138,7 +140,7 @@ public TransactionProcessingResult processTransaction(
previousNonce,
privacyGroupId);

final Bytes initCodeBytes = transaction.getPayload();
final Bytecode initCodeBytes = FullBytecode.wrap(transaction.getPayload());
Code code = contractCreationProcessor.getCodeFromEVMForCreation(initCodeBytes);
initialFrame =
commonMessageFrameBuilder
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

Expand Down Expand Up @@ -489,7 +490,7 @@ protected static void applyOverrides(final MutableAccount account, final StateOv
LOG.debug("applying overrides to state for account {}", account.getAddress());
override.getNonce().ifPresent(account::setNonce);
override.getBalance().ifPresent(account::setBalance);
override.getCode().ifPresent(code -> account.setCode(Bytes.fromHexString(code)));
override.getCode().ifPresent(code -> account.setCode(FullBytecode.fromHexString(code)));
override
.getState()
.ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.datatypes.StorageSlotKey;
import org.hyperledger.besu.ethereum.trie.diffbased.common.StorageSubscriber;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedSnapshotWorldStateKeyValueStorage;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.SnappableKeyValueStorage;
Expand Down Expand Up @@ -80,7 +81,7 @@ public Optional<Bytes> getAccount(final Hash accountHash) {
}

@Override
public Optional<Bytes> getCode(final Hash codeHash, final Hash accountHash) {
public Optional<Bytecode> getCode(final Hash codeHash, final Hash accountHash) {
return isClosedGet() ? Optional.empty() : super.getCode(codeHash, accountHash);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode;
import org.hyperledger.besu.ethereum.worldstate.WorldStateKeyValueStorage;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
Expand Down Expand Up @@ -84,9 +86,9 @@ public FlatDbMode getFlatDbMode() {
return flatDbStrategyProvider.getFlatDbMode();
}

public Optional<Bytes> getCode(final Hash codeHash, final Hash accountHash) {
public Optional<Bytecode> getCode(final Hash codeHash, final Hash accountHash) {
if (codeHash.equals(Hash.EMPTY)) {
return Optional.of(Bytes.EMPTY);
return Optional.of(FullBytecode.EMPTY);
} else {
return getFlatDbStrategy().getFlatCode(codeHash, accountHash, composedWorldStateStorage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedValue;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.TrieLogLayer;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.plugin.data.BlockHeader;
import org.hyperledger.besu.plugin.services.trielogs.TrieLog;
import org.hyperledger.besu.plugin.services.trielogs.TrieLogAccumulator;
Expand Down Expand Up @@ -112,7 +114,7 @@ public static void writeTo(final TrieLog layer, final RLPOutput output) {
writeRlp(accountChange, output, (o, sta) -> sta.writeTo(o));
}

final TrieLog.LogTuple<Bytes> codeChange = layer.getCodeChanges().get(address);
final TrieLog.LogTuple<Bytecode> codeChange = layer.getCodeChanges().get(address);
if (codeChange == null || codeChange.isUnchanged()) {
output.writeNull();
} else {
Expand Down Expand Up @@ -175,8 +177,10 @@ public static TrieLogLayer readFrom(final RLPInput input) {
input.skipNext();
} else {
input.enterList();
final Bytes oldCode = nullOrValue(input, RLPInput::readBytes);
final Bytes newCode = nullOrValue(input, RLPInput::readBytes);
final Bytecode oldCode =
nullOrValue(input, rlpInput -> FullBytecode.wrap(rlpInput.readBytes()));
final Bytecode newCode =
nullOrValue(input, rlpInput -> FullBytecode.wrap(rlpInput.readBytes()));
final boolean isCleared = getOptionalIsCleared(input);
input.leaveList();
newLayer.getCodeChanges().put(address, new DiffBasedValue<>(oldCode, newCode, isCleared));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.preload.StorageConsumingMap;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.plugin.services.storage.SegmentIdentifier;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction;
Expand Down Expand Up @@ -100,7 +101,7 @@ public BonsaiWorldState(
}

@Override
public Optional<Bytes> getCode(@Nonnull final Address address, final Hash codeHash) {
public Optional<Bytecode> getCode(@Nonnull final Address address, final Hash codeHash) {
return getWorldStateStorage().getCode(codeHash, address.addressHash());
}

Expand Down Expand Up @@ -203,7 +204,7 @@ protected void updateCode(
final BonsaiWorldStateUpdateAccumulator worldStateUpdater) {
maybeStateUpdater.ifPresent(
bonsaiUpdater -> {
for (final Map.Entry<Address, DiffBasedValue<Bytes>> codeUpdate :
for (final Map.Entry<Address, DiffBasedValue<Bytecode>> codeUpdate :
worldStateUpdater.getCodeToUpdate().entrySet()) {
final Bytes updatedCode = codeUpdate.getValue().getUpdated();
final Hash accountHash = codeUpdate.getKey().addressHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldView;
import org.hyperledger.besu.evm.ModificationNotAllowedException;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +39,7 @@ public abstract class DiffBasedAccount implements MutableAccount, AccountValue {
protected Hash codeHash;
protected long nonce;
protected Wei balance;
protected Bytes code;
protected Bytecode code;

protected final Map<UInt256, UInt256> updatedStorage = new HashMap<>();

Expand Down Expand Up @@ -100,7 +102,7 @@ public DiffBasedAccount(
final long nonce,
final Wei balance,
final Hash codeHash,
final Bytes code,
final Bytecode code,
final boolean mutable) {
this.context = context;
this.address = address;
Expand Down Expand Up @@ -149,15 +151,15 @@ public void setBalance(final Wei value) {
}

@Override
public Bytes getCode() {
public Bytecode getCode() {
if (code == null) {
code = context.getCode(address, codeHash).orElse(Bytes.EMPTY);
code = context.getCode(address, codeHash).orElse(FullBytecode.EMPTY);
}
return code;
}

@Override
public void setCode(final Bytes code) {
public void setCode(final Bytecode code) {
if (immutable) {
throw new ModificationNotAllowedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.CODE_STORAGE;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction;

Expand All @@ -26,11 +28,11 @@

public class AccountHashCodeStorageStrategy implements CodeStorageStrategy {
@Override
public Optional<Bytes> getFlatCode(
public Optional<Bytecode> getFlatCode(
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage) {
return storage
.get(CODE_STORAGE, accountHash.toArrayUnsafe())
.map(Bytes::wrap)
.map(FullBytecode::wrap)
.filter(b -> Hash.hash(b).equals(codeHash));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.CODE_STORAGE;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.evm.code.bytecode.FullBytecode;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction;

Expand All @@ -27,9 +29,9 @@
public class CodeHashCodeStorageStrategy implements CodeStorageStrategy {

@Override
public Optional<Bytes> getFlatCode(
public Optional<Bytecode> getFlatCode(
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage) {
return storage.get(CODE_STORAGE, codeHash.toArrayUnsafe()).map(Bytes::wrap);
return storage.get(CODE_STORAGE, codeHash.toArrayUnsafe()).map(FullBytecode::wrap);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.trie.diffbased.common.storage.flat;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.evm.code.bytecode.Bytecode;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction;

Expand All @@ -24,7 +25,7 @@

public interface CodeStorageStrategy {

Optional<Bytes> getFlatCode(
Optional<Bytecode> getFlatCode(
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage);

void putFlatCode(
Expand Down
Loading