Browse Source

common changes to unify apis

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
672e56c7b5
  1. 44
      libethrpc/EthStubServer.cpp
  2. 7
      libethrpc/EthStubServer.h
  3. 30
      libethrpc/abstractethstubserver.h
  4. 41
      libethrpc/eth.js
  5. 10
      libethrpc/spec.json
  6. 47
      libqethereum/QEthereum.cpp
  7. 20
      libqethereum/QEthereum.h
  8. 20
      test/ethstubclient.h
  9. 25
      test/jsonrpc.cpp

44
libethrpc/EthStubServer.cpp

@ -110,12 +110,23 @@ std::string EthStubServer::balanceAt(const string &address, const int& block)
return toJS(client()->balanceAt(jsToAddress(address), block)); return toJS(client()->balanceAt(jsToAddress(address), block));
} }
//TODO BlockDetails? dev::FixedHash<32> EthStubServer::numberOrHash(Json::Value const &json) const
Json::Value EthStubServer::block(const string &numberOrHash)
{ {
auto n = jsToU256(numberOrHash); dev::FixedHash<32> hash;
auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : ::jsToFixed<32>(numberOrHash); if (!json["hash"].empty())
return toJson(client()->blockInfo(h)); hash = jsToFixed<32>(json["hash"].asString());
else if (!json["number"].empty())
hash = client()->hashFromNumber((unsigned)json["number"].asInt());
return hash;
}
Json::Value EthStubServer::block(const Json::Value &params)
{
if (!client())
return "";
auto hash = numberOrHash(params);
return toJson(client()->blockInfo(hash));
} }
static TransactionJS toTransaction(const Json::Value &json) static TransactionJS toTransaction(const Json::Value &json)
@ -394,21 +405,22 @@ std::string EthStubServer::transact(const Json::Value &json)
return ret; return ret;
} }
Json::Value EthStubServer::transaction(const int &i, const string &numberOrHash) Json::Value EthStubServer::transaction(const int &i, const Json::Value &params)
{ {
if (!client()){ if (!client())
return Json::Value(); return "";
}
auto n = jsToU256(numberOrHash); auto hash = numberOrHash(params);
auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : jsToFixed<32>(numberOrHash); return toJson(client()->transaction(hash, i));
return toJson(client()->transaction(h, i));
} }
Json::Value EthStubServer::uncle(const int &i, const string &numberOrHash) Json::Value EthStubServer::uncle(const int &i, const Json::Value &params)
{ {
auto n = jsToU256(numberOrHash); if (!client())
auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : jsToFixed<32>(numberOrHash); return "";
return client() ? toJson(client()->uncle(h, i)) : Json::Value();
auto hash = numberOrHash(params);
return toJson(client()->uncle(hash, i));
} }
//TODO watch! //TODO watch!

7
libethrpc/EthStubServer.h

@ -37,7 +37,7 @@ public:
EthStubServer(jsonrpc::AbstractServerConnector* _conn, dev::WebThreeDirect& _web3); EthStubServer(jsonrpc::AbstractServerConnector* _conn, dev::WebThreeDirect& _web3);
virtual std::string balanceAt(const std::string& address, const int& block); virtual std::string balanceAt(const std::string& address, const int& block);
virtual Json::Value block(const std::string& numberOrHash); virtual Json::Value block(const Json::Value& params);
virtual std::string call(const Json::Value& json); virtual std::string call(const Json::Value& json);
virtual std::string codeAt(const std::string& address, const int& block); virtual std::string codeAt(const std::string& address, const int& block);
virtual std::string coinbase(); virtual std::string coinbase();
@ -64,8 +64,8 @@ public:
virtual std::string toDecimal(const std::string& s); virtual std::string toDecimal(const std::string& s);
virtual std::string toFixed(const double& s); virtual std::string toFixed(const double& s);
virtual std::string transact(const Json::Value& json); virtual std::string transact(const Json::Value& json);
virtual Json::Value transaction(const int& i, const std::string& numberOrHash); virtual Json::Value transaction(const int& i, const Json::Value& params);
virtual Json::Value uncle(const int& i, const std::string& numberOrHash); virtual Json::Value uncle(const int& i, const Json::Value &params);
virtual std::string watch(const std::string& json); virtual std::string watch(const std::string& json);
void setKeys(std::vector<dev::KeyPair> _keys) { m_keys = _keys; } void setKeys(std::vector<dev::KeyPair> _keys) { m_keys = _keys; }
@ -74,4 +74,5 @@ private:
dev::WebThreeDirect& m_web3; dev::WebThreeDirect& m_web3;
std::vector<dev::KeyPair> m_keys; std::vector<dev::KeyPair> m_keys;
Json::Value jsontypeToValue(int); Json::Value jsontypeToValue(int);
dev::FixedHash<32> numberOrHash(Json::Value const &_json) const;
}; };

