Browse Source

jsonrpc api changes in progress2

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
9a69bc058a
  1. 5
      libethcore/CommonJS.h
  2. 110
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  3. 7
      libweb3jsonrpc/WebThreeStubServerBase.h
  4. 20
      libweb3jsonrpc/abstractwebthreestubserver.h
  5. 4
      libweb3jsonrpc/spec.json
  6. 22
      test/webthreestubclient.h

5
libethcore/CommonJS.h

@ -105,6 +105,11 @@ template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_i
inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); }
inline int jsToInt(std::string const& _s)
{
return std::stoi(_s, nullptr, 16);
}
inline std::string jsToDecimal(std::string const& _s)
{
return dev::toString(jsToU256(_s));

110
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -24,6 +24,7 @@
// Make sure boost/asio.hpp is included before windows.h.
#include <boost/asio.hpp>
#include <jsonrpccpp/common/exception.h>
#include <libdevcore/CommonData.h>
#include <libsolidity/CompilerStack.h>
#include <libsolidity/Scanner.h>
@ -248,6 +249,31 @@ std::string WebThreeStubServerBase::web3_sha3(std::string const& _param1)
return toJS(sha3(jsToBytes(_param1)));
}
string WebThreeStubServerBase::net_peerCount()
{
return toJS(network()->peerCount());
}
bool WebThreeStubServerBase::net_listening()
{
return network()->isNetworkStarted();
}
std::string WebThreeStubServerBase::eth_coinbase()
{
return toJS(client()->address());
}
bool WebThreeStubServerBase::eth_mining()
{
return client()->isMining();
}
std::string WebThreeStubServerBase::eth_gasPrice()
{
return toJS(10 * dev::eth::szabo);
}
Json::Value WebThreeStubServerBase::eth_accounts()
{
Json::Value ret(Json::arrayValue);
@ -256,6 +282,50 @@ Json::Value WebThreeStubServerBase::eth_accounts()
return ret;
}
string WebThreeStubServerBase::eth_blockNumber()
{
return toJS(client()->number());
}
std::string WebThreeStubServerBase::eth_getBalance(string const& _address, string const& _blockNumber)
{
Address address;
int number;
try
{
address = jsToAddress(_address);
number = jsToInt(_blockNumber);
}
catch (...)
{
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS);
}
return toJS(client()->balanceAt(address, number));
}
Json::Value WebThreeStubServerBase::eth_getStorage(string const& _address, std::string const& _blockNumber)
{
Address address;
int number;
try
{
address = jsToAddress(_address);
number = jsToInt(_blockNumber);
}
catch (...)
{
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS);
}
//TODO: fix this naming !
return toJson(client()->storageAt(address, number));
}
std::string WebThreeStubServerBase::shh_addToGroup(std::string const& _group, std::string const& _who)
{
(void)_group;
@ -263,11 +333,6 @@ std::string WebThreeStubServerBase::shh_addToGroup(std::string const& _group, st
return "";
}
std::string WebThreeStubServerBase::eth_balanceAt(string const& _address)
{
return toJS(client()->balanceAt(jsToAddress(_address), client()->getDefault()));
}
Json::Value WebThreeStubServerBase::eth_blockByHash(std::string const& _hash)
{
return toJson(client()->blockInfo(jsToFixed<32>(_hash)));
@ -360,11 +425,6 @@ std::string WebThreeStubServerBase::eth_codeAt(string const& _address)
return jsFromBinary(client()->codeAt(jsToAddress(_address), client()->getDefault()));
}
std::string WebThreeStubServerBase::eth_coinbase()
{
return toJS(client()->address());
}
double WebThreeStubServerBase::eth_countAt(string const& _address)
{
return (double)(uint64_t)client()->countAt(jsToAddress(_address), client()->getDefault());
@ -390,11 +450,6 @@ double WebThreeStubServerBase::eth_uncleCountByNumber(int _number)
return client()->transactionCount(client()->hashFromNumber(_number));
}
std::string WebThreeStubServerBase::eth_gasPrice()
{
return toJS(10 * dev::eth::szabo);
}
std::string WebThreeStubServerBase::db_get(std::string const& _name, std::string const& _key)
{
string ret = db()->get(_name, _key);
@ -421,16 +476,6 @@ bool WebThreeStubServerBase::shh_haveIdentity(std::string const& _id)
return m_ids.count(jsToPublic(_id)) > 0;
}
bool WebThreeStubServerBase::net_listening()
{
return network()->isNetworkStarted();
}
bool WebThreeStubServerBase::eth_mining()
{
return client()->isMining();
}
int WebThreeStubServerBase::eth_newFilter(Json::Value const& _json)
{
unsigned ret = -1;
@ -557,16 +602,6 @@ std::string WebThreeStubServerBase::eth_solidity(std::string const& _code)
return res;
}
string WebThreeStubServerBase::eth_blockNumber()
{
return toJS(client()->number());
}
string WebThreeStubServerBase::net_peerCount()
{
return toJS(network()->peerCount());
}
bool WebThreeStubServerBase::shh_post(Json::Value const& _json)
{
shh::Message m = toMessage(_json);
@ -662,11 +697,6 @@ std::string WebThreeStubServerBase::eth_stateAt(string const& _address, string c
return toJS(client()->stateAt(jsToAddress(_address), jsToU256(_storage), client()->getDefault()));
}
Json::Value WebThreeStubServerBase::eth_storageAt(string const& _address)
{
return toJson(client()->storageAt(jsToAddress(_address)));
}
std::string WebThreeStubServerBase::eth_transact(Json::Value const& _json)
{
std::string ret;

7
libweb3jsonrpc/WebThreeStubServerBase.h

@ -77,9 +77,12 @@ public:
virtual std::string eth_gasPrice();
virtual Json::Value eth_accounts();
virtual std::string eth_blockNumber();
virtual std::string eth_getBalance(std::string const& _address, std::string const& _blockNumber);
virtual Json::Value eth_getStorage(std::string const& _address, std::string const& _blockNumber);
virtual std::string eth_balanceAt(std::string const& _address);
virtual std::string eth_stateAt(std::string const& _address, std::string const& _storage);
virtual Json::Value eth_blockByHash(std::string const& _hash);
virtual Json::Value eth_blockByNumber(int _number);
virtual std::string eth_call(Json::Value const& _json);
@ -103,8 +106,6 @@ public:
virtual std::string eth_lll(std::string const& _s);
virtual std::string eth_serpent(std::string const& _s);
virtual std::string eth_solidity(std::string const& _code);
virtual std::string eth_stateAt(std::string const& _address, std::string const& _storage);
virtual Json::Value eth_storageAt(std::string const& _address);
virtual std::string eth_transact(Json::Value const& _json);
virtual Json::Value eth_transactionByHash(std::string const& _hash, int _i);
virtual Json::Value eth_transactionByNumber(int _number, int _i);

20
libweb3jsonrpc/abstractwebthreestubserver.h

@ -20,9 +20,9 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(jsonrpc::Procedure("eth_gasPrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_gasPriceI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_accounts", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::eth_accountsI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_blockNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_blockNumberI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_balanceAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_balanceAtI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getBalance", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBalanceI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getStorage", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getStorageI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_stateAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_stateAtI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_storageAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_storageAtI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_countAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_countAtI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_transactionCountByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_transactionCountByHashI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_transactionCountByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_transactionCountByNumberI);
@ -107,17 +107,17 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
(void)request;
response = this->eth_blockNumber();
}
inline virtual void eth_balanceAtI(const Json::Value &request, Json::Value &response)
inline virtual void eth_getBalanceI(const Json::Value &request, Json::Value &response)
{
response = this->eth_balanceAt(request[0u].asString());
response = this->eth_getBalance(request[0u].asString(), request[1u].asString());
}
inline virtual void eth_stateAtI(const Json::Value &request, Json::Value &response)
inline virtual void eth_getStorageI(const Json::Value &request, Json::Value &response)
{
response = this->eth_stateAt(request[0u].asString(), request[1u].asString());
response = this->eth_getStorage(request[0u].asString(), request[1u].asString());
}
inline virtual void eth_storageAtI(const Json::Value &request, Json::Value &response)
inline virtual void eth_stateAtI(const Json::Value &request, Json::Value &response)
{
response = this->eth_storageAt(request[0u].asString());
response = this->eth_stateAt(request[0u].asString(), request[1u].asString());
}
inline virtual void eth_countAtI(const Json::Value &request, Json::Value &response)
{
@ -303,9 +303,9 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual std::string eth_gasPrice() = 0;
virtual Json::Value eth_accounts() = 0;
virtual std::string eth_blockNumber() = 0;
virtual std::string eth_balanceAt(const std::string& param1) = 0;
virtual std::string eth_getBalance(const std::string& param1, const std::string& param2) = 0;
virtual Json::Value eth_getStorage(const std::string& param1, const std::string& param2) = 0;
virtual std::string eth_stateAt(const std::string& param1, const std::string& param2) = 0;
virtual Json::Value eth_storageAt(const std::string& param1) = 0;
virtual double eth_countAt(const std::string& param1) = 0;
virtual double eth_transactionCountByHash(const std::string& param1) = 0;
virtual double eth_transactionCountByNumber(int param1) = 0;

4
libweb3jsonrpc/spec.json

@ -9,10 +9,10 @@
{ "name": "eth_gasPrice", "params": [], "order": [], "returns" : "" },
{ "name": "eth_accounts", "params": [], "order": [], "returns" : [] },
{ "name": "eth_blockNumber", "params": [], "order": [], "returns" : ""},
{ "name": "eth_getBalance", "params": ["", ""], "order": [], "returns" : ""},
{ "name": "eth_getStorage", "params": ["", ""], "order": [], "returns": {}},
{ "name": "eth_balanceAt", "params": [""], "order": [], "returns" : ""},
{ "name": "eth_stateAt", "params": ["", ""], "order": [], "returns": ""},
{ "name": "eth_storageAt", "params": [""], "order": [], "returns": {}},
{ "name": "eth_countAt", "params": [""], "order": [], "returns" : 0.0},
{ "name": "eth_transactionCountByHash", "params": [""], "order": [], "returns" : 0.0},
{ "name": "eth_transactionCountByNumber", "params": [0], "order": [], "returns" : 0.0},

22
test/webthreestubclient.h

@ -92,34 +92,36 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_balanceAt(const std::string& param1) throw (jsonrpc::JsonRpcException)
std::string eth_getBalance(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_balanceAt",p);
p.append(param2);
Json::Value result = this->CallMethod("eth_getBalance",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_stateAt(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
Json::Value eth_getStorage(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
p.append(param2);
Json::Value result = this->CallMethod("eth_stateAt",p);
if (result.isString())
return result.asString();
Json::Value result = this->CallMethod("eth_getStorage",p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value eth_storageAt(const std::string& param1) throw (jsonrpc::JsonRpcException)
std::string eth_stateAt(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_storageAt",p);
if (result.isObject())
return result;
p.append(param2);
Json::Value result = this->CallMethod("eth_stateAt",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}

Loading…
Cancel
Save