Browse Source

jsonrpc api changes in progress

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
5de1cfd16c
  1. 7
      libethcore/CommonJS.h
  2. 46
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  3. 25
      libweb3jsonrpc/WebThreeStubServerBase.h
  4. 61
      libweb3jsonrpc/abstractwebthreestubserver.h
  5. 12
      libweb3jsonrpc/spec.json
  6. 76
      test/webthreestubclient.h

7
libethcore/CommonJS.h

@ -48,6 +48,13 @@ inline std::string toJS(dev::bytes const& _n)
return "0x" + dev::toHex(_n);
}
template< typename T >std::string toJS( T const& i )
{
std::stringstream stream;
stream << "0x" << std::hex << i;
return stream.str();
}
/// Convert string to byte array. Input parameters can be hex or dec. Returns empty array if invalid input e.g neither dec or hex.
bytes jsToBytes(std::string const& _s);
/// Add '0' on the head of @a _b until @a _l.

46
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -24,6 +24,7 @@
// Make sure boost/asio.hpp is included before windows.h.
#include <boost/asio.hpp>
#include <libdevcore/CommonData.h>
#include <libsolidity/CompilerStack.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/SourceReferenceFormatter.h>
@ -389,11 +390,6 @@ double WebThreeStubServerBase::eth_uncleCountByNumber(int _number)
return client()->transactionCount(client()->hashFromNumber(_number));
}
int WebThreeStubServerBase::eth_defaultBlock()
{
return client()->getDefault();
}
std::string WebThreeStubServerBase::eth_gasPrice()
{
return toJS(10 * dev::eth::szabo);
@ -425,7 +421,7 @@ bool WebThreeStubServerBase::shh_haveIdentity(std::string const& _id)
return m_ids.count(jsToPublic(_id)) > 0;
}
bool WebThreeStubServerBase::eth_listening()
bool WebThreeStubServerBase::net_listening()
{
return network()->isNetworkStarted();
}
@ -561,14 +557,14 @@ std::string WebThreeStubServerBase::eth_solidity(std::string const& _code)
return res;
}
int WebThreeStubServerBase::eth_number()
string WebThreeStubServerBase::eth_blockNumber()
{
return client()->number();
return toJS(client()->number());
}
int WebThreeStubServerBase::eth_peerCount()
string WebThreeStubServerBase::net_peerCount()
{
return network()->peerCount();
return toJS(network()->peerCount());
}
bool WebThreeStubServerBase::shh_post(Json::Value const& _json)
@ -600,36 +596,6 @@ bool WebThreeStubServerBase::db_putString(std::string const& _name, std::string
return true;
}
bool WebThreeStubServerBase::eth_setCoinbase(std::string const& _address)
{
client()->setAddress(jsToAddress(_address));
return true;
}
bool WebThreeStubServerBase::eth_setDefaultBlock(int _block)
{
client()->setDefault(_block);
return true;
}
bool WebThreeStubServerBase::eth_setListening(bool _listening)
{
if (_listening)
network()->startNetwork();
else
network()->stopNetwork();
return true;
}
bool WebThreeStubServerBase::eth_setMining(bool _mining)
{
if (_mining)
client()->startMining();
else
client()->stopMining();
return true;
}
Json::Value WebThreeStubServerBase::shh_changed(int _id)
{
Json::Value ret(Json::arrayValue);

25
libweb3jsonrpc/WebThreeStubServerBase.h

@ -68,37 +68,40 @@ public:
WebThreeStubServerBase(jsonrpc::AbstractServerConnector& _conn, std::vector<dev::KeyPair> const& _accounts);
virtual std::string web3_sha3(std::string const& _param1);
virtual std::string net_peerCount();
virtual bool net_listening();
virtual std::string eth_coinbase();
virtual bool eth_mining();
virtual std::string eth_gasPrice();
virtual Json::Value eth_accounts();
virtual std::string eth_blockNumber();
virtual std::string eth_balanceAt(std::string const& _address);
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);
virtual Json::Value eth_changed(int _id);
virtual std::string eth_codeAt(std::string const& _address);
virtual std::string eth_coinbase();
virtual Json::Value eth_compilers();
virtual double eth_countAt(std::string const& _address);
virtual double eth_transactionCountByHash(std::string const& _hash);
virtual double eth_transactionCountByNumber(int _number);
virtual double eth_uncleCountByHash(std::string const& _hash);
virtual double eth_uncleCountByNumber(int _number);
virtual int eth_defaultBlock();
virtual std::string eth_gasPrice();
virtual Json::Value eth_filterLogs(int _id);
virtual bool eth_flush();
virtual Json::Value eth_logs(Json::Value const& _json);
virtual bool eth_listening();
virtual bool eth_mining();
virtual int eth_newFilter(Json::Value const& _json);
virtual int eth_newFilterString(std::string const& _filter);
virtual int eth_number();
virtual int eth_peerCount();
virtual bool eth_setCoinbase(std::string const& _address);
virtual bool eth_setDefaultBlock(int _block);
virtual bool eth_setListening(bool _listening);
virtual std::string eth_lll(std::string const& _s);
virtual std::string eth_serpent(std::string const& _s);
virtual bool eth_setMining(bool _mining);
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);

