Skip to content

Commit

Permalink
fix storage slot key generation and clean code
Browse files Browse the repository at this point in the history
Signed-off-by: Karim Taam <[email protected]>
  • Loading branch information
matkt committed May 17, 2024
1 parent c973906 commit 87dfbcd
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.datatypes.AccessListEntry;
import org.hyperledger.besu.datatypes.AccessWitness;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
Expand All @@ -44,14 +45,22 @@
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Deque;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import io.vertx.core.json.JsonObject;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.slf4j.Logger;
Expand Down Expand Up @@ -469,10 +478,6 @@ public TransactionProcessingResult processTransaction(
.addArgument(sender.getBalance().toShortHexString())
.log();
final long gasUsedByTransaction = transaction.getGasLimit() - initialFrame.getRemainingGas();
LOG.info(
"Gas used by transaction({}): {}",
transaction.getHash().toShortHexString(),
gasUsedByTransaction);
operationTracer.traceEndTransaction(
worldUpdater,
transaction,
Expand All @@ -482,6 +487,8 @@ public TransactionProcessingResult processTransaction(
gasUsedByTransaction,
0L);

// checkTransactionGas(transaction.getHash(), gasUsedByTransaction);

// update the coinbase
final var coinbase = worldState.getOrCreate(miningBeneficiary);
final long usedGas = transaction.getGasLimit() - refundedGas;
Expand Down Expand Up @@ -552,6 +559,43 @@ public TransactionProcessingResult processTransaction(
}
}

public static void checkTransactionGas(final Hash transactionHash, final long expectedGas) {
String url =
"https://rpc.verkle-gen-devnet-6.ethpandaops.io/x/eth_getTransactionReceipt/"
+ transactionHash.toHexString();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().build();

try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Pattern pattern = Pattern.compile("<pre class=\"json\">(.*?)</pre>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(response.body());
if (matcher.find()) {
matcher.find();
String jsonResponseText = matcher.group(1).replaceAll("&#34;", "\"");
JsonObject jsonResponse = new JsonObject(jsonResponseText);
JsonObject result = jsonResponse.getJsonObject("result");
long gasUsed =
Long.parseLong(result.getString("gasUsed").substring(2), 16); // Convert hex to decimal

System.out.println(" expectedGas " + expectedGas);
if (gasUsed != expectedGas) {
System.out.println(
"Gas used ("
+ gasUsed
+ ") does not match expected gas ("
+ expectedGas
+ "). Transaction hash: "
+ transactionHash);
}
} else {
throw new InterruptedException("invalid regex exception ");
}
} catch (IOException | InterruptedException e) {
// e.printStackTrace();
}
}

public void process(final MessageFrame frame, final OperationTracer operationTracer) {
final AbstractMessageProcessor executor = getMessageProcessor(frame.getType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public TrieLogLayer create(final TrieLogAccumulator accumulator, final BlockHead
// by default do not persist empty reads to the trie log
continue;
}

System.out.println(val.getPrior() + " " + val.getUpdated());
layer.addStorageChange(address, slotUpdate.getKey(), val.getPrior(), val.getUpdated());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import static org.hyperledger.besu.evm.internal.Words.clampedAdd;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.ethereum.trie.verkle.adapter.TrieKeyAdapter;
import org.hyperledger.besu.ethereum.trie.verkle.hasher.PedersenHasher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.ethereum.trie.verkle.adapter.TrieKeyAdapter;
import org.hyperledger.besu.ethereum.trie.verkle.hasher.PedersenHasher;

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

Expand Down Expand Up @@ -55,11 +55,17 @@ public List<Address> keys() {
@Override
public long touchAndChargeProofOfAbsence(final Address address) {
long gas = 0;
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
return gas;
}

Expand All @@ -68,8 +74,11 @@ public long touchAndChargeMessageCall(final Address address) {

long gas = 0;

gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));

return gas;
}
Expand All @@ -79,8 +88,10 @@ public long touchAndChargeValueTransfer(final Address caller, final Address targ

long gas = 0;

gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(caller, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(caller, zeroTreeIndex, BALANCE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));

return gas;
}
Expand All @@ -91,11 +102,14 @@ public long touchAndChargeContractCreateInit(

long gas = 0;

gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));

if (createSendsValue) {
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
}
return gas;
}
Expand All @@ -105,11 +119,17 @@ public long touchAndChargeContractCreateCompleted(final Address address) {

long gas = 0;

gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, VERSION_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, NONCE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, CODE_SIZE_LEAF_KEY));

