Electroneum
Loading...
Searching...
No Matches
tools::ringdb Class Reference

#include <ringdb.h>

Inheritance diagram for tools::ringdb:

Public Member Functions

 ringdb (std::string filename, const std::string &genesis)
void close ()
 ~ringdb ()
bool add_rings (const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx)
bool remove_rings (const crypto::chacha_key &chacha_key, const std::vector< crypto::key_image > &key_images)
bool remove_rings (const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx)
bool get_ring (const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, std::vector< uint64_t > &outs)
bool set_ring (const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, const std::vector< uint64_t > &outs, bool relative)
bool blackball (const std::pair< uint64_t, uint64_t > &output)
bool blackball (const std::vector< std::pair< uint64_t, uint64_t > > &outputs)
bool unblackball (const std::pair< uint64_t, uint64_t > &output)
bool blackballed (const std::pair< uint64_t, uint64_t > &output)
bool clear_blackballs ()

Detailed Description

Definition at line 40 of file ringdb.h.

Constructor & Destructor Documentation

◆ ringdb()

tools::ringdb::ringdb ( std::string filename,
const std::string & genesis )

Definition at line 202 of file ringdb.cpp.

202 :
203 filename(filename),
204 env(NULL)
205{
206 MDB_txn *txn;
207 bool tx_active = false;
208 int dbr;
209
211
212 dbr = mdb_env_create(&env);
213 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LDMB environment: " + std::string(mdb_strerror(dbr)));
214 dbr = mdb_env_set_maxdbs(env, 2);
215 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set max env dbs: " + std::string(mdb_strerror(dbr)));
216 const std::string actual_filename = get_rings_filename(filename);
217 dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664);
218 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open rings database file '"
219 + actual_filename + "': " + std::string(mdb_strerror(dbr)));
220
221 dbr = mdb_txn_begin(env, NULL, 0, &txn);
222 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
224 tx_active = true;
225
226 dbr = mdb_dbi_open(txn, ("rings-" + genesis).c_str(), MDB_CREATE, &dbi_rings);
227 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr)));
228 mdb_set_compare(txn, dbi_rings, compare_hash32);
229
230 dbr = mdb_dbi_open(txn, ("blackballs2-" + genesis).c_str(), MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi_blackballs);
231 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr)));
232 mdb_set_dupsort(txn, dbi_blackballs, compare_uint64);
233
234 dbr = mdb_txn_commit(txn);
235 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr)));
236 tx_active = false;
237}
int compare_uint64(const MDB_val *a, const MDB_val *b)
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
Set a custom key comparison function for a database.
void mdb_txn_abort(MDB_txn *txn)
Abandon all the operations of the transaction instead of saving them.
int mdb_txn_commit(MDB_txn *txn)
Commit all the operations of a transaction into the database.
char * mdb_strerror(int err)
Return a string describing a given error code.
int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
Set the maximum number of named databases for the environment.
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
Open a database in the environment.
int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
Set a custom data comparison function for a MDB_DUPSORT database.
int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn)
Create a transaction for use with the environment.
#define MDB_INTEGERKEY
Definition lmdb.h:349
#define MDB_DUPFIXED
Definition lmdb.h:351
#define MDB_DUPSORT
Definition lmdb.h:345
#define MDB_CREATE
Definition lmdb.h:357
struct MDB_txn MDB_txn
Opaque structure for a transaction handle.
Definition lmdb.h:267
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
bool create_directories_if_necessary(const std::string &path)
creates directories for a path
Definition util.cpp:625
std::string get_rings_filename(boost::filesystem::path filename)
Definition ringdb.cpp:91
#define THROW_WALLET_EXCEPTION_IF(cond, err_type,...)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~ringdb()

tools::ringdb::~ringdb ( )

Definition at line 239 of file ringdb.cpp.

240{
241 close();
242}
void close()
Definition ringdb.cpp:244
Here is the call graph for this function:

Member Function Documentation

◆ add_rings()

bool tools::ringdb::add_rings ( const crypto::chacha_key & chacha_key,
const cryptonote::transaction_prefix & tx )

Definition at line 255 of file ringdb.cpp.

