Browse Source

eth_getBlock returns also transactions

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
1fc21d8ae5
  1. 10
      libethereum/Client.cpp
  2. 1
      libethereum/Client.h
  3. 1
      libethereum/Interface.h
  4. 27
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  5. 4
      libweb3jsonrpc/WebThreeStubServerBase.h
  6. 12
      libweb3jsonrpc/abstractwebthreestubserver.h
  7. 4
      libweb3jsonrpc/spec.json
  8. 6
      test/webthreestubclient.h

10
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 {

1
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;

1
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]:

27
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)

4
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);

12
libweb3jsonrpc/abstractwebthreestubserver.h

@ -32,8 +32,8 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
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_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::AbstractServer<AbstractWebThr
}
inline virtual void eth_getBlockByHashI(const Json::Value &request, Json::Value &response)
{
response = this->eth_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::AbstractServer<AbstractWebThr
virtual std::string eth_sendTransaction(const Json::Value& param1) = 0;
virtual std::string eth_call(const Json::Value& param1) = 0;
virtual bool eth_flush() = 0;
virtual Json::Value eth_getBlockByHash(const std::string& param1) = 0;
virtual Json::Value eth_getBlockByNumber(const std::string& param1) = 0;
virtual Json::Value eth_getBlockByHash(const std::string& param1, bool param2) = 0;
virtual Json::Value eth_getBlockByNumber(const std::string& param1, bool param2) = 0;
virtual Json::Value eth_getTransactionByHash(const std::string& param1) = 0;
virtual Json::Value eth_getTransactionByBlockHashAndIndex(const std::string& param1, const std::string& param2) = 0;
virtual Json::Value eth_getTransactionByBlockNumberAndIndex(const std::string& param1, const std::string& param2) = 0;

4
libweb3jsonrpc/spec.json

@ -21,8 +21,8 @@
{ "name": "eth_sendTransaction", "params": [{}], "order": [], "returns": ""},
{ "name": "eth_call", "params": [{}], "order": [], "returns": ""},
{ "name": "eth_flush", "params": [], "order": [], "returns" : true},
{ "name": "eth_getBlockByHash", "params": [""],"order": [], "returns": {}},
{ "name": "eth_getBlockByNumber", "params": [""],"order": [], "returns": {}},
{ "name": "eth_getBlockByHash", "params": ["", false],"order": [], "returns": {}},
{ "name": "eth_getBlockByNumber", "params": ["", false],"order": [], "returns": {}},
{ "name": "eth_getTransactionByHash", "params": [""], "order": [], "returns": {}},
{ "name": "eth_getTransactionByBlockHashAndIndex", "params": ["", ""], "order": [], "returns": {}},
{ "name": "eth_getTransactionByBlockNumberAndIndex", "params": ["", ""], "order": [], "returns": {}},

6
test/webthreestubclient.h

@ -218,20 +218,22 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value eth_getBlockByHash(const std::string& param1) throw (jsonrpc::JsonRpcException)
Json::Value eth_getBlockByHash(const std::string& param1, bool param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
p.append(param2);
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_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;

Loading…
Cancel
Save