return gas;
}
Expand All @@ -120,11 +140,15 @@ public long touchTxOriginAndComputeGas(final Address origin) {

long gas = 0;

gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(origin, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(origin, zeroTreeIndex, NONCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, VERSION_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnWriteAndComputeGas(origin, zeroTreeIndex, BALANCE_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnWriteAndComputeGas(origin, zeroTreeIndex, NONCE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_SIZE_LEAF_KEY));

// modifying this after update on EIP-4762 to not charge simple transfers

Expand All @@ -137,15 +161,21 @@ public long touchTxExistingAndComputeGas(final Address target, final boolean sen

long gas = 0;

gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(target, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(target, zeroTreeIndex, NONCE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, VERSION_LEAF_KEY));
gas = clampedAdd(gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, NONCE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_SIZE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_KECCAK_LEAF_KEY));

if (sendsValue) {
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));
gas =
clampedAdd(
gas, touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));
} else {
gas = clampedAdd(gas,touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));
gas =
clampedAdd(gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BALANCE_LEAF_KEY));
}
// modifying this after update on EIP-4762 to not charge simple transfers

Expand All @@ -156,10 +186,13 @@ public long touchTxExistingAndComputeGas(final Address target, final boolean sen
public long touchCodeChunksUponContractCreation(final Address address, final long codeLength) {
long gas = 0;
for (long i = 0; i < (codeLength + 30) / 31; i++) {
gas = clampedAdd(gas,touchAddressOnWriteAndComputeGas(
address,
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH),
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH)));
gas =
clampedAdd(
gas,
touchAddressOnWriteAndComputeGas(
address,
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH),
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH)));
}
return gas;
}
Expand Down Expand Up @@ -213,7 +246,7 @@ public long touchAddressAndChargeGas(
boolean logEnabled = false;
long gas = 0;
if (accessEvent.isBranchRead()) {
gas = clampedAdd(gas,WITNESS_BRANCH_READ_COST);
gas = clampedAdd(gas, WITNESS_BRANCH_READ_COST);
if (logEnabled) {
System.out.println(
"touchAddressAndChargeGas WitnessBranchReadCost "
Expand All @@ -229,7 +262,7 @@ public long touchAddressAndChargeGas(
}
}
if (accessEvent.isChunkRead()) {
gas = clampedAdd(gas,WITNESS_CHUNK_READ_COST);
gas = clampedAdd(gas, WITNESS_CHUNK_READ_COST);
if (logEnabled) {
System.out.println(
"touchAddressAndChargeGas WitnessChunkReadCost "
Expand All @@ -245,7 +278,7 @@ public long touchAddressAndChargeGas(
}
}
if (accessEvent.isBranchWrite()) {
gas = clampedAdd(gas,WITNESS_BRANCH_WRITE_COST);
gas = clampedAdd(gas, WITNESS_BRANCH_WRITE_COST);
if (logEnabled) {
System.out.println(
"touchAddressAndChargeGas WitnessBranchWriteCost "
Expand All @@ -261,7 +294,7 @@ public long touchAddressAndChargeGas(
}
}
if (accessEvent.isChunkWrite()) {
gas = clampedAdd(gas,WITNESS_CHUNK_WRITE_COST);
gas = clampedAdd(gas, WITNESS_CHUNK_WRITE_COST);
if (logEnabled) {
System.out.println(
"touchAddressAndChargeGas WitnessChunkWriteCost "
Expand All @@ -277,7 +310,7 @@ public long touchAddressAndChargeGas(
}
}
if (accessEvent.isChunkFill()) {
gas = clampedAdd(gas,WITNESS_CHUNK_FILL_COST);
gas = clampedAdd(gas, WITNESS_CHUNK_FILL_COST);
if (logEnabled) {
System.out.println(
"touchAddressAndChargeGas WitnessChunkFillCost "
Expand Down Expand Up @@ -374,6 +407,8 @@ public ChunkAccessKey(

@Override
public List<UInt256> getStorageSlotTreeIndexes(final UInt256 storageKey) {
return List.of(TRIE_KEY_ADAPTER.locateStorageKeyOffset(storageKey),TRIE_KEY_ADAPTER.locateStorageKeySuffix(storageKey));
return List.of(
TRIE_KEY_ADAPTER.locateStorageKeyOffset(storageKey),
TRIE_KEY_ADAPTER.locateStorageKeySuffix(storageKey));
}
}

0 comments on commit 87dfbcd

Please sign in to comment.