diff --git a/libweb3jsonrpc/CMakeLists.txt b/libweb3jsonrpc/CMakeLists.txt index b35ba7627..b24a11196 100644 --- a/libweb3jsonrpc/CMakeLists.txt +++ b/libweb3jsonrpc/CMakeLists.txt @@ -24,6 +24,7 @@ endif() target_link_libraries(${EXECUTABLE} ${LEVELDB_LS}) target_link_libraries(${EXECUTABLE} ${CRYPTOPP_LS}) target_link_libraries(${EXECUTABLE} ${JSONRPC_LS}) +target_link_libraries(${EXECUTABLE} ${LEVELDB_LS}) if(READLINE_LS) target_link_libraries(${EXECUTABLE} ${READLINE_LS}) endif() diff --git a/libweb3jsonrpc/WebThreeStubServer.cpp b/libweb3jsonrpc/WebThreeStubServer.cpp index 30cdf61d5..ba7867f4d 100644 --- a/libweb3jsonrpc/WebThreeStubServer.cpp +++ b/libweb3jsonrpc/WebThreeStubServer.cpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include using namespace std; using namespace dev; @@ -148,6 +150,12 @@ WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn, m_web3(_web3) { setAccounts(_accounts); + + auto path = getDataDir() + "/.web3"; + boost::filesystem::create_directories(path); + ldb::Options o; + o.create_if_missing = true; + ldb::DB::Open(o, path, &m_db); } void WebThreeStubServer::setAccounts(std::vector const& _accounts) @@ -288,6 +296,14 @@ std::string WebThreeStubServer::gasPrice() return toJS(10 * dev::eth::szabo); } +std::string WebThreeStubServer::get(std::string const& _name, std::string const& _key) +{ + bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); + string ret; + m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret); + return toJS(dev::asBytes(ret)); +} + Json::Value WebThreeStubServer::getMessages(int const& _id) { if (!client()) @@ -295,6 +311,14 @@ Json::Value WebThreeStubServer::getMessages(int const& _id) return toJson(client()->messages(_id)); } +std::string WebThreeStubServer::getString(std::string const& _name, std::string const& _key) +{ + bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); + string ret; + m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret); + return ret; +} + bool WebThreeStubServer::listening() { return m_web3.isNetworkStarted(); @@ -341,6 +365,22 @@ int WebThreeStubServer::peerCount() return m_web3.peerCount(); } +bool WebThreeStubServer::put(std::string const& _name, std::string const& _key, std::string const& _value) +{ + bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); + bytes v = jsToBytes(_value); + m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)v.data(), v.size())); + return true; +} + +bool WebThreeStubServer::putString(std::string const& _name, std::string const& _key, std::string const& _value) +{ + bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); + string v = _value; + m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)v.data(), v.size())); + return true; +} + bool WebThreeStubServer::setCoinbase(std::string const& _address) { if (!client()) diff --git a/libweb3jsonrpc/WebThreeStubServer.h b/libweb3jsonrpc/WebThreeStubServer.h index bb6403afe..3dc568933 100644 --- a/libweb3jsonrpc/WebThreeStubServer.h +++ b/libweb3jsonrpc/WebThreeStubServer.h @@ -23,6 +23,11 @@ #pragma once +#pragma warning(push) +#pragma warning(disable: 4100 4267) +#include +#pragma warning(pop) + #include #include #include @@ -31,6 +36,8 @@ #include "abstractwebthreestubserver.h" #pragma GCC diagnostic pop +namespace ldb = leveldb; + namespace dev { class WebThreeDirect; namespace eth { class Interface; } class KeyPair; } class WebThreeStubServer: public AbstractWebThreeStubServer @@ -50,13 +57,17 @@ public: virtual double countAt(std::string const& _address); virtual int defaultBlock(); virtual std::string gasPrice(); + virtual std::string get(std::string const& _name, std::string const& _key); virtual Json::Value getMessages(int const& _id); + virtual std::string getString(std::string const& _name, std::string const& _key); virtual bool listening(); virtual bool mining(); virtual int newFilter(Json::Value const& _json); virtual int newFilterString(std::string const& _filter); virtual int number(); virtual int peerCount(); + virtual bool put(std::string const& _name, std::string const& _key, std::string const& _value); + virtual bool putString(std::string const& _name, std::string const& _key, std::string const& _value); virtual bool setCoinbase(std::string const& _address); virtual bool setDefaultBlock(int const& _block); virtual bool setListening(bool const& _listening); @@ -74,4 +85,8 @@ private: dev::eth::Interface* client() const; dev::WebThreeDirect& m_web3; std::map m_accounts; + + ldb::ReadOptions m_readOptions; + ldb::WriteOptions m_writeOptions; + ldb::DB* m_db; }; diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index 226c585d5..0b16057f8 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -25,13 +25,17 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(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); + this->bindAndAddMethod(new jsonrpc::Procedure("get", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::getI); this->bindAndAddMethod(new jsonrpc::Procedure("getMessages", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::getMessagesI); + 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("listening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::listeningI); 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); this->bindAndAddMethod(new jsonrpc::Procedure("number", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::numberI); this->bindAndAddMethod(new jsonrpc::Procedure("peerCount", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::peerCountI); + this->bindAndAddMethod(new jsonrpc::Procedure("put", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::putI); + this->bindAndAddMethod(new jsonrpc::Procedure("putString", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::putStringI); this->bindAndAddMethod(new jsonrpc::Procedure("setCoinbase", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::setCoinbaseI); this->bindAndAddMethod(new jsonrpc::Procedure("setDefaultBlock", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::setDefaultBlockI); this->bindAndAddMethod(new jsonrpc::Procedure("setListening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::setListeningI); @@ -106,11 +110,21 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServergasPrice(); } + inline virtual void getI(const Json::Value& request, Json::Value& response) + { + response = this->get(request[0u].asString(), request[1u].asString()); + } + inline virtual void getMessagesI(const Json::Value& request, Json::Value& response) { response = this->getMessages(request[0u].asInt()); } + inline virtual void getStringI(const Json::Value& request, Json::Value& response) + { + response = this->getString(request[0u].asString(), request[1u].asString()); + } + inline virtual void listeningI(const Json::Value& request, Json::Value& response) { response = this->listening(); @@ -141,6 +155,16 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerpeerCount(); } + inline virtual void putI(const Json::Value& request, Json::Value& response) + { + response = this->put(request[0u].asString(), request[1u].asString(), request[2u].asString()); + } + + inline virtual void putStringI(const Json::Value& request, Json::Value& response) + { + response = this->putString(request[0u].asString(), request[1u].asString(), request[2u].asString()); + } + inline virtual void setCoinbaseI(const Json::Value& request, Json::Value& response) { response = this->setCoinbase(request[0u].asString()); @@ -209,13 +233,17 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerclient->CallMethod("get",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + Json::Value getMessages(const int& param1) throw (jsonrpc::JsonRpcException) { Json::Value p; @@ -184,6 +198,20 @@ class WebThreeStubClient } + std::string getString(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("getString",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + bool listening() throw (jsonrpc::JsonRpcException) { Json::Value p; @@ -258,6 +286,36 @@ class WebThreeStubClient } + bool put(const std::string& param1, const std::string& param2, const std::string& param3) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); +p.append(param3); + + Json::Value result = this->client->CallMethod("put",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool putString(const std::string& param1, const std::string& param2, const std::string& param3) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); +p.append(param3); + + Json::Value result = this->client->CallMethod("putString",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + bool setCoinbase(const std::string& param1) throw (jsonrpc::JsonRpcException) { Json::Value p;