Browse Source

common changes in jsonrpc && tests

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
008fd25cfe
  1. 40
      libethrpc/EthStubServer.cpp
  2. 4
      libethrpc/EthStubServer.h
  3. 10
      libethrpc/abstractethstubserver.h
  4. 14
      libethrpc/eth.js
  5. 14
      libethrpc/spec.json
  6. 8
      test/ethstubclient.h
  7. 49
      test/jsonrpc.cpp

40
libethrpc/EthStubServer.cpp

@ -36,18 +36,18 @@ static Json::Value toJson(const dev::eth::BlockInfo& bi)
Json::Value res;
res["hash"] = boost::lexical_cast<string>(bi.hash);
res["parentHash"] = boost::lexical_cast<string>(bi.parentHash);
res["sha3Uncles"] = boost::lexical_cast<string>(bi.sha3Uncles);
res["miner"] = boost::lexical_cast<string>(bi.coinbaseAddress);
res["stateRoot"] = boost::lexical_cast<string>(bi.stateRoot);
res["transactionsRoot"] = boost::lexical_cast<string>(bi.transactionsRoot);
res["difficulty"] = boost::lexical_cast<string>(bi.difficulty);
res["number"] = boost::lexical_cast<string>(bi.number);
res["minGasPrice"] = boost::lexical_cast<string>(bi.minGasPrice);
res["gasLimit"] = boost::lexical_cast<string>(bi.gasLimit);
res["timestamp"] = boost::lexical_cast<string>(bi.timestamp);
res["parentHash"] = toJS(bi.parentHash);
res["sha3Uncles"] = toJS(bi.sha3Uncles);
res["miner"] = toJS(bi.coinbaseAddress);
res["stateRoot"] = toJS(bi.stateRoot);
res["transactionsRoot"] = toJS(bi.transactionsRoot);
res["difficulty"] = toJS(bi.difficulty);
res["number"] = (int)bi.number;
res["minGasPrice"] = toJS(bi.minGasPrice);
res["gasLimit"] = (int)bi.gasLimit;
res["timestamp"] = (int)bi.timestamp;
res["extraData"] = jsFromBinary(bi.extraData);
res["nonce"] = boost::lexical_cast<string>(bi.nonce);
res["nonce"] = toJS(bi.nonce);
return res;
}
@ -56,12 +56,12 @@ static Json::Value toJson(const dev::eth::PastMessage& t)
Json::Value res;
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);
res["to"] = toJS(t.to);
res["from"] = toJS(t.from);
res["origin"] = toJS(t.origin);
res["timestamp"] = toJS(t.timestamp);
res["coinbase"] = toJS(t.coinbase);
res["block"] = toJS(t.block);
Json::Value path;
for (int i: t.path)
path.append(i);
@ -169,9 +169,9 @@ std::string EthStubServer::coinbase()
return client() ? toJS(client()->address()) : "";
}
std::string EthStubServer::countAt(const string &a, const int& block)
double EthStubServer::countAt(const string &a, const int& block)
{
return client() ? toJS(client()->countAt(jsToAddress(a), block)) : "";
return client() ? (double)(uint64_t)client()->countAt(jsToAddress(a), block) : 0;
}
int EthStubServer::defaultBlock()
@ -347,7 +347,7 @@ std::string EthStubServer::toFixed(const double &s)
return jsToFixed(s);
}
std::string EthStubServer::transact(const string &json)
std::string EthStubServer::transact(const Json::Value &json)
{
std::string ret;
if (!client())

4
libethrpc/EthStubServer.h

@ -41,7 +41,7 @@ public:
virtual std::string call(const Json::Value& json);
virtual std::string codeAt(const std::string& a, const int& block);
virtual std::string coinbase();
virtual std::string countAt(const std::string& a, const int& block);
virtual double countAt(const std::string& a, const int& block);
virtual int defaultBlock();
virtual std::string fromAscii(const int& padding, const std::string& s);
virtual double fromFixed(const std::string& s);
@ -62,7 +62,7 @@ public:
virtual std::string toAscii(const std::string& s);
virtual std::string toDecimal(const std::string& s);
virtual std::string toFixed(const double& s);
virtual std::string transact(const std::string& json);
virtual std::string transact(const Json::Value& json);
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);

