Browse Source

jsonrpc http watch

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
df557180c0
  1. 51
      libethrpc/EthStubServer.cpp
  2. 5
      libethrpc/EthStubServer.h
  3. 18
      libethrpc/abstractethstubserver.h
  4. 4
      libethrpc/eth.js
  5. 4
      libethrpc/spec.json
  6. 32
      test/ethstubclient.h

51
libethrpc/EthStubServer.cpp

@ -423,25 +423,50 @@ Json::Value EthStubServer::uncle(const int &i, const Json::Value &params)
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

5
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 &params);
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<dev::KeyPair> _keys) { m_keys = _keys; }
private:
dev::eth::Interface* client() const;
dev::WebThreeDirect& m_web3;
std::vector<dev::KeyPair> m_keys;
Json::Value jsontypeToValue(int);
dev::FixedHash<32> numberOrHash(Json::Value const &_json) const;
};

18
libethrpc/abstractethstubserver.h

@ -16,6 +16,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
this->bindAndAddMethod(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::AbstractServer<AbstractEthStubServ
this->bindAndAddMethod(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::AbstractServer<AbstractEthStubServ
this->bindAndAddMethod(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::AbstractServer<AbstractEthStubServ
response = this->call(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::AbstractServer<AbstractEthStubServ
response = this->keys();
}
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::AbstractServer<AbstractEthStubServ
virtual std::string balanceAt(const std::string& address, const int& block) = 0;
virtual Json::Value block(const Json::Value& params) = 0;
virtual std::string call(const Json::Value& json) = 0;
virtual bool check(const int& id) = 0;
virtual std::string codeAt(const std::string& address, const int& block) = 0;
virtual std::string coinbase() = 0;
virtual double countAt(const std::string& address, const int& block) = 0;
@ -215,6 +228,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string gasPrice() = 0;
virtual std::string key() = 0;
virtual Json::Value keys() = 0;
virtual bool killWatch(const int& id) = 0;
virtual bool listening() = 0;
virtual std::string lll(const std::string& s) = 0;
virtual Json::Value messages(const Json::Value& params) = 0;
@ -233,7 +247,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string transact(const Json::Value& json) = 0;
virtual Json::Value transaction(const int& i, const Json::Value& params) = 0;
virtual Json::Value uncle(const int& i, const Json::Value& params) = 0;
virtual std::string watch(const std::string& params) = 0;
virtual int watch(const std::string& params) = 0;
};
#endif //_ABSTRACTETHSTUBSERVER_H_

4
libethrpc/eth.js

@ -43,7 +43,9 @@ var spec = [
{ "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": ""},
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": 0},
{ "method": "check", "params": { "id": 0}, "order": [], "returns": true},
{ "method": "killWatch", "params": { "id": 0}, "order": ["params"], "returns": true},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},

4
libethrpc/spec.json

@ -26,7 +26,9 @@
{ "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": ""},
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": 0},
{ "method": "check", "params": { "id": 0}, "order": [], "returns": true},
{ "method": "killWatch", "params": { "id": 0}, "order": ["params"], "returns": true},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},

32
test/ethstubclient.h

@ -59,6 +59,19 @@ p["block"] = block;
}
bool check(const int& id) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["id"] = id;
Json::Value result = this->client->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());

Loading…
Cancel
Save