Browse Source

js abi connection

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
79267006ce
  1. 2
      libethereum/Client.cpp
  2. 14
      libjsqrc/main.js
  3. 1
      libweb3jsonrpc/CMakeLists.txt
  4. 67
      libweb3jsonrpc/WebThreeStubServer.cpp
  5. 6
      libweb3jsonrpc/WebThreeStubServer.h
  6. 36
      libweb3jsonrpc/abstractwebthreestubserver.h
  7. 6
      libweb3jsonrpc/spec.json
  8. 70
      test/webthreestubclient.h

2
libethereum/Client.cpp

@ -341,7 +341,7 @@ bytes Client::call(Secret _secret, u256 _value, Address _dest, bytes const& _dat
n = temp.transactionsFrom(toAddress(_secret));
}
Transaction t(_value, _gasPrice, _gas, _dest, _data, n, _secret);
u256 gasUsed = temp.execute(t.data(), &out, false);
u256 gasUsed = temp.execute(t.rlp(), &out, false);
(void)gasUsed; // TODO: do something with gasused which it returns.
}
catch (...)

14
libjsqrc/main.js

@ -87,7 +87,11 @@
{ name: 'block', call: blockCall },
{ name: 'transaction', call: transactionCall },
{ name: 'uncle', call: uncleCall },
{ name: 'compile', call: 'compile' }
{ name: 'compilers', call: 'compilers' },
{ name: 'lll', call: 'lll' },
{ name: 'solidity', call: 'solidity' },
{ name: 'contractCreate', call: 'contractCreate' },
{ name: 'contractCall', call: 'contractCall' }
];
return methods;
};
@ -414,10 +418,8 @@
};
Filter.prototype.trigger = function(messages) {
if (!(messages instanceof Array) || messages.length) {
for(var i = 0; i < this.callbacks.length; i++) {
this.callbacks[i].call(this, messages);
}
for(var i = 0; i < this.callbacks.length; i++) {
this.callbacks[i].call(this, messages);
}
};
@ -446,7 +448,7 @@
if(data._id) {
var cb = web3._callbacks[data._id];
if (cb) {
cb.call(this, data.error, data.data);
cb.call(this, data.error, data.data)
delete web3._callbacks[data._id];
}
}

1
libweb3jsonrpc/CMakeLists.txt

@ -18,6 +18,7 @@ endif()
target_link_libraries(${EXECUTABLE} webthree)
target_link_libraries(${EXECUTABLE} secp256k1)
target_link_libraries(${EXECUTABLE} gmp)
target_link_libraries(${EXECUTABLE} solidity)
if(MINIUPNPC_LS)
target_link_libraries(${EXECUTABLE} ${MINIUPNPC_LS})
endif()

67
libweb3jsonrpc/WebThreeStubServer.cpp

@ -31,6 +31,8 @@
#include <libdevcrypto/FileSystem.h>
#include <libwhisper/Message.h>
#include <libwhisper/WhisperHost.h>
#include <libsolidity/CompilerStack.h>
#include <libsolidity/Scanner.h>
using namespace std;
using namespace dev;
@ -373,13 +375,13 @@ static TransactionSkeleton toTransaction(Json::Value const& _json)
ret.data = jsToBytes(_json["code"].asString());
else if (_json["data"].isArray())
for (auto i: _json["data"])
dev::operator +=(ret.data, jsToBytes(jsPadded(i.asString(), 32)));
dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32)));
else if (_json["code"].isArray())
for (auto i: _json["code"])
dev::operator +=(ret.data, jsToBytes(jsPadded(i.asString(), 32)));
dev::operator +=(ret.data, asBytes(jsPadded(i.asString(), 32)));
else if (_json["dataclose"].isArray())
for (auto i: _json["dataclose"])
dev::operator +=(ret.data, jsToBytes(i.asString()));
dev::operator +=(ret.data, asBytes(i.asString()));
}
return ret;
}
@ -516,9 +518,64 @@ std::string WebThreeStubServer::newIdentity()
return toJS(kp.pub());
}
std::string WebThreeStubServer::compile(string const& _s)
Json::Value WebThreeStubServer::compilers()
{
return toJS(dev::eth::compileLLL(_s));
Json::Value ret(Json::arrayValue);
ret.append("lll");
ret.append("solidity");
return ret;
}
static bytes paramsToBytes(Json::Value const& _params)
{
bytes data;
if (_params.isArray())
for (auto i: _params)
// data += asBytes(i.asString());
// data += toBigEndian(jsToU256(i.asString()));
data += asBytes(jsPadded(i.asString(), 33));
cwarn << data;
return data;
}
std::string WebThreeStubServer::contractCall(std::string const& _address, std::string const& _value, Json::Value const& _params)
{
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 = paramsToBytes(_params);
return toJS(client()->call(m_accounts[from].secret(), jsToU256(_value), jsToAddress(_address), paramsToBytes(_params), gas, gasPrice));
}
std::string WebThreeStubServer::contractCreate(std::string const& _bytecode, std::string const& _value)
{
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(), jsToU256(_value), jsToBytes(_bytecode), gas, gasPrice));
}
std::string WebThreeStubServer::lll(std::string const& _code)
{
return toJS(dev::eth::compileLLL(_code));
}
std::string WebThreeStubServer::solidity(std::string const& _code)
{
shared_ptr<dev::solidity::Scanner> scanner = make_shared<dev::solidity::Scanner>();
return toJS(dev::solidity::CompilerStack::compile(_code, scanner));
}
int WebThreeStubServer::number()

