Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5082 from ethereum/smallrpc
Browse files Browse the repository at this point in the history
replace test_addBlock with test_importRawBlock
  • Loading branch information
winsvega committed Jun 26, 2018
2 parents b6710f1 + 6051d8b commit 9fe8dc5
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 37 deletions.
1 change: 1 addition & 0 deletions libethcore/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using errinfo_target = boost::error_info<struct tag_target, h256>;
using errinfo_seedHash = boost::error_info<struct tag_seedHash, h256>;
using errinfo_mixHash = boost::error_info<struct tag_mixHash, h256>;
using errinfo_ethashResult = boost::error_info<struct tag_ethashResult, std::tuple<h256, h256>>;
using errinfo_importResult = boost::error_info<struct tag_importResult, ImportResult>;
using BadFieldError = boost::tuple<errinfo_field, errinfo_data>;

DEV_SIMPLE_EXCEPTION(OutOfGasBase);
Expand Down
35 changes: 23 additions & 12 deletions libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @date 2016
*/

#include <libdevcore/CommonJS.h>
#include <libethashseal/Ethash.h>
#include <libethereum/ClientTest.h>
#include <libethereum/EthereumHost.h>
Expand Down Expand Up @@ -65,22 +66,12 @@ void ClientTest::setChainParams(string const& _genesis)

reopenChain(params, WithExisting::Kill);
}
catch (...)
catch (std::exception const& ex)
{
BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment("Provided configuration is not well formatted."));
BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment(ex.what()));
}
}

bool ClientTest::addBlock(string const& _rlp)
{
if (auto h = m_host.lock())
h->noteNewBlocks();

bytes rlpBytes = fromHex(_rlp, WhenError::Throw);
RLP blockRLP(rlpBytes);
return (m_bq.import(blockRLP.data(), true) == ImportResult::Success);
}

void ClientTest::modifyTimestamp(int64_t _timestamp)
{
Block block(chainParams().accountStartNonce);
Expand Down Expand Up @@ -131,3 +122,23 @@ bool ClientTest::completeSync()
h->completeSync();
return true;
}

h256 ClientTest::importRawBlock(const string& _blockRLP)
{
bytes blockBytes = jsToBytes(_blockRLP, OnFailed::Throw);
h256 blockHash = BlockHeader::headerHashFromBlock(blockBytes);
ImportResult result = queueBlock(blockBytes, true);
if (result != ImportResult::Success)
BOOST_THROW_EXCEPTION(ImportBlockFailed() << errinfo_importResult(result));

if (auto h = m_host.lock())
h->noteNewBlocks();

bool moreToImport = true;
while (moreToImport)
{
tie(ignore, moreToImport, ignore) = syncQueue(100000);
this_thread::sleep_for(chrono::milliseconds(100));
}
return blockHash;
}
5 changes: 3 additions & 2 deletions libethereum/ClientTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace eth
{

DEV_SIMPLE_EXCEPTION(ChainParamsInvalid);
DEV_SIMPLE_EXCEPTION(ImportBlockFailed);

class ClientTest: public Client
{
Expand All @@ -51,8 +52,8 @@ class ClientTest: public Client
void mineBlocks(unsigned _count);
void modifyTimestamp(int64_t _timestamp);
void rewindToBlock(unsigned _number);
bool addBlock(std::string const& _rlp);
bool completeSync();
h256 importRawBlock(std::string const& _blockRLP);
bool completeSync();

protected:
unsigned m_blocksToMine;
Expand Down
23 changes: 12 additions & 11 deletions libweb3jsonrpc/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ string Test::test_getLogHash(string const& _txHash)
catch (std::exception const& ex)
{
cwarn << ex.what();
throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR);
throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, ex.what());
}
}

Expand All @@ -86,9 +86,10 @@ bool Test::test_setChainParams(Json::Value const& param1)
asClientTest(m_eth).setChainParams(output);
asClientTest(m_eth).completeSync(); // set sync state to idle for mining
}
catch (std::exception const&)
catch (std::exception const& ex)
{
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR));
cwarn << ex.what();
throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, ex.what());
}

return true;
Expand Down Expand Up @@ -122,11 +123,12 @@ bool Test::test_modifyTimestamp(int _timestamp)
return true;
}

