Skip to content

Commit

Permalink
Merge pull request #1852 from skalenetwork/enhancement/update-eth-cal…
Browse files Browse the repository at this point in the history
…l-api

Support "input" field in eth_call, eth_estimateGas
  • Loading branch information
DmytroNazarenko authored Mar 20, 2024
2 parents 5452ee7 + 4c8794d commit 73803db
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 50 deletions.
55 changes: 16 additions & 39 deletions libweb3jsonrpc/JsonHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,15 @@ TransactionSkeleton toTransactionSkeleton( Json::Value const& _json ) {
if ( !_json["gasPrice"].empty() )
ret.gasPrice = jsToU256( _json["gasPrice"].asString() );

if ( !_json["data"].empty() ) // ethereum.js has preconstructed the data array
ret.data = jsToBytes( _json["data"].asString(), OnFailed::Throw );

if ( !_json["code"].empty() )
ret.data = jsToBytes( _json["code"].asString(), OnFailed::Throw );

if ( !_json["data"].empty() )
ret.data = jsToBytes( _json["data"].asString(), OnFailed::Throw );

if ( !_json["input"].empty() )
ret.data = jsToBytes( _json["input"].asString(), OnFailed::Throw );

if ( !_json["nonce"].empty() )
ret.nonce = jsToU256( _json["nonce"].asString() );
return ret;
Expand Down Expand Up @@ -531,17 +534,22 @@ TransactionSkeleton rapidJsonToTransactionSkeleton( rapidjson::Value const& _jso
ret.gasPrice = jsToU256( _json["gasPrice"].GetString() );
}

if ( _json.HasMember( "data" ) ) { // ethereum.js has preconstructed
// the data array
if ( _json.HasMember( "code" ) ) {
if ( !_json["code"].IsString() )
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
ret.data = jsToBytes( _json["code"].GetString(), OnFailed::Throw );
}

if ( _json.HasMember( "data" ) ) {
if ( !_json["data"].IsString() )
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
ret.data = jsToBytes( _json["data"].GetString(), OnFailed::Throw );
}

if ( _json.HasMember( "code" ) ) {
if ( !_json["code"].IsString() )
if ( _json.HasMember( "input" ) ) {
if ( !_json["input"].IsString() )
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
ret.data = jsToBytes( _json["code"].GetString(), OnFailed::Throw );
ret.data = jsToBytes( _json["input"].GetString(), OnFailed::Throw );
}

if ( _json.HasMember( "nonce" ) ) {
Expand All @@ -552,37 +560,6 @@ TransactionSkeleton rapidJsonToTransactionSkeleton( rapidjson::Value const& _jso

return ret;
}
/*
dev::eth::LogFilter toLogFilter( Json::Value const& _json ) {
dev::eth::LogFilter filter;
if ( !_json.isObject() || _json.empty() )
return filter;
// check only !empty. it should throw exceptions if input params are incorrect
if ( !_json["fromBlock"].empty() )
filter.withEarliest( jsToFixed< 32 >( _json["fromBlock"].asString() ) );
if ( !_json["toBlock"].empty() )
filter.withLatest( jsToFixed< 32 >( _json["toBlock"].asString() ) );
if ( !_json["address"].empty() ) {
if ( _json["address"].isArray() )
for ( auto i : _json["address"] )
filter.address( jsToAddress( i.asString() ) );
else
filter.address( jsToAddress( _json["address"].asString() ) );
}
if ( !_json["topics"].empty() )
for ( unsigned i = 0; i < _json["topics"].size(); i++ ) {
if ( _json["topics"][i].isArray() ) {
for ( auto t : _json["topics"][i] )
if ( !t.isNull() )
filter.topic( i, jsToFixed< 32 >( t.asString() ) );
} else if ( !_json["topics"][i].isNull() ) // if it is anything else then string, it
// should and will fail
filter.topic( i, jsToFixed< 32 >( _json["topics"][i].asString() ) );
}
return filter;
}
*/

// TODO: this should be removed once we decide to remove backward compatibility with old log filters
dev::eth::LogFilter toLogFilter( Json::Value const& _json ) // commented to avoid warning.
Expand Down
65 changes: 54 additions & 11 deletions test/unittests/libweb3jsonrpc/jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,20 +815,38 @@ BOOST_AUTO_TEST_CASE( simple_contract ) {
JsonRpcFixture fixture;
dev::eth::simulateMining( *( fixture.client ), 1 );


// pragma solidity 0.8.4;
// contract test {
// function f(uint a) returns(uint d) { return a * 7; }
// uint value;
// function f(uint a) public pure returns(uint d) {
// return a * 7;
// }
// function setValue(uint _value) external {
// value = _value;
// }
// }

string compiled =
"6080604052341561000f57600080fd5b60b98061001d6000396000f300"
"608060405260043610603f576000357c01000000000000000000000000"
"00000000000000000000000000000000900463ffffffff168063b3de64"
"8b146044575b600080fd5b3415604e57600080fd5b606a600480360381"
"019080803590602001909291905050506080565b604051808281526020"
"0191505060405180910390f35b60006007820290509190505600a16562"
"7a7a72305820f294e834212334e2978c6dd090355312a3f0f9476b8eb9"
"8fb480406fc2728a960029";
string compiled =
"608060405234801561001057600080fd5b506101ef8061002060003"
"96000f3fe608060405234801561001057600080fd5b506004361061"
"00365760003560e01c8063552410771461003b578063b3de648b146"
"10057575b600080fd5b610055600480360381019061005091906100"
"bc565b610087565b005b610071600480360381019061006c9190610"
"0bc565b610091565b60405161007e91906100f4565b604051809103"
"90f35b8060008190555050565b60006007826100a0919061010f565"
"b9050919050565b6000813590506100b6816101a2565b9291505056"
"5b6000602082840312156100ce57600080fd5b60006100dc8482850"
"16100a7565b91505092915050565b6100ee81610169565b82525050"
"565b600060208201905061010960008301846100e5565b929150505"
"65b600061011a82610169565b915061012583610169565b9250817f"
"fffffffffffffffffffffffffffffffffffffffffffffffffffffff"
"fffffffff048311821515161561015e5761015d610173565b5b8282"
"02905092915050565b6000819050919050565b7f4e487b710000000"
"0000000000000000000000000000000000000000000000000600052"
"601160045260246000fd5b6101ab81610169565b81146101b657600"
"080fd5b5056fea26469706673582212200be8156151b5ef7c250fa7"
"b8c8ed4e2a1c32cd526f9c868223f6838fa1193c9e64736f6c63430"
"008040033";

Json::Value create;
create["code"] = compiled;
Expand All @@ -855,6 +873,31 @@ BOOST_AUTO_TEST_CASE( simple_contract ) {
string result = fixture.rpcClient->eth_call( call, "latest" );
BOOST_CHECK_EQUAL(
result, "0x0000000000000000000000000000000000000000000000000000000000000007" );

Json::Value inputCall;
inputCall["to"] = contractAddress;
inputCall["input"] = "0xb3de648b0000000000000000000000000000000000000000000000000000000000000001";
inputCall["gas"] = "1000000";
inputCall["gasPrice"] = "0";
result = fixture.rpcClient->eth_call( inputCall, "latest" );
BOOST_CHECK_EQUAL(
result, "0x0000000000000000000000000000000000000000000000000000000000000007" );

Json::Value transact;
transact["to"] = contractAddress;
transact["data"] = "0x552410770000000000000000000000000000000000000000000000000000000000000001";
txHash = fixture.rpcClient->eth_sendTransaction( transact );
dev::eth::mineTransaction( *( fixture.client ), 1 );
auto res = fixture.rpcClient->eth_getTransactionReceipt( txHash );
BOOST_REQUIRE_EQUAL( res["status"], string( "0x1" ) );

Json::Value inputTx;
inputTx["to"] = contractAddress;
inputTx["input"] = "0x552410770000000000000000000000000000000000000000000000000000000000000002";
txHash = fixture.rpcClient->eth_sendTransaction( inputTx );
dev::eth::mineTransaction( *( fixture.client ), 1 );
res = fixture.rpcClient->eth_getTransactionReceipt( txHash );
BOOST_REQUIRE_EQUAL( res["status"], string( "0x1" ) );
}

/*
Expand Down

0 comments on commit 73803db

Please sign in to comment.