From 6dd8e62089caf87e131119f55e7be7f5495aef02 Mon Sep 17 00:00:00 2001 From: lupin012 <58134934+lupin012@users.noreply.github.com> Date: Sat, 27 Apr 2024 11:17:29 +0200 Subject: [PATCH 1/5] add blob check on evm executor --- silkworm/rpc/core/evm_executor.cpp | 33 +++++++++++++++++++++++++++--- silkworm/rpc/core/evm_executor.hpp | 4 +++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/silkworm/rpc/core/evm_executor.cpp b/silkworm/rpc/core/evm_executor.cpp index e9954bcb92..9a0a27b3da 100644 --- a/silkworm/rpc/core/evm_executor.cpp +++ b/silkworm/rpc/core/evm_executor.cpp @@ -203,6 +203,23 @@ std::optional EVMExecutor::pre_check(const EVM& evm } } } + + if (evm.block().header.blob_gas_used && rev >= EVMC_CANCUN) { + if (evm.block().header.excess_blob_gas) { + std::string error = "internal Error Cancun is active but ExcessBlobGas is nil"; + return PreCheckResult{error, PreCheckErrorCode::kInternalError}; + } + auto blob_gas_price = evm.block().header.blob_gas_price(); + auto max_fee_per_blob_gas = txn.max_fee_per_blob_gas; + if (blob_gas_price > max_fee_per_blob_gas) { + std::string from = address_to_hex(*txn.sender()); + std::string error = "max fee per blob gas too low: address " + from + ", maxFeePerBlobGas: " + intx::to_string(max_fee_per_blob_gas) + + " blobGasPrice: " + intx::to_string(*blob_gas_price) + + ", excessBlobGas: " + std::to_string(*evm.block().header.excess_blob_gas); + return PreCheckResult{error, PreCheckErrorCode::kMaxFeePerBlobGasTooLOwError}; + } + } + if (txn.gas_limit < g0) { std::string from = address_to_hex(*txn.sender()); std::string error = "intrinsic gas too low: address " + from + ", have " + std::to_string(txn.gas_limit) + ", want " + intx::to_string(g0); @@ -250,12 +267,22 @@ ExecutionResult EVMExecutor::call( if (txn.max_fee_per_gas > 0 || txn.max_priority_fee_per_gas > 0) { // This method should be called after check (max_fee and base_fee) present in pre_check() method const intx::uint256 effective_gas_price{txn.effective_gas_price(base_fee_per_gas)}; - want = txn.gas_limit * effective_gas_price; + want = txn.gas_limit * effective_gas_price + txn.value; } else { - want = 0; + want = txn.value; } + + if (evm.block().header.blob_gas_used && rev >= EVMC_CANCUN) { + // compute blob fee for eip-4844 data blobs if any + auto blob_gas_used = evm.block().header.blob_gas_used; + auto max_fee_per_blob_gas = txn.max_fee_per_blob_gas; + auto max_blob_fee = max_fee_per_blob_gas * *blob_gas_used; + + want += max_blob_fee; + } + const auto have = ibs_state_.get_balance(*txn.sender()); - if (have < want + txn.value) { + if (have < want) { if (!gas_bailout) { Bytes data{}; std::string from = address_to_hex(*txn.sender()); diff --git a/silkworm/rpc/core/evm_executor.hpp b/silkworm/rpc/core/evm_executor.hpp index a676a5927a..a83f36e4be 100644 --- a/silkworm/rpc/core/evm_executor.hpp +++ b/silkworm/rpc/core/evm_executor.hpp @@ -45,7 +45,9 @@ enum PreCheckErrorCode { kIntrinsicGasTooLow, kInsufficientFunds, kFeeCapLessThanBlockFeePerGas, - kTipHigherThanFeeCap + kTipHigherThanFeeCap, + kInternalError, + kMaxFeePerBlobGasTooLOwError }; struct ExecutionResult { From f06849fc1e232de369e21f7d2b0bd03fd0fe2532 Mon Sep 17 00:00:00 2001 From: lupin012 <58134934+lupin012@users.noreply.github.com> Date: Fri, 3 May 2024 22:10:28 +0200 Subject: [PATCH 2/5] set last tag --- .github/workflows/rpc-integration-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rpc-integration-tests.yml b/.github/workflows/rpc-integration-tests.yml index 7241584283..5e9cf77d58 100644 --- a/.github/workflows/rpc-integration-tests.yml +++ b/.github/workflows/rpc-integration-tests.yml @@ -27,7 +27,7 @@ jobs: - name: Checkout RPC Tests Repository & Install Requirements run: | rm -rf ${{runner.workspace}}/rpc-tests - git -c advice.detachedHead=false clone --depth 1 --branch v0.12.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests + git -c advice.detachedHead=false clone --depth 1 --branch v0.16.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests cd ${{runner.workspace}}/rpc-tests pip3 install -r requirements.txt From 47dd014e94b9b4ee5adfefad1e604b353058114b Mon Sep 17 00:00:00 2001 From: lupin012 <58134934+lupin012@users.noreply.github.com> Date: Fri, 3 May 2024 22:10:50 +0200 Subject: [PATCH 3/5] calc blob costs --- silkworm/rpc/core/evm_executor.cpp | 18 +++++++++--------- silkworm/rpc/core/evm_executor.hpp | 3 +-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/silkworm/rpc/core/evm_executor.cpp b/silkworm/rpc/core/evm_executor.cpp index 9a0a27b3da..615b6cfa26 100644 --- a/silkworm/rpc/core/evm_executor.cpp +++ b/silkworm/rpc/core/evm_executor.cpp @@ -204,6 +204,7 @@ std::optional EVMExecutor::pre_check(const EVM& evm } } + if (evm.block().header.blob_gas_used && rev >= EVMC_CANCUN) { if (evm.block().header.excess_blob_gas) { std::string error = "internal Error Cancun is active but ExcessBlobGas is nil"; @@ -216,7 +217,7 @@ std::optional EVMExecutor::pre_check(const EVM& evm std::string error = "max fee per blob gas too low: address " + from + ", maxFeePerBlobGas: " + intx::to_string(max_fee_per_blob_gas) + " blobGasPrice: " + intx::to_string(*blob_gas_price) + ", excessBlobGas: " + std::to_string(*evm.block().header.excess_blob_gas); - return PreCheckResult{error, PreCheckErrorCode::kMaxFeePerBlobGasTooLOwError}; + return PreCheckResult{error, PreCheckErrorCode::kMaxFeePerBlobGasTooLowError}; } } @@ -263,26 +264,25 @@ ExecutionResult EVMExecutor::call( return {std::nullopt, txn.gas_limit, data, pre_check_result->pre_check_error, pre_check_result->pre_check_error_code}; } + // EIP-1559 normal gas cost intx::uint256 want; if (txn.max_fee_per_gas > 0 || txn.max_priority_fee_per_gas > 0) { // This method should be called after check (max_fee and base_fee) present in pre_check() method const intx::uint256 effective_gas_price{txn.effective_gas_price(base_fee_per_gas)}; - want = txn.gas_limit * effective_gas_price + txn.value; + want = txn.gas_limit * effective_gas_price; } else { - want = txn.value; + want = 0; } + // EIP-4844 blob gas cost (calc_data_fee) if (evm.block().header.blob_gas_used && rev >= EVMC_CANCUN) { // compute blob fee for eip-4844 data blobs if any - auto blob_gas_used = evm.block().header.blob_gas_used; - auto max_fee_per_blob_gas = txn.max_fee_per_blob_gas; - auto max_blob_fee = max_fee_per_blob_gas * *blob_gas_used; - - want += max_blob_fee; + const intx::uint256 blob_gas_price{evm.block().header.blob_gas_price().value_or(0)}; + want += txn.total_blob_gas() * blob_gas_price; } const auto have = ibs_state_.get_balance(*txn.sender()); - if (have < want) { + if (have < want + txn.value) { if (!gas_bailout) { Bytes data{}; std::string from = address_to_hex(*txn.sender()); diff --git a/silkworm/rpc/core/evm_executor.hpp b/silkworm/rpc/core/evm_executor.hpp index a83f36e4be..568dc2ec85 100644 --- a/silkworm/rpc/core/evm_executor.hpp +++ b/silkworm/rpc/core/evm_executor.hpp @@ -46,8 +46,7 @@ enum PreCheckErrorCode { kInsufficientFunds, kFeeCapLessThanBlockFeePerGas, kTipHigherThanFeeCap, - kInternalError, - kMaxFeePerBlobGasTooLOwError + kMaxFeePerBlobGasTooLowError }; struct ExecutionResult { From ff3f6ae06979bcc300d9e936ab5861df8b6ded1f Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 3 May 2024 20:11:38 +0000 Subject: [PATCH 4/5] make fmt --- silkworm/rpc/core/evm_executor.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/silkworm/rpc/core/evm_executor.cpp b/silkworm/rpc/core/evm_executor.cpp index 615b6cfa26..6c6533b563 100644 --- a/silkworm/rpc/core/evm_executor.cpp +++ b/silkworm/rpc/core/evm_executor.cpp @@ -204,7 +204,6 @@ std::optional EVMExecutor::pre_check(const EVM& evm } } - if (evm.block().header.blob_gas_used && rev >= EVMC_CANCUN) { if (evm.block().header.excess_blob_gas) { std::string error = "internal Error Cancun is active but ExcessBlobGas is nil"; From 5330e356707778e6e37def0152d9a816711ce53a Mon Sep 17 00:00:00 2001 From: lupin012 <58134934+lupin012@users.noreply.github.com> Date: Fri, 3 May 2024 22:17:55 +0200 Subject: [PATCH 5/5] add error value --- silkworm/rpc/core/evm_executor.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/silkworm/rpc/core/evm_executor.hpp b/silkworm/rpc/core/evm_executor.hpp index 568dc2ec85..39609fde64 100644 --- a/silkworm/rpc/core/evm_executor.hpp +++ b/silkworm/rpc/core/evm_executor.hpp @@ -42,11 +42,12 @@ namespace silkworm::rpc { enum PreCheckErrorCode { - kIntrinsicGasTooLow, - kInsufficientFunds, kFeeCapLessThanBlockFeePerGas, - kTipHigherThanFeeCap, - kMaxFeePerBlobGasTooLowError + kInsufficientFunds, + kInternalError, + kIntrinsicGasTooLow, + kMaxFeePerBlobGasTooLowError, + kTipHigherThanFeeCap }; struct ExecutionResult {