Browse Source

jsonrpc in progress

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
dd419086d3
  1. 153
      eth/EthStubServer.cpp
  2. 12
      eth/EthStubServer.h
  3. 36
      eth/abstractethstubserver.h
  4. 12
      eth/eth.js
  5. 12
      eth/spec.json
  6. 10
      libdevcore/CommonJS.h

153
eth/EthStubServer.cpp

@ -194,72 +194,195 @@ std::string EthStubServer::lll(const string &s)
return toJS(dev::eth::compileLLL(s));
}
std::string EthStubServer::messages(const string &json)
static dev::eth::MessageFilter toMessageFilter(const Json::Value &json)
{
dev::eth::MessageFilter filter;
if (!json.isObject() || json.empty()){
return filter;
}
if (!json["earliest"].empty())
filter.withEarliest(json["earliest"].asInt());
if (!json["latest"].empty())
filter.withLatest(json["lastest"].asInt());
if (!json["max"].empty())
filter.withMax(json["max"].asInt());
if (!json["skip"].empty())
filter.withSkip(json["skip"].asInt());
if (!json["from"].empty())
{
if (json["from"].isArray())
for (auto i : json["from"])
filter.from(jsToAddress(i.asString()));
else
filter.from(jsToAddress(json["from"].asString()));
}
if (!json["to"].empty())
{
if (json["to"].isArray())
for (auto i : json["to"])
filter.from(jsToAddress(i.asString()));
else
filter.from(jsToAddress(json["to"].asString()));
}
if (!json["altered"].empty())
{
if (json["altered"].isArray())
for (auto i: json["altered"])
if (i.isObject())
filter.altered(jsToAddress(i["id"].asString()), jsToU256(i["at"].asString()));
else
filter.altered((jsToAddress(i.asString())));
else if (json["altered"].isObject())
filter.altered(jsToAddress(json["altered"]["id"].asString()), jsToU256(json["altered"]["at"].asString()));
else
filter.altered(jsToAddress(json["altered"].asString()));
}
return filter;
}
int EthStubServer::number()
Json::Value EthStubServer::messages(const Json::Value &json)
{
Json::Value res;
if (!client())
return res;
dev::eth::PastMessages pms = client()->messages(toMessageFilter(json));
for (dev::eth::PastMessage const & t: pms)
{
res["input"] = jsFromBinary(t.input);
res["output"] = jsFromBinary(t.output);
res["to"] = boost::lexical_cast<string>(t.to);
res["from"] = boost::lexical_cast<string>(t.from);
res["origin"] = boost::lexical_cast<string>(t.origin);
res["timestamp"] = boost::lexical_cast<string>(t.timestamp);
res["coinbase"] = boost::lexical_cast<string>(t.coinbase);
res["block"] = boost::lexical_cast<string>(t.block);
Json::Value path;
for (int i: t.path)
path.append(i);
res["path"] = path;
res["number"] = (int)t.number;
}
return res;
}
int EthStubServer::number()
{
return client() ? client()->number() + 1 : 0;
}
//TODO!
int EthStubServer::peerCount()
{
return m_web3.peerCount();
return /*client() ? (unsigned)client()->peerCount() :*/ 0;
//return m_web3.peerCount();
}
std::string EthStubServer::secretToAddress(const string &s)
{
return toJS(KeyPair(jsToSecret(s)).address());
}
std::string EthStubServer::setListening(const string &l)
Json::Value EthStubServer::setListening(const bool &l)
{
if (!client())
return Json::Value();
/* if (l)
client()->startNetwork();
else
client()->stopNetwork();*/
return Json::Value();
}
std::string EthStubServer::setMining(const string &l)
Json::Value EthStubServer::setMining(const bool &l)
{
if (!client())
return Json::Value();
if (l)
client()->startMining();
else
client()->stopMining();
return Json::Value();
}
std::string EthStubServer::sha3(const string &s)
{
return toJS(dev::eth::sha3(jsToBytes(s)));
}
std::string EthStubServer::stateAt(const string &a, const int& block, const string &p)
{
return client() ? toJS(client()->stateAt(jsToAddress(a), jsToU256(p), block)) : "";
}
std::string EthStubServer::toAscii(const string &s)
{
return jsToBinary(s);
}
std::string EthStubServer::toDecimal(const string &s)
{
return jsToDecimal(s);
}
std::string EthStubServer::toFixed(const string &s)
std::string EthStubServer::toFixed(const double &s)
{
return jsToFixed(s);
}
std::string EthStubServer::transact(const string &json)
{
std::string ret;
if (!client())
return ret;
TransactionJS t = toTransaction(json);
if (!t.from && m_keys.size())
{
auto b = m_keys.front();
for (auto a: m_keys)
if (client()->balanceAt(KeyPair(a).address()) > client()->balanceAt(KeyPair(b).address()))
b = a;
t.from = b.secret();
}
if (!t.gasPrice)
t.gasPrice = 10 * dev::eth::szabo;
if (!t.gas)
t.gas = min<u256>(client()->gasLimitRemaining(), client()->balanceAt(KeyPair(t.from).address()) / t.gasPrice);
if (t.to)
client()->transact(t.from, t.value, t.to, t.data, t.gas, t.gasPrice);
else
ret = toJS(client()->transact(t.from, t.value, t.data, t.gas, t.gasPrice));
client()->flushTransactions();
return ret;
}
std::string EthStubServer::transaction(const string &i, const string &numberOrHash)
Json::Value EthStubServer::transaction(const int &i, const string &numberOrHash)
{
if (!client()){
return Json::Value();
}
auto n = jsToU256(numberOrHash);
auto h = n < client()->number() ? client()->hashFromNumber((unsigned)n) : jsToFixed<32>(numberOrHash);
dev::eth::Transaction t = client()->transaction(h, i);
Json::Value res;
res["hash"] = boost::lexical_cast<string>(t.sha3());
res["input"] = jsFromBinary(t.data);
res["to"] = boost::lexical_cast<string>(t.receiveAddress);
res["from"] = boost::lexical_cast<string>(t.sender());
res["gas"] = (int)t.gas;
res["gasPrice"] = boost::lexical_cast<string>(t.gasPrice);
res["nonce"] = boost::lexical_cast<string>(t.nonce);
res["value"] = boost::lexical_cast<string>(t.value);
return res;
}
std::string EthStubServer::uncle(const string &i, const string &numberOrHash)
Json::Value EthStubServer::uncle(const int &i, const string &numberOrHash)
{
}