6
libweb3jsonrpc/WebThreeStubServer.h

@ -73,7 +73,9 @@ public:
virtual bool changed(int const& _id);
virtual std::string codeAt(std::string const& _address);
virtual std::string coinbase();
virtual std::string compile(std::string const& _s);
virtual Json::Value compilers();
virtual std::string contractCall(std::string const& _address, std::string const& _value, Json::Value const& _params);
virtual std::string contractCreate(std::string const& _bytecode, std::string const& _value);
virtual double countAt(std::string const& _address);
virtual int defaultBlock();
virtual std::string gasPrice();
@ -82,6 +84,7 @@ public:
virtual std::string getString(std::string const& _name, std::string const& _key);
virtual bool haveIdentity(std::string const& _id);
virtual bool listening();
virtual std::string lll(std::string const& _code);
virtual bool mining();
virtual int newFilter(Json::Value const& _json);
virtual int newFilterString(std::string const& _filter);
@ -99,6 +102,7 @@ public:
virtual Json::Value shhChanged(int const& _id);
virtual int shhNewFilter(Json::Value const& _json);
virtual bool shhUninstallFilter(int const& _id);
virtual std::string solidity(std::string const& _code);
virtual std::string stateAt(std::string const& _address, std::string const& _storage);
virtual std::string transact(Json::Value const& _json);
virtual Json::Value transactionByHash(std::string const& _hash, int const& _i);

36
libweb3jsonrpc/abstractwebthreestubserver.h