61
libweb3jsonrpc/abstractwebthreestubserver.h

@ -13,18 +13,13 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
AbstractWebThreeStubServer(jsonrpc::AbstractServerConnector &conn, jsonrpc::serverVersion_t type = jsonrpc::JSONRPC_SERVER_V2) : jsonrpc::AbstractServer<AbstractWebThreeStubServer>(conn, type)
{
this->bindAndAddMethod(jsonrpc::Procedure("web3_sha3", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::web3_sha3I);
this->bindAndAddMethod(jsonrpc::Procedure("net_peerCount", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::net_peerCountI);
this->bindAndAddMethod(jsonrpc::Procedure("net_listening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::net_listeningI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_coinbase", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_coinbaseI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_setCoinbase", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_setCoinbaseI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_listening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_listeningI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_setListening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_setListeningI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_mining", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_miningI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_setMining", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_setMiningI);
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_peerCount", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_peerCountI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_defaultBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_defaultBlockI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_setDefaultBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_setDefaultBlockI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_number", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_numberI);
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_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);
@ -77,33 +72,26 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
{
response = this->web3_sha3(request[0u].asString());
}
inline virtual void eth_coinbaseI(const Json::Value &request, Json::Value &response)
inline virtual void net_peerCountI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_coinbase();
response = this->net_peerCount();
}
inline virtual void eth_setCoinbaseI(const Json::Value &request, Json::Value &response)
{
response = this->eth_setCoinbase(request[0u].asString());
}
inline virtual void eth_listeningI(const Json::Value &request, Json::Value &response)
inline virtual void net_listeningI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_listening();
response = this->net_listening();
}
inline virtual void eth_setListeningI(const Json::Value &request, Json::Value &response)
inline virtual void eth_coinbaseI(const Json::Value &request, Json::Value &response)
{
response = this->eth_setListening(request[0u].asBool());
(void)request;
response = this->eth_coinbase();
}
inline virtual void eth_miningI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_mining();
}
inline virtual void eth_setMiningI(const Json::Value &request, Json::Value &response)
{
response = this->eth_setMining(request[0u].asBool());
}
inline virtual void eth_gasPriceI(const Json::Value &request, Json::Value &response)
{
(void)request;
@ -114,24 +102,10 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
(void)request;
response = this->eth_accounts();
}
inline virtual void eth_peerCountI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_peerCount();
}
inline virtual void eth_defaultBlockI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_defaultBlock();
}
inline virtual void eth_setDefaultBlockI(const Json::Value &request, Json::Value &response)
{
response = this->eth_setDefaultBlock(request[0u].asInt());
}
inline virtual void eth_numberI(const Json::Value &request, Json::Value &response)
inline virtual void eth_blockNumberI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->eth_number();
response = this->eth_blockNumber();
}
inline virtual void eth_balanceAtI(const Json::Value &request, Json::Value &response)
{
@ -322,18 +296,13 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->shh_getMessages(request[0u].asInt());
}
virtual std::string web3_sha3(const std::string& param1) = 0;
virtual std::string net_peerCount() = 0;
virtual bool net_listening() = 0;
virtual std::string eth_coinbase() = 0;
virtual bool eth_setCoinbase(const std::string& param1) = 0;
virtual bool eth_listening() = 0;
virtual bool eth_setListening(bool param1) = 0;
virtual bool eth_mining() = 0;
virtual bool eth_setMining(bool param1) = 0;
virtual std::string eth_gasPrice() = 0;
virtual Json::Value eth_accounts() = 0;
virtual int eth_peerCount() = 0;
virtual int eth_defaultBlock() = 0;
virtual bool eth_setDefaultBlock(int param1) = 0;
virtual int eth_number() = 0;
virtual std::string eth_blockNumber() = 0;
virtual std::string eth_balanceAt(const std::string& param1) = 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;

