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

Commit

Permalink
new json test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Feb 27, 2018
1 parent 37f1f60 commit 2dacf37
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
7 changes: 7 additions & 0 deletions eth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,13 @@ int main(int argc, char** argv)
web3.setIdealPeerCount(peers);
web3.setPeerStretch(peerStretch);
// std::shared_ptr<eth::BasicGasPricer> gasPricer = make_shared<eth::BasicGasPricer>(u256(double(ether / 1000) / etherPrice), u256(blockFees * 1000));

// allow any gasPrice in testing mode
if (testingMode)
{
askPrice = 1;
bidPrice = 1;
}
std::shared_ptr<eth::TrivialGasPricer> gasPricer = make_shared<eth::TrivialGasPricer>(askPrice, bidPrice);
eth::Client* c = nodeMode == NodeMode::Full ? web3.ethereum() : nullptr;
if (c)
Expand Down
1 change: 1 addition & 0 deletions libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void ClientTest::setChainParams(string const& _genesis)

reopenChain(params, WithExisting::Kill);
setAuthor(params.author); //for some reason author is not being set
completeSync(); // set sync state to idle for mining
}
catch (...)
{
Expand Down
56 changes: 55 additions & 1 deletion libweb3jsonrpc/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
#include "Test.h"
#include <jsonrpccpp/common/errors.h>
#include <jsonrpccpp/common/exception.h>
#include <libethereum/ClientTest.h>
#include <libdevcore/CommonJS.h>
#include <libethcore/Common.h>
#include <libethereum/ChainParams.h>
#include <libethereum/ClientTest.h>
#include <libethereum/Transaction.h>

using namespace std;
using namespace dev;
Expand All @@ -33,6 +36,57 @@ using namespace jsonrpc;

Test::Test(eth::Client& _eth): m_eth(_eth) {}

string Test::test_getPostState(Json::Value const& param1)
{
(void)param1;
h256 postState = m_eth.pendingInfo().stateRoot();
return toJS(postState);
}

string Test::test_addTransaction(Json::Value const& param1)
{
try
{
if (param1["to"].empty())
{
// Create transaction constructor with secretKey
string data = param1["data"].asString();
eth::Transaction tr(u256(param1["value"].asString()),
u256(param1["gasPrice"].asString()), u256(param1["gasLimit"].asString()),
fromHex(data.substr(0, 2) == "0x" ? data.substr(2) : data, WhenError::Throw),
u256(param1["nonce"].asString()), Secret(param1["secretKey"].asString()));

if (asClientTest(m_eth).injectTransaction(tr.rlp()) == eth::ImportResult::Success)
return toJS(tr.sha3());
else
return toJS(h256());
}
else
{
// Transaction constructor with secretKey
string data = param1["data"].asString();
eth::Transaction tr(u256(param1["value"].asString()),
u256(param1["gasPrice"].asString()), u256(param1["gasLimit"].asString()),
Address(param1["to"].asString()),
fromHex(data.substr(0, 2) == "0x" ? data.substr(2) : data, WhenError::Throw),
u256(param1["nonce"].asString()), Secret(param1["secretKey"].asString()));

if (asClientTest(m_eth).injectTransaction(tr.rlp()) == eth::ImportResult::Success)
{
return toJS(tr.sha3());
}
else
return toJS(h256());
}
}
catch (...)
{
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
}

return "";
}

bool Test::test_setChainParams(Json::Value const& param1)
{
try
Expand Down
10 changes: 6 additions & 4 deletions libweb3jsonrpc/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ class Test: public TestFace
return RPCModules{RPCModule{"test", "1.0"}};
}

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 std::string test_getPostState(const Json::Value& param1) override;
virtual std::string test_addTransaction(const Json::Value& param1) 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
22 changes: 21 additions & 1 deletion libweb3jsonrpc/TestFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ namespace dev {
public:
TestFace()
{
this->bindAndAddMethod(
jsonrpc::Procedure("test_getPostState", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_OBJECT, NULL),
&dev::rpc::TestFace::test_getPostStateI);
this->bindAndAddMethod(
jsonrpc::Procedure("test_addTransaction", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_OBJECT, NULL),
&dev::rpc::TestFace::test_addTransactionI);
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_getPostStateI(
const Json::Value& request, Json::Value& response)
{
response = this->test_getPostState(request[0u]);
}
inline virtual void test_addTransactionI(
const Json::Value& request, Json::Value& response)
{
response = this->test_addTransaction(request[0u]);
}
inline virtual void test_setChainParamsI(const Json::Value &request, Json::Value &response)
{
response = this->test_setChainParams(request[0u]);
Expand All @@ -41,6 +58,9 @@ namespace dev {
{
response = this->test_rewindToBlock(request[0u].asInt());
}

virtual std::string test_getPostState(const Json::Value& param1) = 0;
virtual std::string test_addTransaction(const Json::Value& 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;
Expand Down
2 changes: 2 additions & 0 deletions libweb3jsonrpc/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[
{ "name": "test_getPostState", "params": [{}], "order": [], "returns": ""},
{ "name": "test_addTransaction", "params": [{}], "order": [], "returns": ""},
{ "name": "test_setChainParams", "params": [{}], "order": [], "returns": false},
{ "name": "test_mineBlocks", "params": [0], "returns": false },
{ "name": "test_modifyTimestamp", "params": [0], "returns": false },
Expand Down

0 comments on commit 2dacf37

Please sign in to comment.