@ -22,7 +22,9 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(new jsonrpc::Procedure("changed", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::changedI);
this->bindAndAddMethod(new jsonrpc::Procedure("codeAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::codeAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("coinbase", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::coinbaseI);
this->bindAndAddMethod(new jsonrpc::Procedure("compile", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::compileI);
this->bindAndAddMethod(new jsonrpc::Procedure("compilers", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::compilersI);
this->bindAndAddMethod(new jsonrpc::Procedure("contractCall", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::contractCallI);
this->bindAndAddMethod(new jsonrpc::Procedure("contractCreate", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::contractCreateI);
this->bindAndAddMethod(new jsonrpc::Procedure("countAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_REAL, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::countAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("defaultBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::defaultBlockI);
this->bindAndAddMethod(new jsonrpc::Procedure("gasPrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::gasPriceI);
@ -31,6 +33,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(new jsonrpc::Procedure("getString", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::getStringI);
this->bindAndAddMethod(new jsonrpc::Procedure("haveIdentity", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::haveIdentityI);
this->bindAndAddMethod(new jsonrpc::Procedure("listening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::listeningI);
this->bindAndAddMethod(new jsonrpc::Procedure("lll", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::lllI);
this->bindAndAddMethod(new jsonrpc::Procedure("mining", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::miningI);
this->bindAndAddMethod(new jsonrpc::Procedure("newFilter", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::newFilterI);
this->bindAndAddMethod(new jsonrpc::Procedure("newFilterString", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::newFilterStringI);
@ -48,6 +51,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(new jsonrpc::Procedure("shhChanged", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::shhChangedI);
this->bindAndAddMethod(new jsonrpc::Procedure("shhNewFilter", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::shhNewFilterI);
this->bindAndAddMethod(new jsonrpc::Procedure("shhUninstallFilter", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::shhUninstallFilterI);
this->bindAndAddMethod(new jsonrpc::Procedure("solidity", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::solidityI);
this->bindAndAddMethod(new jsonrpc::Procedure("stateAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::stateAtI);
this->bindAndAddMethod(new jsonrpc::Procedure("transact", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::transactI);
this->bindAndAddMethod(new jsonrpc::Procedure("transactionByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::transactionByHashI);
@ -103,9 +107,19 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->coinbase();
}
inline virtual void compileI(const Json::Value& request, Json::Value& response)
inline virtual void compilersI(const Json::Value& request, Json::Value& response)
{
response = this->compile(request[0u].asString());
response = this->compilers();
}
inline virtual void contractCallI(const Json::Value& request, Json::Value& response)
{
response = this->contractCall(request[0u].asString(), request[1u].asString(), request[2u]);
}
inline virtual void contractCreateI(const Json::Value& request, Json::Value& response)
{
response = this->contractCreate(request[0u].asString(), request[1u].asString());
}
inline virtual void countAtI(const Json::Value& request, Json::Value& response)
@ -148,6 +162,11 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->listening();
}
inline virtual void lllI(const Json::Value& request, Json::Value& response)
{
response = this->lll(request[0u].asString());
}
inline virtual void miningI(const Json::Value& request, Json::Value& response)
{
response = this->mining();
@ -233,6 +252,11 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->shhUninstallFilter(request[0u].asInt());
}
inline virtual void solidityI(const Json::Value& request, Json::Value& response)
{
response = this->solidity(request[0u].asString());
}
inline virtual void stateAtI(const Json::Value& request, Json::Value& response)
{
response = this->stateAt(request[0u].asString(), request[1u].asString());
@ -278,7 +302,9 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual bool changed(const int& param1) = 0;
virtual std::string codeAt(const std::string& param1) = 0;
virtual std::string coinbase() = 0;
virtual std::string compile(const std::string& param1) = 0;
virtual Json::Value compilers() = 0;
virtual std::string contractCall(const std::string& param1, const std::string& param2, const Json::Value& param3) = 0;
virtual std::string contractCreate(const std::string& param1, const std::string& param2) = 0;
virtual double countAt(const std::string& param1) = 0;
virtual int defaultBlock() = 0;
virtual std::string gasPrice() = 0;
@ -287,6 +313,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual std::string getString(const std::string& param1, const std::string& param2) = 0;
virtual bool haveIdentity(const std::string& param1) = 0;
virtual bool listening() = 0;
virtual std::string lll(const std::string& param1) = 0;
virtual bool mining() = 0;
virtual int newFilter(const Json::Value& param1) = 0;
virtual int newFilterString(const std::string& param1) = 0;
@ -304,6 +331,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual Json::Value shhChanged(const int& param1) = 0;
virtual int shhNewFilter(const Json::Value& param1) = 0;
virtual bool shhUninstallFilter(const int& param1) = 0;
virtual std::string solidity(const std::string& param1) = 0;
virtual std::string stateAt(const std::string& param1, const std::string& param2) = 0;
virtual std::string transact(const Json::Value& param1) = 0;
virtual Json::Value transactionByHash(const std::string& param1, const int& param2) = 0;

6
libweb3jsonrpc/spec.json

@ -27,7 +27,11 @@
{ "method": "uncleByHash", "params": ["", 0], "order": [], "returns": {}},
{ "method": "uncleByNumber", "params": [0, 0], "order": [], "returns": {}},
{ "method": "compile", "params": [""], "order": [], "returns": ""},
{ "method": "compilers", "params": [], "order": [], "returns": []},
{ "method": "lll", "params": [""], "order": [], "returns": ""},
{ "method": "solidity", "params": [""], "order": [], "returns": ""},
{ "method": "contractCreate", "params": ["", ""], "order": [], "returns": ""},
{ "method": "contractCall", "params": ["", "", []], "order": [], "returns": ""},
{ "method": "newFilter", "params": [{}], "order": [], "returns": 0},
{ "method": "newFilterString", "params": [""], "order": [], "returns": 0},

70
test/webthreestubclient.h

@ -19,18 +19,6 @@ class WebThreeStubClient
delete this->client;
}
std::string account() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->client->CallMethod("account",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value accounts() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
@ -147,12 +135,40 @@ p.append(param2);
}
std::string compile(const std::string& param1) throw (jsonrpc::JsonRpcException)
Json::Value compilers() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->client->CallMethod("compilers",p);
if (result.isArray())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string contractCall(const std::string& param1, const std::string& param2, const Json::Value& param3) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
p.append(param2);
p.append(param3);
Json::Value result = this->client->CallMethod("contractCall",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string contractCreate(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("compile",p);
Json::Value result = this->client->CallMethod("contractCreate",p);
if (result.isString())
return result.asString();
else
@ -263,6 +279,19 @@ p.append(param2);
}
std::string lll(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->client->CallMethod("lll",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool mining() throw (jsonrpc::JsonRpcException)
{
Json::Value p;
@ -485,6 +514,19 @@ p.append(param3);
}
std::string solidity(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->client->CallMethod("solidity",p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
std::string stateAt(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;

Loading…
Cancel
Save