Skip to content

Commit

Permalink
Merge pull request #595 from eosnetworkfoundation/arhag/bring-0.5-to-…
Browse files Browse the repository at this point in the history
…main

[0.5 -> main] EstimateGasOracle: fix estimate_gas; Return better result when accessed by GET
  • Loading branch information
arhag authored Jun 14, 2023
2 parents ad3ae54 + 74a8213 commit 280a590
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
3 changes: 2 additions & 1 deletion peripherals/proxy/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ http {
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';
return 204;
add_header 'Content-Type' 'text/html';
return 200 '<!DOCTYPE html>This is an API endpoint that only accepts JSON-RPC requests. <br> Please visit <a href = "https://docs.eosnetwork.com/docs/latest/eos-evm/">https://docs.eosnetwork.com/docs/latest/eos-evm/</a> for information about EOS EVM. \n';
}

resolver 127.0.0.11;
Expand Down
38 changes: 18 additions & 20 deletions silkrpc/core/estimate_gas_oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ boost::asio::awaitable<intx::uint256> EstimateGasOracle::estimate_gas(const Call
auto mid = (hi + lo) / 2;
transaction.gas_limit = mid;

auto failed = co_await try_execution(transaction);
auto [failed, _]= co_await try_execution(transaction);

if (failed) {
lo = mid;
Expand All @@ -97,10 +97,19 @@ boost::asio::awaitable<intx::uint256> EstimateGasOracle::estimate_gas(const Call

if (hi == cap) {
transaction.gas_limit = hi;
auto failed = co_await try_execution(transaction);
auto [failed, res] = co_await try_execution(transaction);
SILKRPC_DEBUG << "HI == cap tested again with " << (failed ? "failure" : "succeed") << "\n";

if (failed) {
if(res && res->error_code != evmc_status_code::EVMC_OUT_OF_GAS) {
const auto error_message = EVMExecutor<>::get_error_message(res->error_code, res->data);
SILKRPC_DEBUG << "result message " << error_message << ", code " << res->error_code << "\n";
if (res->data.empty()) {
throw EstimateGasException{-32000, error_message};
} else {
throw EstimateGasException{3, error_message, res->data};
}
}
throw EstimateGasException{-1, "gas required exceeds allowance (" + std::to_string(cap) + ")"};
}
}
Expand All @@ -109,30 +118,19 @@ boost::asio::awaitable<intx::uint256> EstimateGasOracle::estimate_gas(const Call
co_return hi;
}

boost::asio::awaitable<bool> EstimateGasOracle::try_execution(const silkworm::Transaction& transaction) {
boost::asio::awaitable<std::tuple<bool, std::optional<ExecutionResult>>> EstimateGasOracle::try_execution(const silkworm::Transaction& transaction) {
const auto result = co_await executor_(transaction);

bool failed = true;
if (result.pre_check_error) {
SILKRPC_DEBUG << "result error " << result.pre_check_error.value() << "\n";
throw EstimateGasException{-32000, result.pre_check_error.value()};
} else if (result.error_code == evmc_status_code::EVMC_SUCCESS) {
SILKRPC_DEBUG << "result SUCCESS\n";
failed = false;
} else if (result.error_code == evmc_status_code::EVMC_OUT_OF_GAS) {
SILKRPC_DEBUG << "result EVMC_OUT_OF_GAS\n";
failed = true;
} else {
const auto error_message = EVMExecutor<>::get_error_message(result.error_code, result.data);
SILKRPC_DEBUG << "result message " << error_message << ", code " << result.error_code << "\n";
if (result.data.empty()) {
throw EstimateGasException{-32000, error_message};
} else {
throw EstimateGasException{3, error_message, result.data};
}
if(!result.pre_check_error.value().starts_with("intrinsic gas too low"))
throw EstimateGasException{-32000, result.pre_check_error.value()};

co_return std::make_tuple<bool, std::optional<ExecutionResult>>(true, {});
}

co_return failed;
bool failed = result.error_code != evmc_status_code::EVMC_SUCCESS;
co_return std::make_tuple<bool, std::optional<ExecutionResult>>(std::move(failed), std::move(result));
}

} // namespace silkrpc::ego
2 changes: 1 addition & 1 deletion silkrpc/core/estimate_gas_oracle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class EstimateGasOracle {
boost::asio::awaitable<intx::uint256> estimate_gas(const Call& call, uint64_t block_number);

private:
boost::asio::awaitable<bool> try_execution(const silkworm::Transaction& transaction);
boost::asio::awaitable<std::tuple<bool, std::optional<ExecutionResult>>> try_execution(const silkworm::Transaction& transaction);

const BlockHeaderProvider& block_header_provider_;
const AccountReader& account_reader_;
Expand Down

0 comments on commit 280a590

Please sign in to comment.