Electroneum
db_lmdb.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #pragma once
29 
30 #include <atomic>
31 
33 #include "cryptonote_basic/blobdatatype.h" // for type blobdata
34 #include "ringct/rctTypes.h"
35 #include <boost/thread/tss.hpp>
36 
37 #include <lmdb.h>
38 
39 #define ENABLE_AUTO_RESIZE
40 
41 namespace cryptonote
42 {
43 
44 typedef struct txindex {
48 
49 typedef struct mdb_txn_cursors
50 {
54 
57 
65 
67 
70 
72 
79 
80 #define m_cur_blocks m_cursors->m_txc_blocks
81 #define m_cur_block_heights m_cursors->m_txc_block_heights
82 #define m_cur_block_info m_cursors->m_txc_block_info
83 #define m_cur_output_txs m_cursors->m_txc_output_txs
84 #define m_cur_output_amounts m_cursors->m_txc_output_amounts
85 #define m_cur_txs m_cursors->m_txc_txs
86 #define m_cur_txs_pruned m_cursors->m_txc_txs_pruned
87 #define m_cur_txs_prunable m_cursors->m_txc_txs_prunable
88 #define m_cur_txs_prunable_hash m_cursors->m_txc_txs_prunable_hash
89 #define m_cur_txs_prunable_tip m_cursors->m_txc_txs_prunable_tip
90 #define m_cur_tx_indices m_cursors->m_txc_tx_indices
91 #define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
92 #define m_cur_spent_keys m_cursors->m_txc_spent_keys
93 #define m_cur_txpool_meta m_cursors->m_txc_txpool_meta
94 #define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
95 #define m_cur_hf_versions m_cursors->m_txc_hf_versions
96 #define m_cur_properties m_cursors->m_txc_properties
97 #define m_cur_validators m_cursors->m_txc_validators
98 #define m_cur_utxos m_cursors->m_txc_utxos
99 #define m_cur_addr_outputs m_cursors->m_txc_addr_outputs
100 #define m_cur_tx_inputs m_cursors->m_txc_tx_inputs
101 
102 typedef struct mdb_rflags
103 {
104  bool m_rf_txn;
110  bool m_rf_txs;
127 
128 typedef struct mdb_threadinfo
129 {
130  MDB_txn *m_ti_rtxn; // per-thread read txn
131  mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
132  mdb_rflags m_ti_rflags; // per-thread read state
133 
134  ~mdb_threadinfo();
136 
138 {
139  mdb_txn_safe(const bool check=true);
140  ~mdb_txn_safe();
141 
142  void commit(std::string message = "");
143 
144  // This should only be needed for batch transaction which must be ensured to
145  // be aborted before mdb_env_close, not after. So we can't rely on
146  // BlockchainLMDB destructor to call mdb_txn_safe destructor, as that's too late
147  // to properly abort, since mdb_env_close would have been called earlier.
148  void abort();
149  void uncheck();
150 
151  operator MDB_txn*()
152  {
153  return m_txn;
154  }
155 
156  operator MDB_txn**()
157  {
158  return &m_txn;
159  }
160 
161  uint64_t num_active_tx() const;
162 
163  static void prevent_new_txns();
164  static void wait_no_active_txns();
165  static void allow_new_txns();
166 
169  bool m_batch_txn = false;
170  bool m_check;
171  static std::atomic<uint64_t> num_active_txns;
172 
173  // could use a mutex here, but this should be sufficient.
174  static std::atomic_flag creation_gate;
175 };
176 
177 
178 // If m_batch_active is set, a batch transaction exists beyond this class, such
179 // as a batch import with verification enabled, or possibly (later) a batch
180 // network sync.
181 //
182 // For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
183 // and get_tx(), when m_batch_active is set, the lookup uses the batch
184 // transaction. This isn't only because the transaction is available, but it's
185 // necessary so that lookups include the database updates only present in the
186 // current batch write.
187 //
188 // A regular network sync without batch writes is expected to open a new read
189 // transaction, as those lookups are part of the validation done prior to the
190 // write for block and tx data, so no write transaction is open at the time.
192 {
193 public:
194  BlockchainLMDB(bool batch_transactions=true);
195  ~BlockchainLMDB();
196 
197  virtual void open(const std::string& filename, const int mdb_flags=0);
198 
199  virtual void close();
200 
201  virtual void sync();
202 
203  virtual void safesyncmode(const bool onoff);
204 
205  virtual void reset();
206 
207  virtual std::vector<std::string> get_filenames() const;
208 
209  virtual bool remove_data_file(const std::string& folder) const;
210 
211  virtual std::string get_db_name() const;
212 
213  virtual bool lock();
214 
215  virtual void unlock();
216 
217  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
218 
219  virtual uint64_t get_block_height(const crypto::hash& h) const;
220 
221  virtual block_header get_block_header(const crypto::hash& h) const;
222 
223  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
224 
226 
227  virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const;
228 
229  virtual uint64_t get_block_timestamp(const uint64_t& height) const;
230 
231  virtual uint64_t get_top_block_timestamp() const;
232 
233  virtual size_t get_block_weight(const uint64_t& height) const;
234 
235  virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const;
236 
238 
240 
241  virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
242 
244 
245  virtual uint64_t get_block_long_term_weight(const uint64_t& height) const;
246 
247  virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const;
248 
250 
251  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
252 
253  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
254 
255  virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const;
256 
257  virtual block get_top_block() const;
258 
259  virtual uint64_t height() const;
260 
261  virtual bool tx_exists(const crypto::hash& h) const;
262  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
263 
264  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
265 
266  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
267  virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
268  virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
269  virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const;
270 
271  virtual uint64_t get_tx_count() const;
272 
273  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
274 
275  virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
276 
277  virtual uint64_t get_num_outputs(const uint64_t& amount) const;
278 
279  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt) const;
280  virtual void get_output_key(const epee::span<const uint64_t> &amounts, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs, bool allow_partial = false) const;
281 
282  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
283  virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
284  std::vector<tx_out_index> &tx_out_indices) const;
285 
286  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
287  virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
288 
289  virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes) const;
290 
291  virtual bool has_key_image(const crypto::key_image& img) const;
292 
293  virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t& meta);
294  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
295  virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const;
296  virtual bool txpool_has_tx(const crypto::hash &txid) const;
297  virtual void remove_txpool_tx(const crypto::hash& txid);
298  virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
299  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
300  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
301  virtual uint32_t get_blockchain_pruning_seed() const;
302  virtual bool prune_blockchain(uint32_t pruning_seed = 0);
303  virtual bool update_pruning();
304  virtual bool check_pruning();
305 
306  virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob = false, bool include_unrelayed_txes = true) const;
307 
308  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
309  virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const;
310  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
311  virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)> f) const;
312  virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const;
313 
314  virtual uint64_t add_block( const std::pair<block, blobdata>& blk
315  , size_t block_weight
316  , uint64_t long_term_block_weight
317  , const difficulty_type& cumulative_difficulty
318  , const uint64_t& coins_generated
319  , const std::vector<std::pair<transaction, blobdata>>& txs
320  );
321 
322  virtual void set_batch_transactions(bool batch_transactions);
323  virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
324  virtual void batch_commit();
325  virtual void batch_stop();
326  virtual void batch_abort();
327 
328  virtual void block_wtxn_start();
329  virtual void block_wtxn_stop();
330  virtual void block_wtxn_abort();
331  virtual bool block_rtxn_start() const;
332  virtual void block_rtxn_stop() const;
333  virtual void block_rtxn_abort() const;
334 
335  bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
336 
337  virtual void pop_block(block& blk, std::vector<transaction>& txs);
338 
339  virtual bool can_thread_bulk_indices() const { return true; }
340 
351  std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> get_output_histogram(const std::vector<uint64_t> &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const;
352 
353  bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const;
354 
355  virtual std::vector<address_outputs> get_addr_output_all(const crypto::public_key& combined_key);
356  virtual std::vector<address_outputs> get_addr_output_batch(const crypto::public_key& combined_key, uint64_t start_db_index = 0, uint64_t batch_size = 100, bool desc = false);
357  virtual uint64_t get_balance(const crypto::public_key& combined_key);
358  virtual tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index);
359 
360  // helper functions
361  static int compare_uint64(const MDB_val *a, const MDB_val *b);
362  static int compare_hash32(const MDB_val *a, const MDB_val *b);
363  static int compare_string(const MDB_val *a, const MDB_val *b);
364  static int compare_data(const MDB_val *a, const MDB_val *b);
365  static int compare_publickey(const MDB_val *a, const MDB_val *b);
366 
367 private:
368  void do_resize(uint64_t size_increase=0);
369 
370  bool need_resize(uint64_t threshold_size=0) const;
371  void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes);
372  uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;
373 
374  virtual void add_block( const block& blk
375  , size_t block_weight
376  , uint64_t long_term_block_weight
377  , const difficulty_type& cumulative_difficulty
378  , const uint64_t& coins_generated
379  , uint64_t num_rct_outs
380  , const crypto::hash& block_hash
381  );
382 
383  virtual void remove_block();
384 
385  virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash);
386 
387  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
388 
389  virtual uint64_t add_output(const crypto::hash& tx_hash,
390  const tx_out& tx_output,
391  const uint64_t& local_index,
392  const uint64_t unlock_time,
393  const rct::key *commitment
394  );
395 
396  virtual void add_tx_amount_output_indices(const uint64_t tx_id,
397  const std::vector<uint64_t>& amount_output_indices
398  );
399 
400  void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
401 
402  void remove_output(const uint64_t amount, const uint64_t& out_index);
403 
404  virtual void prune_outputs(uint64_t amount);
405 
406  virtual void add_spent_key(const crypto::key_image& k_image);
407 
408  virtual void remove_spent_key(const crypto::key_image& k_image);
409 
410  uint64_t num_outputs() const;
411 
412  // Hard fork
413  virtual void set_hard_fork_version(uint64_t height, uint8_t version);
414  virtual uint8_t get_hard_fork_version(uint64_t height) const;
415  virtual void check_hard_fork_info();
416  virtual void drop_hard_fork_info();
417 
418  inline void check_open() const;
419 
420  bool prune_worker(int mode, uint32_t pruning_seed);
421 
422  virtual bool is_read_only() const;
423 
424  virtual uint64_t get_database_size() const;
425 
426  std::vector<uint64_t> get_block_info_64bit_fields(uint64_t start_height, size_t count, off_t offset) const;
427 
428  uint64_t get_max_block_size();
429  void add_max_block_size(uint64_t sz);
430 
431  // fix up anything that may be wrong due to past bugs
432  virtual void fixup();
433 
434  // Validator List
435  virtual void set_validator_list(std::string validators, uint32_t expiration_date);
436  virtual std::string get_validator_list() const;
437  blobdata validator_to_blob(const validator_db& v) const;
438  validator_db validator_from_blob(const blobdata blob) const;
439 
440  blobdata output_to_blob(const tx_out& output) const;
441  tx_out output_from_blob(const blobdata& blob) const;
442 
443  virtual void add_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key combined_key, uint64_t amount, uint64_t unlock_time, bool is_coinbase = false);
444  virtual bool check_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index);
445  virtual uint64_t get_utxo_unlock_time(const crypto::hash tx_hash, const uint32_t relative_out_index);
446  virtual void remove_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index);
447 
448  virtual void add_addr_output(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key& combined_key, uint64_t amount, uint64_t unlock_time);
449  virtual void remove_addr_output(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key& combined_key, uint64_t amount, uint64_t unlock_time);
450 
451  virtual void add_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::hash parent_tx_hash, const uint64_t in_index);
452  virtual void remove_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index);
453 
454  // migrate from older DB version to current
455  void migrate(const uint32_t oldversion);
456 
457  // migrate from DB version 0 to 1
458  void migrate_0_1();
459 
460  // migrate from DB version 1 to 2
461  void migrate_1_2();
462 
463  // migrate from DB version 2 to 3
464  void migrate_2_3();
465 
466  // migrate from DB version 3 to 4
467  void migrate_3_4();
468 
469  // migrate from DB version 4 to 5
470  void migrate_4_5();
471 
472  void cleanup_batch();
473 
474 private:
475  MDB_env* m_env;
476 
477  MDB_dbi m_blocks;
478  MDB_dbi m_block_heights;
479  MDB_dbi m_block_info;
480 
481  MDB_dbi m_txs;
482  MDB_dbi m_txs_pruned;
483  MDB_dbi m_txs_prunable;
484  MDB_dbi m_txs_prunable_hash;
485  MDB_dbi m_txs_prunable_tip;
486  MDB_dbi m_tx_indices;
487  MDB_dbi m_tx_outputs;
488 
489  MDB_dbi m_output_txs;
490  MDB_dbi m_output_amounts;
491 
492  MDB_dbi m_spent_keys;
493 
494  MDB_dbi m_txpool_meta;
495  MDB_dbi m_txpool_blob;
496 
497  MDB_dbi m_hf_starting_heights;
498  MDB_dbi m_hf_versions;
499 
500  MDB_dbi m_validators;
501  MDB_dbi m_utxos;
502  MDB_dbi m_addr_outputs;
503  MDB_dbi m_tx_inputs;
504 
505  MDB_dbi m_properties;
506 
507  mutable uint64_t m_cum_size; // used in batch size estimation
508  mutable unsigned int m_cum_count;
509  std::string m_folder;
510  mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
511  mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
512  boost::thread::id m_writer;
513 
514  bool m_batch_transactions; // support for batch transactions
515  bool m_batch_active; // whether batch transaction is in progress
516 
517  mdb_txn_cursors m_wcursors;
518  mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
519 
520 #if defined(__arm__)
521  // force a value so it can compile with 32-bit ARM
522  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
523 #else
524 #if defined(ENABLE_AUTO_RESIZE)
525  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
526 #else
527  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
528 #endif
529 #endif
530 
531  constexpr static float RESIZE_PERCENT = 0.9f;
532 };
533 
534 } // namespace cryptonote
The BlockchainDB backing store interface declaration/contract.
virtual bool update_pruning()
prunes recent blockchain changes as needed, iff pruning is enabled
Definition: db_lmdb.cpp:2731
virtual size_t get_block_weight(const uint64_t &height) const
fetch a block's weight
Definition: db_lmdb.cpp:2988
static int compare_string(const MDB_val *a, const MDB_val *b)
Definition: db_lmdb.cpp:165
virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &meta)
add a txpool transaction
Definition: db_lmdb.cpp:2215
virtual bool for_blocks_range(const uint64_t &h1, const uint64_t &h2, std::function< bool(uint64_t, const crypto::hash &, const cryptonote::block &)>) const
runs a function over a range of blocks
Definition: db_lmdb.cpp:3822
virtual uint32_t get_blockchain_pruning_seed() const
get the blockchain pruning seed
Definition: db_lmdb.cpp:2408
virtual void pop_block(block &blk, std::vector< transaction > &txs)
pops the top block off the blockchain
Definition: db_lmdb.cpp:4313
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const
fetch a block's cumulative number of rct outputs
Definition: db_lmdb.cpp:2911
virtual void batch_stop()
ends a batch transaction
Definition: db_lmdb.cpp:4091
virtual void open(const std::string &filename, const int mdb_flags=0)
open a db, or create it if necessary.
Definition: db_lmdb.cpp:1350
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t &index) const
gets an output's tx hash and index
Definition: db_lmdb.cpp:3693
virtual void set_batch_transactions(bool batch_transactions)
sets whether or not to batch transactions
Definition: db_lmdb.cpp:4143
virtual void reset()
Remove everything from the BlockchainDB.
Definition: db_lmdb.cpp:1643
virtual void unlock()
This function releases the BlockchainDB lock.
Definition: db_lmdb.cpp:1748
virtual block get_top_block() const
fetch the top block
Definition: db_lmdb.cpp:3317
virtual void block_rtxn_stop() const
Definition: db_lmdb.cpp:4192
virtual difficulty_type get_block_difficulty(const uint64_t &height) const
fetch a block's difficulty
Definition: db_lmdb.cpp:3188
virtual std::vector< std::vector< uint64_t > > get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes) const
gets output indices (amount-specific) for a transaction's outputs
Definition: db_lmdb.cpp:3729
virtual tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index)
Definition: db_lmdb.cpp:1944
static int compare_data(const MDB_val *a, const MDB_val *b)
Definition: db_lmdb.cpp:172
virtual uint64_t add_block(const std::pair< block, blobdata > &blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const std::vector< std::pair< transaction, blobdata >> &txs)
handles the addition of a new block to BlockchainDB
Definition: db_lmdb.cpp:4284
virtual block_header get_block_header(const crypto::hash &h) const
fetch a block header
Definition: db_lmdb.cpp:2853
virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes=true) const
get the number of transactions in the txpool
Definition: db_lmdb.cpp:2267
std::map< uint64_t, std::tuple< uint64_t, uint64_t, uint64_t > > get_output_histogram(const std::vector< uint64_t > &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const
return a histogram of outputs on the blockchain
Definition: db_lmdb.cpp:4451
virtual void batch_abort()
aborts a batch transaction
Definition: db_lmdb.cpp:4120
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const
get a txpool transaction's metadata
Definition: db_lmdb.cpp:2358
virtual bool has_key_image(const crypto::key_image &img) const
check if a key image is stored as spent
Definition: db_lmdb.cpp:3772
virtual crypto::hash top_block_hash(uint64_t *block_height=NULL) const
fetch the top block's hash
Definition: db_lmdb.cpp:3302
virtual void block_wtxn_abort()
Definition: db_lmdb.cpp:4261
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const
fetch a block's already generated coins
Definition: db_lmdb.cpp:3205
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const
fetch the last N blocks' long term weights
Definition: db_lmdb.cpp:3122
static int compare_publickey(const MDB_val *a, const MDB_val *b)
Definition: db_lmdb.cpp:188
virtual uint64_t get_tx_count() const
fetches the total number of transactions ever
Definition: db_lmdb.cpp:3581
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const
checks if a block exists
Definition: db_lmdb.cpp:2793
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
get a txpool transaction's blob
Definition: db_lmdb.cpp:2379
virtual bool get_pruned_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const
fetches the pruned transaction blob with the given hash
Definition: db_lmdb.cpp:3491
virtual uint64_t get_num_outputs(const uint64_t &amount) const
fetches the number of outputs of a given amount
Definition: db_lmdb.cpp:3635
virtual uint64_t get_block_height(const crypto::hash &h) const
gets the height of the block with a given hash
Definition: db_lmdb.cpp:2832
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const
fetch a list of blocks
Definition: db_lmdb.cpp:3274
virtual bool tx_exists(const crypto::hash &h) const
check if a transaction with a given hash exists
Definition: db_lmdb.cpp:3368
virtual bool block_rtxn_start() const
Definition: db_lmdb.cpp:4199
virtual void block_rtxn_abort() const
Definition: db_lmdb.cpp:4277
virtual std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const
fetch the last N blocks' weights
Definition: db_lmdb.cpp:3117
BlockchainLMDB(bool batch_transactions=true)
Definition: db_lmdb.cpp:1331
virtual std::vector< transaction > get_tx_list(const std::vector< crypto::hash > &hlist) const
fetches a list of transactions based on their hashes
Definition: db_lmdb.cpp:3598
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0)
tells the BlockchainDB to start a new "batch" of blocks
Definition: db_lmdb.cpp:4011
virtual void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff)
sets a block's cumulative difficulty
Definition: db_lmdb.cpp:3127
virtual void remove_txpool_tx(const crypto::hash &txid)
remove a txpool transaction
Definition: db_lmdb.cpp:2328
virtual bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>, bool pruned) const
runs a function over all transactions stored
Definition: db_lmdb.cpp:3873
virtual uint64_t get_block_long_term_weight(const uint64_t &height) const
fetch a block's long term weight
Definition: db_lmdb.cpp:3228
virtual std::vector< address_outputs > get_addr_output_all(const crypto::public_key &combined_key)
Definition: db_lmdb.cpp:2040
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const
fetch a block's cumulative difficulty
Definition: db_lmdb.cpp:3163
virtual bool txpool_has_tx(const crypto::hash &txid) const
check whether a txid is in the txpool
Definition: db_lmdb.cpp:2312
virtual bool check_pruning()
checks pruning was done correctly, iff enabled
Definition: db_lmdb.cpp:2736
virtual void batch_commit()
Definition: db_lmdb.cpp:4054
virtual uint64_t get_block_timestamp(const uint64_t &height) const
fetch a block's timestamp
Definition: db_lmdb.cpp:2888
virtual bool lock()
acquires the BlockchainDB lock
Definition: db_lmdb.cpp:1740
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const
fetches the transaction blob with the given hash
Definition: db_lmdb.cpp:3455
virtual bool get_prunable_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const
fetches the prunable transaction blob with the given hash
Definition: db_lmdb.cpp:3521
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index, bool include_commitmemt) const
get some of an output's data
Definition: db_lmdb.cpp:3659
virtual uint64_t get_top_block_timestamp() const
fetch the top block's timestamp
Definition: db_lmdb.cpp:2973
virtual void safesyncmode(const bool onoff)
toggle safe syncs for the DB
Definition: db_lmdb.cpp:1637
virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta)
update a txpool transaction's metadata
Definition: db_lmdb.cpp:2241
virtual bool get_prunable_tx_hash(const crypto::hash &tx_hash, crypto::hash &prunable_hash) const
fetches the prunable transaction hash
Definition: db_lmdb.cpp:3551
virtual std::string get_db_name() const
gets the name of the folder the BlockchainDB's file(s) should be in
Definition: db_lmdb.cpp:1732
virtual void block_wtxn_start()
Definition: db_lmdb.cpp:4206
virtual tx_out_index get_output_tx_and_index(const uint64_t &amount, const uint64_t &index) const
gets an output's tx hash and index
Definition: db_lmdb.cpp:3716
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const
fetches the block with the given hash
Definition: db_lmdb.cpp:2824
virtual void close()
close the BlockchainDB
Definition: db_lmdb.cpp:1605
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const
fetch a block blob by height
Definition: db_lmdb.cpp:2862
virtual uint64_t height() const
fetch the current blockchain height
Definition: db_lmdb.cpp:3332
virtual void block_wtxn_stop()
Definition: db_lmdb.cpp:4239
virtual bool for_all_outputs(std::function< bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)> f) const
runs a function over all outputs stored
Definition: db_lmdb.cpp:3935
virtual std::vector< crypto::hash > get_hashes_range(const uint64_t &h1, const uint64_t &h2) const
fetch a list of block hashes
Definition: db_lmdb.cpp:3288
virtual std::vector< address_outputs > get_addr_output_batch(const crypto::public_key &combined_key, uint64_t start_db_index=0, uint64_t batch_size=100, bool desc=false)
Definition: db_lmdb.cpp:2081
bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector< uint64_t > &distribution, uint64_t &base) const
Definition: db_lmdb.cpp:4544
virtual std::vector< std::string > get_filenames() const
get all files used by the BlockchainDB (if any)
Definition: db_lmdb.cpp:1701
virtual uint64_t get_balance(const crypto::public_key &combined_key)
Definition: db_lmdb.cpp:2141
virtual bool prune_blockchain(uint32_t pruning_seed=0)
prunes the blockchain
Definition: db_lmdb.cpp:2726
virtual bool can_thread_bulk_indices() const
Definition: db_lmdb.h:339
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const
fetch a block's hash
Definition: db_lmdb.cpp:3251
static int compare_hash32(const MDB_val *a, const MDB_val *b)
Definition: db_lmdb.cpp:151
virtual bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const
runs a function over all key images stored
Definition: db_lmdb.cpp:3789
virtual uint64_t get_tx_block_height(const crypto::hash &h) const
fetches the height of a transaction's block
Definition: db_lmdb.cpp:3612
static int compare_uint64(const MDB_val *a, const MDB_val *b)
Definition: db_lmdb.cpp:143
virtual void sync()
sync the BlockchainDB with disk
Definition: db_lmdb.cpp:1621
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const
fetch a transaction's unlock time/height
Definition: db_lmdb.cpp:3434
virtual bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata *)> f, bool include_blob=false, bool include_unrelayed_txes=true) const
runs a function over all txpool transactions
Definition: db_lmdb.cpp:2741
virtual bool remove_data_file(const std::string &folder) const
remove file(s) storing the database
Definition: db_lmdb.cpp:1717
Non-owning sequence of data. Does not deep copy.
Definition: span.h:57
std::string message("Message requiring signing")
struct MDB_env MDB_env
Opaque structure for a database environment.
Definition: lmdb.h:260
struct MDB_txn MDB_txn
Opaque structure for a transaction handle.
Definition: lmdb.h:267
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition: lmdb.h:270
struct MDB_cursor MDB_cursor
Opaque structure for navigating through a database.
Definition: lmdb.h:273
POD_CLASS public_key
Definition: crypto.h:76
POD_CLASS key_image
Definition: crypto.h:102
POD_CLASS hash
Definition: hash.h:50
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
struct cryptonote::mdb_threadinfo mdb_threadinfo
bool is_coinbase(const transaction &tx)
struct cryptonote::txindex txindex
std::string blobdata
Definition: blobdatatype.h:39
std::pair< crypto::hash, uint64_t > tx_out_index
struct cryptonote::mdb_rflags mdb_rflags
struct cryptonote::mdb_txn_cursors mdb_txn_cursors
mdb_size_t count(MDB_cursor *cur)
version
Supported socks variants.
Definition: socks.h:58
::std::string string
Definition: gtest-port.h:1097
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:286
mdb_txn_cursors m_ti_rcursors
Definition: db_lmdb.h:131
MDB_cursor * m_txc_tx_outputs
Definition: db_lmdb.h:64
MDB_cursor * m_txc_txs_prunable
Definition: db_lmdb.h:60
MDB_cursor * m_txc_hf_versions
Definition: db_lmdb.h:71
MDB_cursor * m_txc_tx_inputs
Definition: db_lmdb.h:77
MDB_cursor * m_txc_tx_indices
Definition: db_lmdb.h:63
MDB_cursor * m_txc_blocks
Definition: db_lmdb.h:51
MDB_cursor * m_txc_txpool_meta
Definition: db_lmdb.h:68
MDB_cursor * m_txc_txs
Definition: db_lmdb.h:58
MDB_cursor * m_txc_output_amounts
Definition: db_lmdb.h:56
MDB_cursor * m_txc_output_txs
Definition: db_lmdb.h:55
MDB_cursor * m_txc_txs_pruned
Definition: db_lmdb.h:59
MDB_cursor * m_txc_block_heights
Definition: db_lmdb.h:52
MDB_cursor * m_txc_utxos
Definition: db_lmdb.h:75
MDB_cursor * m_txc_block_info
Definition: db_lmdb.h:53
MDB_cursor * m_txc_txs_prunable_tip
Definition: db_lmdb.h:62
MDB_cursor * m_txc_properties
Definition: db_lmdb.h:73
MDB_cursor * m_txc_txpool_blob
Definition: db_lmdb.h:69
MDB_cursor * m_txc_addr_outputs
Definition: db_lmdb.h:76
MDB_cursor * m_txc_spent_keys
Definition: db_lmdb.h:66
MDB_cursor * m_txc_txs_prunable_hash
Definition: db_lmdb.h:61
MDB_cursor * m_txc_validators
Definition: db_lmdb.h:74
static void prevent_new_txns()
Definition: db_lmdb.cpp:480
mdb_threadinfo * m_tinfo
Definition: db_lmdb.h:167
static void allow_new_txns()
Definition: db_lmdb.cpp:490
uint64_t num_active_tx() const
Definition: db_lmdb.cpp:475
static void wait_no_active_txns()
Definition: db_lmdb.cpp:485
static std::atomic< uint64_t > num_active_txns
Definition: db_lmdb.h:171
mdb_txn_safe(const bool check=true)
Definition: db_lmdb.cpp:400
void commit(std::string message="")
Definition: db_lmdb.cpp:446
static std::atomic_flag creation_gate
Definition: db_lmdb.h:174
a struct containing output metadata
crypto::hash key
Definition: db_lmdb.h:45
tx_data_t data
Definition: db_lmdb.h:46
a struct containing txpool per transaction metadata