30
libethrpc/abstractethstubserver.h

@ -14,7 +14,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
jsonrpc::AbstractServer<AbstractEthStubServer>(conn) jsonrpc::AbstractServer<AbstractEthStubServer>(conn)
{ {
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("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, "numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::blockI); 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("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::callI);
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("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("coinbase", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::coinbaseI);
@ -27,7 +27,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
this->bindAndAddMethod(new jsonrpc::Procedure("keys", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::keysI); this->bindAndAddMethod(new jsonrpc::Procedure("keys", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::keysI);
this->bindAndAddMethod(new jsonrpc::Procedure("listening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::listeningI); 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("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, "json",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::messagesI); this->bindAndAddMethod(new jsonrpc::Procedure("messages", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "params",jsonrpc::JSON_OBJECT, NULL), &AbstractEthStubServer::messagesI);
this->bindAndAddMethod(new jsonrpc::Procedure("mining", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::miningI); this->bindAndAddMethod(new jsonrpc::Procedure("mining", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::miningI);
this->bindAndAddMethod(new jsonrpc::Procedure("number", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::numberI); this->bindAndAddMethod(new jsonrpc::Procedure("number", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::numberI);
this->bindAndAddMethod(new jsonrpc::Procedure("peerCount", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::peerCountI); this->bindAndAddMethod(new jsonrpc::Procedure("peerCount", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::peerCountI);
@ -41,9 +41,9 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
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("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_REAL, NULL), &AbstractEthStubServer::toFixedI); this->bindAndAddMethod(new jsonrpc::Procedure("toFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_REAL, NULL), &AbstractEthStubServer::toFixedI);
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("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,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::transactionI); 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,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::uncleI); 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, "json",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::watchI); this->bindAndAddMethod(new jsonrpc::Procedure("watch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "params",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::watchI);
} }
@ -54,7 +54,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void blockI(const Json::Value& request, Json::Value& response) inline virtual void blockI(const Json::Value& request, Json::Value& response)
{ {
response = this->block(request["numberOrHash"].asString()); response = this->block(request["params"]);
} }
inline virtual void callI(const Json::Value& request, Json::Value& response) inline virtual void callI(const Json::Value& request, Json::Value& response)
@ -119,7 +119,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void messagesI(const Json::Value& request, Json::Value& response) inline virtual void messagesI(const Json::Value& request, Json::Value& response)
{ {
response = this->messages(request["json"]); response = this->messages(request["params"]);
} }
inline virtual void miningI(const Json::Value& request, Json::Value& response) inline virtual void miningI(const Json::Value& request, Json::Value& response)
@ -189,22 +189,22 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void transactionI(const Json::Value& request, Json::Value& response) inline virtual void transactionI(const Json::Value& request, Json::Value& response)
{ {
response = this->transaction(request["i"].asInt(), request["numberOrHash"].asString()); response = this->transaction(request["i"].asInt(), request["params"]);
} }
inline virtual void uncleI(const Json::Value& request, Json::Value& response) inline virtual void uncleI(const Json::Value& request, Json::Value& response)
{ {
response = this->uncle(request["i"].asInt(), request["numberOrHash"].asString()); response = this->uncle(request["i"].asInt(), request["params"]);
} }
inline virtual void watchI(const Json::Value& request, Json::Value& response) inline virtual void watchI(const Json::Value& request, Json::Value& response)
{ {
response = this->watch(request["json"].asString()); response = this->watch(request["params"].asString());
} }
virtual std::string balanceAt(const std::string& address, const int& block) = 0; virtual std::string balanceAt(const std::string& address, const int& block) = 0;
virtual Json::Value block(const std::string& numberOrHash) = 0; virtual Json::Value block(const Json::Value& params) = 0;
virtual std::string call(const Json::Value& json) = 0; virtual std::string call(const Json::Value& json) = 0;
virtual std::string codeAt(const std::string& address, const int& block) = 0; virtual std::string codeAt(const std::string& address, const int& block) = 0;
virtual std::string coinbase() = 0; virtual std::string coinbase() = 0;
@ -217,7 +217,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual Json::Value keys() = 0; virtual Json::Value keys() = 0;
virtual bool listening() = 0; virtual bool listening() = 0;
virtual std::string lll(const std::string& s) = 0; virtual std::string lll(const std::string& s) = 0;
virtual Json::Value messages(const Json::Value& json) = 0; virtual Json::Value messages(const Json::Value& params) = 0;
virtual bool mining() = 0; virtual bool mining() = 0;
virtual int number() = 0; virtual int number() = 0;
virtual int peerCount() = 0; virtual int peerCount() = 0;
@ -231,9 +231,9 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string toDecimal(const std::string& s) = 0; virtual std::string toDecimal(const std::string& s) = 0;
virtual std::string toFixed(const double& s) = 0; virtual std::string toFixed(const double& s) = 0;
virtual std::string transact(const Json::Value& json) = 0; virtual std::string transact(const Json::Value& json) = 0;
virtual Json::Value transaction(const int& i, const std::string& numberOrHash) = 0; virtual Json::Value transaction(const int& i, const Json::Value& params) = 0;
virtual Json::Value uncle(const int& i, const std::string& numberOrHash) = 0; virtual Json::Value uncle(const int& i, const Json::Value& params) = 0;
virtual std::string watch(const std::string& json) = 0; virtual std::string watch(const std::string& params) = 0;
}; };
#endif //_ABSTRACTETHSTUBSERVER_H_ #endif //_ABSTRACTETHSTUBSERVER_H_

41
libethrpc/eth.js

@ -38,12 +38,12 @@ var spec = [
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": {}}, { "method": "block", "params": { "params": {}}, "order": ["params"], "returns": {}},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": {}}, { "method": "transaction", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": {}}, { "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "messages", "params": { "json": {}}, "order": ["json"], "returns": []}, { "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "watch", "params": { "json": ""}, "order": ["json"], "returns": ""}, { "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": ""},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""}, { "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""}, { "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},
@ -57,8 +57,6 @@ var spec = [
window.eth = (function ethScope() { window.eth = (function ethScope() {
var m_reqId = 0 var m_reqId = 0
var ret = {} var ret = {}
@ -116,6 +114,23 @@ window.eth = (function ethScope() {
return addPrefix(s, "set"); return addPrefix(s, "set");
}; };
var defaults = function (def, obj) {
if (!def) {
return obj;
}
var rewriteProperties = function (dst, p) {
Object.keys(p).forEach(function (key) {
if (p[key] !== undefined) {
dst[key] = p[key];
}
});
};
var res = {};
rewriteProperties(res, def);
rewriteProperties(res, obj);
return res;
};
var setupProperties = function (root, spec) { var setupProperties = function (root, spec) {
var properties = [ var properties = [
{ name: "coinbase", getter: "coinbase", setter: "setCoinbase" }, { name: "coinbase", getter: "coinbase", setter: "setCoinbase" },
@ -153,10 +168,10 @@ window.eth = (function ethScope() {
var setupMethods = function (root, spec) { var setupMethods = function (root, spec) {
var methods = [ var methods = [
{ name: "balanceAt", async: "getBalanceAt" }, { name: "balanceAt", async: "getBalanceAt", default: {block: 0} },
{ name: "stateAt", async: "getStateAt" }, { name: "stateAt", async: "getStateAt", default: {block: 0} },
{ name: "countAt", async: "getCountAt" }, { name: "countAt", async: "getCountAt", default: {block: 0} },
{ name: "codeAt", async: "getCodeAt" }, { name: "codeAt", async: "getCodeAt", default: {block: 0} },
{ name: "transact", async: "makeTransact"}, { name: "transact", async: "makeTransact"},
{ name: "call", async: "makeCall" }, { name: "call", async: "makeCall" },
{ name: "messages", async: "getMessages" }, { name: "messages", async: "getMessages" },
@ -165,11 +180,11 @@ window.eth = (function ethScope() {
methods.forEach(function (method) { methods.forEach(function (method) {
root[method.name] = function () { root[method.name] = function () {
return reqSync(method.name, getParams(spec, method.name, arguments)); return reqSync(method.name, defaults(method.default, getParams(spec, method.name, arguments)));
}; };
if (method.async) { if (method.async) {
root[method.async] = function () { root[method.async] = function () {
return reqAsync(method.name, getParams(spec, method.name, arguments), arguments[arguments.length - 1]); return reqAsync(method.name, defaults(method.default, getParams(spec, method.name, arguments)), arguments[arguments.length - 1]);
}; };
}; };
}); });

10
libethrpc/spec.json

@ -21,12 +21,12 @@
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": {}}, { "method": "block", "params": { "params": {}}, "order": ["params"], "returns": {}},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": {}}, { "method": "transaction", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": {}}, { "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "messages", "params": { "json": {}}, "order": ["json"], "returns": []}, { "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "watch", "params": { "json": ""}, "order": ["json"], "returns": ""}, { "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": ""},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""}, { "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""}, { "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},

47
libqethereum/QEthereum.cpp

@ -126,9 +126,9 @@ QString QEthereum::coinbase() const
return m_client ? toQJS(client()->address()) : ""; return m_client ? toQJS(client()->address()) : "";
} }
QString QEthereum::number() const unsigned QEthereum::number() const
{ {
return m_client ? QString::number(client()->number() + 1) : ""; return client() ? client()->number() + 1 : 0;
} }
QString QEthereum::account() const QString QEthereum::account() const
@ -367,7 +367,6 @@ static QString toJson(dev::eth::BlockInfo const& _bi)
{ {
QJsonObject v; QJsonObject v;
v["hash"] = toQJS(_bi.hash); v["hash"] = toQJS(_bi.hash);
v["parentHash"] = toQJS(_bi.parentHash); v["parentHash"] = toQJS(_bi.parentHash);
v["sha3Uncles"] = toQJS(_bi.sha3Uncles); v["sha3Uncles"] = toQJS(_bi.sha3Uncles);
v["miner"] = toQJS(_bi.coinbaseAddress); v["miner"] = toQJS(_bi.coinbaseAddress);
@ -389,7 +388,6 @@ static QString toJson(dev::eth::Transaction const& _bi)
{ {
QJsonObject v; QJsonObject v;
v["hash"] = toQJS(_bi.sha3()); v["hash"] = toQJS(_bi.sha3());
v["input"] = ::fromBinary(_bi.data); v["input"] = ::fromBinary(_bi.data);
v["to"] = toQJS(_bi.receiveAddress); v["to"] = toQJS(_bi.receiveAddress);
v["from"] = toQJS(_bi.sender()); v["from"] = toQJS(_bi.sender());
@ -401,25 +399,42 @@ static QString toJson(dev::eth::Transaction const& _bi)
return QString::fromUtf8(QJsonDocument(v).toJson()); return QString::fromUtf8(QJsonDocument(v).toJson());
} }
QString QEthereum::getUncle(QString _numberOrHash, int _i) const dev::FixedHash<32> QEthereum::numberOrHash(QString const &_json) const
{
QJsonObject f = QJsonDocument::fromJson(_json.toUtf8()).object();
dev::FixedHash<32> hash;
if (f.contains("hash"))
hash = ::toFixed<32>(f["hash"].toString());
else if (f.contains("number"))
hash = client()->hashFromNumber((unsigned)f["number"].toInt());
return hash;
}
QString QEthereum::_private_getBlock(QString _json) const
{ {
auto n = toU256(_numberOrHash); if (!client())
auto h = n < m_client->number() ? m_client->hashFromNumber((unsigned)n) : ::toFixed<32>(_numberOrHash); return "";
return m_client ? toJson(m_client->uncle(h, _i)) : "";
auto hash = numberOrHash(_json);
return toJson(client()->blockInfo(hash), client()->blockDetails(hash));
} }
QString QEthereum::getTransaction(QString _numberOrHash, int _i) const QString QEthereum::_private_getTransaction(QString _json, int _i) const
{ {
auto n = toU256(_numberOrHash); if (!client())
auto h = n < m_client->number() ? m_client->hashFromNumber((unsigned)n) : ::toFixed<32>(_numberOrHash); return "";
return m_client ? toJson(m_client->transaction(h, _i)) : "";
auto hash = numberOrHash(_json);
return toJson(client()->transaction(hash, _i));
} }
QString QEthereum::getBlock(QString _numberOrHash) const QString QEthereum::_private_getUncle(QString _json, int _i) const
{ {
auto n = toU256(_numberOrHash); if (!client())
auto h = n < m_client->number() ? m_client->hashFromNumber((unsigned)n) : ::toFixed<32>(_numberOrHash); return "";
return m_client ? toJson(m_client->blockInfo(h), m_client->blockDetails(h)) : "";
auto hash = numberOrHash(_json);
return toJson(client()->uncle(hash, _i));
} }
QString QEthereum::_private_getMessages(QString _json) const QString QEthereum::_private_getMessages(QString _json) const

20
libqethereum/QEthereum.h

@ -148,9 +148,9 @@ public:
Q_INVOKABLE QString/*dev::u256*/ stateAt(QString/*dev::Address*/ _a, QString/*dev::u256*/ _p) const; Q_INVOKABLE QString/*dev::u256*/ stateAt(QString/*dev::Address*/ _a, QString/*dev::u256*/ _p) const;
Q_INVOKABLE QString/*dev::u256*/ codeAt(QString/*dev::Address*/ _a) const; Q_INVOKABLE QString/*dev::u256*/ codeAt(QString/*dev::Address*/ _a) const;
Q_INVOKABLE QString/*json*/ getBlock(QString _numberOrHash/*unsigned if < number(), hash otherwise*/) const; Q_INVOKABLE QString/*json*/ _private_getBlock(QString _json) const;
Q_INVOKABLE QString/*json*/ getTransaction(QString _numberOrHash/*unsigned if < number(), hash otherwise*/, int _index) const; Q_INVOKABLE QString/*json*/ _private_getTransaction(QString _json, int _index) const;
Q_INVOKABLE QString/*json*/ getUncle(QString _numberOrHash/*unsigned if < number(), hash otherwise*/, int _index) const; Q_INVOKABLE QString/*json*/ _private_getUncle(QString _json, int _index) const;
Q_INVOKABLE QString/*json*/ _private_getMessages(QString _attribs/*json*/) const; Q_INVOKABLE QString/*json*/ _private_getMessages(QString _attribs/*json*/) const;
@ -169,7 +169,7 @@ public:
QString/*dev::Address*/ coinbase() const; QString/*dev::Address*/ coinbase() const;
QString/*dev::u256*/ gasPrice() const { return toQJS(10 * dev::eth::szabo); } QString/*dev::u256*/ gasPrice() const { return toQJS(10 * dev::eth::szabo); }
QString/*dev::u256*/ number() const; unsigned/*dev::u256*/ number() const;
int getDefault() const; int getDefault() const;
QString/*dev::KeyPair*/ key() const; QString/*dev::KeyPair*/ key() const;
@ -205,11 +205,12 @@ private:
Q_PROPERTY(QStringList keys READ keys NOTIFY keysChanged) Q_PROPERTY(QStringList keys READ keys NOTIFY keysChanged)
Q_PROPERTY(unsigned peerCount READ peerCount NOTIFY miningChanged) Q_PROPERTY(unsigned peerCount READ peerCount NOTIFY miningChanged)
Q_PROPERTY(int defaultBlock READ getDefault WRITE setDefault) Q_PROPERTY(int defaultBlock READ getDefault WRITE setDefault)
Q_PROPERTY(QString number READ number NOTIFY watchChanged) Q_PROPERTY(unsigned number READ number NOTIFY watchChanged)
dev::eth::Interface* m_client; dev::eth::Interface* m_client;
std::vector<unsigned> m_watches; std::vector<unsigned> m_watches;
QList<dev::KeyPair> m_accounts; QList<dev::KeyPair> m_accounts;
dev::FixedHash<32> numberOrHash(QString const &_json) const;
}; };
class QWhisper: public QObject class QWhisper: public QObject
@ -279,11 +280,14 @@ private:
frame->evaluateJavaScript("eth.makeTransact = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.transact.apply(null, args)); }},0);}"); \ frame->evaluateJavaScript("eth.makeTransact = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.transact.apply(null, args)); }},0);}"); \
frame->evaluateJavaScript("eth.call = function(a) { var ret = eth.doCallJson(JSON.stringify(a)); return ret; }"); \ frame->evaluateJavaScript("eth.call = function(a) { var ret = eth.doCallJson(JSON.stringify(a)); return ret; }"); \
frame->evaluateJavaScript("eth.makeCall = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.call.apply(null, args)); }},0);}"); \ frame->evaluateJavaScript("eth.makeCall = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.call.apply(null, args)); }},0);}"); \
frame->evaluateJavaScript("eth.block = function(a) { return JSON.parse(eth._private_getBlock(JSON.stringify(a))); }"); \
frame->evaluateJavaScript("eth.getBlock = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.block.apply(null, args)); }},0);}"); \
frame->evaluateJavaScript("eth.transaction = function(a, i) { return JSON.parse(eth._private_getTransaction(JSON.stringify(a), i)); }"); \
frame->evaluateJavaScript("eth.getTransaction = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.transaction.apply(null, args)); }},0);}"); \
frame->evaluateJavaScript("eth.uncle = function(a, i) { return JSON.parse(eth._private_getUncle(JSON.stringify(a), i)); }"); \
frame->evaluateJavaScript("eth.getUncle = function() { var args = Array.prototype.slice.call(arguments, 0, -1); f = arguments[arguments.length - 1]; window.setTimeout(function () { if (f) { f(eth.uncle.apply(null, args)); }},0);}"); \
frame->evaluateJavaScript("eth.makeWatch = function(a) { var ww = eth.newWatch(a); var ret = { w: ww }; ret.uninstall = function() { eth.killWatch(w); }; ret.changed = function(f) { eth.watchChanged.connect(function(nw) { if (nw == ww) f() }); }; ret.messages = function() { return JSON.parse(eth.watchMessages(this.w)) }; return ret; }"); \ frame->evaluateJavaScript("eth.makeWatch = function(a) { var ww = eth.newWatch(a); var ret = { w: ww }; ret.uninstall = function() { eth.killWatch(w); }; ret.changed = function(f) { eth.watchChanged.connect(function(nw) { if (nw == ww) f() }); }; ret.messages = function() { return JSON.parse(eth.watchMessages(this.w)) }; return ret; }"); \
frame->evaluateJavaScript("eth.watch = function(a) { return eth.makeWatch(JSON.stringify(a)) }"); \ frame->evaluateJavaScript("eth.watch = function(a) { return eth.makeWatch(JSON.stringify(a)) }"); \
frame->evaluateJavaScript("eth.block = function(a) { return JSON.parse(eth.getBlock(a)); }"); \
frame->evaluateJavaScript("eth.transaction = function(a) { return JSON.parse(eth.getTransaction(a)); }"); \
frame->evaluateJavaScript("eth.uncle = function(a) { return JSON.parse(eth.getUncle(a)); }"); \
frame->evaluateJavaScript("shh.makeWatch = function(a) { var ww = shh.newWatch(a); var ret = { w: ww }; ret.uninstall = function() { shh.killWatch(w); }; ret.changed = function(f) { shh.watchChanged.connect(function(nw) { if (nw == ww) f() }); }; ret.messages = function() { return JSON.parse(shh.watchMessages(this.w)) }; return ret; }"); \ frame->evaluateJavaScript("shh.makeWatch = function(a) { var ww = shh.newWatch(a); var ret = { w: ww }; ret.uninstall = function() { shh.killWatch(w); }; ret.changed = function(f) { shh.watchChanged.connect(function(nw) { if (nw == ww) f() }); }; ret.messages = function() { return JSON.parse(shh.watchMessages(this.w)) }; return ret; }"); \
frame->evaluateJavaScript("shh.watch = function(a) { return shh.makeWatch(JSON.stringify(a)) }"); \ frame->evaluateJavaScript("shh.watch = function(a) { return shh.makeWatch(JSON.stringify(a)) }"); \
} }

20
test/ethstubclient.h

@ -33,10 +33,10 @@ p["block"] = block;
} }
Json::Value block(const std::string& numberOrHash) throw (jsonrpc::JsonRpcException) Json::Value block(const Json::Value& params) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["numberOrHash"] = numberOrHash; p["params"] = params;
Json::Value result = this->client->CallMethod("block",p); Json::Value result = this->client->CallMethod("block",p);
if (result.isObject()) if (result.isObject())
@ -199,10 +199,10 @@ p["s"] = s;
} }
Json::Value messages(const Json::Value& json) throw (jsonrpc::JsonRpcException) Json::Value messages(const Json::Value& params) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["json"] = json; p["params"] = params;
Json::Value result = this->client->CallMethod("messages",p); Json::Value result = this->client->CallMethod("messages",p);
if (result.isArray()) if (result.isArray())
@ -380,11 +380,11 @@ p["storage"] = storage;
} }
Json::Value transaction(const int& i, const std::string& numberOrHash) throw (jsonrpc::JsonRpcException) Json::Value transaction(const int& i, const Json::Value& params) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["i"] = i; p["i"] = i;
p["numberOrHash"] = numberOrHash; p["params"] = params;
Json::Value result = this->client->CallMethod("transaction",p); Json::Value result = this->client->CallMethod("transaction",p);
if (result.isObject()) if (result.isObject())
@ -394,11 +394,11 @@ p["numberOrHash"] = numberOrHash;
} }
Json::Value uncle(const int& i, const std::string& numberOrHash) throw (jsonrpc::JsonRpcException) Json::Value uncle(const int& i, const Json::Value& params) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["i"] = i; p["i"] = i;
p["numberOrHash"] = numberOrHash; p["params"] = params;
Json::Value result = this->client->CallMethod("uncle",p); Json::Value result = this->client->CallMethod("uncle",p);
if (result.isObject()) if (result.isObject())
@ -408,10 +408,10 @@ p["numberOrHash"] = numberOrHash;
} }
std::string watch(const std::string& json) throw (jsonrpc::JsonRpcException) std::string watch(const std::string& params) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["json"] = json; p["params"] = params;
Json::Value result = this->client->CallMethod("watch",p); Json::Value result = this->client->CallMethod("watch",p);
if (result.isString()) if (result.isString())

25
test/jsonrpc.cpp

@ -58,11 +58,7 @@ BOOST_AUTO_TEST_CASE(jsonrpc_balanceAt)
dev::KeyPair key = KeyPair::create(); dev::KeyPair key = KeyPair::create();
auto address = key.address(); auto address = key.address();
string balance = jsonrpcClient->balanceAt(toJS(address), 0); string balance = jsonrpcClient->balanceAt(toJS(address), 0);
BOOST_CHECK_EQUAL(jsToDecimal(toJS(web3.ethereum()->balanceAt(address))), balance); BOOST_CHECK_EQUAL(toJS(web3.ethereum()->balanceAt(address)), balance);
}
BOOST_AUTO_TEST_CASE(jsonrpc_block)
{
} }
BOOST_AUTO_TEST_CASE(jsonrpc_call) BOOST_AUTO_TEST_CASE(jsonrpc_call)
@ -162,10 +158,16 @@ BOOST_AUTO_TEST_CASE(jsonrpc_keys)
BOOST_AUTO_TEST_CASE(jsonrpc_lll) BOOST_AUTO_TEST_CASE(jsonrpc_lll)
{ {
} }
BOOST_AUTO_TEST_CASE(jsonrpc_messages) BOOST_AUTO_TEST_CASE(jsonrpc_messages)
{ {
cnote << "Testing jsonrpc messages...";
Json::Value msgs = jsonrpcClient->messages(Json::Value());
auto messages = web3.ethereum()->messages(dev::eth::MessageFilter());
BOOST_CHECK_EQUAL(msgs.isArray(), true);
BOOST_CHECK_EQUAL(msgs.size(), messages.size());
} }
BOOST_AUTO_TEST_CASE(jsonrpc_number) BOOST_AUTO_TEST_CASE(jsonrpc_number)
@ -293,19 +295,6 @@ BOOST_AUTO_TEST_CASE(jsonrpc_transact)
BOOST_CHECK_EQUAL(txAmount, jsToU256(messages[0u]["value"].asString())); BOOST_CHECK_EQUAL(txAmount, jsToU256(messages[0u]["value"].asString()));
} }
BOOST_AUTO_TEST_CASE(jsonrpc_transaction)
{
// TODO! not working?
// auto messages = jsonrpcClient->messages(Json::Value());
// auto transactionNumber = messages[0u]["path"][0u].asInt();
// auto transactionBlock = messages[0u]["block"].asString();
// Json::Value p = jsonrpcClient->transaction(transactionNumber, transactionBlock);
}
BOOST_AUTO_TEST_CASE(jsonrpc_uncle)
{
}
BOOST_AUTO_TEST_CASE(jsonrpc_watch) BOOST_AUTO_TEST_CASE(jsonrpc_watch)
{ {

Loading…
Cancel
Save