diff --git a/libethrpc/EthStubServer.cpp b/libethrpc/EthStubServer.cpp index f57c834fd..86b8210cb 100644 --- a/libethrpc/EthStubServer.cpp +++ b/libethrpc/EthStubServer.cpp @@ -423,25 +423,50 @@ Json::Value EthStubServer::uncle(const int &i, const Json::Value ¶ms) return toJson(client()->uncle(hash, i)); } -//TODO watch! -std::string EthStubServer::watch(const string &json) +int EthStubServer::watch(const string &json) { + unsigned ret = -1; + if (!client()) + return ret; + if (json.compare("chain") == 0) + ret = client()->installWatch(dev::eth::ChainChangedFilter); + else if (json.compare("pending") == 0) + ret = client()->installWatch(dev::eth::PendingChangedFilter); + else + { + Json::Reader reader; + Json::Value object; + reader.parse(json, object); + ret = client()->installWatch(toMessageFilter(object)); + } + return ret; } +bool EthStubServer::check(const int& id) +{ + if (!client()) + return false; + return client()->checkWatch(id); +} -Json::Value EthStubServer::jsontypeToValue(int _jsontype) +bool EthStubServer::killWatch(const int& id) { - switch (_jsontype) - { - case jsonrpc::JSON_STRING: return ""; //Json::stringValue segfault, fuck knows why - case jsonrpc::JSON_BOOLEAN: return Json::booleanValue; - case jsonrpc::JSON_INTEGER: return Json::intValue; - case jsonrpc::JSON_REAL: return Json::realValue; - case jsonrpc::JSON_OBJECT: return Json::objectValue; - case jsonrpc::JSON_ARRAY: return Json::arrayValue; - default: return Json::nullValue; - } + if (!client()) + return false; + client()->uninstallWatch(id); + return true; } + + + + + #endif + + + + + + diff --git a/libethrpc/EthStubServer.h b/libethrpc/EthStubServer.h index 8fe93f078..66bbede10 100644 --- a/libethrpc/EthStubServer.h +++ b/libethrpc/EthStubServer.h @@ -66,13 +66,14 @@ public: virtual std::string transact(const Json::Value& json); virtual Json::Value transaction(const int& i, const Json::Value& params); virtual Json::Value uncle(const int& i, const Json::Value ¶ms); - virtual std::string watch(const std::string& json); + virtual int watch(const std::string& json); + virtual bool check(const int& id); + virtual bool killWatch(const int& id); void setKeys(std::vector _keys) { m_keys = _keys; } private: dev::eth::Interface* client() const; dev::WebThreeDirect& m_web3; std::vector m_keys; - Json::Value jsontypeToValue(int); dev::FixedHash<32> numberOrHash(Json::Value const &_json) const; }; diff --git a/libethrpc/abstractethstubserver.h b/libethrpc/abstractethstubserver.h index 24c594b8c..c2e1b1f48 100644 --- a/libethrpc/abstractethstubserver.h +++ b/libethrpc/abstractethstubserver.h @@ -16,6 +16,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("balanceAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "address",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::balanceAtI); this->bindAndAddMethod(new jsonrpc::Procedure("block", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "params",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::blockI); this->bindAndAddMethod(new jsonrpc::Procedure("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::callI); + this->bindAndAddMethod(new jsonrpc::Procedure("check", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, "id",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::checkI); this->bindAndAddMethod(new jsonrpc::Procedure("codeAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "address",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_REAL, "address",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::countAtI); @@ -25,6 +26,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("gasPrice", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::gasPriceI); this->bindAndAddMethod(new jsonrpc::Procedure("key", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::keyI); this->bindAndAddMethod(new jsonrpc::Procedure("keys", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::keysI); + this->bindAndAddMethod(new jsonrpc::Procedure("killWatch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, "id",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::killWatchI); this->bindAndAddMethod(new jsonrpc::Procedure("listening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::listeningI); this->bindAndAddMethod(new jsonrpc::Procedure("lll", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::lllI); this->bindAndAddMethod(new jsonrpc::Procedure("messages", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "params",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::messagesI); @@ -43,7 +45,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("transact", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::transactI); this->bindAndAddMethod(new jsonrpc::Procedure("transaction", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "i",jsonrpc::JSON_INTEGER,"params",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::transactionI); this->bindAndAddMethod(new jsonrpc::Procedure("uncle", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "i",jsonrpc::JSON_INTEGER,"params",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::uncleI); - this->bindAndAddMethod(new jsonrpc::Procedure("watch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "params",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::watchI); + this->bindAndAddMethod(new jsonrpc::Procedure("watch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, "params",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::watchI); } @@ -62,6 +64,11 @@ class AbstractEthStubServer : public jsonrpc::AbstractServercall(request["json"]); } + inline virtual void checkI(const Json::Value& request, Json::Value& response) + { + response = this->check(request["id"].asInt()); + } + inline virtual void codeAtI(const Json::Value& request, Json::Value& response) { response = this->codeAt(request["address"].asString(), request["block"].asInt()); @@ -107,6 +114,11 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerkeys(); } + inline virtual void killWatchI(const Json::Value& request, Json::Value& response) + { + response = this->killWatch(request["id"].asInt()); + } + inline virtual void listeningI(const Json::Value& request, Json::Value& response) { response = this->listening(); @@ -206,6 +218,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServerclient->CallMethod("check",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + std::string codeAt(const std::string& address, const int& block) throw (jsonrpc::JsonRpcException) { Json::Value p; @@ -174,6 +187,19 @@ p["s"] = s; } + bool killWatch(const int& id) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p["id"] = id; + + Json::Value result = this->client->CallMethod("killWatch",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + bool listening() throw (jsonrpc::JsonRpcException) { Json::Value p; @@ -408,14 +434,14 @@ p["params"] = params; } - std::string watch(const std::string& params) throw (jsonrpc::JsonRpcException) + int watch(const std::string& params) throw (jsonrpc::JsonRpcException) { Json::Value p; p["params"] = params; Json::Value result = this->client->CallMethod("watch",p); - if (result.isString()) - return result.asString(); + if (result.isInt()) + return result.asInt(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());