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

Verkle 645 #7004

Merged
merged 13 commits into from Apr 29, 2024
Expand Up @@ -6,12 +6,8 @@

public interface AccessWitness {

void merge(AccessWitness other);

List<Address> keys();

AccessWitness copy();

long touchAndChargeProofOfAbsence(Address address);

long touchAndChargeValueTransfer(Address caller, Address target);
Expand Down
Expand Up @@ -21,10 +21,10 @@

import org.hyperledger.besu.collections.trie.BytesTrieSet;
import org.hyperledger.besu.datatypes.AccessListEntry;
import org.hyperledger.besu.datatypes.AccessWitness;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.AccessWitness;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.feemarket.CoinbaseFeePriceCalculator;
Expand Down Expand Up @@ -337,7 +337,7 @@ public TransactionProcessingResult processTransaction(
if (warmCoinbase) {
addressList.add(miningBeneficiary);
}
final AccessWitness accessWitness = new AccessWitness();
final AccessWitness accessWitness = gasCalculator.newAccessWitness();
final long intrinsicGas =
gasCalculator.transactionIntrinsicGasCost(
transaction.getPayload(), transaction.isContractCreation());
Expand Down
Expand Up @@ -171,7 +171,7 @@ public static GetBlockHeadersData create(

public static GetBlockHeadersData create(
final Hash hash, final int maxHeaders, final int skip, final boolean reverse) {
//System.out.println("create " + hash + " " + maxHeaders);
// System.out.println("create " + hash + " " + maxHeaders);
return new GetBlockHeadersData(
Optional.of(hash), OptionalLong.empty(), maxHeaders, skip, reverse);
}
Expand Down
4 changes: 1 addition & 3 deletions evm/src/main/java/org/hyperledger/besu/evm/EVM.java
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.evm;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hyperledger.besu.evm.operation.PushOperation.PUSH_BASE;
import static org.hyperledger.besu.evm.operation.SwapOperation.SWAP_BASE;

import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -53,7 +52,6 @@
import org.hyperledger.besu.evm.operation.OrOperation;
import org.hyperledger.besu.evm.operation.PopOperation;
import org.hyperledger.besu.evm.operation.Push0Operation;
import org.hyperledger.besu.evm.operation.PushOperation;
import org.hyperledger.besu.evm.operation.SDivOperation;
import org.hyperledger.besu.evm.operation.SGtOperation;
import org.hyperledger.besu.evm.operation.SLtOperation;
Expand Down Expand Up @@ -184,7 +182,6 @@ public void runToHalt(final MessageFrame frame, final OperationTracer tracing) {
var operationTracer = tracing == OperationTracer.NO_TRACING ? null : tracing;
byte[] code = frame.getCode().getBytes().toArrayUnsafe();


Operation[] operationArray = operations.getOperations();
while (frame.getState() == MessageFrame.State.CODE_EXECUTING) {
Operation currentOperation;
Expand Down Expand Up @@ -292,6 +289,7 @@ public void runToHalt(final MessageFrame frame, final OperationTracer tracing) {
} catch (final UnderflowException ue) {
result = UNDERFLOW_RESPONSE;
}

final ExceptionalHaltReason haltReason = result.getHaltReason();
if (haltReason != null) {
LOG.trace("MessageFrame evaluation halted because of {}", haltReason);
Expand Down
Expand Up @@ -120,7 +120,8 @@ public long getWarmStorageReadCost() {

// Zeroed out old costs
@Override
public long getBalanceOperationGasCost(final MessageFrame frame) {
public long getBalanceOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return 0L;
}

Expand All @@ -136,7 +137,8 @@ public long extCodeHashOperationGasCost(
}

@Override
public long getExtCodeSizeOperationGasCost() {
public long getExtCodeSizeOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return 0L;
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.stateless.Eip4762AccessWitness;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -258,11 +259,15 @@ public long pushOperationGasCost(
}

@Override
public long getBalanceOperationGasCost(final MessageFrame frame) {
return frame
.getAccessWitness()
.touchAddressOnWriteAndComputeGas(
frame.getContractAddress(), UInt256.ZERO, BALANCE_LEAF_KEY);
public long getBalanceOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return maybeAddress
.map(
address ->
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(address, UInt256.ZERO, BALANCE_LEAF_KEY))
.orElse(0L);
}

@Override
Expand All @@ -277,6 +282,28 @@ public long extCodeHashOperationGasCost(
.orElse(0L);
}

@Override
public long getExtCodeSizeOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return maybeAddress
.map(
address -> {
long gasCost =
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(address, UInt256.ZERO, VERSION_LEAF_KEY);
gasCost =
clampedAdd(
gasCost,
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(
address, UInt256.ZERO, CODE_SIZE_LEAF_KEY));
return gasCost;
})
.orElse(0L);
}

@Override
public long selfDestructOperationGasCost(
final MessageFrame frame,
Expand Down Expand Up @@ -307,12 +334,10 @@ public long selfDestructOperationGasCost(
public long getSloadOperationGasCost(final MessageFrame frame, final UInt256 key) {
AccessWitness accessWitness = frame.getAccessWitness();
List<UInt256> treeIndexes = accessWitness.getStorageSlotTreeIndexes(key);
long gasCost =
frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(
frame.getContractAddress(), treeIndexes.get(0), treeIndexes.get(1));
return gasCost;
return frame
.getAccessWitness()
.touchAddressOnReadAndComputeGas(
frame.getContractAddress(), treeIndexes.get(0), treeIndexes.get(1));
}

@Override
Expand Down Expand Up @@ -342,4 +367,9 @@ public long completedCreateContractGasCost(final MessageFrame frame) {
.getAccessWitness()
.touchAndChargeContractCreateCompleted(frame.getContractAddress());
}

@Override
public AccessWitness newAccessWitness() {
return new Eip4762AccessWitness();
}
}
Expand Up @@ -235,11 +235,6 @@ public long pushOperationGasCost(
return getVeryLowTierGasCost();
}

@Override
public long extCodeSizeOperationGasCost(final MessageFrame frame) {
return getBaseTierGasCost();
}

@SuppressWarnings("removal")
@Override
public long callOperationGasCost(
Expand Down Expand Up @@ -410,7 +405,8 @@ public long memoryExpansionGasCost(
}

@Override
public long getBalanceOperationGasCost(final MessageFrame frame) {
public long getBalanceOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return BALANCE_OPERATION_GAS_COST;
}

Expand Down Expand Up @@ -462,7 +458,8 @@ public long extCodeHashOperationGasCost(
}

@Override
public long getExtCodeSizeOperationGasCost() {
public long getExtCodeSizeOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return extCodeBaseGasCost();
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.stateless.NoopAccessWitness;
import org.hyperledger.besu.evm.operation.BalanceOperation;
import org.hyperledger.besu.evm.operation.BlockHashOperation;
import org.hyperledger.besu.evm.operation.ExpOperation;
Expand Down Expand Up @@ -320,9 +321,6 @@ long codeCopyOperationGasCost(

long pushOperationGasCost(MessageFrame frame, long codeOffset, long readSize, long codeSize);


long extCodeSizeOperationGasCost(MessageFrame frame);

/**
* Returns the cost of expanding memory for the specified access.
*
Expand All @@ -338,9 +336,11 @@ long codeCopyOperationGasCost(
/**
* Returns the cost for executing a {@link BalanceOperation}.
*
* @param frame The current frame
* @param maybeAddress targeted address
* @return the cost for executing the balance operation
*/
long getBalanceOperationGasCost(MessageFrame frame);
long getBalanceOperationGasCost(MessageFrame frame, final Optional<Address> maybeAddress);

/**
* Returns the cost for executing a {@link BlockHashOperation}.
Expand Down Expand Up @@ -379,16 +379,20 @@ long extCodeCopyOperationGasCost(
/**
* Returns the cost for executing a {@link ExtCodeHashOperation}.
*
* @param frame The current frame
* @param maybeAddress targeted address
* @return the cost for executing the external code hash operation
*/
long extCodeHashOperationGasCost(final MessageFrame frame, Optional<Address> address);
long extCodeHashOperationGasCost(final MessageFrame frame, Optional<Address> maybeAddress);

/**
* Returns the cost for executing a {@link ExtCodeSizeOperation}.
*
* @param frame The current frame
* @param maybeAddress targeted address
* @return the cost for executing the external code size operation
*/
long getExtCodeSizeOperationGasCost();
long getExtCodeSizeOperationGasCost(MessageFrame frame, Optional<Address> maybeAddress);

/**
* Returns the cost for executing a {@link JumpDestOperation}.
Expand Down Expand Up @@ -684,4 +688,7 @@ default long computeBaseAccessEventsCost(
return 0L;
}

default AccessWitness newAccessWitness() {
return new NoopAccessWitness();
}
}
Expand Up @@ -130,7 +130,8 @@ public long getSloadOperationGasCost(final MessageFrame frame, final UInt256 key

@Override
// As per https://eips.ethereum.org/EIPS/eip-1884
public long getBalanceOperationGasCost(final MessageFrame frame) {
public long getBalanceOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return BALANCE_OPERATION_GAS_COST;
}

Expand Down
Expand Up @@ -22,6 +22,8 @@
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.Words;

import java.util.Optional;

import org.apache.tuweni.units.bigints.UInt256;

/** The Tangerine whistle gas calculator. */
Expand All @@ -40,7 +42,8 @@ public class TangerineWhistleGasCalculator extends HomesteadGasCalculator {
private static final long SLOAD_OPERATION_GAS_COST = 200L;

@Override
public long getBalanceOperationGasCost(final MessageFrame frame) {
public long getBalanceOperationGasCost(
final MessageFrame frame, final Optional<Address> maybeAddress) {
return BALANCE_OPERATION_GAS_COST;
}

Expand Down
@@ -1,4 +1,4 @@
package org.hyperledger.besu.ethereum.core;
package org.hyperledger.besu.evm.gascalculator.stateless;

public class AccessEvents {
private boolean branchRead;
Expand Down
@@ -1,4 +1,4 @@
package org.hyperledger.besu.ethereum.core;
package org.hyperledger.besu.evm.gascalculator.stateless;

import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.BALANCE_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_KECCAK_LEAF_KEY;
Expand All @@ -20,7 +20,7 @@

import org.apache.tuweni.units.bigints.UInt256;

public class AccessWitness implements org.hyperledger.besu.datatypes.AccessWitness {
public class Eip4762AccessWitness implements org.hyperledger.besu.datatypes.AccessWitness {

private static final long WITNESS_BRANCH_READ_COST = 1900;
private static final long WITNESS_CHUNK_READ_COST = 200;
Expand All @@ -34,42 +34,23 @@ public class AccessWitness implements org.hyperledger.besu.datatypes.AccessWitne
private final Map<BranchAccessKey, Byte> branches;
private final Map<ChunkAccessKey, Byte> chunks;

public AccessWitness() {
public Eip4762AccessWitness() {
this(new HashMap<>(), new HashMap<>());
}

public AccessWitness(
public Eip4762AccessWitness(
final Map<BranchAccessKey, Byte> branches, final Map<ChunkAccessKey, Byte> chunks) {
this.branches = branches;
this.chunks = chunks;
}

@Override
public void merge(final org.hyperledger.besu.datatypes.AccessWitness other) {
// TODO VERKLE
// for (BranchAccessKey k : other.getBranches.keySet()) {
// this.branches.put(k, (byte) (this.branches.get(k) | other.getBranches.get(k)));
// }
// for (Map.Entry<ChunkAccessKey, Byte> entry : other.getChunks.entrySet()) {
// this.chunks.put(entry.getKey(), (byte) (this.chunks.get(entry.getKey()) |
// entry.getValue()));
// }
}

@Override
public List<Address> keys() {
return this.chunks.keySet().stream()
.map(chunkAccessKey -> chunkAccessKey.branchAccessKey().address())
.toList();
}

@Override
public AccessWitness copy() {
AccessWitness naw = new AccessWitness();
naw.merge(this);
return naw;
}

@Override
public long touchAndChargeProofOfAbsence(final Address address) {
long gas = 0;
Expand Down Expand Up @@ -359,7 +340,7 @@ public AccessEvents touchAddress(
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccessWitness that = (AccessWitness) o;
Eip4762AccessWitness that = (Eip4762AccessWitness) o;
return Objects.equals(branches, that.branches) && Objects.equals(chunks, that.chunks);
}

Expand Down