Browse Source

ldb access from api

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
b5b3eb32df
  1. 1
      libweb3jsonrpc/CMakeLists.txt
  2. 40
      libweb3jsonrpc/WebThreeStubServer.cpp
  3. 15
      libweb3jsonrpc/WebThreeStubServer.h
  4. 28
      libweb3jsonrpc/abstractwebthreestubserver.h
  5. 8
      libweb3jsonrpc/spec.json
  6. 58
      test/webthreestubclient.h

1
libweb3jsonrpc/CMakeLists.txt

@ -24,6 +24,7 @@ endif()
target_link_libraries(${EXECUTABLE} ${LEVELDB_LS}) target_link_libraries(${EXECUTABLE} ${LEVELDB_LS})
target_link_libraries(${EXECUTABLE} ${CRYPTOPP_LS}) target_link_libraries(${EXECUTABLE} ${CRYPTOPP_LS})
target_link_libraries(${EXECUTABLE} ${JSONRPC_LS}) target_link_libraries(${EXECUTABLE} ${JSONRPC_LS})
target_link_libraries(${EXECUTABLE} ${LEVELDB_LS})
if(READLINE_LS) if(READLINE_LS)
target_link_libraries(${EXECUTABLE} ${READLINE_LS}) target_link_libraries(${EXECUTABLE} ${READLINE_LS})
endif() endif()

40
libweb3jsonrpc/WebThreeStubServer.cpp

