From f3e7ab987c8ce6f228204f32d3341b2d94b2b770 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 5 Mar 2015 02:02:42 +0100 Subject: [PATCH] jsonrpc api changes in progress4 --- libweb3jsonrpc/WebThreeStubServerBase.cpp | 211 +++++++++++++------- libweb3jsonrpc/WebThreeStubServerBase.h | 19 +- libweb3jsonrpc/abstractwebthreestubserver.h | 48 ++--- libweb3jsonrpc/spec.json | 16 +- test/webthreestubclient.h | 33 +-- 5 files changed, 194 insertions(+), 133 deletions(-) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index 313622564..fd1441c1a 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -368,6 +368,7 @@ string WebThreeStubServerBase::eth_getTransactionCount(string const& _address, s string WebThreeStubServerBase::eth_getBlockTransactionCountByHash(std::string const& _blockHash) { h256 hash; + try { hash = jsToFixed<32>(_blockHash); @@ -397,21 +398,54 @@ string WebThreeStubServerBase::eth_getBlockTransactionCountByNumber(string const return toJS(client()->transactionCount(client()->hashFromNumber(number))); } -std::string WebThreeStubServerBase::shh_addToGroup(std::string const& _group, std::string const& _who) +string WebThreeStubServerBase::eth_getUncleCountByBlockHash(std::string const& _blockHash) { - (void)_group; - (void)_who; - return ""; + h256 hash; + + try + { + hash = jsToFixed<32>(_blockHash); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJS(client()->uncleCount(hash)); } -Json::Value WebThreeStubServerBase::eth_blockByHash(std::string const& _hash) +string WebThreeStubServerBase::eth_getUncleCountByBlockNumber(string const& _blockNumber) { - return toJson(client()->blockInfo(jsToFixed<32>(_hash))); + int number; + + try + { + number = jsToInt(_blockNumber); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJS(client()->uncleCount(client()->hashFromNumber(number))); } -Json::Value WebThreeStubServerBase::eth_blockByNumber(int _number) +std::string WebThreeStubServerBase::eth_getData(string const& _address, string const& _blockNumber) { - return toJson(client()->blockInfo(client()->hashFromNumber(_number))); + Address address; + int number; + + try + { + address = jsToAddress(_address); + number = jsToInt(_blockNumber); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return jsFromBinary(client()->codeAt(address, number)); } static TransactionSkeleton toTransaction(Json::Value const& _json) @@ -419,58 +453,77 @@ static TransactionSkeleton toTransaction(Json::Value const& _json) TransactionSkeleton ret; if (!_json.isObject() || _json.empty()) return ret; - + if (_json["from"].isString()) ret.from = jsToAddress(_json["from"].asString()); if (_json["to"].isString()) ret.to = jsToAddress(_json["to"].asString()); else ret.creation = true; - if (!_json["value"].empty()) - { - if (_json["value"].isString()) - ret.value = jsToU256(_json["value"].asString()); - else if (_json["value"].isInt()) - ret.value = u256(_json["value"].asInt()); - } - if (!_json["gas"].empty()) - { - if (_json["gas"].isString()) - ret.gas = jsToU256(_json["gas"].asString()); - else if (_json["gas"].isInt()) - ret.gas = u256(_json["gas"].asInt()); - } - if (!_json["gasPrice"].empty()) - { - if (_json["gasPrice"].isString()) - ret.gasPrice = jsToU256(_json["gasPrice"].asString()); - else if (_json["gasPrice"].isInt()) - ret.gas = u256(_json["gas"].asInt()); - } - if (!_json["data"].empty()) - { - if (_json["data"].isString()) // ethereum.js has preconstructed the data array - ret.data = jsToBytes(_json["data"].asString()); - else if (_json["data"].isArray()) // old style: array of 32-byte-padded values. TODO: remove PoC-8 - for (auto i: _json["data"]) - dev::operator +=(ret.data, padded(jsToBytes(i.asString()), 32)); - } + + if (_json["value"].isString()) + ret.value = jsToU256(_json["value"].asString()); + + if (_json["gas"].isString()) + ret.gas = jsToU256(_json["gas"].asString()); + if (_json["gasPrice"].isString()) + ret.gasPrice = jsToU256(_json["gasPrice"].asString()); + + if (_json["data"].isString()) // ethereum.js has preconstructed the data array + ret.data = jsToBytes(_json["data"].asString()); + if (_json["code"].isString()) ret.data = jsToBytes(_json["code"].asString()); return ret; } -bool WebThreeStubServerBase::eth_flush() +std::string WebThreeStubServerBase::eth_sendTransaction(Json::Value const& _json) { - client()->flushTransactions(); - return true; + TransactionSkeleton t; + + try + { + t = toTransaction(_json); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + std::string ret; + if (!t.from) + t.from = m_accounts->getDefaultTransactAccount(); + if (t.creation) + ret = toJS(right160(sha3(rlpList(t.from, client()->countAt(t.from)))));; + if (!t.gasPrice) + t.gasPrice = 10 * dev::eth::szabo; + if (!t.gas) + t.gas = min(client()->gasLimitRemaining(), client()->balanceAt(t.from) / t.gasPrice); + + if (m_accounts->isRealAccount(t.from)) + authenticate(t, false); + else if (m_accounts->isProxyAccount(t.from)) + authenticate(t, true); + + return ret; } + std::string WebThreeStubServerBase::eth_call(Json::Value const& _json) { + TransactionSkeleton t; + + try + { + t = toTransaction(_json); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + std::string ret; - TransactionSkeleton t = toTransaction(_json); if (!t.from) t.from = m_accounts->getDefaultTransactAccount(); if (!m_accounts->isRealAccount(t.from)) @@ -480,30 +533,61 @@ std::string WebThreeStubServerBase::eth_call(Json::Value const& _json) if (!t.gas) t.gas = min(client()->gasLimitRemaining(), client()->balanceAt(t.from) / t.gasPrice); ret = toJS(client()->call(m_accounts->secretKey(t.from), t.value, t.to, t.data, t.gas, t.gasPrice)); + return ret; } -Json::Value WebThreeStubServerBase::eth_changed(int _id) +bool WebThreeStubServerBase::eth_flush() { - auto entries = client()->checkWatch(_id); - if (entries.size()) - cnote << "FIRING WATCH" << _id << entries.size(); - return toJson(entries); + client()->flushTransactions(); + return true; } -std::string WebThreeStubServerBase::eth_codeAt(string const& _address) +Json::Value WebThreeStubServerBase::eth_getBlockByHash(string const& _blockHash) { - return jsFromBinary(client()->codeAt(jsToAddress(_address), client()->getDefault())); + h256 hash; + + try + { + hash = jsToFixed<32>(_blockHash); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJson(client()->blockInfo(hash)); } -double WebThreeStubServerBase::eth_uncleCountByHash(std::string const& _hash) +Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNumber) { - return client()->transactionCount(jsToFixed<32>(_hash)); + int number; + + try + { + number = jsToInt(_blockNumber); + } + catch (...) + { + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); + } + + return toJson(client()->blockInfo(client()->hashFromNumber(number))); } -double WebThreeStubServerBase::eth_uncleCountByNumber(int _number) +std::string WebThreeStubServerBase::shh_addToGroup(std::string const& _group, std::string const& _who) { - return client()->transactionCount(client()->hashFromNumber(_number)); + (void)_group; + (void)_who; + return ""; +} + +Json::Value WebThreeStubServerBase::eth_changed(int _id) +{ + auto entries = client()->checkWatch(_id); + if (entries.size()) + cnote << "FIRING WATCH" << _id << entries.size(); + return toJson(entries); } std::string WebThreeStubServerBase::db_get(std::string const& _name, std::string const& _key) @@ -748,27 +832,6 @@ bool WebThreeStubServerBase::shh_uninstallFilter(int _id) return true; } -std::string WebThreeStubServerBase::eth_transact(Json::Value const& _json) -{ - std::string ret; - TransactionSkeleton t = toTransaction(_json); - if (!t.from) - t.from = m_accounts->getDefaultTransactAccount(); - if (t.creation) - ret = toJS(right160(sha3(rlpList(t.from, client()->countAt(t.from)))));; - if (!t.gasPrice) - t.gasPrice = 10 * dev::eth::szabo; - if (!t.gas) - t.gas = min(client()->gasLimitRemaining(), client()->balanceAt(t.from) / t.gasPrice); - - if (m_accounts->isRealAccount(t.from)) - authenticate(t, false); - else if (m_accounts->isProxyAccount(t.from)) - authenticate(t, true); - - return ret; -} - void WebThreeStubServerBase::authenticate(TransactionSkeleton const& _t, bool _toProxy) { if (_toProxy) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.h b/libweb3jsonrpc/WebThreeStubServerBase.h index dd9351dde..7cf975ee6 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.h +++ b/libweb3jsonrpc/WebThreeStubServerBase.h @@ -83,21 +83,21 @@ public: 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_getUncleCountByBlockHash(std::string const& _blockHash); + virtual std::string eth_getUncleCountByBlockNumber(std::string const& _blockNumber); + virtual std::string eth_getData(std::string const& _address, std::string const& _blockNumber); + virtual std::string eth_sendTransaction(Json::Value const& _json); + virtual std::string eth_call(Json::Value const& _json); + virtual bool eth_flush(); + virtual Json::Value eth_getBlockByHash(std::string const& _blockHash); + virtual Json::Value eth_getBlockByNumber(std::string const& _blockNumber); + - 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 Json::Value eth_compilers(); - virtual double eth_uncleCountByHash(std::string const& _hash); - virtual double eth_uncleCountByNumber(int _number); - virtual Json::Value eth_filterLogs(int _id); - virtual bool eth_flush(); virtual Json::Value eth_logs(Json::Value const& _json); @@ -106,7 +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_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); virtual Json::Value eth_uncleByHash(std::string const& _hash, int _i); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index 65a2d70f2..3611d2330 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -26,14 +26,14 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(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); - this->bindAndAddMethod(jsonrpc::Procedure("eth_transact", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::eth_transactI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getUncleCountByBlockHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getUncleCountByBlockHashI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getUncleCountByBlockNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getUncleCountByBlockNumberI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getData", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getDataI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_sendTransaction", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::eth_sendTransactionI); this->bindAndAddMethod(jsonrpc::Procedure("eth_call", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::eth_callI); this->bindAndAddMethod(jsonrpc::Procedure("eth_flush", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_flushI); - this->bindAndAddMethod(jsonrpc::Procedure("eth_blockByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_blockByHashI); - this->bindAndAddMethod(jsonrpc::Procedure("eth_blockByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_blockByNumberI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockByHashI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockByNumberI); this->bindAndAddMethod(jsonrpc::Procedure("eth_transactionByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_transactionByHashI); this->bindAndAddMethod(jsonrpc::Procedure("eth_transactionByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_INTEGER,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_transactionByNumberI); this->bindAndAddMethod(jsonrpc::Procedure("eth_uncleByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_uncleByHashI); @@ -131,21 +131,21 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServereth_getBlockTransactionCountByNumber(request[0u].asString()); } - inline virtual void eth_uncleCountByHashI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getUncleCountByBlockHashI(const Json::Value &request, Json::Value &response) { - response = this->eth_uncleCountByHash(request[0u].asString()); + response = this->eth_getUncleCountByBlockHash(request[0u].asString()); } - inline virtual void eth_uncleCountByNumberI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getUncleCountByBlockNumberI(const Json::Value &request, Json::Value &response) { - response = this->eth_uncleCountByNumber(request[0u].asInt()); + response = this->eth_getUncleCountByBlockNumber(request[0u].asString()); } - inline virtual void eth_codeAtI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getDataI(const Json::Value &request, Json::Value &response) { - response = this->eth_codeAt(request[0u].asString()); + response = this->eth_getData(request[0u].asString(), request[1u].asString()); } - inline virtual void eth_transactI(const Json::Value &request, Json::Value &response) + inline virtual void eth_sendTransactionI(const Json::Value &request, Json::Value &response) { - response = this->eth_transact(request[0u]); + response = this->eth_sendTransaction(request[0u]); } inline virtual void eth_callI(const Json::Value &request, Json::Value &response) { @@ -156,13 +156,13 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServereth_flush(); } - inline virtual void eth_blockByHashI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getBlockByHashI(const Json::Value &request, Json::Value &response) { - response = this->eth_blockByHash(request[0u].asString()); + response = this->eth_getBlockByHash(request[0u].asString()); } - inline virtual void eth_blockByNumberI(const Json::Value &request, Json::Value &response) + inline virtual void eth_getBlockByNumberI(const Json::Value &request, Json::Value &response) { - response = this->eth_blockByNumber(request[0u].asInt()); + response = this->eth_getBlockByNumber(request[0u].asString()); } inline virtual void eth_transactionByHashI(const Json::Value &request, Json::Value &response) { @@ -309,14 +309,14 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerCallMethod("eth_uncleCountByHash",p); - if (result.isDouble()) - return result.asDouble(); + Json::Value result = this->CallMethod("eth_getUncleCountByBlockHash",p); + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - double eth_uncleCountByNumber(int param1) throw (jsonrpc::JsonRpcException) + std::string eth_getUncleCountByBlockNumber(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_uncleCountByNumber",p); - if (result.isDouble()) - return result.asDouble(); + Json::Value result = this->CallMethod("eth_getUncleCountByBlockNumber",p); + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - std::string eth_codeAt(const std::string& param1) throw (jsonrpc::JsonRpcException) + std::string eth_getData(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_codeAt",p); + p.append(param2); + Json::Value result = this->CallMethod("eth_getData",p); if (result.isString()) return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - std::string eth_transact(const Json::Value& param1) throw (jsonrpc::JsonRpcException) + std::string eth_sendTransaction(const Json::Value& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_transact",p); + Json::Value result = this->CallMethod("eth_sendTransaction",p); if (result.isString()) return result.asString(); else @@ -217,21 +218,21 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_blockByHash(const std::string& param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_getBlockByHash(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_blockByHash",p); + Json::Value result = this->CallMethod("eth_getBlockByHash",p); if (result.isObject()) return result; else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_blockByNumber(int param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_getBlockByNumber(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); - Json::Value result = this->CallMethod("eth_blockByNumber",p); + Json::Value result = this->CallMethod("eth_getBlockByNumber",p); if (result.isObject()) return result; else