From bff6ece8baf7eb80d13dd6800cb8eba60515b12e Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 6 Jun 2024 18:19:05 +0100 Subject: [PATCH 1/3] bug 1911 eth feeHistory --- libweb3jsonrpc/Eth.cpp | 9 ++++----- libweb3jsonrpc/Eth.h | 2 +- libweb3jsonrpc/EthFace.h | 14 +++++++++----- .../libweb3jsonrpc/WebThreeStubClient.cpp | 9 ++++++--- test/unittests/libweb3jsonrpc/WebThreeStubClient.h | 4 +++- test/unittests/libweb3jsonrpc/jsonrpc.cpp | 2 ++ 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 11097c4fd..ba032a18a 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -962,7 +962,7 @@ Json::Value Eth::eth_createAccessList( return result; } -Json::Value Eth::eth_feeHistory( const std::string& _blockCount, const std::string& _newestBlock, +Json::Value Eth::eth_feeHistory( dev::u256 _blockCount, const std::string& _newestBlock, const Json::Value& _rewardPercentiles ) { try { if ( !_rewardPercentiles.isArray() ) @@ -974,8 +974,7 @@ Json::Value Eth::eth_feeHistory( const std::string& _blockCount, const std::stri } } - auto blockCount = jsToU256( _blockCount ); - if ( blockCount > MAX_BLOCK_RANGE ) + if ( _blockCount > MAX_BLOCK_RANGE ) throw std::runtime_error( "Max block range reached. Please try smaller blockCount." ); auto newestBlock = jsToBlockNumber( _newestBlock ); @@ -984,10 +983,10 @@ Json::Value Eth::eth_feeHistory( const std::string& _blockCount, const std::stri auto result = Json::Value( Json::objectValue ); dev::u256 oldestBlock; - if ( blockCount > newestBlock ) + if ( _blockCount > newestBlock ) oldestBlock = 1; else - oldestBlock = dev::u256( newestBlock ) - blockCount + 1; + oldestBlock = dev::u256( newestBlock ) - _blockCount + 1; result["oldestBlock"] = toJS( oldestBlock ); result["baseFeePerGas"] = Json::Value( Json::arrayValue ); diff --git a/libweb3jsonrpc/Eth.h b/libweb3jsonrpc/Eth.h index 2ecf0be5d..ae4c011f2 100644 --- a/libweb3jsonrpc/Eth.h +++ b/libweb3jsonrpc/Eth.h @@ -219,7 +219,7 @@ class Eth : public dev::rpc::EthFace, public skutils::json_config_file_accessor virtual Json::Value eth_createAccessList( const Json::Value& param1, const std::string& param2 ) override; virtual Json::Value eth_feeHistory( - const std::string& param1, const std::string& param2, const Json::Value& param3 ) override; + dev::u256 param1, const std::string& param2, const Json::Value& param3 ) override; virtual std::string eth_maxPriorityFeePerGas() override; void setTransactionDefaults( eth::TransactionSkeleton& _t ); diff --git a/libweb3jsonrpc/EthFace.h b/libweb3jsonrpc/EthFace.h index c87edf57b..a7f7991c8 100644 --- a/libweb3jsonrpc/EthFace.h +++ b/libweb3jsonrpc/EthFace.h @@ -7,6 +7,7 @@ #include "ModularServer.h" +#include #include #include @@ -222,8 +223,7 @@ class EthFace : public ServerInterface< EthFace > { jsonrpc::JSON_OBJECT, "param2", jsonrpc::JSON_STRING, NULL ), &dev::rpc::EthFace::eth_createAccessListI ); this->bindAndAddMethod( jsonrpc::Procedure( "eth_feeHistory", jsonrpc::PARAMS_BY_POSITION, - jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, "param2", - jsonrpc::JSON_STRING, "param3", jsonrpc::JSON_ARRAY, NULL ), + jsonrpc::JSON_OBJECT, NULL ), &dev::rpc::EthFace::eth_feeHistoryI ); this->bindAndAddMethod( jsonrpc::Procedure( "eth_maxPriorityFeePerGas", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL ), @@ -449,8 +449,12 @@ class EthFace : public ServerInterface< EthFace > { if ( !request.isArray() || request.size() != 3 ) BOOST_THROW_EXCEPTION( jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS ) ); - response = - this->eth_feeHistory( request[0u].asString(), request[1u].asString(), request[2u] ); + if ( !request[0u].isString() && !request[0u].isUInt() ) + BOOST_THROW_EXCEPTION( + jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS ) ); + auto blockCount = request[0u].isString() ? dev::jsToU256( request[0u].asString() ) : + dev::u256( request[0u].asUInt() ); + response = this->eth_feeHistory( blockCount, request[1u].asString(), request[2u] ); } inline virtual void eth_maxPriorityFeePerGasI( const Json::Value& request, Json::Value& response ) { @@ -525,7 +529,7 @@ class EthFace : public ServerInterface< EthFace > { virtual Json::Value eth_createAccessList( const Json::Value& param1, const std::string& param2 ) = 0; virtual Json::Value eth_feeHistory( - const std::string& param1, const std::string& param2, const Json::Value& param3 ) = 0; + dev::u256 param1, const std::string& param2, const Json::Value& param3 ) = 0; virtual std::string eth_maxPriorityFeePerGas() = 0; }; diff --git a/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp b/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp index 6891aacef..b37a52a91 100644 --- a/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp +++ b/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp @@ -841,10 +841,13 @@ Json::Value WebThreeStubClient::eth_createAccessList( const Json::Value& param1, throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString() ); } - -Json::Value WebThreeStubClient::eth_feeHistory( const std::string& param1, const std::string& param2, const Json::Value& param3 ) { +#include +Json::Value WebThreeStubClient::eth_feeHistory( const Json::Value& param1, const std::string& param2, const Json::Value& param3 ) { Json::Value p; - p.append( param1 ); + if ( param1.isString() ) + p.append( param1.asString() ); + if ( param1.isUInt() ) + p.append( param1.asUInt() ); p.append( param2 ); p.append( param3 ); Json::Value result = this->CallMethod( "eth_feeHistory", p ); diff --git a/test/unittests/libweb3jsonrpc/WebThreeStubClient.h b/test/unittests/libweb3jsonrpc/WebThreeStubClient.h index 2be6ca519..9125b97ab 100644 --- a/test/unittests/libweb3jsonrpc/WebThreeStubClient.h +++ b/test/unittests/libweb3jsonrpc/WebThreeStubClient.h @@ -5,6 +5,8 @@ #ifndef JSONRPC_CPP_STUB_WEBTHREESTUBCLIENT_H_ #define JSONRPC_CPP_STUB_WEBTHREESTUBCLIENT_H_ +#include + #include class WebThreeStubClient : public jsonrpc::Client { @@ -98,7 +100,7 @@ class WebThreeStubClient : public jsonrpc::Client { std::string eth_sendRawTransaction( const std::string& param1 ) noexcept( false ); std::string eth_maxPriorityFeePerGas() noexcept( false ); Json::Value eth_createAccessList( const Json::Value& param1, const std::string& param2 ) noexcept( false ); - Json::Value eth_feeHistory( const std::string& param1, const std::string& param2, const Json::Value& param3 ) noexcept( false ); + Json::Value eth_feeHistory( const Json::Value& param1, const std::string& param2, const Json::Value& param3 ) noexcept( false ); bool eth_notePassword( const std::string& param1 ) noexcept( false ); bool db_put( const std::string& param1, const std::string& param2, const std::string& param3 ) noexcept( false ); diff --git a/test/unittests/libweb3jsonrpc/jsonrpc.cpp b/test/unittests/libweb3jsonrpc/jsonrpc.cpp index c004aaf6d..aefed42db 100644 --- a/test/unittests/libweb3jsonrpc/jsonrpc.cpp +++ b/test/unittests/libweb3jsonrpc/jsonrpc.cpp @@ -3226,6 +3226,8 @@ BOOST_AUTO_TEST_CASE( eip1559RpcMethods ) { BOOST_REQUIRE_EQUAL( feeHistory["reward"][i][j].asString(), toJS( 0 ) ); } } + + BOOST_REQUIRE_NO_THROW( fixture.rpcClient->eth_feeHistory( blockCnt, "latest", percentiles ) ); } BOOST_AUTO_TEST_CASE( etherbase_generation2 ) { From 8cb6ecfa5488b5ed434e9549f78c7d451e8ffb56 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 6 Jun 2024 18:56:03 +0100 Subject: [PATCH 2/3] #1911 format --- test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp b/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp index b37a52a91..c297164dc 100644 --- a/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp +++ b/test/unittests/libweb3jsonrpc/WebThreeStubClient.cpp @@ -841,7 +841,7 @@ Json::Value WebThreeStubClient::eth_createAccessList( const Json::Value& param1, throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString() ); } -#include + Json::Value WebThreeStubClient::eth_feeHistory( const Json::Value& param1, const std::string& param2, const Json::Value& param3 ) { Json::Value p; if ( param1.isString() ) From cbda0bd6b2351db2a7eedafdcd24c88a96b06d3c Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 6 Jun 2024 19:48:31 +0100 Subject: [PATCH 3/3] #1911 fix unittests --- test/unittests/libethereum/ClientTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittests/libethereum/ClientTest.cpp b/test/unittests/libethereum/ClientTest.cpp index ed9bdb878..9cc2e0b93 100644 --- a/test/unittests/libethereum/ClientTest.cpp +++ b/test/unittests/libethereum/ClientTest.cpp @@ -39,7 +39,7 @@ using namespace dev::test; using namespace dev::p2p; namespace fs = boost::filesystem; -static size_t rand_port = 1024 + rand() % 64000; +static size_t rand_port = ( srand(time(nullptr)), 1024 + rand() % 64000 ); struct FixtureCommon { const string BTRFS_FILE_PATH = "btrfs.file";