From 87dfbcd744f10ab07b127d6352f39767ae825703 Mon Sep 17 00:00:00 2001 From: Karim Taam Date: Fri, 17 May 2024 11:34:05 +0200 Subject: [PATCH] fix storage slot key generation and clean code Signed-off-by: Karim Taam --- .../mainnet/MainnetTransactionProcessor.java | 52 +++++++- .../verkle/trielog/TrieLogFactoryImpl.java | 2 - .../stateless/Eip4762AccessWitness.java | 115 ++++++++++++------ 3 files changed, 123 insertions(+), 46 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java index d819a95b866..a770946133e 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java @@ -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; @@ -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; @@ -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, @@ -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; @@ -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 response = client.send(request, HttpResponse.BodyHandlers.ofString()); + Pattern pattern = Pattern.compile("
(.*?)
", Pattern.DOTALL); + Matcher matcher = pattern.matcher(response.body()); + if (matcher.find()) { + matcher.find(); + String jsonResponseText = matcher.group(1).replaceAll(""", "\""); + 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()); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/verkle/trielog/TrieLogFactoryImpl.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/verkle/trielog/TrieLogFactoryImpl.java index ae1de46e881..1da18d91d4a 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/verkle/trielog/TrieLogFactoryImpl.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/trie/diffbased/verkle/trielog/TrieLogFactoryImpl.java @@ -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()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/stateless/Eip4762AccessWitness.java b/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/stateless/Eip4762AccessWitness.java index 67ce54e7e0a..6a8243b0999 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/stateless/Eip4762AccessWitness.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/stateless/Eip4762AccessWitness.java @@ -10,6 +10,8 @@ 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; @@ -17,8 +19,6 @@ 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 { @@ -55,11 +55,17 @@ public List
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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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 @@ -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 @@ -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; } @@ -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 " @@ -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 " @@ -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 " @@ -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 " @@ -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 " @@ -374,6 +407,8 @@ public ChunkAccessKey( @Override public List 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)); } }