From 32d404a32b87e64737431f696a727fd824bdb664 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 5 Mar 2015 01:34:02 +0100 Subject: [PATCH] jsonrpc api changes in progress3 --- libweb3jsonrpc/WebThreeStubServerBase.cpp | 93 ++++++++++++++++----- libweb3jsonrpc/WebThreeStubServerBase.h | 10 +-- libweb3jsonrpc/abstractwebthreestubserver.h | 32 +++---- libweb3jsonrpc/spec.json | 9 +- test/webthreestubclient.h | 30 +++---- 5 files changed, 114 insertions(+), 60 deletions(-) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index 9f73c46bd..313622564 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -307,7 +307,7 @@ std::string WebThreeStubServerBase::eth_getBalance(string const& _address, strin } -Json::Value WebThreeStubServerBase::eth_getStorage(string const& _address, std::string const& _blockNumber) +Json::Value WebThreeStubServerBase::eth_getStorage(string const& _address, string const& _blockNumber) { Address address; int number; @@ -326,6 +326,77 @@ Json::Value WebThreeStubServerBase::eth_getStorage(string const& _address, std:: return toJson(client()->storageAt(address, number)); } +std::string WebThreeStubServerBase::eth_getStorageAt(string const& _address, string const& _position, string const& _blockNumber) +{ + Address address; + u256 position; + int number; + + try + { + address = jsToAddress(_address); + position = jsToU256(_position); + number = jsToInt(_blockNumber); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + //TODO: fix this naming ! + return toJS(client()->stateAt(address, position, number)); +} + +string WebThreeStubServerBase::eth_getTransactionCount(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()->countAt(address, number)); +} + +string WebThreeStubServerBase::eth_getBlockTransactionCountByHash(std::string const& _blockHash) +{ + h256 hash; + try + { + hash = jsToFixed<32>(_blockHash); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJS(client()->transactionCount(hash)); +} + + +string WebThreeStubServerBase::eth_getBlockTransactionCountByNumber(string const& _blockNumber) +{ + int number; + + try + { + number = jsToInt(_blockNumber); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJS(client()->transactionCount(client()->hashFromNumber(number))); +} + std::string WebThreeStubServerBase::shh_addToGroup(std::string const& _group, std::string const& _who) { (void)_group; @@ -425,21 +496,6 @@ std::string WebThreeStubServerBase::eth_codeAt(string const& _address) return jsFromBinary(client()->codeAt(jsToAddress(_address), client()->getDefault())); } -double WebThreeStubServerBase::eth_countAt(string const& _address) -{ - return (double)(uint64_t)client()->countAt(jsToAddress(_address), client()->getDefault()); -} - -double WebThreeStubServerBase::eth_transactionCountByHash(std::string const& _hash) -{ - return client()->transactionCount(jsToFixed<32>(_hash)); -} - -double WebThreeStubServerBase::eth_transactionCountByNumber(int _number) -{ - return client()->transactionCount(client()->hashFromNumber(_number)); -} - double WebThreeStubServerBase::eth_uncleCountByHash(std::string const& _hash) { return client()->transactionCount(jsToFixed<32>(_hash)); @@ -692,11 +748,6 @@ bool WebThreeStubServerBase::shh_uninstallFilter(int _id) return true; } -std::string WebThreeStubServerBase::eth_stateAt(string const& _address, string const& _storage) -{ - return toJS(client()->stateAt(jsToAddress(_address), jsToU256(_storage), client()->getDefault())); -} - std::string WebThreeStubServerBase::eth_transact(Json::Value const& _json) { std::string ret; diff --git a/libweb3jsonrpc/WebThreeStubServerBase.h b/libweb3jsonrpc/WebThreeStubServerBase.h index 3051f1dd7..dd9351dde 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.h +++ b/libweb3jsonrpc/WebThreeStubServerBase.h @@ -79,10 +79,12 @@ public: 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_getStorageAt(std::string const& _address, std::string const& _position, std::string const& _blockNumber); + virtual std::string eth_getTransactionCount(std::string const& _address, std::string const& _blockNumber); + virtual std::string eth_getBlockTransactionCountByHash(std::string const& _blockHash); + virtual std::string eth_getBlockTransactionCountByNumber(std::string const& _blockNumber); - 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); @@ -90,9 +92,7 @@ public: virtual std::string eth_codeAt(std::string const& _address); 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); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index 156b8f286..65a2d70f2 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -22,10 +22,10 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(jsonrpc::Procedure("eth_blockNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_blockNumberI); 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_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); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getStorageAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getStorageAtI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getTransactionCount", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getTransactionCountI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockTransactionCountByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockTransactionCountByHashI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockTransactionCountByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockTransactionCountByNumberI); this->bindAndAddMethod(jsonrpc::Procedure("eth_uncleCountByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_uncleCountByHashI); this->bindAndAddMethod(jsonrpc::Procedure("eth_uncleCountByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_uncleCountByNumberI); this->bindAndAddMethod(jsonrpc::Procedure("eth_codeAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_codeAtI); @@ -115,21 +115,21 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServereth_getStorage(request[0u].asString(), request[1u].asString()); } - inline virtual void eth_stateAtI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getStorageAtI(const Json::Value &request, Json::Value &response) { - response = this->eth_stateAt(request[0u].asString(), request[1u].asString()); + response = this->eth_getStorageAt(request[0u].asString(), request[1u].asString(), request[2u].asString()); } - inline virtual void eth_countAtI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getTransactionCountI(const Json::Value &request, Json::Value &response) { - response = this->eth_countAt(request[0u].asString()); + response = this->eth_getTransactionCount(request[0u].asString(), request[1u].asString()); } - inline virtual void eth_transactionCountByHashI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getBlockTransactionCountByHashI(const Json::Value &request, Json::Value &response) { - response = this->eth_transactionCountByHash(request[0u].asString()); + response = this->eth_getBlockTransactionCountByHash(request[0u].asString()); } - inline virtual void eth_transactionCountByNumberI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getBlockTransactionCountByNumberI(const Json::Value &request, Json::Value &response) { - response = this->eth_transactionCountByNumber(request[0u].asInt()); + response = this->eth_getBlockTransactionCountByNumber(request[0u].asString()); } inline virtual void eth_uncleCountByHashI(const Json::Value &request, Json::Value &response) { @@ -305,10 +305,10 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerCallMethod("eth_stateAt",p); + p.append(param3); + Json::Value result = this->CallMethod("eth_getStorageAt",p); if (result.isString()) return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - double eth_countAt(const std::string& param1) throw (jsonrpc::JsonRpcException) + std::string eth_getTransactionCount(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_countAt",p); - if (result.isDouble()) - return result.asDouble(); + p.append(param2); + Json::Value result = this->CallMethod("eth_getTransactionCount",p); + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - double eth_transactionCountByHash(const std::string& param1) throw (jsonrpc::JsonRpcException) + std::string eth_getBlockTransactionCountByHash(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_transactionCountByHash",p); - if (result.isDouble()) - return result.asDouble(); + Json::Value result = this->CallMethod("eth_getBlockTransactionCountByHash",p); + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - double eth_transactionCountByNumber(int param1) throw (jsonrpc::JsonRpcException) + std::string eth_getBlockTransactionCountByNumber(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_transactionCountByNumber",p); - if (result.isDouble()) - return result.asDouble(); + Json::Value result = this->CallMethod("eth_getBlockTransactionCountByNumber",p); + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); }