Browse Source

removed unnecessary methods from jsonrpc and added contract call tests

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
7bdc646dcc
  1. 2
      libjsqrc/ethereum.min.js
  2. 60
      libweb3jsonrpc/WebThreeStubServer.cpp
  3. 2
      libweb3jsonrpc/WebThreeStubServer.h
  4. 14
      libweb3jsonrpc/abstractwebthreestubserver.h
  5. 2
      libweb3jsonrpc/spec.json
  6. 28
      test/jsonrpc.cpp
  7. 27
      test/webthreestubclient.h

2
libjsqrc/ethereum.min.js

File diff suppressed because one or more lines are too long

60
libweb3jsonrpc/WebThreeStubServer.cpp

@ -369,22 +369,11 @@ static TransactionSkeleton toTransaction(Json::Value const& _json)
if (!_json["gasPrice"].empty())
ret.gasPrice = jsToU256(_json["gasPrice"].asString());
if (!_json["data"].empty() || _json["code"].empty() || _json["dataclose"].empty())
{
if (_json["data"].isString())
ret.data = jsToBytes(_json["data"].asString());
else if (_json["code"].isString())
ret.data = jsToBytes(_json["code"].asString());
else if (_json["data"].isArray())
for (auto i: _json["data"])
dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32)));
else if (_json["code"].isArray())
for (auto i: _json["code"])
dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32)));
else if (_json["dataclose"].isArray())
for (auto i: _json["dataclose"])
dev::operator +=(ret.data, asBytes(i.asString()));
}
if (!_json["data"].empty() && _json["data"].isString())
ret.data = jsToBytes(_json["data"].asString());
else if (!_json["code"].empty() && _json["code"].isString())
ret.data = jsToBytes(_json["code"].asString());
return ret;
}
@ -528,45 +517,6 @@ Json::Value WebThreeStubServer::eth_compilers()
return ret;
}
static bytes toMethodCall(int const& _index, Json::Value const& _params)
{
bytes data(1, _index);
if (_params.isArray())
for (auto i: _params)
data += asBytes(jsPadded(i.asString(), 32));
cwarn << data;
return data;
}
std::string WebThreeStubServer::eth_contractCall(std::string const& _address, std::string const& _bytes)
{
auto from = m_accounts.begin()->first;
for (auto a: m_accounts)
if (client()->balanceAt(a.first) > client()->balanceAt(from))
from = a.first;
cwarn << "Silently signing transaction from address" << from.abridged() << ": User validation hook goes here.";
auto gasPrice = 10 * dev::eth::szabo;
auto gas = min<u256>(client()->gasLimitRemaining(), client()->balanceAt(from) / gasPrice);
auto bytes = jsToBytes(_bytes);
return toJS(client()->call(m_accounts[from].secret(), 0, jsToAddress(_address), bytes, gas, gasPrice));
}
std::string WebThreeStubServer::eth_contractCreate(std::string const& _bytecode)
{
auto from = m_accounts.begin()->first;
for (auto a: m_accounts)
if (client()->balanceAt(a.first) > client()->balanceAt(from))
from = a.first;
cwarn << "Silently signing transaction from address" << from.abridged() << ": User validation hook goes here.";
auto gasPrice = 10 * dev::eth::szabo;
auto gas = min<u256>(client()->gasLimitRemaining(), client()->balanceAt(from) / gasPrice);
return toJS(client()->transact(m_accounts[from].secret(), 0, jsToBytes(_bytecode), gas, gasPrice));
}
std::string WebThreeStubServer::eth_lll(std::string const& _code)
{
return toJS(dev::eth::compileLLL(_code));

2
libweb3jsonrpc/WebThreeStubServer.h

@ -73,8 +73,6 @@ public:
virtual std::string eth_codeAt(std::string const& _address);
virtual std::string eth_coinbase();
virtual Json::Value eth_compilers();
virtual std::string eth_contractCall(std::string const& _address, std::string const& _bytes);
virtual std::string eth_contractCreate(std::string const& _bytecode);
virtual double eth_countAt(std::string const& _address);
virtual int eth_defaultBlock();
virtual std::string eth_gasPrice();

14
libweb3jsonrpc/abstractwebthreestubserver.h

@ -26,8 +26,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(new jsonrpc::Procedure("eth_codeAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_codeAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_coinbase", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_coinbaseI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_compilers", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::eth_compilersI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_contractCall", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_contractCallI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_contractCreate", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_contractCreateI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_countAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_countAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_defaultBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::eth_defaultBlockI);
this->bindAndAddMethod(new jsonrpc::Procedure("eth_gasPrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::eth_gasPriceI);
@ -127,16 +125,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->eth_compilers();
}
inline virtual void eth_contractCallI(const Json::Value& request, Json::Value& response)
{
response = this->eth_contractCall(request[0u].asString(), request[1u].asString());
}
inline virtual void eth_contractCreateI(const Json::Value& request, Json::Value& response)
{
response = this->eth_contractCreate(request[0u].asString());
}
inline virtual void eth_countAtI(const Json::Value& request, Json::Value& response)
{
response = this->eth_countAt(request[0u].asString());
@ -306,8 +294,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual std::string eth_codeAt(const std::string& param1) = 0;
virtual std::string eth_coinbase() = 0;
virtual Json::Value eth_compilers() = 0;
virtual std::string eth_contractCall(const std::string& param1, const std::string& param2) = 0;
virtual std::string eth_contractCreate(const std::string& param1) = 0;
virtual double eth_countAt(const std::string& param1) = 0;
virtual int eth_defaultBlock() = 0;
virtual std::string eth_gasPrice() = 0;

2
libweb3jsonrpc/spec.json

@ -30,8 +30,6 @@
{ "method": "eth_compilers", "params": [], "order": [], "returns": []},
{ "method": "eth_lll", "params": [""], "order": [], "returns": ""},
{ "method": "eth_solidity", "params": [""], "order": [], "returns": ""},
{ "method": "eth_contractCreate", "params": [""], "order": [], "returns": ""},
{ "method": "eth_contractCall", "params": ["", ""], "order": [], "returns": ""},
{ "method": "eth_newFilter", "params": [{}], "order": [], "returns": 0},
{ "method": "eth_newFilterString", "params": [""], "order": [], "returns": 0},

28
test/jsonrpc.cpp

@ -237,7 +237,35 @@ BOOST_AUTO_TEST_CASE(jsonrpc_transact)
BOOST_CHECK_EQUAL(jsToDecimal(balanceString2), "750000000000000000");
BOOST_CHECK_EQUAL(txAmount, balance2);
}
BOOST_AUTO_TEST_CASE(simple_contract)
{
cnote << "Testing jsonrpc contract...";
KeyPair kp = KeyPair::create();
web3->ethereum()->setAddress(kp.address());
jsonrpcServer->setAccounts({kp});
dev::eth::mine(*(web3->ethereum()), 1);
char const* sourceCode = "contract test {\n"
" function f(uint a) returns(uint d) { return a * 7; }\n"
"}\n";
string compiled = jsonrpcClient->eth_solidity(sourceCode);
Json::Value create;
create["code"] = compiled;
string contractAddress = jsonrpcClient->eth_transact(create);
dev::eth::mine(*(web3->ethereum()), 1);
Json::Value call;
call["to"] = contractAddress;
call["data"] = "0x00000000000000000000000000000000000000000000000000000000000000001";
string result = jsonrpcClient->eth_call(call);
BOOST_CHECK_EQUAL(result, "0x0000000000000000000000000000000000000000000000000000000000000007");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

27
test/webthreestubclient.h

@ -191,33 +191,6 @@ p.append(param3);
}
std::string eth_contractCall(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
p.append(param2);
Json::Value result = this->client->CallMethod("eth_contractCall",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string eth_contractCreate(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->client->CallMethod("eth_contractCreate",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
double eth_countAt(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;

Loading…
Cancel
Save