12
eth/EthStubServer.h

@ -51,20 +51,20 @@ public:
virtual std::string key();
virtual Json::Value keys();
virtual std::string lll(const std::string& s);
virtual std::string messages(const std::string& json);
virtual Json::Value messages(const Json::Value& json);
virtual int number();
virtual int peerCount();
virtual std::string secretToAddress(const std::string& s);
virtual std::string setListening(const std::string& l);
virtual std::string setMining(const std::string& l);
virtual Json::Value setListening(const bool& l);
virtual Json::Value setMining(const bool& l);
virtual std::string sha3(const std::string& s);
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);
virtual std::string toFixed(const double& s);
virtual std::string transact(const std::string& json);
virtual std::string transaction(const std::string& i, const std::string& numberOrHash);
virtual std::string uncle(const std::string& i, const std::string& numberOrHash);
virtual Json::Value transaction(const int& i, const std::string& numberOrHash);
virtual Json::Value uncle(const int& i, const std::string& numberOrHash);
virtual std::string watch(const std::string& json);
void setKeys(std::vector<dev::KeyPair> _keys) { m_keys = _keys; }

36
eth/abstractethstubserver.h

@ -28,20 +28,20 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
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("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_STRING, "json",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::messagesI);
this->bindAndAddMethod(new jsonrpc::Procedure("messages", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "json",jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::messagesI);
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("secretToAddress", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::secretToAddressI);
this->bindAndAddMethod(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("setListening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "l",jsonrpc::JSON_BOOLEAN, NULL), &AbstractEthStubServer::setListeningI);
this->bindAndAddMethod(new jsonrpc::Procedure("setMining", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "l",jsonrpc::JSON_BOOLEAN, 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_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);
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_STRING, NULL), &AbstractEthStubServer::transactI);
this->bindAndAddMethod(new jsonrpc::Procedure("transaction", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "i",jsonrpc::JSON_STRING,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::transactionI);
this->bindAndAddMethod(new jsonrpc::Procedure("uncle", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "i",jsonrpc::JSON_STRING,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::uncleI);
this->bindAndAddMethod(new jsonrpc::Procedure("transaction", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "i",jsonrpc::JSON_INTEGER,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::transactionI);
this->bindAndAddMethod(new jsonrpc::Procedure("uncle", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, "i",jsonrpc::JSON_INTEGER,"numberOrHash",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::uncleI);
this->bindAndAddMethod(new jsonrpc::Procedure("watch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::watchI);
}
@ -123,7 +123,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void messagesI(const Json::Value& request, Json::Value& response)
{
response = this->messages(request["json"].asString());
response = this->messages(request["json"]);
}
inline virtual void numberI(const Json::Value& request, Json::Value& response)
@ -143,12 +143,12 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void setListeningI(const Json::Value& request, Json::Value& response)
{
response = this->setListening(request["l"].asString());
response = this->setListening(request["l"].asBool());
}
inline virtual void setMiningI(const Json::Value& request, Json::Value& response)
{
response = this->setMining(request["l"].asString());
response = this->setMining(request["l"].asBool());
}
inline virtual void sha3I(const Json::Value& request, Json::Value& response)
@ -173,7 +173,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void toFixedI(const Json::Value& request, Json::Value& response)
{
response = this->toFixed(request["s"].asString());
response = this->toFixed(request["s"].asDouble());
}
inline virtual void transactI(const Json::Value& request, Json::Value& response)
@ -183,12 +183,12 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void transactionI(const Json::Value& request, Json::Value& response)
{
response = this->transaction(request["i"].asString(), request["numberOrHash"].asString());
response = this->transaction(request["i"].asInt(), request["numberOrHash"].asString());
}
inline virtual void uncleI(const Json::Value& request, Json::Value& response)
{
response = this->uncle(request["i"].asString(), request["numberOrHash"].asString());
response = this->uncle(request["i"].asInt(), request["numberOrHash"].asString());
}
inline virtual void watchI(const Json::Value& request, Json::Value& response)
@ -212,20 +212,20 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string key() = 0;
virtual Json::Value keys() = 0;
virtual std::string lll(const std::string& s) = 0;
virtual std::string messages(const std::string& json) = 0;
virtual Json::Value messages(const Json::Value& json) = 0;
virtual int number() = 0;
virtual int peerCount() = 0;
virtual std::string secretToAddress(const std::string& s) = 0;
virtual std::string setListening(const std::string& l) = 0;
virtual std::string setMining(const std::string& l) = 0;
virtual Json::Value setListening(const bool& l) = 0;
virtual Json::Value setMining(const bool& l) = 0;
virtual std::string sha3(const std::string& s) = 0;
virtual std::string stateAt(const std::string& a, const int& block, const std::string& p) = 0;
virtual std::string toAscii(const std::string& s) = 0;
virtual std::string toDecimal(const std::string& s) = 0;
virtual std::string toFixed(const std::string& s) = 0;
virtual std::string toFixed(const double& s) = 0;
virtual std::string transact(const std::string& json) = 0;
virtual std::string transaction(const std::string& i, const std::string& numberOrHash) = 0;
virtual std::string uncle(const std::string& i, const std::string& numberOrHash) = 0;
virtual Json::Value transaction(const int& i, const std::string& numberOrHash) = 0;
virtual Json::Value uncle(const int& i, const std::string& numberOrHash) = 0;
virtual std::string watch(const std::string& json) = 0;
};

12
eth/eth.js

@ -20,9 +20,9 @@ var spec = [
// properties
{ "method": "coinbase", "params": null, "order": [], "returns" : "" },
{ "method": "isListening", "params": null, "order": [], "returns" : false },
{ "method": "setListening", "params": { "l": "" }, "order" : ["l"], "returns" : ""},
{ "method": "setListening", "params": { "l": "" }, "order" : ["l"], "returns" : undefined},
{ "method": "isMining", "params": null, "order": [], "returns" : false },
{ "method": "setMining", "params": { "l": "" }, "order" : ["l"], "returns" : ""},
{ "method": "setMining", "params": { "l": "" }, "order" : ["l"], "returns" : undefined},
{ "method": "gasPrice", "params": null, "order": [], "returns" : "" },
{ "method": "key", "params": null, "order": [], "returns" : "" },
{ "method": "keys", "params": null, "order": [], "returns" : [] },
@ -42,11 +42,11 @@ var spec = [
// blockchain
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": []},
{ "method": "transaction", "params": { "numberOrHash": "", "i": ""}, "order": ["numberOrHash", "i"], "returns": ""},
{ "method": "uncle", "params": { "numberOrHash": "", "i": ""}, "order": ["numberOrHash", "i"], "returns": ""},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
// watches and message filtering
{ "method": "messages", "params": { "json": ""}, "order": ["json"], "returns": ""},
{ "method": "messages", "params": { "json": []}, "order": ["json"], "returns": []},
{ "method": "watch", "params": { "json": ""}, "order": ["json"], "returns": ""},
// misc
@ -56,7 +56,7 @@ var spec = [
{ "method": "toAscii", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "fromAscii", "params": { "s": "", "padding": 0}, "order": ["s", "padding"], "returns": ""},
{ "method": "toDecimal", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": 0.0}, "order": ["s"], "returns" : ""},
{ "method": "fromFixed", "params": {"s": ""}, "order": ["s"], "returns" : 0.0},
{ "method": "offset", "params": {"s": "", "offset": ""}, "order": ["s", "offset"], "returns" : ""},
];

12
eth/spec.json

@ -1,9 +1,9 @@
[
{ "method": "coinbase", "params": null, "order": [], "returns" : "" },
{ "method": "isListening", "params": null, "order": [], "returns" : false },
{ "method": "setListening", "params": { "l": "" }, "order" : ["l"], "returns" : ""},
{ "method": "setListening", "params": { "l": false }, "order" : ["l"], "returns" : []},
{ "method": "isMining", "params": null, "order": [], "returns" : false },
{ "method": "setMining", "params": { "l": "" }, "order" : ["l"], "returns" : ""},
{ "method": "setMining", "params": { "l": false }, "order" : ["l"], "returns" : []},
{ "method": "gasPrice", "params": null, "order": [], "returns" : "" },
{ "method": "key", "params": null, "order": [], "returns" : "" },
{ "method": "keys", "params": null, "order": [], "returns" : [] },
@ -21,10 +21,10 @@
{ "method": "call", "params": { "json": []}, "order": ["json"], "returns": ""},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": []},
{ "method": "transaction", "params": { "numberOrHash": "", "i": ""}, "order": ["numberOrHash", "i"], "returns": ""},
{ "method": "uncle", "params": { "numberOrHash": "", "i": ""}, "order": ["numberOrHash", "i"], "returns": ""},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "messages", "params": { "json": ""}, "order": ["json"], "returns": ""},
{ "method": "messages", "params": { "json": []}, "order": ["json"], "returns": []},
{ "method": "watch", "params": { "json": ""}, "order": ["json"], "returns": ""},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
@ -33,7 +33,7 @@
{ "method": "toAscii", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "fromAscii", "params": { "s": "", "padding": 0}, "order": ["s", "padding"], "returns": ""},
{ "method": "toDecimal", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": 0.0}, "order": ["s"], "returns" : ""},
{ "method": "fromFixed", "params": {"s": ""}, "order": ["s"], "returns" : 0.0}
]

10
libdevcore/CommonJS.h

@ -54,6 +54,16 @@ inline Address jsToAddress(std::string const& _s) { return jsToFixed<20>(_s); }
inline Secret jsToSecret(std::string const& _s) { return jsToFixed<32>(_s); }
inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); }
inline std::string jsToBinary(std::string const& _s)
{
return jsUnpadded(dev::toString(jsToBytes(_s)));
}
inline std::string jsToDecimal(std::string const& _s)
{
return dev::toString(jsToU256(_s));
}
inline std::string jsFromBinary(dev::bytes _s, unsigned _padding = 32)
{
_s.resize(std::max<unsigned>(_s.size(), _padding));

Loading…
Cancel
Save