Electroneum
db_lmdb.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, 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_protocol/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 mdb_txn_cursors
45 {
46  MDB_cursor *m_txc_blocks;
47  MDB_cursor *m_txc_block_heights;
48  MDB_cursor *m_txc_block_info;
49 
50  MDB_cursor *m_txc_output_txs;
51  MDB_cursor *m_txc_output_amounts;
52 
53  MDB_cursor *m_txc_txs;
54  MDB_cursor *m_txc_tx_indices;
55  MDB_cursor *m_txc_tx_outputs;
56 
57  MDB_cursor *m_txc_spent_keys;
58 
59  MDB_cursor *m_txc_txpool_meta;
60  MDB_cursor *m_txc_txpool_blob;
61 
62  MDB_cursor *m_txc_hf_versions;
64 
65 #define m_cur_blocks m_cursors->m_txc_blocks
66 #define m_cur_block_heights m_cursors->m_txc_block_heights
67 #define m_cur_block_info m_cursors->m_txc_block_info
68 #define m_cur_output_txs m_cursors->m_txc_output_txs
69 #define m_cur_output_amounts m_cursors->m_txc_output_amounts
70 #define m_cur_txs m_cursors->m_txc_txs
71 #define m_cur_tx_indices m_cursors->m_txc_tx_indices
72 #define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
73 #define m_cur_spent_keys m_cursors->m_txc_spent_keys
74 #define m_cur_txpool_meta m_cursors->m_txc_txpool_meta
75 #define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
76 #define m_cur_hf_versions m_cursors->m_txc_hf_versions
77 
78 typedef struct mdb_rflags
79 {
80  bool m_rf_txn;
86  bool m_rf_txs;
93 } mdb_rflags;
94 
95 typedef struct mdb_threadinfo
96 {
97  MDB_txn *m_ti_rtxn; // per-thread read txn
98  mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
99  mdb_rflags m_ti_rflags; // per-thread read state
100 
101  ~mdb_threadinfo();
103 
105 {
106  mdb_txn_safe(const bool check=true);
107  ~mdb_txn_safe();
108 
109  void commit(std::string message = "");
110 
111  // This should only be needed for batch transaction which must be ensured to
112  // be aborted before mdb_env_close, not after. So we can't rely on
113  // BlockchainLMDB destructor to call mdb_txn_safe destructor, as that's too late
114  // to properly abort, since mdb_env_close would have been called earlier.
115  void abort();
116 
117  operator MDB_txn*()
118  {
119  return m_txn;
120  }
121 
122  operator MDB_txn**()
123  {
124  return &m_txn;
125  }
126 
127  uint64_t num_active_tx() const;
128 
129  static void prevent_new_txns();
130  static void wait_no_active_txns();
131  static void allow_new_txns();
132 
134  MDB_txn* m_txn;
135  bool m_batch_txn = false;
136  bool m_check;
137  static std::atomic<uint64_t> num_active_txns;
138 
139  // could use a mutex here, but this should be sufficient.
140  static std::atomic_flag creation_gate;
141 };
142 
143 
144 // If m_batch_active is set, a batch transaction exists beyond this class, such
145 // as a batch import with verification enabled, or possibly (later) a batch
146 // network sync.
147 //
148 // For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
149 // and get_tx(), when m_batch_active is set, the lookup uses the batch
150 // transaction. This isn't only because the transaction is available, but it's
151 // necessary so that lookups include the database updates only present in the
152 // current batch write.
153 //
154 // A regular network sync without batch writes is expected to open a new read
155 // transaction, as those lookups are part of the validation done prior to the
156 // write for block and tx data, so no write transaction is open at the time.
158 {
159 public:
160  BlockchainLMDB(bool batch_transactions=false);
161  ~BlockchainLMDB();
162 
163  virtual void open(const std::string& filename, const int mdb_flags=0);
164 
165  virtual void close();
166 
167  virtual void sync();
168 
169  virtual void safesyncmode(const bool onoff);
170 
171  virtual void reset();
172 
173  virtual std::vector<std::string> get_filenames() const;
174 
175  virtual std::string get_db_name() const;
176 
177  virtual bool lock();
178 
179  virtual void unlock();
180 
181  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
182 
183  virtual uint64_t get_block_height(const crypto::hash& h) const;
184 
185  virtual block_header get_block_header(const crypto::hash& h) const;
186 
187  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
188 
189  virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const;
190 
191  virtual uint64_t get_block_timestamp(const uint64_t& height) const;
192 
193  virtual uint64_t get_top_block_timestamp() const;
194 
195  virtual size_t get_block_size(const uint64_t& height) const;
196 
197  virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;
198 
199  virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
200 
201  virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
202 
203  virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const;
204 
205  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
206 
207  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
208 
209  virtual crypto::hash top_block_hash() const;
210 
211  virtual block get_top_block() const;
212 
213  virtual uint64_t height() const;
214 
215  virtual bool tx_exists(const crypto::hash& h) const;
216  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
217 
218  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
219 
220  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
221 
222  virtual uint64_t get_tx_count() const;
223 
224  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
225 
226  virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
227 
228  virtual uint64_t get_num_outputs(const uint64_t& amount) const;
229 
230  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
231  virtual output_data_t get_output_key(const uint64_t& global_index) const;
232  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);
233 
234  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
235  virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
236  std::vector<tx_out_index> &tx_out_indices) const;
237 
238  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
239  virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
240 
241  virtual std::vector<uint64_t> get_tx_amount_output_indices(const uint64_t tx_id) const;
242 
243  virtual bool has_key_image(const crypto::key_image& img) const;
244 
245  virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t& meta);
246  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
247  virtual uint64_t get_txpool_tx_count() const;
248  virtual bool txpool_has_tx(const crypto::hash &txid) const;
249  virtual void remove_txpool_tx(const crypto::hash& txid);
250  virtual txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash& txid) const;
251  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
252  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
253  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) const;
254 
255  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
256  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;
257  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
258  virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const;
259 
260  virtual uint64_t add_block( const block& blk
261  , const size_t& block_size
262  , const difficulty_type& cumulative_difficulty
263  , const uint64_t& coins_generated
264  , const std::vector<transaction>& txs
265  );
266 
267  virtual void set_batch_transactions(bool batch_transactions);
268  virtual bool batch_start(uint64_t batch_num_blocks=0);
269  virtual void batch_commit();
270  virtual void batch_stop();
271  virtual void batch_abort();
272 
273  virtual void block_txn_start(bool readonly);
274  virtual void block_txn_stop();
275  virtual void block_txn_abort();
276  virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
277  virtual void block_rtxn_stop() const;
278 
279  virtual void pop_block(block& blk, std::vector<transaction>& txs);
280 
281  virtual bool can_thread_bulk_indices() const { return true; }
282 
292  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) const;
293 
294 private:
295  void do_resize(uint64_t size_increase=0);
296 
297  bool need_resize(uint64_t threshold_size=0) const;
298  void check_and_resize_for_batch(uint64_t batch_num_blocks);
299  uint64_t get_estimated_batch_size(uint64_t batch_num_blocks) const;
300 
301  virtual void add_block( const block& blk
302  , const size_t& block_size
303  , const difficulty_type& cumulative_difficulty
304  , const uint64_t& coins_generated
305  , const crypto::hash& block_hash
306  );
307 
308  virtual void remove_block();
309 
310  virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash);
311 
312  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
313 
314  virtual uint64_t add_output(const crypto::hash& tx_hash,
315  const tx_out& tx_output,
316  const uint64_t& local_index,
317  const uint64_t unlock_time,
318  const rct::key *commitment
319  );
320 
321  virtual void add_tx_amount_output_indices(const uint64_t tx_id,
322  const std::vector<uint64_t>& amount_output_indices
323  );
324 
325  void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
326 
327  void remove_output(const uint64_t amount, const uint64_t& out_index);
328 
329  virtual void add_spent_key(const crypto::key_image& k_image);
330 
331  virtual void remove_spent_key(const crypto::key_image& k_image);
332 
333  uint64_t num_outputs() const;
334 
335  // Hard fork
336  virtual void set_hard_fork_version(uint64_t height, uint8_t version);
337  virtual uint8_t get_hard_fork_version(uint64_t height) const;
338  virtual void check_hard_fork_info();
339  virtual void drop_hard_fork_info();
340 
348  blobdata output_to_blob(const tx_out& output) const;
349 
357  tx_out output_from_blob(const blobdata& blob) const;
358 
359  void check_open() const;
360 
361  virtual bool is_read_only() const;
362 
363  // migrate from older DB version to current
364  void migrate(const uint32_t oldversion);
365 
366  // migrate from DB version 0 to 1
367  void migrate_0_1();
368 
369  void cleanup_batch();
370 
371 private:
372  MDB_env* m_env;
373 
374  MDB_dbi m_blocks;
376  MDB_dbi m_block_info;
377 
378  MDB_dbi m_txs;
379  MDB_dbi m_tx_indices;
380  MDB_dbi m_tx_outputs;
381 
382  MDB_dbi m_output_txs;
384 
385  MDB_dbi m_spent_keys;
386 
387  MDB_dbi m_txpool_meta;
388  MDB_dbi m_txpool_blob;
389 
391  MDB_dbi m_hf_versions;
392 
393  MDB_dbi m_properties;
394 
395  mutable uint64_t m_cum_size; // used in batch size estimation
396  mutable unsigned int m_cum_count;
397  std::string m_folder;
398  mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
399  mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
400  boost::thread::id m_writer;
401 
402  bool m_batch_transactions; // support for batch transactions
403  bool m_batch_active; // whether batch transaction is in progress
404 
406  mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
407 
408 #if defined(__arm__)
409  // force a value so it can compile with 32-bit ARM
410  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
411 #else
412 #if defined(ENABLE_AUTO_RESIZE)
413  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
414 #else
415  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
416 #endif
417 #endif
418 
419  constexpr static float RESIZE_PERCENT = 0.8f;
420 };
421 
422 } // namespace cryptonote
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) const
return a histogram of outputs on the blockchain
Definition: db_lmdb.cpp:2992
virtual void batch_stop()
ends a batch transaction
Definition: db_lmdb.cpp:2618
bool m_rf_txn
Definition: db_lmdb.h:80
virtual uint64_t get_top_block_timestamp() const
fetch the top block's timestamp
Definition: db_lmdb.cpp:1792
MDB_cursor * m_txc_txpool_blob
Definition: db_lmdb.h:60
virtual void unlock()
This function releases the BlockchainDB lock.
Definition: db_lmdb.cpp:1403
void do_resize(uint64_t size_increase=0)
Definition: db_lmdb.cpp:433
virtual difficulty_type get_block_difficulty(const uint64_t &height) const
fetch a block's difficulty
Definition: db_lmdb.cpp:1853
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:2376
virtual uint64_t get_num_outputs(const uint64_t &amount) const
fetches the number of outputs of a given amount
Definition: db_lmdb.cpp:2179
mdb_threadinfo * m_tinfo
Definition: db_lmdb.h:133
boost::thread_specific_ptr< mdb_threadinfo > m_tinfo
Definition: db_lmdb.h:406
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition: blockchain_db.cpp:93
MDB_dbi m_txpool_meta
Definition: db_lmdb.h:387
MDB_dbi m_txpool_blob
Definition: db_lmdb.h:388
static constexpr float RESIZE_PERCENT
Definition: db_lmdb.h:419
bool need_resize(uint64_t threshold_size=0) const
Definition: db_lmdb.cpp:501
virtual uint8_t get_hard_fork_version(uint64_t height) const
checks which hardfork version a height is on
Definition: db_lmdb.cpp:3118
virtual void set_batch_transactions(bool batch_transactions)
sets whether or not to batch transactions
Definition: db_lmdb.cpp:2670
bool m_rf_hf_versions
Definition: db_lmdb.h:92
virtual void batch_commit()
Definition: db_lmdb.cpp:2581
mdb_txn_cursors m_wcursors
Definition: db_lmdb.h:405
uint64_t num_outputs() const
Definition: db_lmdb.cpp:1986
MDB_dbi m_tx_outputs
Definition: db_lmdb.h:380
blobdata output_to_blob(const tx_out &output) const
convert a tx output to a blob for storage
Definition: db_lmdb.cpp:1028
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index)
get some of an output's data
Definition: db_lmdb.cpp:2253
virtual bool for_all_outputs(std::function< bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const
runs a function over all outputs stored
Definition: db_lmdb.cpp:2508
bool m_rf_output_amounts
Definition: db_lmdb.h:85
bool m_rf_block_heights
Definition: db_lmdb.h:82
MDB_txn * m_ti_rtxn
Definition: db_lmdb.h:97
virtual uint64_t get_tx_block_height(const crypto::hash &h) const
fetches the height of a transaction's block
Definition: db_lmdb.cpp:2156
virtual uint64_t add_block(const block &blk, const size_t &block_size, 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:2826
virtual std::vector< std::string > get_filenames() const
get all files used by the BlockchainDB (if any)
Definition: db_lmdb.cpp:1371
MDB_cursor * m_txc_txs
Definition: db_lmdb.h:53
Definition: cryptonote_basic.h:372
MDB_dbi m_txs
Definition: db_lmdb.h:378
virtual void block_txn_abort()
Definition: db_lmdb.cpp:2799
MDB_cursor * m_txc_output_amounts
Definition: db_lmdb.h:51
virtual void safesyncmode(const bool onoff)
toggle safe syncs for the DB
Definition: db_lmdb.cpp:1322
MDB_txn * m_txn
Definition: db_lmdb.h:134
MDB_cursor * m_txc_hf_versions
Definition: db_lmdb.h:62
Definition: cryptonote_basic.h:355
mdb_rflags m_ti_rflags
Definition: db_lmdb.h:99
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:2308
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:1488
boost::thread::id m_writer
Definition: db_lmdb.h:400
virtual uint64_t height() const
fetch the current blockchain height
Definition: db_lmdb.cpp:1972
virtual bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>) const
runs a function over all transactions stored
Definition: db_lmdb.cpp:2460
tx_out output_from_blob(const blobdata &blob) const
convert a tx output blob to a tx output
Definition: db_lmdb.cpp:1037
virtual std::vector< uint64_t > get_tx_amount_output_indices(const uint64_t tx_id) const
gets output indices (amount-specific) for a transaction's outputs
Definition: db_lmdb.cpp:2321
MDB_cursor * m_txc_block_info
Definition: db_lmdb.h:48
virtual block get_top_block() const
fetch the top block
Definition: db_lmdb.cpp:1957
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:1387
void check_open() const
Definition: db_lmdb.cpp:1051
struct cryptonote::mdb_rflags mdb_rflags
virtual bool is_read_only() const
is BlockchainDB in read-only mode?
Definition: db_lmdb.cpp:3137
std::string m_folder
Definition: db_lmdb.h:397
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)
remove data about a transaction
Definition: db_lmdb.cpp:798
bool m_rf_blocks
Definition: db_lmdb.h:81
MDB_dbi m_output_amounts
Definition: db_lmdb.h:383
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:1930
void check_and_resize_for_batch(uint64_t batch_num_blocks)
Definition: db_lmdb.cpp:552
bool m_rf_block_info
Definition: db_lmdb.h:83
mdb_txn_safe(const bool check=true)
Definition: db_lmdb.cpp:299
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:1713
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
get a txpool transaction's blob
Definition: db_lmdb.cpp:1596
MDB_cursor * m_txc_txpool_meta
Definition: db_lmdb.h:59
virtual void check_hard_fork_info()
verify hard fork info in database
Definition: db_lmdb.cpp:3082
void abort()
Definition: db_lmdb.cpp:354
bool m_batch_transactions
Definition: db_lmdb.h:402
virtual void block_txn_start(bool readonly)
Definition: db_lmdb.cpp:2721
MDB_dbi m_tx_indices
Definition: db_lmdb.h:379
MDB_cursor * m_txc_spent_keys
Definition: db_lmdb.h:57
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:2142
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
mdb_txn_safe * m_write_txn
Definition: db_lmdb.h:398
virtual void set_hard_fork_version(uint64_t height, uint8_t version)
sets which hardfork version a height is on
Definition: db_lmdb.cpp:3099
void remove_output(const uint64_t amount, const uint64_t &out_index)
Definition: db_lmdb.cpp:953
virtual uint64_t get_txpool_tx_count() const
get the number of transactions in the txpool
Definition: db_lmdb.cpp:1514
static std::atomic_flag creation_gate
Definition: db_lmdb.h:140
virtual void reset()
Remove everything from the BlockchainDB.
Definition: db_lmdb.cpp:1327
mdb_txn_safe * m_write_batch_txn
Definition: db_lmdb.h:399
virtual size_t get_block_size(const uint64_t &height) const
fetch a block's size
Definition: db_lmdb.cpp:1807
void commit(std::string message="")
Definition: db_lmdb.cpp:339
virtual uint64_t add_transaction_data(const crypto::hash &blk_hash, const transaction &tx, const crypto::hash &tx_hash)
store the transaction and its metadata
Definition: db_lmdb.cpp:752
MDB_dbi m_blocks
Definition: db_lmdb.h:374
MDB_dbi m_block_info
Definition: db_lmdb.h:376
MDB_dbi m_output_txs
Definition: db_lmdb.h:382
bool m_batch_active
Definition: db_lmdb.h:403
MDB_cursor * m_txc_tx_outputs
Definition: db_lmdb.h:55
uint64_t m_cum_size
Definition: db_lmdb.h:395
virtual block_header get_block_header(const crypto::hash &h) const
fetch a block header
Definition: db_lmdb.cpp:1734
virtual void remove_txpool_tx(const crypto::hash &txid)
remove a txpool transaction
Definition: db_lmdb.cpp:1547
~BlockchainLMDB()
Definition: db_lmdb.cpp:1058
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) const
runs a function over all txpool transactions
Definition: db_lmdb.cpp:1625
virtual bool tx_exists(const crypto::hash &h) const
check if a transaction with a given hash exists
Definition: db_lmdb.cpp:2000
Definition: db_lmdb.h:78
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:2285
Definition: rctTypes.h:82
static void wait_no_active_txns()
Definition: db_lmdb.cpp:378
Definition: db_lmdb.h:104
~mdb_threadinfo()
Definition: db_lmdb.cpp:288
MDB_dbi m_spent_keys
Definition: db_lmdb.h:385
virtual bool has_key_image(const crypto::key_image &img) const
check if a key image is stored as spent
Definition: db_lmdb.cpp:2359
bool m_rf_txpool_meta
Definition: db_lmdb.h:90
BlockchainLMDB(bool batch_transactions=false)
Definition: db_lmdb.cpp:1069
~mdb_txn_safe()
Definition: db_lmdb.cpp:309
void cleanup_batch()
Definition: db_lmdb.cpp:2608
virtual void block_rtxn_stop() const
Definition: db_lmdb.cpp:2714
static constexpr uint64_t DEFAULT_MAPSIZE
Definition: db_lmdb.h:413
virtual void remove_spent_key(const crypto::key_image &k_image)
remove a spent key
Definition: db_lmdb.cpp:1008
bool m_rf_txs
Definition: db_lmdb.h:86
virtual void sync()
sync the BlockchainDB with disk
Definition: db_lmdb.cpp:1309
mdb_txn_cursors m_ti_rcursors
Definition: db_lmdb.h:98
void migrate(const uint32_t oldversion)
Definition: db_lmdb.cpp:3698
virtual uint64_t get_tx_count() const
fetches the total number of transactions ever
Definition: db_lmdb.cpp:2125
virtual void close()
close the BlockchainDB
Definition: db_lmdb.cpp:1293
bool m_check
Definition: db_lmdb.h:136
uint8_t version
Definition: blockchain.cpp:86
bool m_batch_txn
Definition: db_lmdb.h:135
std::string blobdata
Definition: blobdatatype.h:36
Definition: db_lmdb.h:44
virtual crypto::hash top_block_hash() const
fetch the top block's hash
Definition: db_lmdb.cpp:1944
virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const
Definition: db_lmdb.cpp:2682
virtual void drop_hard_fork_info()
delete hard fork info from database
Definition: db_lmdb.cpp:3086
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const
checks if a block exists
Definition: db_lmdb.cpp:1674
std::uint64_t difficulty_type
Definition: difficulty.h:41
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's outputs
Definition: db_lmdb.cpp:908
void remove_tx_outputs(const uint64_t tx_id, const transaction &tx)
Definition: db_lmdb.cpp:931
MDB_dbi m_hf_versions
Definition: db_lmdb.h:391
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const
fetch a block's already generated coins
Definition: db_lmdb.cpp:1870
POD_CLASS key_image
Definition: crypto.h:86
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const
fetches the block with the given hash
Definition: db_lmdb.cpp:1705
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:131
uint64_t get_estimated_batch_size(uint64_t batch_num_blocks) const
Definition: db_lmdb.cpp:584
Definition: db_lmdb.h:95
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:842
virtual void open(const std::string &filename, const int mdb_flags=0)
open a db, or create it if necessary.
Definition: db_lmdb.cpp:1087
virtual txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash &txid) const
get a txpool transaction's metadata
Definition: db_lmdb.cpp:1577
bool m_rf_output_txs
Definition: db_lmdb.h:84
virtual void add_spent_key(const crypto::key_image &k_image)
store a spent key
Definition: db_lmdb.cpp:991
MDB_dbi m_block_heights
Definition: db_lmdb.h:375
virtual void batch_abort()
Definition: db_lmdb.cpp:2647
virtual uint64_t get_block_timestamp(const uint64_t &height) const
fetch a block's timestamp
Definition: db_lmdb.cpp:1769
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:336
bool m_rf_txpool_blob
Definition: db_lmdb.h:91
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:2409
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const
fetch a block's hash
Definition: db_lmdb.cpp:1893
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const
fetch a block blob by height
Definition: db_lmdb.cpp:1743
POD_CLASS hash
Definition: hash.h:46
bool m_rf_tx_indices
Definition: db_lmdb.h:87
std::pair< crypto::hash, uint64_t > tx_out_index
Definition: blockchain_db.h:103
virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t &meta)
add a txpool transaction
Definition: db_lmdb.cpp:1460
virtual bool can_thread_bulk_indices() const
Definition: db_lmdb.h:281
uint64_t num_active_tx() const
Definition: db_lmdb.cpp:368
MDB_env * m_env
Definition: db_lmdb.h:372
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const
fetch a transaction's unlock time/height
Definition: db_lmdb.cpp:2074
Definition: cryptonote_basic.h:149
bool m_rf_tx_outputs
Definition: db_lmdb.h:88
struct cryptonote::mdb_txn_cursors mdb_txn_cursors
static void prevent_new_txns()
Definition: db_lmdb.cpp:373
virtual void block_txn_stop()
Definition: db_lmdb.cpp:2775
MDB_cursor * m_txc_tx_indices
Definition: db_lmdb.h:54
virtual bool txpool_has_tx(const crypto::hash &txid) const
check whether a txid is in the txpool
Definition: db_lmdb.cpp:1531
virtual void remove_block()
remove data about the top block
Definition: db_lmdb.cpp:713
virtual bool batch_start(uint64_t batch_num_blocks=0)
tells the BlockchainDB to start a new "batch" of blocks
Definition: db_lmdb.cpp:2544
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:1916
MDB_cursor * m_txc_block_heights
Definition: db_lmdb.h:47
void migrate_0_1()
Definition: db_lmdb.cpp:3164
virtual bool lock()
acquires the BlockchainDB lock
Definition: db_lmdb.cpp:1395
a struct containing output metadata
Definition: blockchain_db.h:110
struct cryptonote::mdb_threadinfo mdb_threadinfo
bool m_rf_spent_keys
Definition: db_lmdb.h:89
Definition: cryptonote_basic.h:188
Definition: db_lmdb.h:157
MDB_cursor * m_txc_blocks
Definition: db_lmdb.h:46
MDB_cursor * m_txc_output_txs
Definition: db_lmdb.h:50
unsigned int m_cum_count
Definition: db_lmdb.h:396
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const
fetch a block's cumulative difficulty
Definition: db_lmdb.cpp:1830
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:2095
MDB_dbi m_properties
Definition: db_lmdb.h:393
static std::atomic< uint64_t > num_active_txns
Definition: db_lmdb.h:137
static void allow_new_txns()
Definition: db_lmdb.cpp:383
MDB_dbi m_hf_starting_heights
Definition: db_lmdb.h:390