256{
257 MDB_txn *txn;
258 int dbr;
259 bool tx_active = false;
260
261 dbr = resize_env(env, filename.c_str(), get_ring_data_size(tx.vin.size()));
262 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size");
263 dbr = mdb_txn_begin(env, NULL, 0, &txn);
264 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
266 tx_active = true;
267
268 for (const auto &in: tx.vin)
269 {
270 if (in.type() != typeid(cryptonote::txin_to_key))
271 continue;
272 const auto &txin = boost::get<cryptonote::txin_to_key>(in);
273 const uint32_t ring_size = txin.key_offsets.size();
274 if (ring_size == 1)
275 continue;
276
277 store_relative_ring(txn, dbi_rings, txin.k_image, txin.key_offsets, chacha_key);
278 }
279
280 dbr = mdb_txn_commit(txn);
281 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn adding ring to database: " + std::string(mdb_strerror(dbr)));
282 tx_active = false;
283 return true;
284}
unsigned int uint32_t
Definition stdint.h:126
Here is the call graph for this function:

◆ blackball() [1/2]

bool tools::ringdb::blackball ( const std::pair< uint64_t, uint64_t > & output)

Definition at line 473 of file ringdb.cpp.

474{
475 std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output);
476 return blackball_worker(outputs, BLACKBALL_BLACKBALL);
477}
@ BLACKBALL_BLACKBALL
Definition ringdb.cpp:197
Here is the caller graph for this function:

◆ blackball() [2/2]

bool tools::ringdb::blackball ( const std::vector< std::pair< uint64_t, uint64_t > > & outputs)

Definition at line 468 of file ringdb.cpp.

469{
470 return blackball_worker(outputs, BLACKBALL_BLACKBALL);
471}

◆ blackballed()

bool tools::ringdb::blackballed ( const std::pair< uint64_t, uint64_t > & output)

Definition at line 485 of file ringdb.cpp.

486{
487 std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output);
488 return blackball_worker(outputs, BLACKBALL_QUERY);
489}
@ BLACKBALL_QUERY
Definition ringdb.cpp:197
Here is the caller graph for this function:

◆ clear_blackballs()

bool tools::ringdb::clear_blackballs ( )

Definition at line 491 of file ringdb.cpp.

492{
493 return blackball_worker(std::vector<std::pair<uint64_t, uint64_t>>(), BLACKBALL_CLEAR);
494}
@ BLACKBALL_CLEAR
Definition ringdb.cpp:197
Here is the caller graph for this function:

◆ close()

void tools::ringdb::close ( )

Definition at line 244 of file ringdb.cpp.

245{
246 if (env)
247 {
248 mdb_dbi_close(env, dbi_rings);
249 mdb_dbi_close(env, dbi_blackballs);
250 mdb_env_close(env);
251 env = NULL;
252 }
253}
void mdb_env_close(MDB_env *env)
Close the environment and release the memory map.
void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
Close a database handle. Normally unnecessary. Use with care:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_ring()

bool tools::ringdb::get_ring ( const crypto::chacha_key & chacha_key,
const crypto::key_image & key_image,
std::vector< uint64_t > & outs )

Definition at line 340 of file ringdb.cpp.

