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 834ea6d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
71 changes: 71 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,76 @@ using namespace jsonrpc;

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

Json::Value fillJsonWithState(eth::State const& _state, eth::AccountMaskMap const& _map)
{
bool mapEmpty = (_map.size() == 0);
Json::Value oState;
for (auto const& a : _state.addresses())
{
if (_map.size() && _map.find(a.first) == _map.end())
continue;

Json::Value o;
if (mapEmpty || _map.at(a.first).hasBalance())
o["balance"] = toCompactHexPrefixed(_state.balance(a.first), 1);
if (mapEmpty || _map.at(a.first).hasNonce())
o["nonce"] = toCompactHexPrefixed(_state.getNonce(a.first), 1);

if (mapEmpty || _map.at(a.first).hasStorage())
{
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;
}

if (mapEmpty || _map.at(a.first).hasCode())
{
if (_state.code(a.first).size() > 0)
o["code"] = toHexPrefixed(_state.code(a.first));
else
o["code"] = "";
}
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 int c_postStateJustHashVersion = 1;
const int c_postStateFullStateVersion = 2;
const int c_postStateLogHashVersion = 3;
string Test::test_getPostState(Json::Value const& param1)
{
Json::Value out;
Json::FastWriter fastWriter;
if (u256(param1["version"].asString()) == c_postStateJustHashVersion)
return toJS(m_eth.postState().state().rootHash());
else if (u256(param1["version"].asString()) == c_postStateFullStateVersion)
{
eth::AccountMaskMap _map;
out = fillJsonWithState(m_eth.postState().state(), _map);
return fastWriter.write(out);
}
else if (u256(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);
}
return "";
}

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

0 comments on commit 834ea6d

Please sign in to comment.