diff --git a/eth/EthStubServer.cpp b/eth/EthStubServer.cpp index 77480d7a0..7df6c8132 100644 --- a/eth/EthStubServer.cpp +++ b/eth/EthStubServer.cpp @@ -20,7 +20,7 @@ * @date 2014 */ -#if ETH_JSONRPC +//#if ETH_JSONRPC #include "EthStubServer.h" #include #include @@ -37,69 +37,141 @@ EthStubServer::EthStubServer(jsonrpc::AbstractServerConnector* _conn, WebThreeDi { } -dev::eth::Client& EthStubServer::ethereum() const +dev::eth::Interface* EthStubServer::client() const { - return *m_web3.ethereum(); + return &(*m_web3.ethereum()); } -std::string EthStubServer::balanceAt(const string &a, const string &block) +std::string EthStubServer::balanceAt(const string &a, const int& block) { - + return toJS(client()->balanceAt(jsToAddress(a), block)); } -std::string EthStubServer::block(const string &numberOrHash) +//TODO BlockDetails? +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; } -std::string EthStubServer::call(const string &json) +static TransactionJS toTransaction(const Json::Value &json) { + TransactionJS ret; + if (!json.isObject() || json.empty()){ + return ret; + } + + if (!json["from"].empty()) + ret.from = jsToSecret(json["from"].asString()); + if (!json["to"].empty()) + ret.to = jsToAddress(json["to"].asString()); + if (!json["value"].empty()) + ret.value = jsToU256(json["value"].asString()); + if (!json["gas"].empty()) + ret.gas = jsToU256(json["gas"].asString()); + if (!json["gasPrice"].empty()) + ret.gasPrice = jsToU256(json["gasPrice"].asString()); + if (!json["data"].empty() || json["code"].empty() || json["dataclose"].empty()) + { + if (json["data"].isString()) + ret.data = jsToBytes(json["data"].asString()); + else if (json["code"].isString()) + ret.data = jsToBytes(json["code"].asString()); + else if (json["data"].isArray()) + for (auto i: json["data"]) + dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32))); + else if (json["code"].isArray()) + for (auto i: json["code"]) + dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32))); + else if (json["dataclose"].isArray()) + for (auto i: json["dataclose"]) + dev::operator +=(ret.data, jsToBytes(i.asString())); + } + + return ret; } -std::string EthStubServer::codeAt(const string &a, const string &block) -{ +std::string EthStubServer::call(const Json::Value &json) +{ + std::string ret; + if (!client()) + return ret; + TransactionJS t = toTransaction(json); + if (!t.to) + return ret; + if (!t.from && m_keys.size()) + t.from = m_keys[0].secret(); + if (!t.gasPrice) + t.gasPrice = 10 * dev::eth::szabo; + if (!t.gas) + t.gas = client()->balanceAt(KeyPair(t.from).address()) / t.gasPrice; + ret = toJS(client()->call(t.from, t.value, t.to, t.data, t.gas, t.gasPrice)); + return ret; +} +std::string EthStubServer::codeAt(const string &a, const int& block) +{ + return client() ? jsFromBinary(client()->codeAt(jsToAddress(a), block)) : ""; } std::string EthStubServer::coinbase() { - + return client() ? toJS(client()->address()) : ""; } -int EthStubServer::countAt(const string &a, const string &block) +std::string EthStubServer::countAt(const string &a, const int& block) { - + return client() ? toJS(client()->countAt(jsToAddress(a), block)) : ""; } int EthStubServer::defaultBlock() { - + return client() ? client()->getDefault() : 0; } -std::string EthStubServer::fromAscii(const string &s) +std::string EthStubServer::fromAscii(const int& padding, const std::string& s) { - + return jsFromBinary(s, padding); } -std::string EthStubServer::fromFixed(const string &s) +double EthStubServer::fromFixed(const string &s) { - + return jsFromFixed(s); } std::string EthStubServer::gasPrice() { - + return toJS(10 * dev::eth::szabo); } +//TODO bool EthStubServer::isListening() { - + return /*client() ? client()->haveNetwork() :*/ false; } bool EthStubServer::isMining() { - + return client() ? client()->isMining() : false; } std::string EthStubServer::key() @@ -119,7 +191,7 @@ Json::Value EthStubServer::keys() std::string EthStubServer::lll(const string &s) { - + return toJS(dev::eth::compileLLL(s)); } std::string EthStubServer::messages(const string &json) @@ -157,7 +229,7 @@ std::string EthStubServer::sha3(const string &s) } -std::string EthStubServer::stateAt(const string &a, const string &block, const string &p) +std::string EthStubServer::stateAt(const string &a, const int& block, const string &p) { } @@ -200,24 +272,24 @@ std::string EthStubServer::watch(const string &json) Json::Value EthStubServer::blockJson(const std::string& _hash) { Json::Value res; - auto const& bc = ethereum().blockChain(); +// auto const& bc = client()->blockChain(); - auto b = _hash.length() ? bc.block(h256(_hash)) : bc.block(); +// 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); +// 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; } @@ -236,4 +308,4 @@ Json::Value EthStubServer::jsontypeToValue(int _jsontype) } } -#endif +//#endif diff --git a/eth/EthStubServer.h b/eth/EthStubServer.h index c3f1e060e..4ec46f4ff 100644 --- a/eth/EthStubServer.h +++ b/eth/EthStubServer.h @@ -29,22 +29,22 @@ #include "abstractethstubserver.h" #pragma GCC diagnostic pop -namespace dev { class WebThreeDirect; namespace eth { class Client; } class KeyPair; } +namespace dev { class WebThreeDirect; namespace eth { class Interface; } class KeyPair; } class EthStubServer: public AbstractEthStubServer { public: EthStubServer(jsonrpc::AbstractServerConnector* _conn, dev::WebThreeDirect& _web3); - virtual std::string balanceAt(const std::string& a, const std::string& block); - virtual std::string block(const std::string& numberOrHash); - virtual std::string call(const std::string& json); - virtual std::string codeAt(const std::string& a, const std::string& block); + virtual std::string balanceAt(const std::string& a, const int& block); + virtual Json::Value block(const std::string& numberOrHash); + virtual std::string call(const Json::Value& json); + virtual std::string codeAt(const std::string& a, const int& block); virtual std::string coinbase(); - virtual int countAt(const std::string& a, const std::string& block); + virtual std::string countAt(const std::string& a, const int& block); virtual int defaultBlock(); - virtual std::string fromAscii(const std::string& s); - virtual std::string fromFixed(const std::string& s); + virtual std::string fromAscii(const int& padding, const std::string& s); + virtual double fromFixed(const std::string& s); virtual std::string gasPrice(); virtual bool isListening(); virtual bool isMining(); @@ -58,7 +58,7 @@ public: virtual std::string setListening(const std::string& l); virtual std::string setMining(const std::string& l); virtual std::string sha3(const std::string& s); - virtual std::string stateAt(const std::string& a, const std::string& block, const std::string& p); + virtual std::string stateAt(const std::string& a, const int& block, const std::string& p); virtual std::string toAscii(const std::string& s); virtual std::string toDecimal(const std::string& s); virtual std::string toFixed(const std::string& s); @@ -69,7 +69,7 @@ public: void setKeys(std::vector _keys) { m_keys = _keys; } private: - dev::eth::Client& ethereum() const; + dev::eth::Interface* client() const; dev::WebThreeDirect& m_web3; std::vector m_keys; Json::Value jsontypeToValue(int); diff --git a/eth/abstractethstubserver.h b/eth/abstractethstubserver.h index 168ca425d..89254532d 100644 --- a/eth/abstractethstubserver.h +++ b/eth/abstractethstubserver.h @@ -2,8 +2,8 @@ * THIS FILE IS GENERATED BY jsonrpcstub, DO NOT CHANGE IT!!!!! */ -#ifndef _AbstractEthStubServer_H_ -#define _AbstractEthStubServer_H_ +#ifndef _ABSTRACTETHSTUBSERVER_H_ +#define _ABSTRACTETHSTUBSERVER_H_ #include @@ -11,17 +11,17 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer(conn) + jsonrpc::AbstractServer(conn) { - this->bindAndAddMethod(new jsonrpc::Procedure("balanceAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::balanceAtI); - this->bindAndAddMethod(new jsonrpc::Procedure("block", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::blockI); - this->bindAndAddMethod(new jsonrpc::Procedure("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::callI); - this->bindAndAddMethod(new jsonrpc::Procedure("codeAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::codeAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("balanceAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::balanceAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("block", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::blockI); + this->bindAndAddMethod(new jsonrpc::Procedure("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::callI); + this->bindAndAddMethod(new jsonrpc::Procedure("codeAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::codeAtI); this->bindAndAddMethod(new jsonrpc::Procedure("coinbase", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::coinbaseI); - this->bindAndAddMethod(new jsonrpc::Procedure("countAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::countAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("countAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::countAtI); this->bindAndAddMethod(new jsonrpc::Procedure("defaultBlock", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::defaultBlockI); - this->bindAndAddMethod(new jsonrpc::Procedure("fromAscii", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromAsciiI); - this->bindAndAddMethod(new jsonrpc::Procedure("fromFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromFixedI); + this->bindAndAddMethod(new jsonrpc::Procedure("fromAscii", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "padding",jsonrpc::JSON_INTEGER,"s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromAsciiI); + this->bindAndAddMethod(new jsonrpc::Procedure("fromFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_REAL, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromFixedI); this->bindAndAddMethod(new jsonrpc::Procedure("gasPrice", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::gasPriceI); this->bindAndAddMethod(new jsonrpc::Procedure("isListening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::isListeningI); this->bindAndAddMethod(new jsonrpc::Procedure("isMining", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::isMiningI); @@ -35,7 +35,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("setListening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "l",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::setListeningI); this->bindAndAddMethod(new jsonrpc::Procedure("setMining", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "l",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::setMiningI); this->bindAndAddMethod(new jsonrpc::Procedure("sha3", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::sha3I); - this->bindAndAddMethod(new jsonrpc::Procedure("stateAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_STRING,"p",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::stateAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("stateAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER,"p",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::stateAtI); this->bindAndAddMethod(new jsonrpc::Procedure("toAscii", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::toAsciiI); this->bindAndAddMethod(new jsonrpc::Procedure("toDecimal", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::toDecimalI); this->bindAndAddMethod(new jsonrpc::Procedure("toFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::toFixedI); @@ -48,7 +48,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerbalanceAt(request["a"].asString(), request["block"].asString()); + response = this->balanceAt(request["a"].asString(), request["block"].asInt()); } inline virtual void blockI(const Json::Value& request, Json::Value& response) @@ -58,12 +58,12 @@ class AbstractEthStubServer : public jsonrpc::AbstractServercall(request["json"].asString()); + response = this->call(request["json"]); } inline virtual void codeAtI(const Json::Value& request, Json::Value& response) { - response = this->codeAt(request["a"].asString(), request["block"].asString()); + response = this->codeAt(request["a"].asString(), request["block"].asInt()); } inline virtual void coinbaseI(const Json::Value& request, Json::Value& response) @@ -73,7 +73,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServercountAt(request["a"].asString(), request["block"].asString()); + response = this->countAt(request["a"].asString(), request["block"].asInt()); } inline virtual void defaultBlockI(const Json::Value& request, Json::Value& response) @@ -83,7 +83,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerfromAscii(request["s"].asString()); + response = this->fromAscii(request["padding"].asInt(), request["s"].asString()); } inline virtual void fromFixedI(const Json::Value& request, Json::Value& response) @@ -158,7 +158,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerstateAt(request["a"].asString(), request["block"].asString(), request["p"].asString()); + response = this->stateAt(request["a"].asString(), request["block"].asInt(), request["p"].asString()); } inline virtual void toAsciiI(const Json::Value& request, Json::Value& response) @@ -197,15 +197,15 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer