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

Commit

Permalink
test_getPostState
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Apr 20, 2018
1 parent 003aba5 commit a186c9e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
55 changes: 55 additions & 0 deletions libweb3jsonrpc/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include "Test.h"
#include <libdevcore/CommonJS.h>
#include <jsonrpccpp/common/errors.h>
#include <jsonrpccpp/common/exception.h>
#include <libethereum/ClientTest.h>
Expand All @@ -33,6 +34,60 @@ using namespace jsonrpc;

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

Json::Value fillJsonWithState(eth::State const& _state)
{
Json::Value oState;
for (auto const& a : _state.addresses())
{
Json::Value o;
o["balance"] = toCompactHexPrefixed(_state.balance(a.first), 1);
o["nonce"] = toCompactHexPrefixed(_state.getNonce(a.first), 1);

Json::Value store;
for (auto const& s : _state.storage(a.first))
store[toCompactHexPrefixed(s.second.first, 1)] =
toCompactHexPrefixed(s.second.second, 1);
o["storage"] = store;
o["code"] = toHexPrefixed(_state.code(a.first));
oState[toHexPrefixed(a.first)] = o;
}
return oState;
}

string exportLog(eth::LogEntries const& _logs)
{
RLPStream s;
s.appendList(_logs.size());
for (eth::LogEntry const& l : _logs)
l.streamRLP(s);
return toHexPrefixed(sha3(s.out()));
}

const string c_postStateJustHashVersion = "justhash";
const string c_postStateFullStateVersion = "fullstate";
const string c_postStateLogHashVersion = "justloghash";
string Test::test_getPostState(Json::Value const& param1)
{
Json::Value out;
Json::FastWriter fastWriter;
if (param1["version"].asString() == c_postStateJustHashVersion)
return toJS(m_eth.postState().state().rootHash());
else if (param1["version"].asString() == c_postStateFullStateVersion)
{
out = fillJsonWithState(m_eth.postState().state());
return fastWriter.write(out);
}
else if (param1["version"].asString() == c_postStateLogHashVersion)
{
if (m_eth.blockChain().receipts().receipts.size() != 0)
return exportLog(m_eth.blockChain().receipts().receipts[0].log());
else
return toJS(EmptyListSHA3);
}
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
return "error";
}

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

virtual std::string test_getPostState(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;
Expand Down
11 changes: 10 additions & 1 deletion libweb3jsonrpc/TestFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ 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_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_setChainParamsI(const Json::Value &request, Json::Value &response)
{
response = this->test_setChainParams(request[0u]);
Expand All @@ -41,6 +49,7 @@ namespace dev {
{
response = this->test_rewindToBlock(request[0u].asInt());
}
virtual std::string test_getPostState(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
1 change: 1 addition & 0 deletions libweb3jsonrpc/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "name": "test_getPostState", "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 a186c9e

Please sign in to comment.