From d8ada22704f098f58b2784779e91011d0bd8c47f Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 7 Sep 2023 12:33:05 +0100 Subject: [PATCH 1/3] 1637 use 21k for gasEstimateStep if gas used is less --- libethereum/ClientBase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libethereum/ClientBase.cpp b/libethereum/ClientBase.cpp index a20c2f9b3..6d646f45a 100644 --- a/libethereum/ClientBase.cpp +++ b/libethereum/ClientBase.cpp @@ -132,7 +132,7 @@ std::pair< u256, ExecutionResult > ClientBase::estimateGas( Address const& _from estimateGasStep( upperBound, bk, _from, _dest, _value, gasPrice, _data ); if ( estimatedStep.first ) { auto executionResult = estimatedStep.second; - auto gasUsed = executionResult.gasUsed.convert_to< int64_t >(); + auto gasUsed = std::max( executionResult.gasUsed.convert_to< int64_t >(), lowerBound ); estimatedStep = estimateGasStep( gasUsed, bk, _from, _dest, _value, gasPrice, _data ); if ( estimatedStep.first ) { From d01b21bf79e4130cb2383b9f8ed15bc1be091171 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 7 Sep 2023 19:36:33 +0100 Subject: [PATCH 2/3] 1637 add unittest --- test/unittests/libweb3jsonrpc/jsonrpc.cpp | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/unittests/libweb3jsonrpc/jsonrpc.cpp b/test/unittests/libweb3jsonrpc/jsonrpc.cpp index aaee07279..1ff35dc63 100644 --- a/test/unittests/libweb3jsonrpc/jsonrpc.cpp +++ b/test/unittests/libweb3jsonrpc/jsonrpc.cpp @@ -1804,6 +1804,64 @@ contract Logger{ BOOST_REQUIRE_EQUAL(logs.size(), 24); } +BOOST_AUTO_TEST_CASE( estimate_gas_low_gas_txn ) { + JsonRpcFixture fixture; + dev::eth::simulateMining( *( fixture.client ), 10 ); + + auto senderAddress = fixture.coinbase.address(); + +/* +// SPDX-License-Identifier: None +pragma solidity ^0.6.0; + +contract TestEstimateGas { + uint256[256] number; + uint256 counter = 0; + + function store(uint256 x) public { + number[counter] = x; + counter += 1; + } + + function clear(uint256 pos) public { + number[pos] = 0; + } +} +*/ + + string bytecode = "608060405260006101005534801561001657600080fd5b50610104806100266000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80636057361d146037578063c0fe1af8146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050608d565b005b608b60048036036020811015607657600080fd5b810190808035906020019092919050505060b8565b005b806000610100546101008110609e57fe5b018190555060016101006000828254019250508190555050565b60008082610100811060c657fe5b01819055505056fea26469706673582212206c8da972693a5b8c9bf59c197c4a0c554e9f51abd20047572c9c19125b533d2964736f6c634300060c0033"; + + Json::Value create; + create["code"] = bytecode; + create["gas"] = "180000"; // TODO or change global default of 90000? + + string deployHash = fixture.rpcClient->eth_sendTransaction( create ); + dev::eth::mineTransaction( *( fixture.client ), 1 ); + + Json::Value deployReceipt = fixture.rpcClient->eth_getTransactionReceipt( deployHash ); + string contractAddress = deployReceipt["contractAddress"].asString(); + + Json::Value txStore1; // call store(1) + txStore1["to"] = contractAddress; + txStore1["data"] = "0x6057361d0000000000000000000000000000000000000000000000000000000000000001"; + txStore1["from"] = toJS( senderAddress ); + txStore1["gasPrice"] = fixture.rpcClient->eth_gasPrice(); + string txHash = fixture.rpcClient->eth_call( txStore1, "latest" ); + + Json::Value estimateGasCall; // call clear(0) + estimateGasCall["to"] = contractAddress; + estimateGasCall["data"] = "0xc0fe1af80000000000000000000000000000000000000000000000000000000000000000"; + estimateGasCall["from"] = toJS( senderAddress ); + estimateGasCall["gasPrice"] = fixture.rpcClient->eth_gasPrice(); + string estimatedGas = fixture.rpcClient->eth_estimateGas( estimateGasCall ); + + dev::bytes data = dev::jsToBytes( estimateGasCall["data"].asString() ); + + BOOST_REQUIRE( dev::jsToU256( estimatedGas ) > dev::eth::TransactionBase::baseGasRequired( + false, &data, fixture.client->chainParams().scheduleForBlockNumber( + fixture.client->number() ) ) ); +} + BOOST_AUTO_TEST_CASE( storage_limit_contract ) { JsonRpcFixture fixture; dev::eth::simulateMining( *( fixture.client ), 10 ); From b232296706dc7c8533734902b06fc2dfa5f07808 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 7 Sep 2023 20:02:43 +0100 Subject: [PATCH 3/3] 1637 add unittest --- test/unittests/libweb3jsonrpc/jsonrpc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unittests/libweb3jsonrpc/jsonrpc.cpp b/test/unittests/libweb3jsonrpc/jsonrpc.cpp index 1ff35dc63..fd7df6a06 100644 --- a/test/unittests/libweb3jsonrpc/jsonrpc.cpp +++ b/test/unittests/libweb3jsonrpc/jsonrpc.cpp @@ -1860,6 +1860,7 @@ contract TestEstimateGas { BOOST_REQUIRE( dev::jsToU256( estimatedGas ) > dev::eth::TransactionBase::baseGasRequired( false, &data, fixture.client->chainParams().scheduleForBlockNumber( fixture.client->number() ) ) ); + BOOST_REQUIRE( dev::jsToU256( estimatedGas ) == 21871 ); } BOOST_AUTO_TEST_CASE( storage_limit_contract ) {