Skip to content

Commit

Permalink
rpcdaemon: add eth_estimateGas extra field for gas limit (#2323)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Sep 9, 2024
1 parent f5ef74d commit 8668e77
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rpc-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.47.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
git -c advice.detachedHead=false clone --depth 1 --branch v0.48.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
cd ${{runner.workspace}}/rpc-tests
pip3 install -r requirements.txt
Expand Down
12 changes: 10 additions & 2 deletions silkworm/rpc/commands/eth_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,17 +869,25 @@ Task<void> EthereumRpcApi::handle_eth_get_transaction_receipt(const nlohmann::js
// https://eth.wiki/json-rpc/API#eth_estimategas
Task<void> EthereumRpcApi::handle_eth_estimate_gas(const nlohmann::json& request, nlohmann::json& reply) {
const auto& params = request["params"];
if (params.size() != 1) {
if (params.size() > 2 || params.size() == 0) {
auto error_msg = "invalid eth_estimateGas params: " + params.dump();
SILK_ERROR << error_msg;
reply = make_json_error(request, kInvalidParams, error_msg);
co_return;
}

const auto call = params[0].get<Call>();
SILK_DEBUG << "call: " << call;

auto tx = co_await database_->begin();

std::optional<BlockNum> block_number_for_gas_limit;
if (params.size() == 2) {
const auto block_id = params[1].get<std::string>();
SILK_DEBUG << "block_id: " << block_id;
block_number_for_gas_limit = co_await core::get_block_number(block_id, *tx);
}

try {
tx->set_state_cache_enabled(/*cache_enabled=*/true); // always at latest block

Expand Down Expand Up @@ -908,7 +916,7 @@ Task<void> EthereumRpcApi::handle_eth_estimate_gas(const nlohmann::json& request
};

rpc::EstimateGasOracle estimate_gas_oracle{block_header_provider, account_reader, chain_config, workers_, *tx, *chain_storage};
const auto estimated_gas = co_await estimate_gas_oracle.estimate_gas(call, latest_block);
const auto estimated_gas = co_await estimate_gas_oracle.estimate_gas(call, latest_block, block_number_for_gas_limit);

reply = make_json_content(request, to_quantity(estimated_gas));
} catch (const rpc::EstimateGasException& e) {
Expand Down
13 changes: 6 additions & 7 deletions silkworm/rpc/core/estimate_gas_oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@

namespace silkworm::rpc {

Task<intx::uint256> EstimateGasOracle::estimate_gas(const Call& call, const silkworm::Block& block) {
Task<intx::uint256> EstimateGasOracle::estimate_gas(const Call& call, const silkworm::Block& block, std::optional<BlockNum> block_number_for_gas_limit) {
SILK_DEBUG << "EstimateGasOracle::estimate_gas called";

auto block_number = block.header.number;
const auto block_number = block.header.number;

uint64_t hi = 0;
uint64_t lo = kTxGas - 1;

if (call.gas.value_or(0) >= kTxGas) {
SILK_DEBUG << "Set HI with gas in args: " << call.gas.value_or(0);
SILK_DEBUG << "Set gas limit using call args: " << call.gas.value_or(0);
hi = call.gas.value();
} else {
const auto header = co_await block_header_provider_(block_number);
const auto header = co_await block_header_provider_(block_number_for_gas_limit.value_or(block_number));
if (!header) {
throw EstimateGasException{-1, "header " + std::to_string(block_number) + " not found"};
co_return 0;
}

hi = header->gas_limit;
SILK_DEBUG << "Evaluate HI with gas in block " << header->gas_limit;
SILK_DEBUG << "Set gas limit using block: " << header->gas_limit;
}

std::optional<intx::uint256> gas_price = call.gas_price;
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 {
EstimateGasOracle(const EstimateGasOracle&) = delete;
EstimateGasOracle& operator=(const EstimateGasOracle&) = delete;

Task<intx::uint256> estimate_gas(const Call& call, const silkworm::Block& latest_block);
Task<intx::uint256> estimate_gas(const Call& call, const silkworm::Block& latest_block, std::optional<BlockNum> block_number_for_gas_limit = {});

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

0 comments on commit 8668e77

Please sign in to comment.