Unverified Commit 908d0711 authored by kladko's avatar kladko

SKALE-2077 cleaned up LevelDB init

parent b099b4fb
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <jsonrpccpp/server/connectors/httpserver.h> #include <jsonrpccpp/server/connectors/httpserver.h>
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "common.h"
CSRManagerServer *cs = nullptr; CSRManagerServer *cs = nullptr;
...@@ -27,15 +28,14 @@ Json::Value GetUnsignedCSRsImpl(){ ...@@ -27,15 +28,14 @@ Json::Value GetUnsignedCSRsImpl(){
Json::Value result; Json::Value result;
result["status"] = 0; result["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
//result["hashes"] =;
try{ try{
std::vector<std::string> hashes_vect = csrDb->writeKeysToVector1(MAX_CSR_NUM); vector<string> hashes_vect = LevelDB::getCsrDb()->writeKeysToVector1(MAX_CSR_NUM);
for (int i = 0; i < hashes_vect.size(); i++){ for (int i = 0; i < (int) hashes_vect.size(); i++){
result["hashes"][i] = hashes_vect.at(i); result["hashes"][i] = hashes_vect.at(i);
} }
} catch (RPCException &_e) { } catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl; cerr << " err str " << _e.errString << endl;
result["status"] = _e.status; result["status"] = _e.status;
result["errorMessage"] = _e.errString; result["errorMessage"] = _e.errString;
...@@ -44,7 +44,7 @@ Json::Value GetUnsignedCSRsImpl(){ ...@@ -44,7 +44,7 @@ Json::Value GetUnsignedCSRsImpl(){
return result; return result;
} }
Json::Value SignByHashImpl(const std::string& hash, int status){ Json::Value SignByHashImpl(const string& hash, int status){
Json::Value result; Json::Value result;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -53,46 +53,46 @@ Json::Value SignByHashImpl(const std::string& hash, int status){ ...@@ -53,46 +53,46 @@ Json::Value SignByHashImpl(const std::string& hash, int status){
throw RPCException(-111, "Invalid csr status"); throw RPCException(-111, "Invalid csr status");
} }
std::string csr_db_key = "CSR:HASH:" + hash; string csr_db_key = "CSR:HASH:" + hash;
std::shared_ptr<std::string> csr_ptr = csrDb->readString(csr_db_key); shared_ptr<string> csr_ptr = LevelDB::getCsrDb()->readString(csr_db_key);
if (csr_ptr == nullptr){ if (csr_ptr == nullptr){
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "HASH DOES NOT EXIST IN DB"); throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "HASH DOES NOT EXIST IN DB");
} }
if (status == 0) { if (status == 0) {
std::string csr_name = "sgx_data/cert/" + hash + ".csr"; string csr_name = "sgx_data/cert/" + hash + ".csr";
std::ofstream outfile(csr_name); ofstream outfile(csr_name);
outfile << *csr_ptr << std::endl; outfile << *csr_ptr << endl;
outfile.close(); outfile.close();
if (access(csr_name.c_str(), F_OK) != 0) { if (access(csr_name.c_str(), F_OK) != 0) {
csrDb->deleteKey(csr_db_key); LevelDB::getCsrDb()->deleteKey(csr_db_key);
throw RPCException(FILE_NOT_FOUND, "Csr does not exist"); throw RPCException(FILE_NOT_FOUND, "Csr does not exist");
} }
std::string signClientCert = "cd sgx_data/cert && ./create_client_cert " + hash; string signClientCert = "cd sgx_data/cert && ./create_client_cert " + hash;
if (system(signClientCert.c_str()) == 0) { if (system(signClientCert.c_str()) == 0) {
spdlog::info("CLIENT CERTIFICATE IS SUCCESSFULLY GENERATED"); spdlog::info("CLIENT CERTIFICATE IS SUCCESSFULLY GENERATED");
} else { } else {
spdlog::info("CLIENT CERTIFICATE GENERATION FAILED"); spdlog::info("CLIENT CERTIFICATE GENERATION FAILED");
csrDb->deleteKey(csr_db_key); LevelDB::getCsrDb()->deleteKey(csr_db_key);
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:"; string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->deleteKey(status_db_key); LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
csrStatusDb->writeDataUnique(status_db_key, "-1"); LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, "-1");
throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED"); throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
//exit(-1); //exit(-1);
} }
} }
csrDb->deleteKey(csr_db_key); LevelDB::getCsrDb()->deleteKey(csr_db_key);
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:"; string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->deleteKey(status_db_key); LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
csrStatusDb->writeDataUnique(status_db_key, std::to_string(status)); LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, to_string(status));
result["status"] = status; result["status"] = status;
} catch (RPCException &_e) { } catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl; cerr << " err str " << _e.errString << endl;
result["status"] = _e.status; result["status"] = _e.status;
result["errorMessage"] = _e.errString; result["errorMessage"] = _e.errString;
} }
...@@ -102,12 +102,12 @@ Json::Value SignByHashImpl(const std::string& hash, int status){ ...@@ -102,12 +102,12 @@ Json::Value SignByHashImpl(const std::string& hash, int status){
Json::Value CSRManagerServer::GetUnsignedCSRs(){ Json::Value CSRManagerServer::GetUnsignedCSRs(){
std::lock_guard<std::recursive_mutex> lock(m); lock_guard<recursive_mutex> lock(m);
return GetUnsignedCSRsImpl(); return GetUnsignedCSRsImpl();
} }
Json::Value CSRManagerServer::SignByHash(const std::string& hash, int status){ Json::Value CSRManagerServer::SignByHash(const string& hash, int status){
std::lock_guard<std::recursive_mutex> lock(m); lock_guard<recursive_mutex> lock(m);
return SignByHashImpl(hash, status); return SignByHashImpl(hash, status);
} }
......
...@@ -37,24 +37,24 @@ ...@@ -37,24 +37,24 @@
#include "ServerInit.h" #include "ServerInit.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "common.h"
using namespace leveldb; using namespace leveldb;
static WriteOptions writeOptions; static WriteOptions writeOptions;
static ReadOptions readOptions; static ReadOptions readOptions;
LevelDB* levelDb = nullptr;
LevelDB* csrDb = nullptr;
LevelDB* csrStatusDb = nullptr;
std::shared_ptr<std::string> LevelDB::readString(const std::string &_key) { std::shared_ptr<string> LevelDB::readString(const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
auto result = std::make_shared<std::string>(); auto result = std::make_shared<string>();
if (db == nullptr) { if (db == nullptr) {
throw RPCException(NULL_DATABASE, "Null db"); throw RPCException(NULL_DATABASE, "Null db");
...@@ -75,7 +75,7 @@ std::shared_ptr<std::string> LevelDB::readString(const std::string &_key) { ...@@ -75,7 +75,7 @@ std::shared_ptr<std::string> LevelDB::readString(const std::string &_key) {
return result; return result;
} }
void LevelDB::writeString(const std::string &_key, const std::string &_value) { void LevelDB::writeString(const string &_key, const string &_value) {
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
...@@ -90,11 +90,11 @@ void LevelDB::writeString(const std::string &_key, const std::string &_value) { ...@@ -90,11 +90,11 @@ void LevelDB::writeString(const std::string &_key, const std::string &_value) {
} }
void LevelDB::deleteDHDKGKey (const std::string &_key) { void LevelDB::deleteDHDKGKey (const string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
std::string full_key = "DKG_DH_KEY_" + _key; string full_key = "DKG_DH_KEY_" + _key;
auto status = db->Delete(writeOptions, Slice(_key)); auto status = db->Delete(writeOptions, Slice(_key));
...@@ -106,11 +106,11 @@ void LevelDB::deleteDHDKGKey (const std::string &_key) { ...@@ -106,11 +106,11 @@ void LevelDB::deleteDHDKGKey (const std::string &_key) {
} }
} }
void LevelDB::deleteTempNEK(const std::string &_key){ void LevelDB::deleteTempNEK(const string &_key){
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
std::string prefix = _key.substr(0,8); string prefix = _key.substr(0,8);
if (prefix != "tmp_NEK:") { if (prefix != "tmp_NEK:") {
return; return;
} }
...@@ -122,7 +122,7 @@ void LevelDB::deleteTempNEK(const std::string &_key){ ...@@ -122,7 +122,7 @@ void LevelDB::deleteTempNEK(const std::string &_key){
std::cerr << "key deleted " << _key << std::endl; std::cerr << "key deleted " << _key << std::endl;
} }
void LevelDB::deleteKey(const std::string &_key){ void LevelDB::deleteKey(const string &_key){
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
...@@ -149,7 +149,7 @@ void LevelDB::writeByteArray(const char *_key, size_t _keyLen, const char *value ...@@ -149,7 +149,7 @@ void LevelDB::writeByteArray(const char *_key, size_t _keyLen, const char *value
} }
void LevelDB::writeByteArray(std::string &_key, const char *value, void LevelDB::writeByteArray(string &_key, const char *value,
size_t _valueLen) { size_t _valueLen) {
std::lock_guard<std::recursive_mutex> lock(mutex); std::lock_guard<std::recursive_mutex> lock(mutex);
...@@ -188,13 +188,13 @@ uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor, uint64_t _maxKeysToVi ...@@ -188,13 +188,13 @@ uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor, uint64_t _maxKeysToVi
return readCounter; return readCounter;
} }
std::vector<std::string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){ std::vector<string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){
uint64_t readCounter = 0; uint64_t readCounter = 0;
std::vector<std::string> keys; std::vector<string> keys;
leveldb::Iterator *it = db->NewIterator(readOptions); leveldb::Iterator *it = db->NewIterator(readOptions);
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {
std::string cur_key(it->key().data(), it->key().size()); string cur_key(it->key().data(), it->key().size());
keys.push_back(cur_key); keys.push_back(cur_key);
// keys.push_back(it->key().data()); // keys.push_back(it->key().data());
readCounter++; readCounter++;
...@@ -208,7 +208,7 @@ std::vector<std::string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){ ...@@ -208,7 +208,7 @@ std::vector<std::string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){
return keys; return keys;
} }
void LevelDB::writeDataUnique(const std::string & Name, const std::string &value) { void LevelDB::writeDataUnique(const string & Name, const string &value) {
auto key = Name; auto key = Name;
...@@ -226,7 +226,7 @@ void LevelDB::writeDataUnique(const std::string & Name, const std::string &value ...@@ -226,7 +226,7 @@ void LevelDB::writeDataUnique(const std::string & Name, const std::string &value
} }
LevelDB::LevelDB(std::string &filename) { LevelDB::LevelDB(string &filename) {
leveldb::Options options; leveldb::Options options;
...@@ -243,9 +243,46 @@ LevelDB::LevelDB(std::string &filename) { ...@@ -243,9 +243,46 @@ LevelDB::LevelDB(std::string &filename) {
} }
LevelDB::~LevelDB() { LevelDB::~LevelDB() {
if (db != nullptr)
delete db;
} }
const std::shared_ptr<LevelDB> &LevelDB::getLevelDb() {
CHECK_STATE(levelDb)
return levelDb;
}
const std::shared_ptr<LevelDB> &LevelDB::getCsrDb() {
CHECK_STATE(csrDb)
return csrDb;
}
const std::shared_ptr<LevelDB> &LevelDB::getCsrStatusDb() {
CHECK_STATE(csrStatusDb)
return csrStatusDb;
}
std::shared_ptr<LevelDB> LevelDB::levelDb = nullptr;
std::shared_ptr<LevelDB> LevelDB::csrDb = nullptr;
std::shared_ptr<LevelDB> LevelDB::csrStatusDb = nullptr;
std::shared_ptr<string> LevelDB::sgx_data_folder = nullptr;
bool LevelDB::isInited = false;
void LevelDB::initDBs(string &_sgx_data_folder) {
if (isInited)
return;
auto dbName = _sgx_data_folder + WALLETDB_NAME;
levelDb = make_shared<LevelDB>(dbName);
auto csr_dbname = _sgx_data_folder + "CSR_DB";
csrDb = make_shared<LevelDB>(csr_dbname);
auto csr_status_dbname = _sgx_data_folder + "CSR_STATUS_DB";
csrStatusDb = make_shared<LevelDB>(csr_status_dbname);
}
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <string> #include <string>
#include <mutex> #include <mutex>
#include <vector> #include <vector>
#include "common.h"
namespace leveldb { namespace leveldb {
class DB; class DB;
class Status; class Status;
...@@ -40,7 +40,29 @@ class LevelDB { ...@@ -40,7 +40,29 @@ class LevelDB {
std::recursive_mutex mutex; std::recursive_mutex mutex;
leveldb::DB* db; std::shared_ptr<leveldb::DB> db;
static bool isInited;
static std::shared_ptr<LevelDB> levelDb;
static std::shared_ptr<LevelDB> csrDb;
static std::shared_ptr<LevelDB> csrStatusDb;
static std::shared_ptr<std::string> sgx_data_folder;
public:
static void initDBs(std::string &_sgx_data_folder);
static const std::shared_ptr<LevelDB> &getLevelDb();
static const std::shared_ptr<LevelDB> &getCsrDb();
static const std::shared_ptr<LevelDB> &getCsrStatusDb();
public: public:
...@@ -92,10 +114,6 @@ public: ...@@ -92,10 +114,6 @@ public:
}; };
extern LevelDB* levelDb;
extern LevelDB* csrDb;
extern LevelDB* csrStatusDb;
#endif #endif
\ No newline at end of file
...@@ -47,9 +47,9 @@ void generate_SEK(){ ...@@ -47,9 +47,9 @@ void generate_SEK(){
char *hexEncrKey = (char *) calloc(2*enc_len + 1, 1); char *hexEncrKey = (char *) calloc(2*enc_len + 1, 1);
carray2Hex(encr_SEK, enc_len, hexEncrKey); carray2Hex(encr_SEK, enc_len, hexEncrKey);
std::cerr << "key is " << errMsg << std::endl; std::cegit crr << "key is " << errMsg << std::endl;
levelDb->writeDataUnique("SEK", hexEncrKey); LevelDB::getLevelDb()->writeDataUnique("SEK", hexEncrKey);
free(errMsg); free(errMsg);
free(encr_SEK); free(encr_SEK);
......
...@@ -75,7 +75,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){ ...@@ -75,7 +75,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){
std::string hash = cryptlite::sha256::hash_hex(csr); std::string hash = cryptlite::sha256::hash_hex(csr);
if ( !auto_sign) { if ( !auto_sign) {
std::string db_key = "CSR:HASH:" + hash; std::string db_key = "CSR:HASH:" + hash;
csrDb->writeDataUnique(db_key, csr); LevelDB::getCsrStatusDb()->writeDataUnique(db_key, csr);
} }
if (auto_sign) { if (auto_sign) {
...@@ -96,7 +96,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){ ...@@ -96,7 +96,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){
else{ else{
spdlog::info("CLIENT CERTIFICATE GENERATION FAILED"); spdlog::info("CLIENT CERTIFICATE GENERATION FAILED");
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:"; std::string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->writeDataUnique(status_db_key, std::to_string(FAIL_TO_CREATE_CERTIFICATE)); LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, std::to_string(FAIL_TO_CREATE_CERTIFICATE));
throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED"); throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
//exit(-1); //exit(-1);
} }
...@@ -106,7 +106,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){ ...@@ -106,7 +106,7 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){
result["hash"] = hash; result["hash"] = hash;
std::string db_key = "CSR:HASH:" + hash + "STATUS:"; std::string db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->writeDataUnique(db_key, status); LevelDB::getCsrStatusDb()->writeDataUnique(db_key, status);
} catch (RPCException &_e) { } catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl; std::cerr << " err str " << _e.errString << std::endl;
...@@ -123,21 +123,21 @@ Json::Value GetSertificateImpl(const std::string& hash){ ...@@ -123,21 +123,21 @@ Json::Value GetSertificateImpl(const std::string& hash){
std::string cert; std::string cert;
try{ try{
std::string db_key = "CSR:HASH:" + hash + "STATUS:"; string db_key = "CSR:HASH:" + hash + "STATUS:";
std::shared_ptr<string> status_str_ptr = csrStatusDb->readString(db_key); shared_ptr<string> status_str_ptr = LevelDB::getCsrStatusDb()->readString(db_key);
if (status_str_ptr == nullptr){ if (status_str_ptr == nullptr){
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Data with this name does not exist in csr db"); throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Data with this name does not exist in csr db");
} }
int status = std::atoi(status_str_ptr->c_str()); int status = std::atoi(status_str_ptr->c_str());
if ( status == 0){ if ( status == 0){
std::string crt_name = "cert/" + hash + ".crt"; string crt_name = "cert/" + hash + ".crt";
//if (access(crt_name.c_str(), F_OK) == 0){ //if (access(crt_name.c_str(), F_OK) == 0){
std::ifstream infile(crt_name); ifstream infile(crt_name);
if (!infile.is_open()) { if (!infile.is_open()) {
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:"; string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->deleteKey(status_db_key); LevelDB::getCsrStatusDb()->deleteKey(status_db_key);
csrStatusDb->writeDataUnique(status_db_key, std::to_string(FILE_NOT_FOUND)); LevelDB::getCsrStatusDb()->writeDataUnique(status_db_key, std::to_string(FILE_NOT_FOUND));
throw RPCException(FILE_NOT_FOUND, "Certificate does not exist"); throw RPCException(FILE_NOT_FOUND, "Certificate does not exist");
} else { } else {
ostringstream ss; ostringstream ss;
......
...@@ -77,7 +77,7 @@ void debug_print(){ ...@@ -77,7 +77,7 @@ void debug_print(){
MyVisitor v; MyVisitor v;
levelDb->visitKeys(&v, 100000000); LevelDB::getLevelDb()->visitKeys(&v, 100000000);
} }
int init_https_server(bool check_certs) { int init_https_server(bool check_certs) {
...@@ -314,7 +314,7 @@ Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& te ...@@ -314,7 +314,7 @@ Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& te
std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName); std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName);
std::cerr << "new key name is " << KeyName <<std::endl; std::cerr << "new key name is " << KeyName <<std::endl;
writeDataToDB(KeyName, *key_ptr); writeDataToDB(KeyName, *key_ptr);
levelDb->deleteTempNEK(tempKeyName); LevelDB::getLevelDb()->deleteTempNEK(tempKeyName);
} catch (RPCException &_e) { } catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl; std::cerr << " err str " << _e.errString << std::endl;
...@@ -610,9 +610,9 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s ...@@ -610,9 +610,9 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
for ( int i = 0; i < n; i++){ for ( int i = 0; i < n; i++){
std::string name = polyName + "_" + std::to_string(i) + ":"; std::string name = polyName + "_" + std::to_string(i) + ":";
levelDb -> deleteDHDKGKey(name); LevelDB::getLevelDb() -> deleteDHDKGKey(name);
std::string shareG2_name = "shareG2_" + polyName + "_" + std::to_string(i) + ":"; std::string shareG2_name = "shareG2_" + polyName + "_" + std::to_string(i) + ":";
levelDb -> deleteKey(shareG2_name); LevelDB::getLevelDb() -> deleteKey(shareG2_name);
} }
} catch (RPCException &_e) { } catch (RPCException &_e) {
...@@ -707,7 +707,7 @@ Json::Value MultG2Impl(const std::string& x){ ...@@ -707,7 +707,7 @@ Json::Value MultG2Impl(const std::string& x){
Json::Value IsPolyExistsImpl(const std::string& polyName){ Json::Value IsPolyExistsImpl(const std::string& polyName){
Json::Value result; Json::Value result;
std::shared_ptr<std::string> poly_str_ptr = levelDb->readString(polyName); std::shared_ptr<std::string> poly_str_ptr = LevelDB::getLevelDb()->readString(polyName);
result["IsExist"] = true; result["IsExist"] = true;
if (poly_str_ptr == nullptr){ if (poly_str_ptr == nullptr){
result["IsExist"] = false; result["IsExist"] = false;
...@@ -825,7 +825,7 @@ Json::Value SGXWalletServer::getServerStatus() { ...@@ -825,7 +825,7 @@ Json::Value SGXWalletServer::getServerStatus() {
shared_ptr<string> readFromDb(const string & name, const string & prefix) { shared_ptr<string> readFromDb(const string & name, const string & prefix) {
auto dataStr = levelDb->readString(prefix + name); auto dataStr = LevelDB::getLevelDb()->readString(prefix + name);
if (dataStr == nullptr) { if (dataStr == nullptr) {
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Data with this name does not exist"); throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Data with this name does not exist");
...@@ -836,7 +836,7 @@ shared_ptr<string> readFromDb(const string & name, const string & prefix) { ...@@ -836,7 +836,7 @@ shared_ptr<string> readFromDb(const string & name, const string & prefix) {
shared_ptr<string> readKeyShare(const string &_keyShareName) { shared_ptr<string> readKeyShare(const string &_keyShareName) {
auto keyShareStr = levelDb->readString("BLSKEYSHARE:" + _keyShareName); auto keyShareStr = LevelDB::getLevelDb()->readString("BLSKEYSHARE:" + _keyShareName);
if (keyShareStr == nullptr) { if (keyShareStr == nullptr) {
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Key share with this name does not exist"); throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Key share with this name does not exist");
...@@ -860,11 +860,11 @@ void writeKeyShare(const string &_keyShareName, const string &value, int index, ...@@ -860,11 +860,11 @@ void writeKeyShare(const string &_keyShareName, const string &value, int index,
auto key = "BLSKEYSHARE:" + _keyShareName; auto key = "BLSKEYSHARE:" + _keyShareName;
if (levelDb->readString(_keyShareName) != nullptr) { if (LevelDB::getLevelDb()->readString(_keyShareName) != nullptr) {
throw RPCException(KEY_SHARE_ALREADY_EXISTS, "Key share with this name already exists"); throw RPCException(KEY_SHARE_ALREADY_EXISTS, "Key share with this name already exists");
} }
levelDb->writeString(key, value); LevelDB::getLevelDb()->writeString(key, value);
} }
void writeDataToDB(const string & Name, const string &value) { void writeDataToDB(const string & Name, const string &value) {
...@@ -876,12 +876,12 @@ void writeDataToDB(const string & Name, const string &value) { ...@@ -876,12 +876,12 @@ void writeDataToDB(const string & Name, const string &value) {
auto key = Name; auto key = Name;
if (levelDb->readString(Name) != nullptr) { if (LevelDB::getLevelDb()->readString(Name) != nullptr) {
spdlog::info("name {}", Name, " already exists"); spdlog::info("name {}", Name, " already exists");
throw RPCException(KEY_SHARE_ALREADY_EXISTS, "Data with this name already exists"); throw RPCException(KEY_SHARE_ALREADY_EXISTS, "Data with this name already exists");
} }
levelDb->writeString(key, value); LevelDB::getLevelDb()->writeString(key, value);
if (DEBUG_PRINT) { if (DEBUG_PRINT) {
spdlog::info("{} ", Name, " is written to db "); spdlog::info("{} ", Name, " is written to db ");
} }
......
...@@ -93,16 +93,10 @@ void init_daemon() { ...@@ -93,16 +93,10 @@ void init_daemon() {
} }
} }
static std::string dbName = sgx_data_folder + WALLETDB_NAME; LevelDB::initDBs(sgx_data_folder);
levelDb = new LevelDB(dbName);
static std::string csr_dbname = sgx_data_folder + "CSR_DB";
csrDb = new LevelDB(csr_dbname);
static std::string csr_status_dbname = sgx_data_folder + "CSR_STATUS_DB"; std::shared_ptr<std::string> encr_SEK_ptr = LevelDB::getLevelDb()->readString("SEK");
csrStatusDb = new LevelDB(csr_status_dbname);
std::shared_ptr<std::string> encr_SEK_ptr = levelDb->readString("SEK");
if (encr_SEK_ptr == nullptr){ if (encr_SEK_ptr == nullptr){
spdlog::info("SEK was not created yet"); spdlog::info("SEK was not created yet");
generate_SEK(); generate_SEK();
...@@ -116,9 +110,10 @@ void init_enclave() { ...@@ -116,9 +110,10 @@ void init_enclave() {
eid = 0; eid = 0;
updated = 0; updated = 0;
unsigned long support;
#ifndef SGX_HW_SIM #ifndef SGX_HW_SIM
unsigned long support;
support = get_sgx_support(); support = get_sgx_support();
if (!SGX_OK(support)) { if (!SGX_OK(support)) {
sgx_support_perror(support); sgx_support_perror(support);
...@@ -170,8 +165,6 @@ void init_all(bool check_cert, bool sign_automatically) { ...@@ -170,8 +165,6 @@ void init_all(bool check_cert, bool sign_automatically) {
sgxServerInited = 1; sgxServerInited = 1;
init_daemon();
if (is_sgx_https) { if (is_sgx_https) {
init_https_server(check_cert); init_https_server(check_cert);
init_registration_server(sign_automatically); init_registration_server(sign_automatically);
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#endif #endif
EXTERNC void init_all(bool check_cert, bool sign_automatically); EXTERNC void init_all(bool check_cert, bool sign_automatically);
EXTERNC void init_daemon(); EXTERNC void init_daemon();
......
//
// Created by kladko on 25.01.20.
//
#ifndef SGXWALLET_COMMON_H
#define SGXWALLET_COMMON_H
using namespace std;
#include <stdlib.h>
#include <iostream>
#include <map>
#include <memory>
#define CHECK_ARGUMENT(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
auto __msg__ = string("Argument Check failed:") + #_EXPRESSION_ + "\n" + __CLASS_NAME__ + ":" + __FUNCTION__ + \
+ " " + string(__FILE__) + ":" + to_string(__LINE__); \
throw runtime_error(__msg__);}
#define CHECK_STATE(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
auto __msg__ = string("State check failed::") + #_EXPRESSION_ + " " + string(__FILE__) + ":" + to_string(__LINE__); \
throw runtime_error(__msg__);}
#endif //SGXWALLET_COMMON_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment