From 2c58f14856dd2d3818fb2e10911edc965907f0cd Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 12 Oct 2014 14:40:32 +0200 Subject: [PATCH] few more jsonrpc methods --- eth/EthStubServer.cpp | 125 ++++++++++++++++++++---------------------- eth/EthStubServer.h | 1 - 2 files changed, 58 insertions(+), 68 deletions(-) diff --git a/eth/EthStubServer.cpp b/eth/EthStubServer.cpp index 01df26008..7e7f2ddfc 100644 --- a/eth/EthStubServer.cpp +++ b/eth/EthStubServer.cpp @@ -31,6 +31,54 @@ using namespace std; using namespace dev; using namespace dev::eth; +static Json::Value toJson(const dev::eth::BlockInfo& bi) +{ + Json::Value res; + res["hash"] = boost::lexical_cast(bi.hash); + + res["parentHash"] = boost::lexical_cast(bi.parentHash); + res["sha3Uncles"] = boost::lexical_cast(bi.sha3Uncles); + res["miner"] = boost::lexical_cast(bi.coinbaseAddress); + res["stateRoot"] = boost::lexical_cast(bi.stateRoot); + res["transactionsRoot"] = boost::lexical_cast(bi.transactionsRoot); + res["difficulty"] = boost::lexical_cast(bi.difficulty); + res["number"] = boost::lexical_cast(bi.number); + res["minGasPrice"] = boost::lexical_cast(bi.minGasPrice); + res["gasLimit"] = boost::lexical_cast(bi.gasLimit); + res["timestamp"] = boost::lexical_cast(bi.timestamp); + res["extraData"] = jsFromBinary(bi.extraData); + res["nonce"] = boost::lexical_cast(bi.nonce); + return res; +} + +static Json::Value toJson(const dev::eth::PastMessage& t) +{ + Json::Value res; + res["input"] = jsFromBinary(t.input); + res["output"] = jsFromBinary(t.output); + res["to"] = boost::lexical_cast(t.to); + res["from"] = boost::lexical_cast(t.from); + res["origin"] = boost::lexical_cast(t.origin); + res["timestamp"] = boost::lexical_cast(t.timestamp); + res["coinbase"] = boost::lexical_cast(t.coinbase); + res["block"] = boost::lexical_cast(t.block); + Json::Value path; + for (int i: t.path) + path.append(i); + res["path"] = path; + res["number"] = (int)t.number; + return res; +} + +static Json::Value toJson(const dev::eth::PastMessages& pms) +{ + Json::Value res; + for (dev::eth::PastMessage const & t: pms) + res.append(toJson(t)); + + return res; +} + EthStubServer::EthStubServer(jsonrpc::AbstractServerConnector* _conn, WebThreeDirect& _web3): AbstractEthStubServer(_conn), m_web3(_web3) @@ -52,24 +100,7 @@ Json::Value EthStubServer::block(const string &numberOrHash) { auto n = jsToU256(numberOrHash); auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : ::jsToFixed<32>(numberOrHash); - - Json::Value res; - BlockInfo bi = client()->blockInfo(h); - res["hash"] = boost::lexical_cast(bi.hash); - - res["parentHash"] = boost::lexical_cast(bi.parentHash); - res["sha3Uncles"] = boost::lexical_cast(bi.sha3Uncles); - res["miner"] = boost::lexical_cast(bi.coinbaseAddress); - res["stateRoot"] = boost::lexical_cast(bi.stateRoot); - res["transactionsRoot"] = boost::lexical_cast(bi.transactionsRoot); - res["difficulty"] = boost::lexical_cast(bi.difficulty); - res["number"] = boost::lexical_cast(bi.number); - res["minGasPrice"] = boost::lexical_cast(bi.minGasPrice); - res["gasLimit"] = boost::lexical_cast(bi.gasLimit); - res["timestamp"] = boost::lexical_cast(bi.timestamp); - res["extraData"] = jsFromBinary(bi.extraData); - res["nonce"] = boost::lexical_cast(bi.nonce); - return res; + return toJson(client()->blockInfo(h)); } static TransactionJS toTransaction(const Json::Value &json) @@ -247,26 +278,7 @@ Json::Value EthStubServer::messages(const Json::Value &json) Json::Value res; if (!client()) return res; - dev::eth::PastMessages pms = client()->messages(toMessageFilter(json)); - - for (dev::eth::PastMessage const & t: pms) - { - res["input"] = jsFromBinary(t.input); - res["output"] = jsFromBinary(t.output); - res["to"] = boost::lexical_cast(t.to); - res["from"] = boost::lexical_cast(t.from); - res["origin"] = boost::lexical_cast(t.origin); - res["timestamp"] = boost::lexical_cast(t.timestamp); - res["coinbase"] = boost::lexical_cast(t.coinbase); - res["block"] = boost::lexical_cast(t.block); - Json::Value path; - for (int i: t.path) - path.append(i); - res["path"] = path; - res["number"] = (int)t.number; - } - - return res; + return toJson(client()->messages(toMessageFilter(json))); } int EthStubServer::number() @@ -289,25 +301,25 @@ std::string EthStubServer::secretToAddress(const string &s) Json::Value EthStubServer::setListening(const bool &l) { if (!client()) - return Json::Value(); + return Json::nullValue; /* if (l) client()->startNetwork(); else client()->stopNetwork();*/ - return Json::Value(); + return Json::nullValue; } Json::Value EthStubServer::setMining(const bool &l) { if (!client()) - return Json::Value(); + return Json::nullValue; if (l) client()->startMining(); else client()->stopMining(); - return Json::Value(); + return Json::nullValue; } std::string EthStubServer::sha3(const string &s) @@ -384,38 +396,17 @@ Json::Value EthStubServer::transaction(const int &i, const string &numberOrHash) Json::Value EthStubServer::uncle(const int &i, const string &numberOrHash) { - + auto n = jsToU256(numberOrHash); + auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : jsToFixed<32>(numberOrHash); + return client() ? toJson(client()->uncle(h, i)) : Json::Value(); } +//TODO watch! std::string EthStubServer::watch(const string &json) { } -Json::Value EthStubServer::blockJson(const std::string& _hash) -{ - Json::Value res; -// auto const& bc = client()->blockChain(); - -// auto b = _hash.length() ? bc.block(h256(_hash)) : bc.block(); - -// auto bi = BlockInfo(b); -// res["number"] = boost::lexical_cast(bi.number); -// res["hash"] = boost::lexical_cast(bi.hash); -// res["parentHash"] = boost::lexical_cast(bi.parentHash); -// res["sha3Uncles"] = boost::lexical_cast(bi.sha3Uncles); -// res["coinbaseAddress"] = boost::lexical_cast(bi.coinbaseAddress); -// res["stateRoot"] = boost::lexical_cast(bi.stateRoot); -// res["transactionsRoot"] = boost::lexical_cast(bi.transactionsRoot); -// res["minGasPrice"] = boost::lexical_cast(bi.minGasPrice); -// res["gasLimit"] = boost::lexical_cast(bi.gasLimit); -// res["gasUsed"] = boost::lexical_cast(bi.gasUsed); -// res["difficulty"] = boost::lexical_cast(bi.difficulty); -// res["timestamp"] = boost::lexical_cast(bi.timestamp); -// res["nonce"] = boost::lexical_cast(bi.nonce); - - return res; -} Json::Value EthStubServer::jsontypeToValue(int _jsontype) { diff --git a/eth/EthStubServer.h b/eth/EthStubServer.h index 84b91f0af..f0a3e3b7f 100644 --- a/eth/EthStubServer.h +++ b/eth/EthStubServer.h @@ -73,5 +73,4 @@ private: dev::WebThreeDirect& m_web3; std::vector m_keys; Json::Value jsontypeToValue(int); - Json::Value blockJson(const std::string&); };