bool Test::test_addBlock(std::string const& _rlp)
bool Test::test_rewindToBlock(int _number)
{
try
{
asClientTest(m_eth).addBlock(_rlp);
m_eth.rewind(_number);
asClientTest(m_eth).completeSync();
}
catch (std::exception const&)
{
Expand All @@ -135,16 +137,15 @@ bool Test::test_addBlock(std::string const& _rlp)
return true;
}

bool Test::test_rewindToBlock(int _number)
std::string Test::test_importRawBlock(string const& _blockRLP)
{
try
{
m_eth.rewind(_number);
asClientTest(m_eth).completeSync();
ClientTest& client = asClientTest(m_eth);
return toJS(client.importRawBlock(_blockRLP));
}
catch (std::exception const&)
catch (std::exception const& ex)
{
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR));
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, ex.what()));
}
return true;
}
2 changes: 1 addition & 1 deletion libweb3jsonrpc/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class Test: public TestFace
return RPCModules{RPCModule{"test", "1.0"}};
}
virtual std::string test_getLogHash(std::string const& _param1) override;
virtual std::string test_importRawBlock(std::string const& _blockRLP) override;
virtual bool test_setChainParams(const Json::Value& _param1) override;
virtual bool test_mineBlocks(int _number) override;
virtual bool test_modifyTimestamp(int _timestamp) override;
virtual bool test_addBlock(std::string const& _rlp) override;
virtual bool test_rewindToBlock(int _number) override;

private:
Expand Down
16 changes: 10 additions & 6 deletions libweb3jsonrpc/TestFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ namespace dev {
jsonrpc::Procedure("test_getLogHash", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_BOOLEAN, "param1", jsonrpc::JSON_STRING, NULL),
&dev::rpc::TestFace::test_getLogHashI);
this->bindAndAddMethod(
jsonrpc::Procedure("test_importRawBlock", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_STRING, NULL),
&dev::rpc::TestFace::test_importRawBlockI);
this->bindAndAddMethod(jsonrpc::Procedure("test_setChainParams", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::TestFace::test_setChainParamsI);
this->bindAndAddMethod(jsonrpc::Procedure("test_mineBlocks", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_mineBlocksI);
this->bindAndAddMethod(jsonrpc::Procedure("test_modifyTimestamp", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_modifyTimestampI);
this->bindAndAddMethod(jsonrpc::Procedure("test_addBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &dev::rpc::TestFace::test_addBlockI);
this->bindAndAddMethod(jsonrpc::Procedure("test_rewindToBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_rewindToBlockI);
}
inline virtual void test_getLogHashI(
const Json::Value& request, Json::Value& response)
{
response = this->test_getLogHash(request[0u].asString());
}
inline virtual void test_importRawBlockI(
const Json::Value& request, Json::Value& response)
{
response = this->test_importRawBlock(request[0u].asString());
}
inline virtual void test_setChainParamsI(const Json::Value &request, Json::Value &response)
{
response = this->test_setChainParams(request[0u]);
Expand All @@ -41,19 +49,15 @@ namespace dev {
{
response = this->test_modifyTimestamp(request[0u].asInt());
}
inline virtual void test_addBlockI(const Json::Value &request, Json::Value &response)
{
response = this->test_addBlock(request[0u].asString());
}
inline virtual void test_rewindToBlockI(const Json::Value &request, Json::Value &response)
{
response = this->test_rewindToBlock(request[0u].asInt());
}
virtual std::string test_getLogHash(const std::string& param1) = 0;
virtual std::string test_importRawBlock(const std::string& param1) = 0;
virtual bool test_setChainParams(const Json::Value& param1) = 0;
virtual bool test_mineBlocks(int param1) = 0;
virtual bool test_modifyTimestamp(int param1) = 0;
virtual bool test_addBlock(const std::string& param1) = 0;
virtual bool test_rewindToBlock(int param1) = 0;
};

Expand Down
2 changes: 1 addition & 1 deletion libweb3jsonrpc/test.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{ "name": "test_getLogHash", "params": [""], "order": [], "returns": ""},
{ "name": "test_importRawBlock", "params": [""], "order": [], "returns": ""},
{ "name": "test_setChainParams", "params": [{}], "order": [], "returns": false},
{ "name": "test_mineBlocks", "params": [0], "returns": false },
{ "name": "test_modifyTimestamp", "params": [0], "returns": false },
{ "name": "test_addBlock", "params": [""], "returns": false },
{ "name": "test_rewindToBlock", "params": [0], "returns": false }
]
60 changes: 60 additions & 0 deletions test/unittests/libweb3jsonrpc/WebThreeStubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,66 @@ class WebThreeStubClient : public jsonrpc::Client
public:
WebThreeStubClient(jsonrpc::IClientConnector &conn, jsonrpc::clientVersion_t type = jsonrpc::JSONRPC_CLIENT_V2) : jsonrpc::Client(conn, type) {}

std::string test_getLogHash(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_getLogHash",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string test_importRawBlock(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_importRawBlock",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool test_setChainParams(const Json::Value& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_setChainParams",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool test_mineBlocks(int param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_mineBlocks",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool test_modifyTimestamp(int param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_modifyTimestamp",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool test_rewindToBlock(int param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("test_rewindToBlock",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string web3_sha3(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
Expand Down
Loading

0 comments on commit 9fe8dc5

Please sign in to comment.