Skip to content

Commit

Permalink
rpcdaemon: remove block from EVMExecutor call methods (#2700)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Feb 6, 2025
1 parent 0ebbcca commit a04437c
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion silkworm/rpc/core/call_many.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CallManyResult CallExecutor::executes_all_bundles(const silkworm::ChainConfig& c
for (const auto& call : bundle.transactions) {
silkworm::Transaction txn{call.to_transaction()};

auto call_execution_result = executor.call(block_context.block_with_hash->block, txn);
auto call_execution_result = executor.call(txn);

if (call_execution_result.pre_check_error) {
result.error = call_execution_result.pre_check_error;
Expand Down
8 changes: 4 additions & 4 deletions silkworm/rpc/core/estimate_gas_oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Task<intx::uint256> EstimateGasOracle::estimate_gas(const Call& call, const silk
EVMExecutor executor{block, config_, workers_, state};
auto mid = (hi + lo) / 2;
transaction.gas_limit = mid;
result = try_execution(executor, block, transaction);
result = try_execution(executor, transaction);
if (result.success()) {
hi = mid;
} else {
Expand All @@ -106,7 +106,7 @@ Task<intx::uint256> EstimateGasOracle::estimate_gas(const Call& call, const silk
if (hi == cap) {
EVMExecutor executor{block, config_, workers_, state};
transaction.gas_limit = hi;
result = try_execution(executor, block, transaction);
result = try_execution(executor, transaction);
SILK_DEBUG << "HI == cap tested again with " << (result.error_code == evmc_status_code::EVMC_SUCCESS ? "succeed" : "failed");
} else if (!result.pre_check_error_code || result.pre_check_error_code == PreCheckErrorCode::kIntrinsicGasTooLow) {
result.pre_check_error = std::nullopt;
Expand All @@ -124,8 +124,8 @@ Task<intx::uint256> EstimateGasOracle::estimate_gas(const Call& call, const silk
co_return hi;
}

ExecutionResult EstimateGasOracle::try_execution(EVMExecutor& executor, const silkworm::Block& block, const silkworm::Transaction& transaction) {
return executor.call(block, transaction);
ExecutionResult EstimateGasOracle::try_execution(EVMExecutor& executor, const silkworm::Transaction& transaction) {
return executor.call(transaction);
}

void EstimateGasOracle::throw_exception(ExecutionResult& result) {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/rpc/core/estimate_gas_oracle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class EstimateGasOracle {
Task<intx::uint256> estimate_gas(const Call& call, const silkworm::Block& latest_block, std::optional<TxnId> txn_id, std::optional<BlockNum> block_num_for_gas_limit = {});

protected:
virtual ExecutionResult try_execution(EVMExecutor& executor, const silkworm::Block& block, const silkworm::Transaction& transaction);
virtual ExecutionResult try_execution(EVMExecutor& executor, const silkworm::Transaction& transaction);

private:
void throw_exception(ExecutionResult& result);
Expand Down
34 changes: 17 additions & 17 deletions silkworm/rpc/core/estimate_gas_oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_CASE("estimate gas") {
SECTION("Call empty, always fails but success in last step") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
ExecutionResult expect_result_fail{.error_code = evmc_status_code::EVMC_OUT_OF_GAS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(16)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand All @@ -130,7 +130,7 @@ TEST_CASE("estimate gas") {

SECTION("Call empty, always succeeds") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _)).Times(14).WillRepeatedly(Return(expect_result_ok));
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _)).Times(14).WillRepeatedly(Return(expect_result_ok));
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block, 244087591818874), boost::asio::use_future);
const intx::uint256& estimate_gas = result.get();
CHECK(estimate_gas == kTxGas);
Expand All @@ -139,7 +139,7 @@ TEST_CASE("estimate gas") {
SECTION("Call empty, alternatively fails and succeeds") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
ExecutionResult expect_result_fail{.error_code = evmc_status_code::EVMC_OUT_OF_GAS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(14)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_ok))
Expand All @@ -164,7 +164,7 @@ TEST_CASE("estimate gas") {
SECTION("Call empty, alternatively succeeds and fails") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
ExecutionResult expect_result_fail{.error_code = evmc_status_code::EVMC_OUT_OF_GAS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(14)
.WillOnce(Return(expect_result_ok))
.WillOnce(Return(expect_result_fail))
Expand Down Expand Up @@ -192,7 +192,7 @@ TEST_CASE("estimate gas") {
.pre_check_error = "intrinsic ",
.pre_check_error_code = PreCheckErrorCode::kIntrinsicGasTooLow};
ExecutionResult expect_result_fail{.error_code = evmc_status_code::EVMC_OUT_OF_GAS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(14)
.WillOnce(Return(expect_result_ok))
.WillOnce(Return(expect_result_fail_pre_check))
Expand All @@ -218,7 +218,7 @@ TEST_CASE("estimate gas") {
call.gas = kTxGas * 4;
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
ExecutionResult expect_result_fail{.error_code = evmc_status_code::EVMC_OUT_OF_GAS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(17)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand Down Expand Up @@ -246,7 +246,7 @@ TEST_CASE("estimate gas") {
SECTION("Call with gas, always succeeds") {
call.gas = kTxGas * 4;
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(15)
.WillRepeatedly(Return(expect_result_ok));
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block, 244087591818874), boost::asio::use_future);
Expand All @@ -261,7 +261,7 @@ TEST_CASE("estimate gas") {
call.gas = kTxGas * 2;
call.gas_price = intx::uint256{10'000};

EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(16)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand Down Expand Up @@ -291,7 +291,7 @@ TEST_CASE("estimate gas") {
call.gas = kTxGas * 2;
call.gas_price = intx::uint256{40'000};

EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(13)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand Down Expand Up @@ -319,7 +319,7 @@ TEST_CASE("estimate gas") {
call.gas_price = intx::uint256{10'000};
call.value = intx::uint256{500'000'000};

EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(16)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand Down Expand Up @@ -350,7 +350,7 @@ TEST_CASE("estimate gas") {
call.gas_price = intx::uint256{20'000};
call.value = intx::uint256{500'000'000};

EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(13)
.WillOnce(Return(expect_result_fail))
.WillOnce(Return(expect_result_fail))
Expand All @@ -374,7 +374,7 @@ TEST_CASE("estimate gas") {
SECTION("Call gas above allowance, always succeeds, gas capped") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
call.gas = kGasCap * 2;
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _)).Times(25).WillRepeatedly(Return(expect_result_ok));
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _)).Times(25).WillRepeatedly(Return(expect_result_ok));
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block, 244087591818874), boost::asio::use_future);
const intx::uint256& estimate_gas = result.get();

Expand All @@ -385,7 +385,7 @@ TEST_CASE("estimate gas") {
ExecutionResult expect_result_ok{.error_code = evmc_status_code::EVMC_SUCCESS};
call.gas = kTxGas / 2;

EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _)).Times(14).WillRepeatedly(Return(expect_result_ok));
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _)).Times(14).WillRepeatedly(Return(expect_result_ok));
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block, 244087591818874), boost::asio::use_future);
const intx::uint256& estimate_gas = result.get();

Expand All @@ -397,7 +397,7 @@ TEST_CASE("estimate gas") {
call.value = intx::uint256{2'000'000'000};

try {
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _)).Times(16).WillRepeatedly(Return(expect_result_fail));
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _)).Times(16).WillRepeatedly(Return(expect_result_fail));
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block, 244087591818874), boost::asio::use_future);
result.get();
CHECK(false);
Expand All @@ -420,7 +420,7 @@ TEST_CASE("estimate gas") {
call.value = intx::uint256{500'000'000};

try {
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(2)
.WillOnce(Return(expect_result_fail))
.WillRepeatedly(Return(expect_result_fail_pre_check));
Expand All @@ -447,7 +447,7 @@ TEST_CASE("estimate gas") {
call.value = intx::uint256{500'000'000};

try {
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(2)
.WillOnce(Return(expect_result_fail))
.WillRepeatedly(Return(expect_result_fail_pre_check));
Expand All @@ -473,7 +473,7 @@ TEST_CASE("estimate gas") {
call.value = intx::uint256{500'000'000};

try {
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _, _))
EXPECT_CALL(estimate_gas_oracle, try_execution(_, _))
.Times(2)
.WillOnce(Return(expect_result_fail))
.WillRepeatedly(Return(expect_result_fail_pre_check));
Expand Down
6 changes: 3 additions & 3 deletions silkworm/rpc/core/evm_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ Task<void> DebugExecutor::execute(json::Stream& stream, const ChainStorage& stor
stream.open_array();

Tracers tracers{debug_tracer};
const auto execution_result = executor.call(block, txn, tracers, refunds);
const auto execution_result = executor.call(txn, tracers, refunds);

debug_tracer->flush_logs();
stream.close_array();
Expand Down Expand Up @@ -550,7 +550,7 @@ Task<void> DebugExecutor::execute(

bool refunds = !config_.no_refunds;
Tracers tracers{debug_tracer};
const auto execution_result = executor.call(block, transaction, tracers, refunds);
const auto execution_result = executor.call(transaction, tracers, refunds);

debug_tracer->flush_logs();
stream.close_array();
Expand Down Expand Up @@ -642,7 +642,7 @@ Task<void> DebugExecutor::execute(
auto debug_tracer = std::make_shared<debug::DebugTracer>(stream, config_);
Tracers tracers{debug_tracer};

const auto execution_result = executor.call(block_context.block_with_hash->block, txn, tracers, refunds);
const auto execution_result = executor.call(txn, tracers, refunds);

debug_tracer->flush_logs();
stream.close_array();
Expand Down
17 changes: 9 additions & 8 deletions silkworm/rpc/core/evm_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ void EVMExecutor::reset() {
execution_processor_.reset();
}

ExecutionResult EVMExecutor::convert_validation_result(const ValidationResult& result, const Block& block, const silkworm::Transaction& txn, const EVM& evm) {
ExecutionResult EVMExecutor::convert_validation_result(const ValidationResult& result, const silkworm::Transaction& txn) {
auto& evm = execution_processor_.evm();
auto& block = evm.block();

std::string from = address_to_hex(*txn.sender());
switch (result) {
case ValidationResult::kMaxPriorityFeeGreaterThanMax: {
Expand Down Expand Up @@ -214,7 +217,6 @@ ExecutionResult EVMExecutor::convert_validation_result(const ValidationResult& r
}

ExecutionResult EVMExecutor::call(
const silkworm::Block& block,
const silkworm::Transaction& txn,
const Tracers& tracers,
bool refund,
Expand All @@ -224,7 +226,7 @@ ExecutionResult EVMExecutor::call(
auto& svc = use_service<AnalysisCacheService>(workers_);

evm.analysis_cache = svc.get_analysis_cache();
evm.beneficiary = rule_set_->get_beneficiary(block.header);
evm.beneficiary = rule_set_->get_beneficiary(evm.block().header);
evm.transfer = rule_set_->transfer_func();
evm.bailout = bailout;

Expand All @@ -234,7 +236,7 @@ ExecutionResult EVMExecutor::call(

const auto result = execution_processor_.call(txn, tracers, refund);
if (result.validation_result != ValidationResult::kOk) {
return convert_validation_result(result.validation_result, block, txn, evm);
return convert_validation_result(result.validation_result, txn);
}

ExecutionResult exec_result{result.status, result.gas_left, result.data};
Expand All @@ -245,16 +247,15 @@ ExecutionResult EVMExecutor::call(
}

ExecutionResult EVMExecutor::call_with_receipt(
const silkworm::Block& block,
const silkworm::Transaction& txn,
Receipt& receipt,
const Tracers& tracers,
bool refund,
bool gas_bailout) {
SILK_DEBUG << "EVMExecutor::call: blockNumber: " << block.header.number << " gas_limit: " << txn.gas_limit << " refund: " << refund
SILK_DEBUG << "EVMExecutor::call: blockNumber: " << execution_processor_.evm().block().header.number << " gas_limit: " << txn.gas_limit << " refund: " << refund
<< " gas_bailout: " << gas_bailout << " transaction: " << rpc::Transaction{txn};

const auto exec_result = call(block, txn, tracers, refund, gas_bailout);
const auto exec_result = call(txn, tracers, refund, gas_bailout);

auto& logs = execution_processor_.intra_block_state().logs();

Expand Down Expand Up @@ -290,7 +291,7 @@ Task<ExecutionResult> EVMExecutor::call(
const auto execution_result = co_await async_task(workers.executor(), [&]() -> ExecutionResult {
auto state = state_factory(this_executor, txn_id, chain_storage);
EVMExecutor executor{block, config, workers, state};
return executor.call(block, txn, tracers, refund, gas_bailout);
return executor.call(txn, tracers, refund, gas_bailout);
});
co_return execution_result;
}
Expand Down
4 changes: 1 addition & 3 deletions silkworm/rpc/core/evm_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,12 @@ class EVMExecutor {
EVMExecutor& operator=(const EVMExecutor&) = delete;

ExecutionResult call(
const silkworm::Block& block,
const silkworm::Transaction& txn,
const Tracers& tracers = {},
bool refund = true,
bool gas_bailout = false);

ExecutionResult call_with_receipt(
const silkworm::Block& block,
const silkworm::Transaction& txn,
Receipt& receipt,
const Tracers& tracers = {},
Expand All @@ -132,7 +130,7 @@ class EVMExecutor {
void reset();

private:
ExecutionResult convert_validation_result(const ValidationResult& result, const Block& block, const silkworm::Transaction& txn, const EVM& evm);
ExecutionResult convert_validation_result(const ValidationResult& result, const silkworm::Transaction& txn);

const silkworm::ChainConfig& config_;
WorkerPool& workers_;
Expand Down
12 changes: 6 additions & 6 deletions silkworm/rpc/core/evm_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
block.header.number = block_num;

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {});
const auto result = executor.call(txn, {});
CHECK(result.error_code == std::nullopt);
CHECK(result.pre_check_error.value() == "intrinsic gas too low: have 0, want 53000");
}
Expand All @@ -88,7 +88,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
txn.set_sender(0xa872626373628737383927236382161739290870_address);

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {});
const auto result = executor.call(txn, {});
CHECK(result.error_code == std::nullopt);
CHECK(result.pre_check_error.value() == "fee cap less than block base fee: address 0xa872626373628737383927236382161739290870, gasFeeCap: 2 baseFee: 7");
}
Expand All @@ -104,7 +104,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
txn.max_priority_fee_per_gas = 0x18;

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {});
const auto result = executor.call(txn, {});
CHECK(result.error_code == std::nullopt);
CHECK(result.pre_check_error.value() == "tip higher than fee cap: address 0xa872626373628737383927236382161739290870, tip: 24 gasFeeCap: 2");
}
Expand All @@ -127,7 +127,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
txn.set_sender(0xa872626373628737383927236382161739290870_address);

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {});
const auto result = executor.call(txn, {});
CHECK(result.error_code == std::nullopt);
CHECK(result.pre_check_error.value() == "insufficient funds for gas * price + value: address 0xa872626373628737383927236382161739290870 have 0 want 60000");
}
Expand All @@ -150,7 +150,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
txn.set_sender(0xa872626373628737383927236382161739290870_address);

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {}, false, /* gasBailout */ true);
const auto result = executor.call(txn, {}, false, /* gasBailout */ true);
executor.reset();
CHECK(result.error_code == 0);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ TEST_CASE_METHOD(EVMExecutorTest, "EVMExecutor") {
txn.access_list = access_list;

EVMExecutor executor{block, *chain_config_ptr, workers, state};
const auto result = executor.call(block, txn, {}, true, /* gasBailout */ true);
const auto result = executor.call(txn, {}, true, /* gasBailout */ true);
CHECK(result.error_code == 0);
}

Expand Down
Loading

0 comments on commit a04437c

Please sign in to comment.