Browse Source

getBlockBy** and getBlockTransactionCountBy** return null if block is not found

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
0c23935877
  1. 9
      libethereum/ClientBase.cpp
  2. 26
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  3. 4
      libweb3jsonrpc/WebThreeStubServerBase.h
  4. 8
      libweb3jsonrpc/abstractwebthreestubserver.h
  5. 4
      libweb3jsonrpc/spec.json
  6. 12
      test/libweb3jsonrpc/webthreestubclient.h

9
libethereum/ClientBase.cpp

@ -470,12 +470,17 @@ int ClientBase::compareBlockHashes(h256 _h1, h256 _h2) const
bool ClientBase::isKnown(h256 _hash) const
{
return bc().isKnown(_hash);
return _hash == PendingBlockHash ||
_hash == LatestBlockHash ||
_hash == EarliestBlockHash ||
bc().isKnown(_hash);
}
bool ClientBase::isKnown(BlockNumber _block) const
{
return bc().numberHash(_block) != h256();
return _block == PendingBlock ||
_block == LatestBlock ||
bc().numberHash(_block) != h256();
}
bool ClientBase::isKnownTransaction(h256 _transactionHash) const

26
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -162,11 +162,15 @@ string WebThreeStubServerBase::eth_getTransactionCount(string const& _address, s
}
}
string WebThreeStubServerBase::eth_getBlockTransactionCountByHash(string const& _blockHash)
Json::Value WebThreeStubServerBase::eth_getBlockTransactionCountByHash(string const& _blockHash)
{
try
{
return toJS(client()->transactionCount(jsToFixed<32>(_blockHash)));
h256 blockHash = jsToFixed<32>(_blockHash);
if (!client()->isKnown(blockHash))
return Json::Value(Json::nullValue);
return toJS(client()->transactionCount(blockHash));
}
catch (...)
{
@ -174,10 +178,14 @@ string WebThreeStubServerBase::eth_getBlockTransactionCountByHash(string const&
}
}
string WebThreeStubServerBase::eth_getBlockTransactionCountByNumber(string const& _blockNumber)
Json::Value WebThreeStubServerBase::eth_getBlockTransactionCountByNumber(string const& _blockNumber)
{
try
{
BlockNumber blockNumber = jsToBlockNumber(_blockNumber);
if (!client()->isKnown(blockNumber))
return Json::Value(Json::nullValue);
return toJS(client()->transactionCount(jsToBlockNumber(_blockNumber)));
}
catch (...)
@ -193,6 +201,7 @@ Json::Value WebThreeStubServerBase::eth_getUncleCountByBlockHash(string const& _
h256 blockHash = jsToFixed<32>(_blockHash);
if (!client()->isKnown(blockHash))
return Json::Value(Json::nullValue);
return toJS(client()->uncleCount(blockHash));
}
catch (...)
@ -208,6 +217,7 @@ Json::Value WebThreeStubServerBase::eth_getUncleCountByBlockNumber(string const&
BlockNumber blockNumber = jsToBlockNumber(_blockNumber);
if (!client()->isKnown(blockNumber))
return Json::Value(Json::nullValue);
return toJS(client()->uncleCount(blockNumber));
}
catch (...)
@ -336,7 +346,10 @@ Json::Value WebThreeStubServerBase::eth_getBlockByHash(string const& _blockHash,
{
try
{
auto h = jsToFixed<32>(_blockHash);
h256 h = jsToFixed<32>(_blockHash);
if (!client()->isKnown(h))
return Json::Value(Json::nullValue);
if (_includeTransactions)
return toJson(client()->blockInfo(h), client()->blockDetails(h), client()->uncleHashes(h), client()->transactions(h));
else
@ -352,7 +365,10 @@ Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNum
{
try
{
auto h = jsToBlockNumber(_blockNumber);
BlockNumber h = jsToBlockNumber(_blockNumber);
if (!client()->isKnown(h))
return Json::Value(Json::nullValue);
if (_includeTransactions)
return toJson(client()->blockInfo(h), client()->blockDetails(h), client()->uncleHashes(h), client()->transactions(h));
else

4
libweb3jsonrpc/WebThreeStubServerBase.h

@ -106,8 +106,8 @@ public:
virtual std::string eth_getBalance(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 Json::Value eth_getBlockTransactionCountByHash(std::string const& _blockHash);
virtual Json::Value eth_getBlockTransactionCountByNumber(std::string const& _blockNumber);
virtual Json::Value eth_getUncleCountByBlockHash(std::string const& _blockHash);
virtual Json::Value eth_getUncleCountByBlockNumber(std::string const& _blockNumber);
virtual std::string eth_getCode(std::string const& _address, std::string const& _blockNumber);

8
libweb3jsonrpc/abstractwebthreestubserver.h

@ -27,8 +27,8 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
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_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_getBlockTransactionCountByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockTransactionCountByHashI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getBlockTransactionCountByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getBlockTransactionCountByNumberI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getUncleCountByBlockHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getUncleCountByBlockHashI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getUncleCountByBlockNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getUncleCountByBlockNumberI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_getCode", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_getCodeI);
@ -476,8 +476,8 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual std::string eth_getBalance(const std::string& param1, const std::string& param2) = 0;
virtual std::string eth_getStorageAt(const std::string& param1, const std::string& param2, const std::string& param3) = 0;
virtual std::string eth_getTransactionCount(const std::string& param1, const std::string& param2) = 0;
virtual std::string eth_getBlockTransactionCountByHash(const std::string& param1) = 0;
virtual std::string eth_getBlockTransactionCountByNumber(const std::string& param1) = 0;
virtual Json::Value eth_getBlockTransactionCountByHash(const std::string& param1) = 0;
virtual Json::Value eth_getBlockTransactionCountByNumber(const std::string& param1) = 0;
virtual Json::Value eth_getUncleCountByBlockHash(const std::string& param1) = 0;
virtual Json::Value eth_getUncleCountByBlockNumber(const std::string& param1) = 0;
virtual std::string eth_getCode(const std::string& param1, const std::string& param2) = 0;

4
libweb3jsonrpc/spec.json

@ -16,8 +16,8 @@
{ "name": "eth_getBalance", "params": ["", ""], "order": [], "returns" : ""},
{ "name": "eth_getStorageAt", "params": ["", "", ""], "order": [], "returns": ""},
{ "name": "eth_getTransactionCount", "params": ["", ""], "order": [], "returns" : ""},
{ "name": "eth_getBlockTransactionCountByHash", "params": [""], "order": [], "returns" : ""},
{ "name": "eth_getBlockTransactionCountByNumber", "params": [""], "order": [], "returns" : ""},
{ "name": "eth_getBlockTransactionCountByHash", "params": [""], "order": [], "returns" : {}},
{ "name": "eth_getBlockTransactionCountByNumber", "params": [""], "order": [], "returns" : {}},
{ "name": "eth_getUncleCountByBlockHash", "params": [""], "order": [], "returns" : {}},
{ "name": "eth_getUncleCountByBlockNumber", "params": [""], "order": [], "returns" : {}},
{ "name": "eth_getCode", "params": ["", ""], "order": [], "returns": ""},

12
test/libweb3jsonrpc/webthreestubclient.h

@ -166,23 +166,23 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_getBlockTransactionCountByHash(const std::string& param1) throw (jsonrpc::JsonRpcException)
Json::Value eth_getBlockTransactionCountByHash(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_getBlockTransactionCountByHash",p);
if (result.isString())
return result.asString();
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_getBlockTransactionCountByNumber(const std::string& param1) throw (jsonrpc::JsonRpcException)
Json::Value eth_getBlockTransactionCountByNumber(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("eth_getBlockTransactionCountByNumber",p);
if (result.isString())
return result.asString();
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}

Loading…
Cancel
Save