From 1fc21d8ae59071048ddabc98f41ad1d2c33e8441 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 5 Mar 2015 12:19:16 +0100 Subject: [PATCH] eth_getBlock returns also transactions --- libethereum/Client.cpp | 10 ++++++++ libethereum/Client.h | 1 + libethereum/Interface.h | 1 + libweb3jsonrpc/WebThreeStubServerBase.cpp | 27 ++++++++++++++++++--- libweb3jsonrpc/WebThreeStubServerBase.h | 4 +-- libweb3jsonrpc/abstractwebthreestubserver.h | 12 ++++----- libweb3jsonrpc/spec.json | 4 +-- test/webthreestubclient.h | 6 +++-- 8 files changed, 50 insertions(+), 15 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 47b6088fb..a7b6a1796 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -745,6 +745,16 @@ unsigned Client::uncleCount(h256 _blockHash) const return b[2].itemCount(); } +Transactions Client::transactions(h256 _blockHash) const +{ + auto bl = m_bc.block(_blockHash); + RLP b(bl); + Transactions res; + for (unsigned i = 0; i < b[1].itemCount(); i++) + res.emplace_back(b[1][i].data(), CheckSignature::Range); + return res; +} + LocalisedLogEntries Client::logs(unsigned _watchId) const { try { diff --git a/libethereum/Client.h b/libethereum/Client.h index 360f9517e..a99f3eff2 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -240,6 +240,7 @@ public: virtual BlockInfo uncle(h256 _blockHash, unsigned _i) const; virtual unsigned transactionCount(h256 _blockHash) const; virtual unsigned uncleCount(h256 _blockHash) const; + virtual Transactions transactions(h256 _blockHash) const; /// Differences between transactions. using Interface::diff; diff --git a/libethereum/Interface.h b/libethereum/Interface.h index 847c181e0..b2f708352 100644 --- a/libethereum/Interface.h +++ b/libethereum/Interface.h @@ -105,6 +105,7 @@ public: virtual BlockInfo uncle(h256 _blockHash, unsigned _i) const = 0; virtual unsigned transactionCount(h256 _blockHash) const = 0; virtual unsigned uncleCount(h256 _blockHash) const = 0; + virtual Transactions transactions(h256 _blockHash) const = 0; // [EXTRA API]: diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index 73f012999..c373c0e6c 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -79,6 +79,15 @@ static Json::Value toJson(dev::eth::Transaction const& _t) return res; } +static Json::Value toJson(dev::eth::BlockInfo const& _bi, Transactions const& _ts) +{ + Json::Value res = toJson(_bi); + res["transactions"] = Json::Value(Json::arrayValue); + for (Transaction const& t: _ts) + res["transactions"].append(toJson(t)); + return res; +} + static Json::Value toJson(dev::eth::TransactionSkeleton const& _t) { Json::Value res; @@ -97,6 +106,7 @@ static Json::Value toJson(dev::eth::LocalisedLogEntry const& _e) res["data"] = jsFromBinary(_e.data); res["address"] = toJS(_e.address); + res["topic"] = Json::Value(Json::arrayValue); for (auto const& t: _e.topics) res["topic"].append(toJS(t)); res["number"] = _e.number; @@ -223,6 +233,7 @@ static Json::Value toJson(h256 const& _h, shh::Envelope const& _e, shh::Message res["sent"] = (int)_e.sent(); res["ttl"] = (int)_e.ttl(); res["workProved"] = (int)_e.workProved(); + res["topic"] = Json::Value(Json::arrayValue); for (auto const& t: _e.topic()) res["topic"].append(toJS(t)); res["payload"] = toJS(_m.payload()); @@ -543,7 +554,7 @@ bool WebThreeStubServerBase::eth_flush() return true; } -Json::Value WebThreeStubServerBase::eth_getBlockByHash(string const& _blockHash) +Json::Value WebThreeStubServerBase::eth_getBlockByHash(string const& _blockHash, bool _includeTransactions) { h256 hash; @@ -556,10 +567,14 @@ Json::Value WebThreeStubServerBase::eth_getBlockByHash(string const& _blockHash) throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); } + if (_includeTransactions) { + return toJson(client()->blockInfo(hash), client()->transactions(hash)); + } + return toJson(client()->blockInfo(hash)); } -Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNumber) +Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNumber, bool _includeTransactions) { int number; @@ -572,7 +587,13 @@ Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNum throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS); } - return toJson(client()->blockInfo(client()->hashFromNumber(number))); + h256 hash = client()->hashFromNumber(number); + + if (_includeTransactions) { + return toJson(client()->blockInfo(hash), client()->transactions(hash)); + } + + return toJson(client()->blockInfo(hash)); } Json::Value WebThreeStubServerBase::eth_getTransactionByHash(string const& _transactionHash) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.h b/libweb3jsonrpc/WebThreeStubServerBase.h index 3797fa752..a3e961012 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.h +++ b/libweb3jsonrpc/WebThreeStubServerBase.h @@ -89,8 +89,8 @@ public: 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_getBlockByHash(std::string const& _blockHash, bool _includeTransactions); + virtual Json::Value eth_getBlockByNumber(std::string const& _blockNumber, bool _includeTransactions); virtual Json::Value eth_getTransactionByHash(std::string const& _transactionHash); virtual Json::Value eth_getTransactionByBlockHashAndIndex(std::string const& _blockHash, std::string const& _transactionIndex); virtual Json::Value eth_getTransactionByBlockNumberAndIndex(std::string const& _blockNumber, std::string const& _transactionIndex); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index a454c2bf5..61635f6e0 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -32,8 +32,8 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(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_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_getBlockByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_getBlockByHashI); + this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::eth_getBlockByNumberI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getTransactionByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getTransactionByHashI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getTransactionByBlockHashAndIndex", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getTransactionByBlockHashAndIndexI); this->bindAndAddMethod(jsonrpc::Procedure("eth_getTransactionByBlockNumberAndIndex", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getTransactionByBlockNumberAndIndexI); @@ -157,11 +157,11 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServereth_getBlockByHash(request[0u].asString()); + response = this->eth_getBlockByHash(request[0u].asString(), request[1u].asBool()); } inline virtual void eth_getBlockByNumberI(const Json::Value &request, Json::Value &response) { - response = this->eth_getBlockByNumber(request[0u].asString()); + response = this->eth_getBlockByNumber(request[0u].asString(), request[1u].asBool()); } inline virtual void eth_getTransactionByHashI(const Json::Value &request, Json::Value &response) { @@ -310,8 +310,8 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerCallMethod("eth_getBlockByHash",p); if (result.isObject()) return result; else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_getBlockByNumber(const std::string& param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_getBlockByNumber(const std::string& param1, bool param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); + p.append(param2); Json::Value result = this->CallMethod("eth_getBlockByNumber",p); if (result.isObject()) return result;