12
libweb3jsonrpc/spec.json

@ -1,18 +1,14 @@
[
{ "name": "web3_sha3", "params": [""], "order": [], "returns" : "" },
{ "name": "net_peerCount", "params": [], "order": [], "returns" : "" },
{ "name": "net_listening", "params": [], "order": [], "returns" : false },
{ "name": "eth_coinbase", "params": [], "order": [], "returns" : "" },
{ "name": "eth_setCoinbase", "params": [""], "order": [], "returns" : true },
{ "name": "eth_listening", "params": [], "order": [], "returns" : false },
{ "name": "eth_setListening", "params": [false], "order" : [], "returns" : true },
{ "name": "eth_mining", "params": [], "order": [], "returns" : false },
{ "name": "eth_setMining", "params": [false], "order" : [], "returns" : true },
{ "name": "eth_gasPrice", "params": [], "order": [], "returns" : "" },
{ "name": "eth_accounts", "params": [], "order": [], "returns" : [] },
{ "name": "eth_peerCount", "params": [], "order": [], "returns" : 0 },
{ "name": "eth_defaultBlock", "params": [], "order": [], "returns" : 0},
{ "name": "eth_setDefaultBlock", "params": [0], "order": [], "returns" : true},
{ "name": "eth_number", "params": [], "order": [], "returns" : 0},
{ "name": "eth_blockNumber", "params": [], "order": [], "returns" : ""},
{ "name": "eth_balanceAt", "params": [""], "order": [], "returns" : ""},
{ "name": "eth_stateAt", "params": ["", ""], "order": [], "returns": ""},

76
test/webthreestubclient.h

@ -22,43 +22,33 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_coinbase() throw (jsonrpc::JsonRpcException)
std::string net_peerCount() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_coinbase",p);
Json::Value result = this->CallMethod("net_peerCount",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool eth_setCoinbase(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_setCoinbase",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool eth_listening() throw (jsonrpc::JsonRpcException)
bool net_listening() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_listening",p);
Json::Value result = this->CallMethod("net_listening",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool eth_setListening(bool param1) throw (jsonrpc::JsonRpcException)
std::string eth_coinbase() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_setListening",p);
if (result.isBool())
return result.asBool();
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_coinbase",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
@ -72,16 +62,6 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool eth_setMining(bool param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_setMining",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_gasPrice() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
@ -102,43 +82,13 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
int eth_peerCount() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_peerCount",p);
if (result.isInt())
return result.asInt();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
int eth_defaultBlock() throw (jsonrpc::JsonRpcException)
std::string eth_blockNumber() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_defaultBlock",p);
if (result.isInt())
return result.asInt();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool eth_setDefaultBlock(int param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_setDefaultBlock",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
int eth_number() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("eth_number",p);
if (result.isInt())
return result.asInt();
Json::Value result = this->CallMethod("eth_blockNumber",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}

Loading…
Cancel
Save