From 2ff5f57355e016bee7ff2de0d97bc8bfd6c6f181 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 15 Jul 2015 17:18:34 +0200 Subject: [PATCH] getLogsEx && propert order of returned Ex logs --- libweb3jsonrpc/JsonHelper.cpp | 23 ++++++++++++++------- libweb3jsonrpc/JsonHelper.h | 2 +- libweb3jsonrpc/WebThreeStubServerBase.cpp | 14 ++++++++++++- libweb3jsonrpc/WebThreeStubServerBase.h | 1 + libweb3jsonrpc/abstractwebthreestubserver.h | 6 ++++++ libweb3jsonrpc/spec.json | 1 + test/libweb3jsonrpc/webthreestubclient.h | 10 +++++++++ 7 files changed, 47 insertions(+), 10 deletions(-) diff --git a/libweb3jsonrpc/JsonHelper.cpp b/libweb3jsonrpc/JsonHelper.cpp index 10bd50ea3..9ffb1db0d 100644 --- a/libweb3jsonrpc/JsonHelper.cpp +++ b/libweb3jsonrpc/JsonHelper.cpp @@ -277,13 +277,14 @@ Json::Value toJson(dev::eth::LogEntry const& _e) return res; } -Json::Value toJson(std::map const& _entriesByBlock) +Json::Value toJson(std::unordered_map const& _entriesByBlock, vector const& _order) { Json::Value res(Json::arrayValue); - for (auto const& i: _entriesByBlock) + for (auto const& i: _order) { + auto entries = _entriesByBlock.at(i); Json::Value currentBlock(Json::objectValue); - LocalisedLogEntry entry = i.second[0]; + LocalisedLogEntry entry = entries[0]; if (entry.mined) { @@ -294,18 +295,20 @@ Json::Value toJson(std::map const& _entries else currentBlock["type"] = "pending"; + currentBlock["polarity"] = entry.polarity == BlockPolarity::Live ? true : false; currentBlock["logs"] = Json::Value(Json::arrayValue); - for (LocalisedLogEntry const& e: i.second) + for (LocalisedLogEntry const& e: entries) { Json::Value log(Json::objectValue); log["logIndex"] = e.logIndex; - log["polarity"] = e.polarity == BlockPolarity::Live ? true : false; log["transactionIndex"] = e.transactionIndex; log["transactionHash"] = toJS(e.transactionHash); log["address"] = toJS(e.address); log["data"] = toJS(e.data); - log["topics"] = toJS(e.topics); + log["topics"] = Json::Value(Json::arrayValue); + for (auto const& t: e.topics) + log["topics"].append(toJS(t)); currentBlock["logs"].append(log); } @@ -318,7 +321,8 @@ Json::Value toJson(std::map const& _entries Json::Value toJsonByBlock(LocalisedLogEntries const& _entries) { - map entriesByBlock; + vector order; + unordered_map entriesByBlock; for (dev::eth::LocalisedLogEntry const& e: _entries) { @@ -326,12 +330,15 @@ Json::Value toJsonByBlock(LocalisedLogEntries const& _entries) continue; if (entriesByBlock.count(e.blockHash) == 0) + { entriesByBlock[e.blockHash] = LocalisedLogEntries(); + order.push_back(e.blockHash); + } entriesByBlock[e.blockHash].push_back(e); } - return toJson(entriesByBlock); + return toJson(entriesByBlock, order); } TransactionSkeleton toTransactionSkeleton(Json::Value const& _json) diff --git a/libweb3jsonrpc/JsonHelper.h b/libweb3jsonrpc/JsonHelper.h index 1cf633a2f..a5688d763 100644 --- a/libweb3jsonrpc/JsonHelper.h +++ b/libweb3jsonrpc/JsonHelper.h @@ -62,7 +62,7 @@ Json::Value toJson(TransactionReceipt const& _t); Json::Value toJson(LocalisedTransactionReceipt const& _t); Json::Value toJson(LocalisedLogEntry const& _e); Json::Value toJson(LogEntry const& _e); -Json::Value toJson(std::map const& _entriesByBlock); +Json::Value toJson(std::unordered_map const& _entriesByBlock); Json::Value toJsonByBlock(LocalisedLogEntries const& _entries); TransactionSkeleton toTransactionSkeleton(Json::Value const& _json); LogFilter toLogFilter(Json::Value const& _json); diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index 8b1962941..a223c37d4 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -725,7 +725,19 @@ Json::Value WebThreeStubServerBase::eth_getLogs(Json::Value const& _json) { try { - return toJson(client()->logs(toLogFilter(_json))); + return toJson(client()->logs(toLogFilter(_json, *client()))); + } + catch (...) + { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + +Json::Value WebThreeStubServerBase::eth_getLogsEx(Json::Value const& _json) +{ + try + { + return toJsonByBlock(client()->logs(toLogFilter(_json))); } catch (...) { diff --git a/libweb3jsonrpc/WebThreeStubServerBase.h b/libweb3jsonrpc/WebThreeStubServerBase.h index d90015aec..62209a5b4 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.h +++ b/libweb3jsonrpc/WebThreeStubServerBase.h @@ -136,6 +136,7 @@ public: virtual Json::Value eth_getFilterLogs(std::string const& _filterId); virtual Json::Value eth_getFilterLogsEx(std::string const& _filterId); virtual Json::Value eth_getLogs(Json::Value const& _json); + virtual Json::Value eth_getLogsEx(Json::Value const& _json); virtual Json::Value eth_getWork(); virtual bool eth_submitWork(std::string const& _nonce, std::string const&, std::string const& _mixHash); virtual std::string eth_register(std::string const& _address); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index 988271c95..48421b940 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -57,6 +57,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(jsonrpc::Procedure("eth_getFilterLogs", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getFilterLogsI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getFilterLogsEx", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getFilterLogsExI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getLogs", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::eth_getLogsI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getLogsEx", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::eth_getLogsExI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getWork", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::eth_getWorkI); this->bindAndAddMethod(jsonrpc::Procedure("eth_submitWork", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_submitWorkI); this->bindAndAddMethod(jsonrpc::Procedure("eth_register", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_registerI); @@ -296,6 +297,10 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServereth_getLogs(request[0u]); } + inline virtual void eth_getLogsExI(const Json::Value &request, Json::Value &response) + { + response = this->eth_getLogsEx(request[0u]); + } inline virtual void eth_getWorkI(const Json::Value &request, Json::Value &response) { (void)request; @@ -511,6 +516,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerCallMethod("eth_getLogsEx",p); + if (result.isArray()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } Json::Value eth_getWork() throw (jsonrpc::JsonRpcException) { Json::Value p;