Monero
db_lmdb.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without modification, are
5 // permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this list of
8 // conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 // of conditions and the following disclaimer in the documentation and/or other
12 // materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its contributors may be
15 // used to endorse or promote products derived from this software without specific
16 // prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
19 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #pragma once
28 
29 #include <atomic>
30 
32 #include "cryptonote_basic/blobdatatype.h" // for type blobdata
33 #include "ringct/rctTypes.h"
34 #include <boost/thread/tss.hpp>
35 
36 #include <lmdb.h>
37 
38 #define ENABLE_AUTO_RESIZE
39 
40 namespace cryptonote
41 {
42 
43 typedef struct mdb_txn_cursors
44 {
45  MDB_cursor *m_txc_blocks;
46  MDB_cursor *m_txc_block_heights;
47  MDB_cursor *m_txc_block_info;
48 
49  MDB_cursor *m_txc_output_txs;
50  MDB_cursor *m_txc_output_amounts;
51 
52  MDB_cursor *m_txc_txs;
53  MDB_cursor *m_txc_txs_pruned;
54  MDB_cursor *m_txc_txs_prunable;
56  MDB_cursor *m_txc_tx_indices;
57  MDB_cursor *m_txc_tx_outputs;
58 
59  MDB_cursor *m_txc_spent_keys;
60 
61  MDB_cursor *m_txc_txpool_meta;
62  MDB_cursor *m_txc_txpool_blob;
63 
64  MDB_cursor *m_txc_hf_versions;
66 
67 #define m_cur_blocks m_cursors->m_txc_blocks
68 #define m_cur_block_heights m_cursors->m_txc_block_heights
69 #define m_cur_block_info m_cursors->m_txc_block_info
70 #define m_cur_output_txs m_cursors->m_txc_output_txs
71 #define m_cur_output_amounts m_cursors->m_txc_output_amounts
72 #define m_cur_txs m_cursors->m_txc_txs
73 #define m_cur_txs_pruned m_cursors->m_txc_txs_pruned
74 #define m_cur_txs_prunable m_cursors->m_txc_txs_prunable
75 #define m_cur_txs_prunable_hash m_cursors->m_txc_txs_prunable_hash
76 #define m_cur_tx_indices m_cursors->m_txc_tx_indices
77 #define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
78 #define m_cur_spent_keys m_cursors->m_txc_spent_keys
79 #define m_cur_txpool_meta m_cursors->m_txc_txpool_meta
80 #define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
81 #define m_cur_hf_versions m_cursors->m_txc_hf_versions
82 
83 typedef struct mdb_rflags
84 {
85  bool m_rf_txn;
91  bool m_rf_txs;
101 } mdb_rflags;
102 
103 typedef struct mdb_threadinfo
104 {
105  MDB_txn *m_ti_rtxn; // per-thread read txn
106  mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
107  mdb_rflags m_ti_rflags; // per-thread read state
108 
109  ~mdb_threadinfo();
111 
113 {
114  mdb_txn_safe(const bool check=true);
115  ~mdb_txn_safe();
116 
117  void commit(std::string message = "");
118 
119  // This should only be needed for batch transaction which must be ensured to
120  // be aborted before mdb_env_close, not after. So we can't rely on
121  // BlockchainLMDB destructor to call mdb_txn_safe destructor, as that's too late
122  // to properly abort, since mdb_env_close would have been called earlier.
123  void abort();
124  void uncheck();
125 
126  operator MDB_txn*()
127  {
128  return m_txn;
129  }
130 
131  operator MDB_txn**()
132  {
133  return &m_txn;
134  }
135 
136  uint64_t num_active_tx() const;
137 
138  static void prevent_new_txns();
139  static void wait_no_active_txns();
140  static void allow_new_txns();
141 
143  MDB_txn* m_txn;
144  bool m_batch_txn = false;
145  bool m_check;
146  static std::atomic<uint64_t> num_active_txns;
147 
148  // could use a mutex here, but this should be sufficient.
149  static std::atomic_flag creation_gate;
150 };
151 
152 
153 // If m_batch_active is set, a batch transaction exists beyond this class, such
154 // as a batch import with verification enabled, or possibly (later) a batch
155 // network sync.
156 //
157 // For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
158 // and get_tx(), when m_batch_active is set, the lookup uses the batch
159 // transaction. This isn't only because the transaction is available, but it's
160 // necessary so that lookups include the database updates only present in the
161 // current batch write.
162 //
163 // A regular network sync without batch writes is expected to open a new read
164 // transaction, as those lookups are part of the validation done prior to the
165 // write for block and tx data, so no write transaction is open at the time.
167 {
168 public:
169  BlockchainLMDB(bool batch_transactions=true);
170  ~BlockchainLMDB();
171 
172  virtual void open(const std::string& filename, const int mdb_flags=0);
173 
174  virtual void close();
175 
176  virtual void sync();
177 
178  virtual void safesyncmode(const bool onoff);
179 
180  virtual void reset();
181 
182  virtual std::vector<std::string> get_filenames() const;
183 
184  virtual bool remove_data_file(const std::string& folder) const;
185 
186  virtual std::string get_db_name() const;
187 
188  virtual bool lock();
189 
190  virtual void unlock();
191 
192  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
193 
194  virtual uint64_t get_block_height(const crypto::hash& h) const;
195 
196  virtual block_header get_block_header(const crypto::hash& h) const;
197 
198  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
199 
200  virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const;
201 
202  virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const;
203 
204  virtual uint64_t get_block_timestamp(const uint64_t& height) const;
205 
206  virtual uint64_t get_top_block_timestamp() const;
207 
208  virtual size_t get_block_weight(const uint64_t& height) const;
209 
210  virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;
211 
212  virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
213 
214  virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
215 
216  virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const;
217 
218  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
219 
220  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
221 
222  virtual crypto::hash top_block_hash() const;
223 
224  virtual block get_top_block() const;
225 
226  virtual uint64_t height() const;
227 
228  virtual bool tx_exists(const crypto::hash& h) const;
229  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
230 
231  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
232 
233  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
234  virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
235  virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const;
236 
237  virtual uint64_t get_tx_count() const;
238 
239  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
240 
241  virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
242 
243  virtual uint64_t get_num_outputs(const uint64_t& amount) const;
244 
245  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
246  virtual void get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs, bool allow_partial = false);
247 
248  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
249  virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
250  std::vector<tx_out_index> &tx_out_indices) const;
251 
252  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
253  virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
254 
255  virtual std::vector<uint64_t> get_tx_amount_output_indices(const uint64_t tx_id) const;
256 
257  virtual bool has_key_image(const crypto::key_image& img) const;
258 
259  virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t& meta);
260  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
261  virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const;
262  virtual bool txpool_has_tx(const crypto::hash &txid) const;
263  virtual void remove_txpool_tx(const crypto::hash& txid);
264  virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
265  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
266  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
267  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;
268 
269  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
270  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;
271  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
272  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;
273  virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const;
274 
275  virtual uint64_t add_block( const block& blk
276  , size_t block_weight
277  , const difficulty_type& cumulative_difficulty
278  , const uint64_t& coins_generated
279  , const std::vector<transaction>& txs
280  );
281 
282  virtual void set_batch_transactions(bool batch_transactions);
283  virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
284  virtual void batch_commit();
285  virtual void batch_stop();
286  virtual void batch_abort();
287 
288  virtual void block_txn_start(bool readonly);
289  virtual void block_txn_stop();
290  virtual void block_txn_abort();
291  virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
292  virtual void block_rtxn_stop() const;
293 
294  virtual void pop_block(block& blk, std::vector<transaction>& txs);
295 
296  virtual bool can_thread_bulk_indices() const { return true; }
297 
308  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;
309 
310  bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const;
311 
312 private:
313  void do_resize(uint64_t size_increase=0);
314 
315  bool need_resize(uint64_t threshold_size=0) const;
316  void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes);
317  uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;
318 
319  virtual void add_block( const block& blk
320  , size_t block_weight
321  , const difficulty_type& cumulative_difficulty
322  , const uint64_t& coins_generated
323  , uint64_t num_rct_outs
324  , const crypto::hash& block_hash
325  );
326 
327  virtual void remove_block();
328 
329  virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash);
330 
331  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
332 
333  virtual uint64_t add_output(const crypto::hash& tx_hash,
334  const tx_out& tx_output,
335  const uint64_t& local_index,
336  const uint64_t unlock_time,
337  const rct::key *commitment
338  );
339 
340  virtual void add_tx_amount_output_indices(const uint64_t tx_id,
341  const std::vector<uint64_t>& amount_output_indices
342  );
343 
344  void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
345 
346  void remove_output(const uint64_t amount, const uint64_t& out_index);
347 
348  virtual void add_spent_key(const crypto::key_image& k_image);
349 
350  virtual void remove_spent_key(const crypto::key_image& k_image);
351 
352  uint64_t num_outputs() const;
353 
354  // Hard fork
355  virtual void set_hard_fork_version(uint64_t height, uint8_t version);
356  virtual uint8_t get_hard_fork_version(uint64_t height) const;
357  virtual void check_hard_fork_info();
358  virtual void drop_hard_fork_info();
359 
367  blobdata output_to_blob(const tx_out& output) const;
368 
376  tx_out output_from_blob(const blobdata& blob) const;
377 
378  void check_open() const;
379 
380  virtual bool is_read_only() const;
381 
382  virtual uint64_t get_database_size() const;
383 
384  // fix up anything that may be wrong due to past bugs
385  virtual void fixup();
386 
387  // migrate from older DB version to current
388  void migrate(const uint32_t oldversion);
389 
390  // migrate from DB version 0 to 1
391  void migrate_0_1();
392 
393  // migrate from DB version 1 to 2
394  void migrate_1_2();
395 
396  // migrate from DB version 2 to 3
397  void migrate_2_3();
398 
399  void cleanup_batch();
400 
401 private:
402  MDB_env* m_env;
403 
404  MDB_dbi m_blocks;
406  MDB_dbi m_block_info;
407 
408  MDB_dbi m_txs;
409  MDB_dbi m_txs_pruned;
410  MDB_dbi m_txs_prunable;
412  MDB_dbi m_tx_indices;
413  MDB_dbi m_tx_outputs;
414 
415  MDB_dbi m_output_txs;
417 
418  MDB_dbi m_spent_keys;
419 
420  MDB_dbi m_txpool_meta;
421  MDB_dbi m_txpool_blob;
422 
424  MDB_dbi m_hf_versions;
425 
426  MDB_dbi m_properties;
427 
428  mutable uint64_t m_cum_size; // used in batch size estimation
429  mutable unsigned int m_cum_count;
430  std::string m_folder;
431  mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
432  mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
433  boost::thread::id m_writer;
434 
435  bool m_batch_transactions; // support for batch transactions
436  bool m_batch_active; // whether batch transaction is in progress
437 
439  mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
440 
441 #if defined(__arm__)
442  // force a value so it can compile with 32-bit ARM
443  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
444 #else
445 #if defined(ENABLE_AUTO_RESIZE)
446  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
447 #else
448  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
449 #endif
450 #endif
451 
452  constexpr static float RESIZE_PERCENT = 0.9f;
453 };
454 
455 } // namespace cryptonote
virtual void batch_stop()
ends a batch transaction
Definition: db_lmdb.cpp:2898
bool m_rf_txn
Definition: db_lmdb.h:85
virtual uint64_t get_top_block_timestamp() const
fetch the top block&#39;s timestamp
Definition: db_lmdb.cpp:2004
MDB_cursor * m_txc_txpool_blob
Definition: db_lmdb.h:62
virtual void unlock()
This function releases the BlockchainDB lock.
Definition: db_lmdb.cpp:1533
MDB_dbi m_txs_pruned
Definition: db_lmdb.h:409
void do_resize(uint64_t size_increase=0)
Definition: db_lmdb.cpp:457
virtual difficulty_type get_block_difficulty(const uint64_t &height) const
fetch a block&#39;s difficulty
Definition: db_lmdb.cpp:2065
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:2596
virtual uint64_t get_num_outputs(const uint64_t &amount) const
fetches the number of outputs of a given amount
Definition: db_lmdb.cpp:2449
mdb_threadinfo * m_tinfo
Definition: db_lmdb.h:142
boost::thread_specific_ptr< mdb_threadinfo > m_tinfo
Definition: db_lmdb.h:439
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition: blockchain_db.cpp:117
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:3357
MDB_dbi m_txpool_meta
Definition: db_lmdb.h:420
MDB_dbi m_txpool_blob
Definition: db_lmdb.h:421
static constexpr float RESIZE_PERCENT
Definition: db_lmdb.h:452
bool need_resize(uint64_t threshold_size=0) const
Definition: db_lmdb.cpp:526
void migrate_2_3()
Definition: db_lmdb.cpp:4183
virtual uint8_t get_hard_fork_version(uint64_t height) const
checks which hardfork version a height is on
Definition: db_lmdb.cpp:3444
virtual void set_batch_transactions(bool batch_transactions)
sets whether or not to batch transactions
Definition: db_lmdb.cpp:2950
bool m_rf_hf_versions
Definition: db_lmdb.h:100
virtual void batch_commit()
Definition: db_lmdb.cpp:2861
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:3264
mdb_txn_cursors m_wcursors
Definition: db_lmdb.h:438
uint64_t num_outputs() const
Definition: db_lmdb.cpp:2198
MDB_dbi m_tx_outputs
Definition: db_lmdb.h:413
virtual uint64_t add_transaction_data(const crypto::hash &blk_hash, const transaction &tx, const crypto::hash &tx_hash, const crypto::hash &tx_prunable_hash)
store the transaction and its metadata
Definition: db_lmdb.cpp:792
blobdata output_to_blob(const tx_out &output) const
convert a tx output to a blob for storage
Definition: db_lmdb.cpp:1112
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index)
get some of an output&#39;s data
Definition: db_lmdb.cpp:2473
bool m_rf_output_amounts
Definition: db_lmdb.h:90
bool m_rf_block_heights
Definition: db_lmdb.h:87
MDB_txn * m_ti_rtxn
Definition: db_lmdb.h:105
virtual uint64_t get_tx_block_height(const crypto::hash &h) const
fetches the height of a transaction&#39;s block
Definition: db_lmdb.cpp:2426
virtual std::vector< std::string > get_filenames() const
get all files used by the BlockchainDB (if any)
Definition: db_lmdb.cpp:1486
MDB_cursor * m_txc_txs
Definition: db_lmdb.h:52
virtual uint64_t add_block(const block &blk, size_t block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const std::vector< transaction > &txs)
handles the addition of a new block to BlockchainDB
Definition: db_lmdb.cpp:3098
MDB_dbi m_txs_prunable
Definition: db_lmdb.h:410
Definition: cryptonote_basic.h:366
virtual void fixup()
fix up anything that may be wrong due to past bugs
Definition: db_lmdb.cpp:3486
MDB_dbi m_txs
Definition: db_lmdb.h:408
virtual void block_txn_abort()
Definition: db_lmdb.cpp:3071
MDB_cursor * m_txc_output_amounts
Definition: db_lmdb.h:50
virtual void safesyncmode(const bool onoff)
toggle safe syncs for the DB
Definition: db_lmdb.cpp:1432
MDB_txn * m_txn
Definition: db_lmdb.h:143
MDB_cursor * m_txc_hf_versions
Definition: db_lmdb.h:64
Definition: cryptonote_basic.h:349
virtual size_t get_block_weight(const uint64_t &height) const
fetch a block&#39;s weight
Definition: db_lmdb.cpp:2019
mdb_rflags m_ti_rflags
Definition: db_lmdb.h:107
virtual tx_out_index get_output_tx_and_index(const uint64_t &amount, const uint64_t &index) const
gets an output&#39;s tx hash and index
Definition: db_lmdb.cpp:2528
virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta)
update a txpool transaction&#39;s metadata
Definition: db_lmdb.cpp:1619
boost::thread::id m_writer
Definition: db_lmdb.h:433
virtual uint64_t height() const
fetch the current blockchain height
Definition: db_lmdb.cpp:2184
tx_out output_from_blob(const blobdata &blob) const
convert a tx output blob to a tx output
Definition: db_lmdb.cpp:1121
virtual std::vector< uint64_t > get_tx_amount_output_indices(const uint64_t tx_id) const
gets output indices (amount-specific) for a transaction&#39;s outputs
Definition: db_lmdb.cpp:2541
MDB_cursor * m_txc_block_info
Definition: db_lmdb.h:47
virtual block get_top_block() const
fetch the top block
Definition: db_lmdb.cpp:2169
virtual std::string get_db_name() const
gets the name of the folder the BlockchainDB&#39;s file(s) should be in
Definition: db_lmdb.cpp:1517
void check_open() const
Definition: db_lmdb.cpp:1135
struct cryptonote::mdb_rflags mdb_rflags
virtual bool is_read_only() const
is BlockchainDB in read-only mode?
Definition: db_lmdb.cpp:3463
std::string m_folder
Definition: db_lmdb.h:430
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)
remove data about a transaction
Definition: db_lmdb.cpp:865
bool m_rf_blocks
Definition: db_lmdb.h:86
MDB_dbi m_output_amounts
Definition: db_lmdb.h:416
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:2142
bool m_rf_block_info
Definition: db_lmdb.h:88
mdb_txn_safe(const bool check=true)
Definition: db_lmdb.cpp:317
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:1877
MDB_cursor * m_txc_txs_prunable_hash
Definition: db_lmdb.h:55
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
get a txpool transaction&#39;s blob
Definition: db_lmdb.cpp:1757
MDB_cursor * m_txc_txpool_meta
Definition: db_lmdb.h:61
virtual void check_hard_fork_info()
verify hard fork info in database
Definition: db_lmdb.cpp:3404
void abort()
Definition: db_lmdb.cpp:378
bool m_batch_transactions
Definition: db_lmdb.h:435
virtual void block_txn_start(bool readonly)
Definition: db_lmdb.cpp:3006
MDB_dbi m_tx_indices
Definition: db_lmdb.h:412
MDB_cursor * m_txc_spent_keys
Definition: db_lmdb.h:59
BlockchainLMDB(bool batch_transactions=true)
Definition: db_lmdb.cpp:1153
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:2412
MDB_cursor * m_txc_txs_prunable
Definition: db_lmdb.h:54
MDB_dbi m_txs_prunable_hash
Definition: db_lmdb.h:411
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
mdb_txn_safe * m_write_txn
Definition: db_lmdb.h:431
virtual void set_hard_fork_version(uint64_t height, uint8_t version)
sets which hardfork version a height is on
Definition: db_lmdb.cpp:3425
void remove_output(const uint64_t amount, const uint64_t &out_index)
Definition: db_lmdb.cpp:1037
static std::atomic_flag creation_gate
Definition: db_lmdb.h:149
virtual void reset()
Remove everything from the BlockchainDB.
Definition: db_lmdb.cpp:1438
mdb_txn_safe * m_write_batch_txn
Definition: db_lmdb.h:432
void commit(std::string message="")
Definition: db_lmdb.cpp:363
MDB_dbi m_blocks
Definition: db_lmdb.h:404
MDB_dbi m_block_info
Definition: db_lmdb.h:406
MDB_dbi m_output_txs
Definition: db_lmdb.h:415
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:1786
bool m_batch_active
Definition: db_lmdb.h:436
MDB_cursor * m_txc_tx_outputs
Definition: db_lmdb.h:57
bool m_rf_txs_pruned
Definition: db_lmdb.h:92
uint64_t m_cum_size
Definition: db_lmdb.h:428
virtual block_header get_block_header(const crypto::hash &h) const
fetch a block header
Definition: db_lmdb.cpp:1898
virtual void remove_txpool_tx(const crypto::hash &txid)
remove a txpool transaction
Definition: db_lmdb.cpp:1706
~BlockchainLMDB()
Definition: db_lmdb.cpp:1142
virtual bool tx_exists(const crypto::hash &h) const
check if a transaction with a given hash exists
Definition: db_lmdb.cpp:2212
Definition: db_lmdb.h:83
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t &index) const
gets an output&#39;s tx hash and index
Definition: db_lmdb.cpp:2505
Definition: rctTypes.h:78
static void wait_no_active_txns()
Definition: db_lmdb.cpp:402
Definition: db_lmdb.h:112
~mdb_threadinfo()
Definition: db_lmdb.cpp:306
MDB_dbi m_spent_keys
Definition: db_lmdb.h:418
virtual bool has_key_image(const crypto::key_image &img) const
check if a key image is stored as spent
Definition: db_lmdb.cpp:2579
void uncheck()
Definition: db_lmdb.cpp:357
bool m_rf_txpool_meta
Definition: db_lmdb.h:98
~mdb_txn_safe()
Definition: db_lmdb.cpp:327
void cleanup_batch()
Definition: db_lmdb.cpp:2888
virtual void block_rtxn_stop() const
Definition: db_lmdb.cpp:2999
static constexpr uint64_t DEFAULT_MAPSIZE
Definition: db_lmdb.h:446
virtual void remove_spent_key(const crypto::key_image &k_image)
remove a spent key
Definition: db_lmdb.cpp:1092
bool m_rf_txs
Definition: db_lmdb.h:91
virtual void sync()
sync the BlockchainDB with disk
Definition: db_lmdb.cpp:1416
virtual uint64_t get_database_size() const
get disk space requirements
Definition: db_lmdb.cpp:3476
mdb_txn_cursors m_ti_rcursors
Definition: db_lmdb.h:106
void migrate(const uint32_t oldversion)
Definition: db_lmdb.cpp:4318
virtual uint64_t get_tx_count() const
fetches the total number of transactions ever
Definition: db_lmdb.cpp:2395
virtual void close()
close the BlockchainDB
Definition: db_lmdb.cpp:1400
bool m_check
Definition: db_lmdb.h:145
uint8_t version
Definition: blockchain.cpp:87
bool m_batch_txn
Definition: db_lmdb.h:144
std::string blobdata
Definition: blobdatatype.h:35
Definition: db_lmdb.h:43
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:2680
virtual crypto::hash top_block_hash() const
fetch the top block&#39;s hash
Definition: db_lmdb.cpp:2156
Definition: base.py:1
virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const
Definition: db_lmdb.cpp:2962
bool m_rf_txs_prunable_hash
Definition: db_lmdb.h:94
virtual void drop_hard_fork_info()
delete hard fork info from database
Definition: db_lmdb.cpp:3408
void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes)
Definition: db_lmdb.cpp:573
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const
checks if a block exists
Definition: db_lmdb.cpp:1838
std::uint64_t difficulty_type
Definition: difficulty.h:40
virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector< uint64_t > &amount_output_indices)
store amount output indices for a tx&#39;s outputs
Definition: db_lmdb.cpp:992
void remove_tx_outputs(const uint64_t tx_id, const transaction &tx)
Definition: db_lmdb.cpp:1015
MDB_dbi m_hf_versions
Definition: db_lmdb.h:424
void migrate_1_2()
Definition: db_lmdb.cpp:4041
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const
fetch a block&#39;s already generated coins
Definition: db_lmdb.cpp:2082
POD_CLASS key_image
Definition: crypto.h:89
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const
fetches the block with the given hash
Definition: db_lmdb.cpp:1869
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:136
Definition: db_lmdb.h:103
virtual uint64_t add_output(const crypto::hash &tx_hash, const tx_out &tx_output, const uint64_t &local_index, const uint64_t unlock_time, const rct::key *commitment)
store an output
Definition: db_lmdb.cpp:926
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const
fetch a block&#39;s cumulative number of rct outputs
Definition: db_lmdb.cpp:1956
virtual void open(const std::string &filename, const int mdb_flags=0)
open a db, or create it if necessary.
Definition: db_lmdb.cpp:1172
bool m_rf_output_txs
Definition: db_lmdb.h:89
MDB_cursor * m_txc_txs_pruned
Definition: db_lmdb.h:53
virtual void add_spent_key(const crypto::key_image &k_image)
store a spent key
Definition: db_lmdb.cpp:1075
MDB_dbi m_block_heights
Definition: db_lmdb.h:405
virtual void batch_abort()
Definition: db_lmdb.cpp:2927
virtual uint64_t get_block_timestamp(const uint64_t &height) const
fetch a block&#39;s timestamp
Definition: db_lmdb.cpp:1933
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:343
bool m_rf_txpool_blob
Definition: db_lmdb.h:99
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const
get a txpool transaction&#39;s metadata
Definition: db_lmdb.cpp:1736
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:2629
virtual bool remove_data_file(const std::string &folder) const
remove file(s) storing the database
Definition: db_lmdb.cpp:1502
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const
fetch a block&#39;s hash
Definition: db_lmdb.cpp:2105
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const
fetch a block blob by height
Definition: db_lmdb.cpp:1907
POD_CLASS hash
Definition: hash.h:49
bool m_rf_tx_indices
Definition: db_lmdb.h:95
uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const
Definition: db_lmdb.cpp:605
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:2335
std::pair< crypto::hash, uint64_t > tx_out_index
Definition: blockchain_db.h:104
virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t &meta)
add a txpool transaction
Definition: db_lmdb.cpp:1591
virtual bool can_thread_bulk_indices() const
Definition: db_lmdb.h:296
uint64_t num_active_tx() const
Definition: db_lmdb.cpp:392
MDB_env * m_env
Definition: db_lmdb.h:402
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const
fetch a transaction&#39;s unlock time/height
Definition: db_lmdb.cpp:2278
Definition: cryptonote_basic.h:143
bool m_rf_tx_outputs
Definition: db_lmdb.h:96
bool m_rf_txs_prunable
Definition: db_lmdb.h:93
struct cryptonote::mdb_txn_cursors mdb_txn_cursors
static void prevent_new_txns()
Definition: db_lmdb.cpp:397
virtual void block_txn_stop()
Definition: db_lmdb.cpp:3047
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:2818
MDB_cursor * m_txc_tx_indices
Definition: db_lmdb.h:56
virtual bool txpool_has_tx(const crypto::hash &txid) const
check whether a txid is in the txpool
Definition: db_lmdb.cpp:1690
virtual void remove_block()
remove data about the top block
Definition: db_lmdb.cpp:755
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:2128
MDB_cursor * m_txc_block_heights
Definition: db_lmdb.h:46
void migrate_0_1()
Definition: db_lmdb.cpp:3507
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:2742
virtual bool lock()
acquires the BlockchainDB lock
Definition: db_lmdb.cpp:1525
a struct containing output metadata
Definition: blockchain_db.h:115
struct cryptonote::mdb_threadinfo mdb_threadinfo
bool m_rf_spent_keys
Definition: db_lmdb.h:97
Definition: cryptonote_basic.h:182
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:2365
Definition: db_lmdb.h:166
MDB_cursor * m_txc_blocks
Definition: db_lmdb.h:45
MDB_cursor * m_txc_output_txs
Definition: db_lmdb.h:49
unsigned int m_cum_count
Definition: db_lmdb.h:429
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const
fetch a block&#39;s cumulative difficulty
Definition: db_lmdb.cpp:2042
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:1645
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:2299
MDB_dbi m_properties
Definition: db_lmdb.h:426
static std::atomic< uint64_t > num_active_txns
Definition: db_lmdb.h:146
static void allow_new_txns()
Definition: db_lmdb.cpp:407
MDB_dbi m_hf_starting_heights
Definition: db_lmdb.h:423