-
Notifications
You must be signed in to change notification settings - Fork 35
/
LevelDB.cpp
308 lines (226 loc) · 7.94 KB
/
LevelDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright (C) 2019-Present SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file LevelDB.cpp
@author Stan Kladko
@date 2019
*/
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include "leveldb/db.h"
#include <jsonrpccpp/client.h>
#include "LevelDB.h"
#include "SGXException.h"
#include "sgxwallet_common.h"
#include "ServerInit.h"
#include "common.h"
#include "third_party/spdlog/spdlog.h"
using namespace leveldb;
static WriteOptions writeOptions;
static ReadOptions readOptions;
shared_ptr<string> LevelDB::readNewStyleValue(const string &value) {
Json::Value key_data;
Json::Reader reader;
reader.parse(value.c_str(), key_data);
return std::make_shared<string>(key_data["value"].asString());
}
std::shared_ptr<string> LevelDB::readString(const string &_key) {
auto result = std::make_shared<string>();
CHECK_STATE(db)
auto status = db->Get(readOptions, _key, result.get());
throwExceptionOnError(status);
if (status.IsNotFound()) {
return nullptr;
}
if (result->at(0) == '{') {
return readNewStyleValue(*result);
}
return result;
}
void LevelDB::writeString(const string &_key, const string &_value) {
Json::Value writerData;
writerData["value"] = _value;
writerData["timestamp"] = std::to_string(std::time(nullptr));
Json::FastWriter fastWriter;
std::string output = fastWriter.write(writerData);
auto status = db->Put(writeOptions, Slice(_key), Slice(output));
throwExceptionOnError(status);
}
void LevelDB::deleteDHDKGKey(const string &_key) {
string full_key = "DKG_DH_KEY_" + _key;
auto status = db->Delete(writeOptions, Slice(full_key));
throwExceptionOnError(status);
}
void LevelDB::deleteTempNEK(const string &_key) {
CHECK_STATE(_key.rfind("tmp_NEK", 0) == 0);
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
}
void LevelDB::deleteKey(const string &_key) {
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
}
void LevelDB::throwExceptionOnError(Status _status) {
if (_status.IsNotFound())
return;
if (!_status.ok()) {
throw SGXException(
COULD_NOT_ACCESS_DATABASE,
("Could not access database database:" + _status.ToString()).c_str());
}
}
uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor,
uint64_t _maxKeysToVisit) {
CHECK_STATE(_visitor);
uint64_t readCounter = 0;
shared_ptr<leveldb::Iterator> it(db->NewIterator(readOptions));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
_visitor->visitDBKey(it->key().data());
readCounter++;
if (readCounter >= _maxKeysToVisit) {
break;
}
}
return readCounter;
}
std::vector<string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit) {
uint64_t readCounter = 0;
std::vector<string> keys;
shared_ptr<leveldb::Iterator> it(db->NewIterator(readOptions));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
string cur_key(it->key().data(), it->key().size());
keys.push_back(cur_key);
readCounter++;
if (readCounter >= _maxKeysToVisit) {
break;
}
}
return keys;
}
void LevelDB::writeDataUnique(const string &name, const string &value) {
if (readString(name)) {
spdlog::debug("Name {} already exists", name);
throw SGXException(KEY_SHARE_ALREADY_EXISTS,
"Data with this name already exists");
}
writeString(name, value);
}
pair<stringstream, uint64_t> LevelDB::getAllKeys() {
stringstream keysInfo;
leveldb::Iterator *it = db->NewIterator(readOptions);
uint64_t counter = 0;
for (it->SeekToFirst(); it->Valid(); it->Next()) {
++counter;
string key = it->key().ToString();
string value;
if (it->value().ToString()[0] == '{') {
// new style keys
Json::Value key_data;
Json::Reader reader;
reader.parse(it->value().ToString().c_str(), key_data);
string timestamp_to_date_command =
"date -d @" + key_data["timestamp"].asString();
value = " VALUE: " + key_data["value"].asString() +
", TIMESTAMP: " + exec(timestamp_to_date_command.c_str()) + '\n';
} else {
// old style keys
value = " VALUE: " + it->value().ToString();
}
keysInfo << "KEY: " << key << ',' << value;
}
return {std::move(keysInfo), counter};
}
pair<string, uint64_t> LevelDB::getLatestCreatedKey() {
leveldb::Iterator *it = db->NewIterator(readOptions);
int64_t latest_timestamp = 0;
string latest_created_key_name = "";
for (it->SeekToFirst(); it->Valid(); it->Next()) {
if (it->value().ToString()[0] == '{') {
// new style keys
Json::Value key_data;
Json::Reader reader;
reader.parse(it->value().ToString().c_str(), key_data);
if (std::stoi(key_data["timestamp"].asString()) > latest_timestamp) {
latest_timestamp = std::stoi(key_data["timestamp"].asString());
latest_created_key_name = it->key().ToString();
}
} else {
// old style keys
// assuming server has at least one new-style key created
continue;
}
}
return {latest_created_key_name, latest_timestamp};
}
LevelDB::LevelDB(string &filename) {
leveldb::Options options;
options.create_if_missing = true;
if (!leveldb::DB::Open(options, filename, (leveldb::DB **)&db).ok()) {
throw std::runtime_error("Unable to open levelDB database");
}
if (db == nullptr) {
throw std::runtime_error("Null levelDB object");
}
}
LevelDB::~LevelDB() {}
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;
string LevelDB::sgx_data_folder;
bool LevelDB::isInited = false;
void LevelDB::initDataFolderAndDBs() {
CHECK_STATE(!isInited)
isInited = true;
spdlog::info("Initing wallet database ... ");
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
spdlog::error("Could not get current working directory.");
throw SGXException(COULD_NOT_GET_WORKING_DIRECTORY,
"Could not get current working directory.");
}
sgx_data_folder = string(cwd) + "/" + SGXDATA_FOLDER;
struct stat info;
if (stat(sgx_data_folder.c_str(), &info) != 0) {
spdlog::info("sgx_data folder does not exist. Creating ...");
if (system(("mkdir " + sgx_data_folder).c_str()) == 0) {
spdlog::info("Successfully created sgx_data folder");
} else {
spdlog::error("Could not create sgx_data folder.");
throw SGXException(ERROR_CREATING_SGX_DATA_FOLDER,
"Could not create sgx_data folder.");
}
}
spdlog::info("Opening wallet databases");
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);
spdlog::info("Successfully opened databases");
}
const string &LevelDB::getSgxDataFolder() { return sgx_data_folder; }