10
libethrpc/abstractethstubserver.h

@ -18,7 +18,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
this->bindAndAddMethod(new jsonrpc::Procedure("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_ARRAY, NULL), &AbstractEthStubServer::callI);
this->bindAndAddMethod(new jsonrpc::Procedure("codeAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "a",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_STRING, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::countAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("countAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_REAL, "a",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::countAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("defaultBlock", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_INTEGER, NULL), &AbstractEthStubServer::defaultBlockI);
this->bindAndAddMethod(new jsonrpc::Procedure("fromAscii", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "padding",jsonrpc::JSON_INTEGER,"s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromAsciiI);
this->bindAndAddMethod(new jsonrpc::Procedure("fromFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_REAL, "s",jsonrpc::JSON_STRING, NULL), &AbstractEthStubServer::fromFixedI);
@ -39,7 +39,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
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_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("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_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);
@ -178,7 +178,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
inline virtual void transactI(const Json::Value& request, Json::Value& response)
{
response = this->transact(request["json"].asString());
response = this->transact(request["json"]);
}
inline virtual void transactionI(const Json::Value& request, Json::Value& response)
@ -202,7 +202,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string call(const Json::Value& json) = 0;
virtual std::string codeAt(const std::string& a, const int& block) = 0;
virtual std::string coinbase() = 0;
virtual std::string countAt(const std::string& a, const int& block) = 0;
virtual double countAt(const std::string& a, const int& block) = 0;
virtual int defaultBlock() = 0;
virtual std::string fromAscii(const int& padding, const std::string& s) = 0;
virtual double fromFixed(const std::string& s) = 0;
@ -223,7 +223,7 @@ class AbstractEthStubServer : public jsonrpc::AbstractServer<AbstractEthStubServ
virtual std::string toAscii(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 transact(const std::string& 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 uncle(const int& i, const std::string& numberOrHash) = 0;
virtual std::string watch(const std::string& json) = 0;

14
libethrpc/eth.js

@ -33,20 +33,20 @@ var spec = [
// synchronous getters
{ "method": "balanceAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : ""},
{ "method": "stateAt", "params": { "a": "", "s": "", "block": 0}, "order": ["a", "s", "block"], "returns": ""},
{ "method": "countAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : ""},
{ "method": "countAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : 0.0},
{ "method": "codeAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns": ""},
// transactions
{ "method": "transact", "params": { "json": ""}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": []}, "order": ["json"], "returns": ""},
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
// blockchain
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": []},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "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

14
libethrpc/spec.json

@ -14,17 +14,17 @@
{ "method": "balanceAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : ""},
{ "method": "stateAt", "params": { "a": "", "s": "", "block": 0}, "order": ["a", "s", "block"], "returns": ""},
{ "method": "countAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : ""},
{ "method": "countAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns" : 0.0},
{ "method": "codeAt", "params": { "a": "", "block": 0}, "order": ["a", "block"], "returns": ""},
{ "method": "transact", "params": { "json": ""}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": []}, "order": ["json"], "returns": ""},
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "returns": []},
{ "method": "transaction", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "uncle", "params": { "numberOrHash": "", "i": 0}, "order": ["numberOrHash", "i"], "returns": []},
{ "method": "block", "params": { "numberOrHash": ""}, "order": ["numberOrHash"], "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": ""},

8
test/ethstubclient.h

@ -85,15 +85,15 @@ p["block"] = block;
}
std::string countAt(const std::string& a, const int& block) throw (jsonrpc::JsonRpcException)
double countAt(const std::string& a, const int& block) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["a"] = a;
p["block"] = block;
Json::Value result = this->client->CallMethod("countAt",p);
if (result.isString())
return result.asString();
if (result.isDouble())
return result.asDouble();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
@ -354,7 +354,7 @@ p["s"] = s;
}
std::string transact(const std::string& json) throw (jsonrpc::JsonRpcException)
std::string transact(const Json::Value& json) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["json"] = json;

49
test/jsonrpc.cpp

@ -8,6 +8,7 @@
#include <libdevcore/CommonJS.h>
#include <libwebthree/WebThree.h>
#include <libethrpc/EthStubServer.h>
#include <libethrpc/CorsHttpServer.h>
#include <jsonrpc/connectors/httpserver.h>
#include <jsonrpc/connectors/httpclient.h>
#include "JsonSpiritHeaders.h"
@ -39,7 +40,7 @@ struct JsonrpcFixture {
web3.setIdealPeerCount(5);
web3.ethereum()->setForceMining(true);
jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::HttpServer(8080), web3));
jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::CorsHttpServer(8080), web3));
jsonrpcServer->setKeys(keys);
jsonrpcServer->StartListening();
@ -78,6 +79,10 @@ BOOST_AUTO_TEST_CASE(jsonrpc_coinbase)
BOOST_AUTO_TEST_CASE(jsonrpc_countAt)
{
cnote << "Testing jsonrpc countAt...";
auto address = keys[0].address();
double countAt = jsonrpcClient->countAt(toJS(address), 0);
BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address, 0));
}
BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
@ -89,10 +94,20 @@ BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
BOOST_AUTO_TEST_CASE(jsonrpc_fromAscii)
{
cnote << "Testing jsonrpc fromAscii...";
string testString = "1234567890987654";
string fromAscii = jsonrpcClient->fromAscii(32, testString);
BOOST_CHECK_EQUAL(fromAscii, jsFromBinary(testString, 32));
}
BOOST_AUTO_TEST_CASE(jsonrpc_fromFixed)
{
cnote << "Testing jsonrpc fromFixed...";
string testString = "1234567890987654";
double fromFixed = jsonrpcClient->fromFixed(testString);
BOOST_CHECK_EQUAL(jsFromFixed(testString), fromFixed);
BOOST_CHECK_EQUAL(testString, jsToFixed(fromFixed));
}
BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice)
@ -106,6 +121,7 @@ BOOST_AUTO_TEST_CASE(jsonrpc_isListening)
{
//TODO
cnote << "Testing jsonrpc isListening...";
string testString = "1234567890987654";
}
BOOST_AUTO_TEST_CASE(jsonrpc_isMining)
@ -205,7 +221,10 @@ BOOST_AUTO_TEST_CASE(jsonrpc_sha3)
BOOST_AUTO_TEST_CASE(jsonrpc_stateAt)
{
cnote << "Testing jsonrpc stateAt...";
auto address = keys[0].address();
string stateAt = jsonrpcClient->stateAt(toJS(address), 0, "0");
BOOST_CHECK_EQUAL(toJS(web3.ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt);
}
BOOST_AUTO_TEST_CASE(jsonrpc_toAscii)
@ -236,10 +255,36 @@ BOOST_AUTO_TEST_CASE(jsonrpc_toFixed)
BOOST_AUTO_TEST_CASE(jsonrpc_transact)
{
cnote << "Testing jsonrpc transact...";
web3.ethereum()->setAddress(keys[0].address());
auto receiver = KeyPair::create();
dev::eth::mine(*(web3.ethereum()), 1);
auto balance = web3.ethereum()->balanceAt(keys[0].address(), 0);
BOOST_REQUIRE(balance > 0);
auto txAmount = balance / 2u;
auto gasPrice = 10 * dev::eth::szabo;
auto gas = dev::eth::c_txGas;
Json::Value t;
t["from"] = toJS(keys[0].secret());
t["value"] = toJS(txAmount);
t["to"] = toJS(receiver.address());
t["data"] = toJS(bytes());
t["gas"] = toJS(gas);
t["gasPrice"] = toJS(gasPrice);
jsonrpcClient->transact(t);
dev::eth::mine(*(web3.ethereum()), 1);
auto balance2 = web3.ethereum()->balanceAt(receiver.address());
BOOST_REQUIRE(balance2 > 0);
BOOST_CHECK_EQUAL(txAmount, balance2);
}
BOOST_AUTO_TEST_CASE(jsonrpc_transaction)
{
}
BOOST_AUTO_TEST_CASE(jsonrpc_uncle)

Loading…
Cancel
Save