@ -27,6 +27,8 @@
#include <libethereum/Client.h> #include <libethereum/Client.h>
#include <libwebthree/WebThree.h> #include <libwebthree/WebThree.h>
#include <libdevcore/CommonJS.h> #include <libdevcore/CommonJS.h>
#include <boost/filesystem.hpp>
#include <libdevcrypto/FileSystem.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
@ -148,6 +150,12 @@ WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn,
m_web3(_web3) m_web3(_web3)
{ {
setAccounts(_accounts); 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<dev::KeyPair> const& _accounts) void WebThreeStubServer::setAccounts(std::vector<dev::KeyPair> const& _accounts)
@ -288,6 +296,14 @@ std::string WebThreeStubServer::gasPrice()
return toJS(10 * dev::eth::szabo); 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) Json::Value WebThreeStubServer::getMessages(int const& _id)
{ {
if (!client()) if (!client())
@ -295,6 +311,14 @@ Json::Value WebThreeStubServer::getMessages(int const& _id)
return toJson(client()->messages(_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() bool WebThreeStubServer::listening()
{ {
return m_web3.isNetworkStarted(); return m_web3.isNetworkStarted();
@ -341,6 +365,22 @@ int WebThreeStubServer::peerCount()
return m_web3.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) bool WebThreeStubServer::setCoinbase(std::string const& _address)
{ {
if (!client()) if (!client())

15
libweb3jsonrpc/WebThreeStubServer.h

@ -23,6 +23,11 @@
#pragma once #pragma once
#pragma warning(push)
#pragma warning(disable: 4100 4267)
#include <leveldb/db.h>
#pragma warning(pop)
#include <iostream> #include <iostream>
#include <jsonrpc/rpc.h> #include <jsonrpc/rpc.h>
#include <libdevcrypto/Common.h> #include <libdevcrypto/Common.h>
@ -31,6 +36,8 @@
#include "abstractwebthreestubserver.h" #include "abstractwebthreestubserver.h"
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
namespace ldb = leveldb;
namespace dev { class WebThreeDirect; namespace eth { class Interface; } class KeyPair; } namespace dev { class WebThreeDirect; namespace eth { class Interface; } class KeyPair; }
class WebThreeStubServer: public AbstractWebThreeStubServer class WebThreeStubServer: public AbstractWebThreeStubServer
@ -50,13 +57,17 @@ public:
virtual double countAt(std::string const& _address); virtual double countAt(std::string const& _address);
virtual int defaultBlock(); virtual int defaultBlock();
virtual std::string gasPrice(); virtual std::string gasPrice();
virtual std::string get(std::string const& _name, std::string const& _key);
virtual Json::Value getMessages(int const& _id); virtual Json::Value getMessages(int const& _id);
virtual std::string getString(std::string const& _name, std::string const& _key);
virtual bool listening(); virtual bool listening();
virtual bool mining(); virtual bool mining();
virtual int newFilter(Json::Value const& _json); virtual int newFilter(Json::Value const& _json);
virtual int newFilterString(std::string const& _filter); virtual int newFilterString(std::string const& _filter);
virtual int number(); virtual int number();
virtual int peerCount(); 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 setCoinbase(std::string const& _address);
virtual bool setDefaultBlock(int const& _block); virtual bool setDefaultBlock(int const& _block);
virtual bool setListening(bool const& _listening); virtual bool setListening(bool const& _listening);
@ -74,4 +85,8 @@ private:
dev::eth::Interface* client() const; dev::eth::Interface* client() const;
dev::WebThreeDirect& m_web3; dev::WebThreeDirect& m_web3;
std::map<dev::Address, dev::KeyPair> m_accounts; std::map<dev::Address, dev::KeyPair> m_accounts;
ldb::ReadOptions m_readOptions;
ldb::WriteOptions m_writeOptions;
ldb::DB* m_db;
}; };

28
libweb3jsonrpc/abstractwebthreestubserver.h

@ -25,13 +25,17 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
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("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("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("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("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("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("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("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("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("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("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("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("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); 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::AbstractServer<AbstractWebThr
response = this->gasPrice(); response = this->gasPrice();
} }
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) inline virtual void getMessagesI(const Json::Value& request, Json::Value& response)
{ {
response = this->getMessages(request[0u].asInt()); 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) inline virtual void listeningI(const Json::Value& request, Json::Value& response)
{ {
response = this->listening(); response = this->listening();
@ -141,6 +155,16 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
response = this->peerCount(); response = this->peerCount();
} }
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) inline virtual void setCoinbaseI(const Json::Value& request, Json::Value& response)
{ {
response = this->setCoinbase(request[0u].asString()); response = this->setCoinbase(request[0u].asString());
@ -209,13 +233,17 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual double countAt(const std::string& param1) = 0; virtual double countAt(const std::string& param1) = 0;
virtual int defaultBlock() = 0; virtual int defaultBlock() = 0;
virtual std::string gasPrice() = 0; virtual std::string gasPrice() = 0;
virtual std::string get(const std::string& param1, const std::string& param2) = 0;
virtual Json::Value getMessages(const int& param1) = 0; virtual Json::Value getMessages(const int& param1) = 0;
virtual std::string getString(const std::string& param1, const std::string& param2) = 0;
virtual bool listening() = 0; virtual bool listening() = 0;
virtual bool mining() = 0; virtual bool mining() = 0;
virtual int newFilter(const Json::Value& param1) = 0; virtual int newFilter(const Json::Value& param1) = 0;
virtual int newFilterString(const std::string& param1) = 0; virtual int newFilterString(const std::string& param1) = 0;
virtual int number() = 0; virtual int number() = 0;
virtual int peerCount() = 0; virtual int peerCount() = 0;
virtual bool put(const std::string& param1, const std::string& param2, const std::string& param3) = 0;
virtual bool putString(const std::string& param1, const std::string& param2, const std::string& param3) = 0;
virtual bool setCoinbase(const std::string& param1) = 0; virtual bool setCoinbase(const std::string& param1) = 0;
virtual bool setDefaultBlock(const int& param1) = 0; virtual bool setDefaultBlock(const int& param1) = 0;
virtual bool setListening(const bool& param1) = 0; virtual bool setListening(const bool& param1) = 0;

8
libweb3jsonrpc/spec.json

@ -33,7 +33,13 @@
{ "method": "newFilterString", "params": [""], "order": [], "returns": 0}, { "method": "newFilterString", "params": [""], "order": [], "returns": 0},
{ "method": "uninstallFilter", "params": [0], "order": [], "returns": true}, { "method": "uninstallFilter", "params": [0], "order": [], "returns": true},
{ "method": "changed", "params": [0], "order": [], "returns": true}, { "method": "changed", "params": [0], "order": [], "returns": true},
{ "method": "getMessages", "params": [0], "order": [], "returns": []} { "method": "getMessages", "params": [0], "order": [], "returns": []},
{ "method": "put", "params": ["", "", ""], "order": [], "returns": true},
{ "method": "get", "params": ["", ""], "order": [], "returns": ""},
{ "method": "putString", "params": ["", "", ""], "order": [], "returns": true},
{ "method": "getString", "params": ["", ""], "order": [], "returns": ""}
] ]

58
test/webthreestubclient.h

@ -171,6 +171,20 @@ class WebThreeStubClient
} }
std::string get(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("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 getMessages(const int& param1) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; 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) bool listening() throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; 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) bool setCoinbase(const std::string& param1) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;

Loading…
Cancel
Save