From 7bdc646dccdba745726382c57feaa0f37e4c39ff Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 13 Nov 2014 12:25:49 +0100 Subject: [PATCH] removed unnecessary methods from jsonrpc and added contract call tests --- libjsqrc/ethereum.min.js | 2 +- libweb3jsonrpc/WebThreeStubServer.cpp | 60 ++------------------- libweb3jsonrpc/WebThreeStubServer.h | 2 - libweb3jsonrpc/abstractwebthreestubserver.h | 14 ----- libweb3jsonrpc/spec.json | 2 - test/jsonrpc.cpp | 28 ++++++++++ test/webthreestubclient.h | 27 ---------- 7 files changed, 34 insertions(+), 101 deletions(-) diff --git a/libjsqrc/ethereum.min.js b/libjsqrc/ethereum.min.js index 1befc1804..5b1ff1c6f 100644 --- a/libjsqrc/ethereum.min.js +++ b/libjsqrc/ethereum.min.js @@ -1 +1 @@ -require=function t(e,n,r){function i(a,s){if(!n[a]){if(!e[a]){var c="function"==typeof require&&require;if(!s&&c)return c(a,!0);if(o)return o(a,!0);var l=new Error("Cannot find module '"+a+"'");throw l.code="MODULE_NOT_FOUND",l}var u=n[a]={exports:{}};e[a][0].call(u.exports,function(t){var n=e[a][1][t];return i(n?n:t)},u,u.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;ar&&(e=t.charCodeAt(r),0!==e);r+=2)n+=String.fromCharCode(parseInt(t.substr(r,2),16));return n},toDecimal:function(t){return parseInt(t,16)},fromAscii:function(t,e){e=void 0===e?32:e;for(var n=this.toHex(t);n.length<2*e;)n+="00";return"0x"+n},eth:{prototype:Object(),watch:function(t){return new s(t,i)}},db:{prototype:Object()},shh:{prototype:Object(),watch:function(t){return new s(t,o)}},on:function(t,e,n){return void 0===g._events[t]&&(g._events[t]={}),g._events[t][e]=n,this},off:function(t,e){return void 0!==g._events[t]&&delete g._events[t][e],this},trigger:function(t,e,n){var r,i=g._events[t];i&&i[e]&&(r=i[e])(n)}},m=g.eth;p(m,c()),v(m,l()),p(g.db,u()),p(g.shh,h()),i={changed:"eth_changed"},p(i,f()),o={changed:"shh_changed"},p(o,d()),a=function(){var t,e;this.queued=[],this.polls=[],this.ready=!1,this.provider=void 0,this.id=1,t=this,(e=function(){t.provider&&t.provider.poll&&t.polls.forEach(function(e){e.data._id=t.id,t.id++,t.provider.poll(e.data,e.id)}),setTimeout(e,12e3)})()},a.prototype.send=function(t,e){t._id=this.id,e&&(g._callbacks[t._id]=e),t.args=t.args||[],this.id++,void 0!==this.provider?this.provider.send(t):(console.warn("provider is not set"),this.queued.push(t))},a.prototype.set=function(t){void 0!==this.provider&&void 0!==this.provider.unload&&this.provider.unload(),this.provider=t,this.ready=!0},a.prototype.sendQueued=function(){for(var t=0;this.queued.length;t++)this.send(this.queued[t])},a.prototype.installed=function(){return void 0!==this.provider},a.prototype.startPolling=function(t,e){this.provider&&this.provider.poll&&this.polls.push({data:t,id:e})},a.prototype.stopPolling=function(t){var e,n;for(e=this.polls.length;e--;)n=this.polls[e],n.id===t&&this.polls.splice(e,1)},g.provider=new a,g.setProvider=function(t){t.onmessage=r,g.provider.set(t),g.provider.sendQueued()},g.haveProvider=function(){return!!g.provider.provider},s=function(t,e){this.impl=e,this.callbacks=[];var n=this;this.promise=e.newFilter(t),this.promise.then(function(t){n.id=t,g.on(e.changed,t,n.trigger.bind(n)),g.provider.startPolling({call:e.changed,args:[t]},t)})},s.prototype.arrived=function(t){this.changed(t)},s.prototype.changed=function(t){var e=this;this.promise.then(function(){e.callbacks.push(t)})},s.prototype.trigger=function(t){for(var e=0;er&&(t=e.charCodeAt(r),0!==t);r+=2)n+=String.fromCharCode(parseInt(e.substr(r,2),16));return n},toDecimal:function(e){return parseInt(e,16)},fromAscii:function(e,t){t=void 0===t?32:t;for(var n=this.toHex(e);n.length<2*t;)n+="00";return"0x"+n},eth:{prototype:Object(),watch:function(e){return new a(e,o)}},db:{prototype:Object()},shh:{prototype:Object(),watch:function(e){return new a(e,i)}},on:function(e,t,n){return void 0===g._events[e]&&(g._events[e]={}),g._events[e][t]=n,this},off:function(e,t){return void 0!==g._events[e]&&delete g._events[e][t],this},trigger:function(e,t,n){var r,o=g._events[e];o&&o[t]&&(r=o[t])(n)}},m=g.eth;f(m,c()),v(m,u()),f(g.db,l()),f(g.shh,h()),o={changed:"eth_changed"},f(o,d()),i={changed:"shh_changed"},f(i,p()),s=function(){var e,t;this.queued=[],this.polls=[],this.ready=!1,this.provider=void 0,this.id=1,e=this,(t=function(){e.provider&&e.provider.poll&&e.polls.forEach(function(t){t.data._id=e.id,e.id++,e.provider.poll(t.data,t.id)}),setTimeout(t,12e3)})()},s.prototype.send=function(e,t){e._id=this.id,t&&(g._callbacks[e._id]=t),e.args=e.args||[],this.id++,void 0!==this.provider?this.provider.send(e):(console.warn("provider is not set"),this.queued.push(e))},s.prototype.set=function(e){void 0!==this.provider&&void 0!==this.provider.unload&&this.provider.unload(),this.provider=e,this.ready=!0},s.prototype.sendQueued=function(){for(var e=0;this.queued.length;e++)this.send(this.queued[e])},s.prototype.installed=function(){return void 0!==this.provider},s.prototype.startPolling=function(e,t){this.provider&&this.provider.poll&&this.polls.push({data:e,id:t})},s.prototype.stopPolling=function(e){var t,n;for(t=this.polls.length;t--;)n=this.polls[t],n.id===e&&this.polls.splice(t,1)},g.provider=new s,g.setProvider=function(e){e.onmessage=r,g.provider.set(e),g.provider.sendQueued()},g.haveProvider=function(){return!!g.provider.provider},a=function(e,t){this.impl=t,this.callbacks=[];var n=this;this.promise=t.newFilter(e),this.promise.then(function(e){n.id=e,g.on(t.changed,e,n.trigger.bind(n)),g.provider.startPolling({call:t.changed,args:[e]},e)})},a.prototype.arrived=function(e){this.changed(e)},a.prototype.changed=function(e){var t=this;this.promise.then(function(){t.callbacks.push(e)})},a.prototype.trigger=function(e){for(var t=0;tfirst; - 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(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(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)); diff --git a/libweb3jsonrpc/WebThreeStubServer.h b/libweb3jsonrpc/WebThreeStubServer.h index bb4f258f6..e358e110c 100644 --- a/libweb3jsonrpc/WebThreeStubServer.h +++ b/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(); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index 0ae4193fa..dba6e02cd 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -26,8 +26,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(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::AbstractServereth_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::AbstractServerethereum()->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() diff --git a/test/webthreestubclient.h b/test/webthreestubclient.h index 6d844de8e..66c4bef35 100644 --- a/test/webthreestubclient.h +++ b/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;