From 5619789270a920ed66b751211cf593ee25eb6352 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 5 May 2014 17:22:31 +0100 Subject: [PATCH] New JS-API files. Build fixes. --- eth/EthStubServer.cpp | 2 + eth/abstractethstubserver.h | 127 ++++++++++++++++++++++++++++++++++++ eth/eth.js | 101 ++++++++++++++++++++++++++++ eth/main.cpp | 3 - eth/spec.json | 21 ++++++ 5 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 eth/abstractethstubserver.h create mode 100644 eth/eth.js create mode 100644 eth/spec.json diff --git a/eth/EthStubServer.cpp b/eth/EthStubServer.cpp index 7f1a825c5..a2aadec8a 100644 --- a/eth/EthStubServer.cpp +++ b/eth/EthStubServer.cpp @@ -20,6 +20,7 @@ * @date 2014 */ +#if ETH_JSONRPC #include "EthStubServer.h" #include #include "CommonJS.h" @@ -128,3 +129,4 @@ std::string EthStubServer::secretToAddress(const std::string& _a) { return toJS(KeyPair(jsToSecret(_a)).address()); } +#endif diff --git a/eth/abstractethstubserver.h b/eth/abstractethstubserver.h new file mode 100644 index 000000000..74a50c514 --- /dev/null +++ b/eth/abstractethstubserver.h @@ -0,0 +1,127 @@ +/** + * THIS FILE IS GENERATED BY jsonrpcstub, DO NOT CHANGE IT!!!!! + */ + +#ifndef _ABSTRACTETHSTUBSERVER_H_ +#define _ABSTRACTETHSTUBSERVER_H_ + +#include + +class AbstractEthStubServer : public jsonrpc::AbstractServer +{ + public: + AbstractEthStubServer(jsonrpc::AbstractServerConnector* conn) : + jsonrpc::AbstractServer(conn) + { + this->bindAndAddMethod(new jsonrpc::Procedure("balanceAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::balanceAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("check", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "a",jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::checkI); + this->bindAndAddMethod(new jsonrpc::Procedure("coinbase", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::coinbaseI); + this->bindAndAddMethod(new jsonrpc::Procedure("create", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "bCode",jsonrpc::JSON_STRING,"sec",jsonrpc::JSON_STRING,"xEndowment",jsonrpc::JSON_STRING,"xGas",jsonrpc::JSON_STRING,"xGasPrice",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::createI); + this->bindAndAddMethod(new jsonrpc::Procedure("gasPrice", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::gasPriceI); + this->bindAndAddMethod(new jsonrpc::Procedure("isContractAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, "a",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::isContractAtI); + 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); + 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("peerCount", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::peerCountI); + this->bindAndAddMethod(new jsonrpc::Procedure("secretToAddress", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::secretToAddressI); + this->bindAndAddMethod(new jsonrpc::Procedure("storageAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING,"x",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::storageAtI); + this->bindAndAddMethod(new jsonrpc::Procedure("transact", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "aDest",jsonrpc::JSON_STRING,"bData",jsonrpc::JSON_STRING,"sec",jsonrpc::JSON_STRING,"xGas",jsonrpc::JSON_STRING,"xGasPrice",jsonrpc::JSON_STRING,"xValue",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::transactI); + this->bindAndAddMethod(new jsonrpc::Procedure("txCountAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::txCountAtI); + + } + + inline virtual void balanceAtI(const Json::Value& request, Json::Value& response) + { + response = this->balanceAt(request["a"].asString()); + } + + inline virtual void checkI(const Json::Value& request, Json::Value& response) + { + response = this->check(request["a"]); + } + + inline virtual void coinbaseI(const Json::Value& request, Json::Value& response) + { + response = this->coinbase(); + } + + inline virtual void createI(const Json::Value& request, Json::Value& response) + { + response = this->create(request["bCode"].asString(), request["sec"].asString(), request["xEndowment"].asString(), request["xGas"].asString(), request["xGasPrice"].asString()); + } + + inline virtual void gasPriceI(const Json::Value& request, Json::Value& response) + { + response = this->gasPrice(); + } + + inline virtual void isContractAtI(const Json::Value& request, Json::Value& response) + { + response = this->isContractAt(request["a"].asString()); + } + + inline virtual void isListeningI(const Json::Value& request, Json::Value& response) + { + response = this->isListening(); + } + + inline virtual void isMiningI(const Json::Value& request, Json::Value& response) + { + response = this->isMining(); + } + + inline virtual void keyI(const Json::Value& request, Json::Value& response) + { + response = this->key(); + } + + inline virtual void keysI(const Json::Value& request, Json::Value& response) + { + response = this->keys(); + } + + inline virtual void peerCountI(const Json::Value& request, Json::Value& response) + { + response = this->peerCount(); + } + + inline virtual void secretToAddressI(const Json::Value& request, Json::Value& response) + { + response = this->secretToAddress(request["a"].asString()); + } + + inline virtual void storageAtI(const Json::Value& request, Json::Value& response) + { + response = this->storageAt(request["a"].asString(), request["x"].asString()); + } + + inline virtual void transactI(const Json::Value& request, Json::Value& response) + { + response = this->transact(request["aDest"].asString(), request["bData"].asString(), request["sec"].asString(), request["xGas"].asString(), request["xGasPrice"].asString(), request["xValue"].asString()); + } + + inline virtual void txCountAtI(const Json::Value& request, Json::Value& response) + { + response = this->txCountAt(request["a"].asString()); + } + + + virtual std::string balanceAt(const std::string& a) = 0; + virtual Json::Value check(const Json::Value& a) = 0; + virtual std::string coinbase() = 0; + virtual Json::Value create(const std::string& bCode, const std::string& sec, const std::string& xEndowment, const std::string& xGas, const std::string& xGasPrice) = 0; + virtual std::string gasPrice() = 0; + virtual bool isContractAt(const std::string& a) = 0; + virtual bool isListening() = 0; + virtual bool isMining() = 0; + virtual std::string key() = 0; + virtual Json::Value keys() = 0; + virtual int peerCount() = 0; + virtual std::string secretToAddress(const std::string& a) = 0; + virtual std::string storageAt(const std::string& a, const std::string& x) = 0; + virtual Json::Value transact(const std::string& aDest, const std::string& bData, const std::string& sec, const std::string& xGas, const std::string& xGasPrice, const std::string& xValue) = 0; + virtual std::string txCountAt(const std::string& a) = 0; + +}; +#endif //_ABSTRACTETHSTUBSERVER_H_ diff --git a/eth/eth.js b/eth/eth.js new file mode 100644 index 000000000..63554d093 --- /dev/null +++ b/eth/eth.js @@ -0,0 +1,101 @@ +var spec = [ + { "method": "coinbase", "params": null, "returns" : "" }, + { "method": "isListening", "params": null, "returns" : false }, + { "method": "isMining", "params": null, "returns" : false }, + { "method": "gasPrice", "params": null, "returns" : "" }, + { "method": "key", "params": null, "returns" : "" }, + { "method": "keys", "params": null, "returns" : [""] }, + { "method": "peerCount", "params": null, "returns" : 0 }, + { "method": "balanceAt", "params": { "a": "" }, "order": ["a"], "returns" : "" }, + { "method": "storageAt", "params": { "a": "", "x": "" }, "order": ["a", "x"], "returns" : "" }, + { "method": "txCountAt", "params": { "a": "" }, "order": ["a"], "returns" : "" }, + { "method": "isContractAt", "params": { "a": "" }, "order": ["a"], "returns" : false }, + { "method": "create", "params": { "sec": "", "xEndowment": "", "bCode": "", "xGas": "", "xGasPrice": "" }, "order": ["sec", "xEndowment", "bCode", "xGas", "xGasPrice"] }, + { "method": "transact", "params": { "sec": "", "xValue": "", "aDest": "", "bData": "", "xGas": "", "xGasPrice": "" }, "order": ["sec", "xValue", "aDest", "bData", "xGas", "xGasPrice"] }, + { "method": "secretToAddress", "params": { "a": "" }, "order": ["a"], "returns" : "" } +]; + +var eth = (function ethScope() { + var m_reqId = 0 + var ret = {} + function reqSync(m, p) { + var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId } + m_reqId++ + var request = new XMLHttpRequest(); + request.open("POST", "http://localhost:8080", false); + request.send(JSON.stringify(req)); + return JSON.parse(request.responseText).result; + } + function reqAsync(m, p, f) { + var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId } + m_reqId++ + var request = new XMLHttpRequest(); + request.open("POST", "http://localhost:8080", true); + request.send(JSON.stringify(req)); + request.onreadystatechange = function() { + if (request.readyState === 4) + f(JSON.parse(request.responseText).result) + }; + } + function isEmpty(obj) { + for (var prop in obj) + if (obj.hasOwnProperty(prop)) + return false + return true + } + + var m_watching = {}; + + for (si in spec) (function(s) { + var m = s.method; + var am = "get" + m.slice(0, 1).toUpperCase() + m.slice(1); + var getParams = function(a) { + var p = s.params ? {} : null + for (j in s.order) + p[s.order[j]] = a[j] + return p + }; + ret[am] = function() { return reqAsync(m, getParams(arguments), arguments[s.order.length]) } + ret[m] = function() { return reqSync(m, getParams(arguments)) } + })(spec[si]); + + ret.check = function() { + if (!m_watching) + return + var watching = []; + for (var w in m_watching) + watching.push(w) + console.log("Checking " + JSON.stringify(watching)); + var changed = reqSync("check", watching); + console.log("Got " + JSON.stringify(changed)); + for (var c in changed) + m_watching[c]() + setTimeout(this.check, 5000) + } + + ret.watch = function(a, fx, f) { + var old = isEmpty(m_watching) + if (f) + m_watching[a + fx] = f + else + m_watching[a] = fx + (f ? f : fx)() + if (isEmpty(m_watching) != old) + this.check() + } + ret.unwatch = function(f, fx) { + delete m_watching[fx ? f + fx : f]; + } + ret.newBlock = function(f) { + console.log(JSON.stringify(m_watching) + " " + (m_watching == {})) + var old = isEmpty(m_watching) + m_watching[""] = f + f() + console.log(JSON.stringify(m_watching) + " " + (m_watching == {})) + console.log("Check?" + (m_watching === {}) + "!=" + old) + if (isEmpty(m_watching) != old) + this.check() + } + return ret; +}()); + diff --git a/eth/main.cpp b/eth/main.cpp index 532e24e90..e7433c5ae 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -20,9 +20,6 @@ * Ethereum client. */ -#define ETH_READLINE 1 -#define ETH_JSONRPC 1 - #include #include #include diff --git a/eth/spec.json b/eth/spec.json new file mode 100644 index 000000000..eaeb2c295 --- /dev/null +++ b/eth/spec.json @@ -0,0 +1,21 @@ +[ + { "method": "coinbase", "params": null, "returns" : "" }, + { "method": "isListening", "params": null, "returns" : false }, + { "method": "isMining", "params": null, "returns" : false }, + { "method": "gasPrice", "params": null, "returns" : "" }, + { "method": "key", "params": null, "returns" : "" }, + { "method": "keys", "params": null, "returns" : [""] }, + { "method": "peerCount", "params": null, "returns" : 0 }, + { "method": "balanceAt", "params": { "a": "" }, "order": ["a"], "returns" : "" }, + { "method": "storageAt", "params": { "a": "", "x": "" }, "order": ["a", "x"], "returns" : "" }, + { "method": "txCountAt", "params": { "a": "" }, "order": ["a"], "returns" : "" }, + { "method": "isContractAt", "params": { "a": "" }, "order": ["a"], "returns" : false }, + { "method": "create", "params": { "sec": "", "xEndowment": "", "bCode": "", "xGas": "", "xGasPrice": "" }, "order": ["sec", "xEndowment", "bCode", "xGas", "xGasPrice"] }, + { "method": "transact", "params": { "sec": "", "xValue": "", "aDest": "", "bData": "", "xGas": "", "xGasPrice": "" }, "order": ["sec", "xValue", "aDest", "bData", "xGas", "xGasPrice"] }, + { "method": "secretToAddress", "params": { "a": "" }, "order": ["a"], "returns" : "" } + , + + { "method": "check", "params": { "a": [ "", "", "" ] }, "returns" : [ "", "" ] } +] + +