341{
342 MDB_txn *txn;
343 int dbr;
344 bool tx_active = false;
345
346 dbr = resize_env(env, filename.c_str(), 0);
347 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr)));
348 dbr = mdb_txn_begin(env, NULL, 0, &txn);
349 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
351 tx_active = true;
352
353 MDB_val key, data;
354 std::string key_ciphertext = encrypt(key_image, chacha_key);
355 key.mv_data = (void*)key_ciphertext.data();
356 key.mv_size = key_ciphertext.size();
357 dbr = mdb_get(txn, dbi_rings, &key, &data);
358 THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr)));
359 if (dbr == MDB_NOTFOUND)
360 return false;
361 THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size");
362
363 std::string data_plaintext = decrypt(std::string((const char*)data.mv_data, data.mv_size), key_image, chacha_key);
364 outs = decompress_ring(data_plaintext);
365 MDEBUG("Found ring for key image " << key_image << ":");
366 MDEBUG("Relative: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " "));
368 MDEBUG("Absolute: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " "));
369
370 dbr = mdb_txn_commit(txn);
371 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn getting ring from database: " + std::string(mdb_strerror(dbr)));
372 tx_active = false;
373 return true;
374}
#define MDB_NOTFOUND
Definition lmdb.h:439
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Get items from a database.
void * mv_data
Definition lmdb.h:288
size_t mv_size
Definition lmdb.h:287
struct MDB_val MDB_val
Generic structure used for passing keys and data in and out of the database.
const char * key
#define MDEBUG(x)
Definition misc_log_ex.h:76
std::vector< uint64_t > relative_output_offsets_to_absolute(const std::vector< uint64_t > &off)
void decrypt(const void *ciphertext, size_t length, const uint8_t *key, const uint8_t *iv, char *plaintext, size_t *plaintext_len)
Definition protocol.cpp:120
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_rings() [1/2]

bool tools::ringdb::remove_rings ( const crypto::chacha_key & chacha_key,
const cryptonote::transaction_prefix & tx )

Definition at line 323 of file ringdb.cpp.

324{
325 std::vector<crypto::key_image> key_images;
326 key_images.reserve(tx.vin.size());
327 for (const auto &in: tx.vin)
328 {
329 if (in.type() != typeid(cryptonote::txin_to_key))
330 continue;
331 const auto &txin = boost::get<cryptonote::txin_to_key>(in);
332 const uint32_t ring_size = txin.key_offsets.size();
333 if (ring_size == 1)
334 continue;
335 key_images.push_back(txin.k_image);
336 }
337 return remove_rings(chacha_key, key_images);
338}
bool remove_rings(const crypto::chacha_key &chacha_key, const std::vector< crypto::key_image > &key_images)
Definition ringdb.cpp:286
Here is the call graph for this function:

◆ remove_rings() [2/2]

bool tools::ringdb::remove_rings ( const crypto::chacha_key & chacha_key,
const std::vector< crypto::key_image > & key_images )

Definition at line 286 of file ringdb.cpp.

287{
288 MDB_txn *txn;
289 int dbr;
290 bool tx_active = false;
291
292 dbr = resize_env(env, filename.c_str(), 0);
293 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size");
294 dbr = mdb_txn_begin(env, NULL, 0, &txn);
295 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
297 tx_active = true;
298
299 for (const crypto::key_image &key_image: key_images)
300 {
301 MDB_val key, data;
302 std::string key_ciphertext = encrypt(key_image, chacha_key);
303 key.mv_data = (void*)key_ciphertext.data();
304 key.mv_size = key_ciphertext.size();
305
306 dbr = mdb_get(txn, dbi_rings, &key, &data);
307 THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr)));
308 if (dbr == MDB_NOTFOUND)
309 continue;
310 THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size");
311
312 MDEBUG("Removing ring data for key image " << key_image);
313 dbr = mdb_del(txn, dbi_rings, &key, NULL);
314 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to remove ring to database: " + std::string(mdb_strerror(dbr)));
315 }
316
317 dbr = mdb_txn_commit(txn);
318 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn removing ring to database: " + std::string(mdb_strerror(dbr)));
319 tx_active = false;
320 return true;
321}
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Delete items from a database.
POD_CLASS key_image
Definition crypto.h:105
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_ring()

bool tools::ringdb::set_ring ( const crypto::chacha_key & chacha_key,
const crypto::key_image & key_image,
const std::vector< uint64_t > & outs,
bool relative )

Definition at line 376 of file ringdb.cpp.

377{
378 MDB_txn *txn;
379 int dbr;
380 bool tx_active = false;
381
382 dbr = resize_env(env, filename.c_str(), outs.size() * 64);
383 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr)));
384 dbr = mdb_txn_begin(env, NULL, 0, &txn);
385 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
387 tx_active = true;
388
389 store_relative_ring(txn, dbi_rings, key_image, relative ? outs : cryptonote::absolute_output_offsets_to_relative(outs), chacha_key);
390
391 dbr = mdb_txn_commit(txn);
392 THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn setting ring to database: " + std::string(mdb_strerror(dbr)));
393 tx_active = false;
394 return true;
395}
std::vector< uint64_t > absolute_output_offsets_to_relative(const std::vector< uint64_t > &off)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unblackball()

bool tools::ringdb::unblackball ( const std::pair< uint64_t, uint64_t > & output)

Definition at line 479 of file ringdb.cpp.

480{
481 std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output);
482 return blackball_worker(outputs, BLACKBALL_UNBLACKBALL);
483}
@ BLACKBALL_UNBLACKBALL
Definition ringdb.cpp:197
Here is the caller graph for this function:

The documentation for this class was generated from the following files:
  • /home/abuild/rpmbuild/BUILD/electroneum-5.1.3.1-build/electroneum-5.1.3.1/src/wallet/ringdb.h
  • /home/abuild/rpmbuild/BUILD/electroneum-5.1.3.1-build/electroneum-5.1.3.1/src/wallet/ringdb.cpp