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

Commit a186c9e

Browse files
committed
test_getPostState
1 parent 003aba5 commit a186c9e

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

libweb3jsonrpc/Test.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include "Test.h"
24+
#include <libdevcore/CommonJS.h>
2425
#include <jsonrpccpp/common/errors.h>
2526
#include <jsonrpccpp/common/exception.h>
2627
#include <libethereum/ClientTest.h>
@@ -33,6 +34,60 @@ using namespace jsonrpc;
3334

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

37+
Json::Value fillJsonWithState(eth::State const& _state)
38+
{
39+
Json::Value oState;
40+
for (auto const& a : _state.addresses())
41+
{
42+
Json::Value o;
43+
o["balance"] = toCompactHexPrefixed(_state.balance(a.first), 1);
44+
o["nonce"] = toCompactHexPrefixed(_state.getNonce(a.first), 1);
45+
46+
Json::Value store;
47+
for (auto const& s : _state.storage(a.first))
48+
store[toCompactHexPrefixed(s.second.first, 1)] =
49+
toCompactHexPrefixed(s.second.second, 1);
50+
o["storage"] = store;
51+
o["code"] = toHexPrefixed(_state.code(a.first));
52+
oState[toHexPrefixed(a.first)] = o;
53+
}
54+
return oState;
55+
}
56+
57+
string exportLog(eth::LogEntries const& _logs)
58+
{
59+
RLPStream s;
60+
s.appendList(_logs.size());
61+
for (eth::LogEntry const& l : _logs)
62+
l.streamRLP(s);
63+
return toHexPrefixed(sha3(s.out()));
64+
}
65+
66+
const string c_postStateJustHashVersion = "justhash";
67+
const string c_postStateFullStateVersion = "fullstate";
68+
const string c_postStateLogHashVersion = "justloghash";
69+
string Test::test_getPostState(Json::Value const& param1)
70+
{
71+
Json::Value out;
72+
Json::FastWriter fastWriter;
73+
if (param1["version"].asString() == c_postStateJustHashVersion)
74+
return toJS(m_eth.postState().state().rootHash());
75+
else if (param1["version"].asString() == c_postStateFullStateVersion)
76+
{
77+
out = fillJsonWithState(m_eth.postState().state());
78+
return fastWriter.write(out);
79+
}
80+
else if (param1["version"].asString() == c_postStateLogHashVersion)
81+
{
82+
if (m_eth.blockChain().receipts().receipts.size() != 0)
83+
return exportLog(m_eth.blockChain().receipts().receipts[0].log());
84+
else
85+
return toJS(EmptyListSHA3);
86+
}
87+
BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
88+
return "error";
89+
}
90+
3691
bool Test::test_setChainParams(Json::Value const& param1)
3792
{
3893
try

libweb3jsonrpc/Test.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Test: public TestFace
4343
return RPCModules{RPCModule{"test", "1.0"}};
4444
}
4545

46+
virtual std::string test_getPostState(const Json::Value& param1) override;
4647
virtual bool test_setChainParams(const Json::Value &param1) override;
4748
virtual bool test_mineBlocks(int _number) override;
4849
virtual bool test_modifyTimestamp(int _timestamp) override;

libweb3jsonrpc/TestFace.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ namespace dev {
1414
public:
1515
TestFace()
1616
{
17+
this->bindAndAddMethod(
18+
jsonrpc::Procedure("test_getPostState", jsonrpc::PARAMS_BY_POSITION,
19+
jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_OBJECT, NULL),
20+
&dev::rpc::TestFace::test_getPostStateI);
1721
this->bindAndAddMethod(jsonrpc::Procedure("test_setChainParams", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::TestFace::test_setChainParamsI);
1822
this->bindAndAddMethod(jsonrpc::Procedure("test_mineBlocks", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_mineBlocksI);
1923
this->bindAndAddMethod(jsonrpc::Procedure("test_modifyTimestamp", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_modifyTimestampI);
2024
this->bindAndAddMethod(jsonrpc::Procedure("test_addBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &dev::rpc::TestFace::test_addBlockI);
2125
this->bindAndAddMethod(jsonrpc::Procedure("test_rewindToBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::TestFace::test_rewindToBlockI);
2226
}
23-
27+
inline virtual void test_getPostStateI(
28+
const Json::Value& request, Json::Value& response)
29+
{
30+
response = this->test_getPostState(request[0u]);
31+
}
2432
inline virtual void test_setChainParamsI(const Json::Value &request, Json::Value &response)
2533
{
2634
response = this->test_setChainParams(request[0u]);
@@ -41,6 +49,7 @@ namespace dev {
4149
{
4250
response = this->test_rewindToBlock(request[0u].asInt());
4351
}
52+
virtual std::string test_getPostState(const Json::Value& param1) = 0;
4453
virtual bool test_setChainParams(const Json::Value& param1) = 0;
4554
virtual bool test_mineBlocks(int param1) = 0;
4655
virtual bool test_modifyTimestamp(int param1) = 0;

libweb3jsonrpc/test.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
2+
{ "name": "test_getPostState", "params": [{}], "order": [], "returns": ""},
23
{ "name": "test_setChainParams", "params": [{}], "order": [], "returns": false},
34
{ "name": "test_mineBlocks", "params": [0], "returns": false },
45
{ "name": "test_modifyTimestamp", "params": [0], "returns": false },

0 commit comments

Comments
 (0)