Electroneum
db_lmdb.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, 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;
63 
64  MDB_cursor *m_txc_validators;
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_tx_indices m_cursors->m_txc_tx_indices
74 #define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
75 #define m_cur_spent_keys m_cursors->m_txc_spent_keys
76 #define m_cur_txpool_meta m_cursors->m_txc_txpool_meta
77 #define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
78 #define m_cur_hf_versions m_cursors->m_txc_hf_versions
79 #define m_cur_validators m_cursors->m_txc_validators
80 
81 typedef struct mdb_rflags
82 {
83  bool m_rf_txn;
89  bool m_rf_txs;
97 } mdb_rflags;
98 
99 typedef struct mdb_threadinfo
100 {
101  MDB_txn *m_ti_rtxn; // per-thread read txn
102  mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
103  mdb_rflags m_ti_rflags; // per-thread read state
104 
105  ~mdb_threadinfo();
107 
109 {
110  mdb_txn_safe(const bool check=true);
111  ~mdb_txn_safe();
112 
113  void commit(std::string message = "");
114 
115  // This should only be needed for batch transaction which must be ensured to
116  // be aborted before mdb_env_close, not after. So we can't rely on
117  // BlockchainLMDB destructor to call mdb_txn_safe destructor, as that's too late
118  // to properly abort, since mdb_env_close would have been called earlier.
119  void abort();
120 
121  operator MDB_txn*()
122  {
123  return m_txn;
124  }
125 
126  operator MDB_txn**()
127  {
128  return &m_txn;
129  }
130 
131  uint64_t num_active_tx() const;
132 
133  static void prevent_new_txns();
134  static void wait_no_active_txns();
135  static void allow_new_txns();
136 
138  MDB_txn* m_txn;
139  bool m_batch_txn = false;
140  bool m_check;
141  static std::atomic<uint64_t> num_active_txns;
142 
143  // could use a mutex here, but this should be sufficient.
144  static std::atomic_flag creation_gate;
145 };
146 
147 
148 // If m_batch_active is set, a batch transaction exists beyond this class, such
149 // as a batch import with verification enabled, or possibly (later) a batch
150 // network sync.
151 //
152 // For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
153 // and get_tx(), when m_batch_active is set, the lookup uses the batch
154 // transaction. This isn't only because the transaction is available, but it's
155 // necessary so that lookups include the database updates only present in the
156 // current batch write.
157 //
158 // A regular network sync without batch writes is expected to open a new read
159 // transaction, as those lookups are part of the validation done prior to the
160 // write for block and tx data, so no write transaction is open at the time.
162 {
163 public:
164  BlockchainLMDB(bool batch_transactions=false);
165  ~BlockchainLMDB();
166 
167  virtual void open(const std::string& filename, const int mdb_flags=0);
168 
169  virtual void close();
170 
171  virtual void sync();
172 
173  virtual void safesyncmode(const bool onoff);
174 
175  virtual void reset();
176 
177  virtual std::vector<std::string> get_filenames() const;
178 
179  virtual std::string get_db_name() const;
180 
181  virtual bool lock();
182 
183  virtual void unlock();
184 
185  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
186 
187  virtual uint64_t get_block_height(const crypto::hash& h) const;
188 
189  virtual block_header get_block_header(const crypto::hash& h) const;
190 
191  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
192 
193  virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const;
194 
195  virtual uint64_t get_block_timestamp(const uint64_t& height) const;
196 
197  virtual uint64_t get_top_block_timestamp() const;
198 
199  virtual size_t get_block_size(const uint64_t& height) const;
200 
201  virtual void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff);
202 
203  virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;
204 
205  virtual size_t get_block_weight(const uint64_t& height) const;
206 
207  virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
208 
209  virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
210 
211  virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const;
212 
213  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
214 
215  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
216 
217  virtual crypto::hash top_block_hash() const;
218 
219  virtual block get_top_block() const;
220 
221  virtual uint64_t height() const;
222 
223  virtual bool tx_exists(const crypto::hash& h) const;
224  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
225 
226  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
227 
228  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
229 
230  virtual uint64_t get_tx_count() const;
231 
232  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
233 
234  virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
235 
236  virtual uint64_t get_num_outputs(const uint64_t& amount) const;
237 
238  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
239  virtual output_data_t get_output_key(const uint64_t& global_index) const;
240  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);
241 
242  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
243  virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
244  std::vector<tx_out_index> &tx_out_indices) const;
245 
246  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
247  virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
248 
249  virtual std::vector<uint64_t> get_tx_amount_output_indices(const uint64_t tx_id) const;
250 
251  virtual bool has_key_image(const crypto::key_image& img) const;
252 
253  virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t& meta);
254  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
255  virtual uint64_t get_txpool_tx_count() const;
256  virtual bool txpool_has_tx(const crypto::hash &txid) const;
257  virtual void remove_txpool_tx(const crypto::hash& txid);
258  virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
259  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
260  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
261  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;
262 
263  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
264  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;
265  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
266  virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const;
267 
268  virtual uint64_t add_block( const block& blk
269  , const size_t& block_size
270  , const difficulty_type& cumulative_difficulty
271  , const uint64_t& coins_generated
272  , const std::vector<transaction>& txs
273  );
274 
275  virtual void set_batch_transactions(bool batch_transactions);
276  virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
277  virtual void batch_commit();
278  virtual void batch_stop();
279  virtual void batch_abort();
280 
281  virtual void block_txn_start(bool readonly);
282  virtual void block_txn_stop();
283  virtual void block_txn_abort();
284  virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
285  virtual void block_rtxn_stop() const;
286 
287  virtual void pop_block(block& blk, std::vector<transaction>& txs);
288 
289  virtual bool can_thread_bulk_indices() const { return true; }
290 
300  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;
301 
302 private:
303  void do_resize(uint64_t size_increase=0);
304 
305  bool need_resize(uint64_t threshold_size=0) const;
306  void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes);
307  uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;
308 
309  virtual void add_block( const block& blk
310  , const size_t& block_size
311  , const difficulty_type& cumulative_difficulty
312  , const uint64_t& coins_generated
313  , const crypto::hash& block_hash
314  );
315 
316  virtual void remove_block();
317 
318  virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash);
319 
320  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
321 
322  virtual uint64_t add_output(const crypto::hash& tx_hash,
323  const tx_out& tx_output,
324  const uint64_t& local_index,
325  const uint64_t unlock_time,
326  const rct::key *commitment
327  );
328 
329  virtual void add_tx_amount_output_indices(const uint64_t tx_id,
330  const std::vector<uint64_t>& amount_output_indices
331  );
332 
333  void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
334 
335  void remove_output(const uint64_t amount, const uint64_t& out_index);
336 
337  virtual void add_spent_key(const crypto::key_image& k_image);
338 
339  virtual void remove_spent_key(const crypto::key_image& k_image);
340 
341  uint64_t num_outputs() const;
342 
343  // Hard fork
344  virtual void set_hard_fork_version(uint64_t height, uint8_t version);
345  virtual uint8_t get_hard_fork_version(uint64_t height) const;
346  virtual void check_hard_fork_info();
347  virtual void drop_hard_fork_info();
348 
349  // Validator List
350  virtual void set_validator_list(std::string validators, uint32_t expiration_date);
351  virtual std::string get_validator_list() const;
352 
360  blobdata output_to_blob(const tx_out& output) const;
361 
369  tx_out output_from_blob(const blobdata& blob) const;
370 
371  blobdata validator_to_blob(const validator_db& v) const;
372 
373  validator_db validator_from_blob(const blobdata& blob) const;
374 
375  void check_open() const;
376 
377  virtual bool is_read_only() const;
378 
379  // migrate from older DB version to current
380  void migrate(const uint32_t oldversion);
381 
382  // migrate from DB version 0 to 1
383  void migrate_0_1();
384 
385  void cleanup_batch();
386 
387 private:
388  MDB_env* m_env;
389 
390  MDB_dbi m_blocks;
392  MDB_dbi m_block_info;
393 
394  MDB_dbi m_txs;
395  MDB_dbi m_tx_indices;
396  MDB_dbi m_tx_outputs;
397 
398  MDB_dbi m_output_txs;
400 
401  MDB_dbi m_spent_keys;
402 
403  MDB_dbi m_txpool_meta;
404  MDB_dbi m_txpool_blob;
405 
407  MDB_dbi m_hf_versions;
408 
409  MDB_dbi m_validators;
410 
411  MDB_dbi m_properties;
412 
413  mutable uint64_t m_cum_size; // used in batch size estimation
414  mutable unsigned int m_cum_count;
415  std::string m_folder;
416  mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
417  mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
418  boost::thread::id m_writer;
419 
420  bool m_batch_transactions; // support for batch transactions
421  bool m_batch_active; // whether batch transaction is in progress
422 
424  mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
425 
426 #if defined(__arm__)
427  // force a value so it can compile with 32-bit ARM
428  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
429 #else
430 #if defined(ENABLE_AUTO_RESIZE)
431  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
432 #else
433  constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
434 #endif
435 #endif
436 
437  constexpr static float RESIZE_PERCENT = 0.8f;
438 };
439 
440 } // 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:3092
virtual void batch_stop()
ends a batch transaction
Definition: db_lmdb.cpp:2718
bool m_rf_txn
Definition: db_lmdb.h:83
virtual uint64_t get_top_block_timestamp() const
fetch the top block&#39;s timestamp
Definition: db_lmdb.cpp:1827
MDB_cursor * m_txc_txpool_blob
Definition: db_lmdb.h:60
virtual void unlock()
This function releases the BlockchainDB lock.
Definition: db_lmdb.cpp:1436
virtual void set_validator_list(std::string validators, uint32_t expiration_date)
Definition: db_lmdb.cpp:3199
void do_resize(uint64_t size_increase=0)
Definition: db_lmdb.cpp:435
virtual difficulty_type get_block_difficulty(const uint64_t &height) const
fetch a block&#39;s difficulty
Definition: db_lmdb.cpp:1947
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:2470
virtual uint64_t get_num_outputs(const uint64_t &amount) const
fetches the number of outputs of a given amount
Definition: db_lmdb.cpp:2273
mdb_threadinfo * m_tinfo
Definition: db_lmdb.h:137
boost::thread_specific_ptr< mdb_threadinfo > m_tinfo
Definition: db_lmdb.h:424
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition: blockchain_db.cpp:117
MDB_dbi m_txpool_meta
Definition: db_lmdb.h:403
MDB_dbi m_txpool_blob
Definition: db_lmdb.h:404
static constexpr float RESIZE_PERCENT
Definition: db_lmdb.h:437
bool need_resize(uint64_t threshold_size=0) const
Definition: db_lmdb.cpp:503
virtual uint8_t get_hard_fork_version(uint64_t height) const
checks which hardfork version a height is on
Definition: db_lmdb.cpp:3270
virtual void set_batch_transactions(bool batch_transactions)
sets whether or not to batch transactions
Definition: db_lmdb.cpp:2770
bool m_rf_hf_versions
Definition: db_lmdb.h:95
virtual void batch_commit()
Definition: db_lmdb.cpp:2681
mdb_txn_cursors m_wcursors
Definition: db_lmdb.h:423
uint64_t num_outputs() const
Definition: db_lmdb.cpp:2080
MDB_dbi m_tx_outputs
Definition: db_lmdb.h:396
blobdata output_to_blob(const tx_out &output) const
convert a tx output to a blob for storage
Definition: db_lmdb.cpp:1034
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:2347
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:2602
bool m_rf_output_amounts
Definition: db_lmdb.h:88
virtual std::string get_validator_list() const
Definition: db_lmdb.cpp:3221
bool m_rf_block_heights
Definition: db_lmdb.h:85
MDB_txn * m_ti_rtxn
Definition: db_lmdb.h:101
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:2250
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:2926
virtual std::vector< std::string > get_filenames() const
get all files used by the BlockchainDB (if any)
Definition: db_lmdb.cpp:1404
MDB_cursor * m_txc_txs
Definition: db_lmdb.h:53
Definition: cryptonote_basic.h:382
MDB_dbi m_txs
Definition: db_lmdb.h:394
virtual void block_txn_abort()
Definition: db_lmdb.cpp:2899
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:1353
MDB_txn * m_txn
Definition: db_lmdb.h:138
MDB_cursor * m_txc_hf_versions
Definition: db_lmdb.h:62
Definition: cryptonote_basic.h:365
virtual size_t get_block_weight(const uint64_t &height) const
Definition: db_lmdb.cpp:1865
mdb_rflags m_ti_rflags
Definition: db_lmdb.h:103
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:2402
MDB_dbi m_validators
Definition: db_lmdb.h:409
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:1521
boost::thread::id m_writer
Definition: db_lmdb.h:418
virtual uint64_t height() const
fetch the current blockchain height
Definition: db_lmdb.cpp:2066
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:2554
tx_out output_from_blob(const blobdata &blob) const
convert a tx output blob to a tx output
Definition: db_lmdb.cpp:1043
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:2415
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:2051
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:1420
void check_open() const
Definition: db_lmdb.cpp:1080
struct cryptonote::mdb_rflags mdb_rflags
virtual bool is_read_only() const
is BlockchainDB in read-only mode?
Definition: db_lmdb.cpp:3289
std::string m_folder
Definition: db_lmdb.h:415
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)
remove data about a transaction
Definition: db_lmdb.cpp:805
bool m_rf_blocks
Definition: db_lmdb.h:84
MDB_dbi m_output_amounts
Definition: db_lmdb.h:399
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:2024
bool m_rf_block_info
Definition: db_lmdb.h:86
mdb_txn_safe(const bool check=true)
Definition: db_lmdb.cpp:301
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:1748
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:1631
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:3182
void abort()
Definition: db_lmdb.cpp:356
bool m_batch_transactions
Definition: db_lmdb.h:420
virtual void block_txn_start(bool readonly)
Definition: db_lmdb.cpp:2821
MDB_dbi m_tx_indices
Definition: db_lmdb.h:395
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:2236
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
Definition: cryptonote_basic.h:162
mdb_txn_safe * m_write_txn
Definition: db_lmdb.h:416
MDB_cursor * m_txc_validators
Definition: db_lmdb.h:64
virtual void set_hard_fork_version(uint64_t height, uint8_t version)
sets which hardfork version a height is on
Definition: db_lmdb.cpp:3251
void remove_output(const uint64_t amount, const uint64_t &out_index)
Definition: db_lmdb.cpp:959
blobdata validator_to_blob(const validator_db &v) const
Definition: db_lmdb.cpp:1057
virtual uint64_t get_txpool_tx_count() const
get the number of transactions in the txpool
Definition: db_lmdb.cpp:1547
static std::atomic_flag creation_gate
Definition: db_lmdb.h:144
virtual void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff)
sets a block&#39;s cumulative difficulty
Definition: db_lmdb.cpp:1888
virtual void reset()
Remove everything from the BlockchainDB.
Definition: db_lmdb.cpp:1358
mdb_txn_safe * m_write_batch_txn
Definition: db_lmdb.h:417
virtual size_t get_block_size(const uint64_t &height) const
fetch a block&#39;s size
Definition: db_lmdb.cpp:1842
void commit(std::string message="")
Definition: db_lmdb.cpp:341
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:759
MDB_dbi m_blocks
Definition: db_lmdb.h:390
MDB_dbi m_block_info
Definition: db_lmdb.h:392
MDB_dbi m_output_txs
Definition: db_lmdb.h:398
bool m_batch_active
Definition: db_lmdb.h:421
MDB_cursor * m_txc_tx_outputs
Definition: db_lmdb.h:55
uint64_t m_cum_size
Definition: db_lmdb.h:413
virtual block_header get_block_header(const crypto::hash &h) const
fetch a block header
Definition: db_lmdb.cpp:1769
virtual void remove_txpool_tx(const crypto::hash &txid)
remove a txpool transaction
Definition: db_lmdb.cpp:1580
~BlockchainLMDB()
Definition: db_lmdb.cpp:1087
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:1660
virtual bool tx_exists(const crypto::hash &h) const
check if a transaction with a given hash exists
Definition: db_lmdb.cpp:2094
Definition: db_lmdb.h:81
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:2379
Definition: rctTypes.h:82
static void wait_no_active_txns()
Definition: db_lmdb.cpp:380
Definition: db_lmdb.h:108
~mdb_threadinfo()
Definition: db_lmdb.cpp:290
MDB_dbi m_spent_keys
Definition: db_lmdb.h:401
virtual bool has_key_image(const crypto::key_image &img) const
check if a key image is stored as spent
Definition: db_lmdb.cpp:2453
bool m_rf_txpool_meta
Definition: db_lmdb.h:93
BlockchainLMDB(bool batch_transactions=false)
Definition: db_lmdb.cpp:1098
~mdb_txn_safe()
Definition: db_lmdb.cpp:311
void cleanup_batch()
Definition: db_lmdb.cpp:2708
virtual void block_rtxn_stop() const
Definition: db_lmdb.cpp:2814
static constexpr uint64_t DEFAULT_MAPSIZE
Definition: db_lmdb.h:431
virtual void remove_spent_key(const crypto::key_image &k_image)
remove a spent key
Definition: db_lmdb.cpp:1014
bool m_rf_txs
Definition: db_lmdb.h:89
virtual void sync()
sync the BlockchainDB with disk
Definition: db_lmdb.cpp:1340
mdb_txn_cursors m_ti_rcursors
Definition: db_lmdb.h:102
void migrate(const uint32_t oldversion)
Definition: db_lmdb.cpp:3850
virtual uint64_t get_tx_count() const
fetches the total number of transactions ever
Definition: db_lmdb.cpp:2219
virtual void close()
close the BlockchainDB
Definition: db_lmdb.cpp:1324
bool m_check
Definition: db_lmdb.h:140
uint8_t version
Definition: blockchain.cpp:86
bool m_batch_txn
Definition: db_lmdb.h:139
std::string blobdata
Definition: blobdatatype.h:36
Definition: db_lmdb.h:44
virtual crypto::hash top_block_hash() const
fetch the top block&#39;s hash
Definition: db_lmdb.cpp:2038
virtual bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const
Definition: db_lmdb.cpp:2782
virtual void drop_hard_fork_info()
delete hard fork info from database
Definition: db_lmdb.cpp:3186
void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes)
Definition: db_lmdb.cpp:550
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const
checks if a block exists
Definition: db_lmdb.cpp:1709
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&#39;s outputs
Definition: db_lmdb.cpp:915
void remove_tx_outputs(const uint64_t tx_id, const transaction &tx)
Definition: db_lmdb.cpp:938
MDB_dbi m_hf_versions
Definition: db_lmdb.h:407
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:1964
POD_CLASS key_image
Definition: crypto.h:93
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const
fetches the block with the given hash
Definition: db_lmdb.cpp:1740
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:137
Definition: db_lmdb.h:99
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:849
virtual void open(const std::string &filename, const int mdb_flags=0)
open a db, or create it if necessary.
Definition: db_lmdb.cpp:1116
validator_db validator_from_blob(const blobdata &blob) const
Definition: db_lmdb.cpp:1066
bool m_rf_output_txs
Definition: db_lmdb.h:87
virtual void add_spent_key(const crypto::key_image &k_image)
store a spent key
Definition: db_lmdb.cpp:997
MDB_dbi m_block_heights
Definition: db_lmdb.h:391
virtual void batch_abort()
Definition: db_lmdb.cpp:2747
virtual uint64_t get_block_timestamp(const uint64_t &height) const
fetch a block&#39;s timestamp
Definition: db_lmdb.cpp:1804
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:342
bool m_rf_txpool_blob
Definition: db_lmdb.h:94
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:1610
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:2503
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const
fetch a block&#39;s hash
Definition: db_lmdb.cpp:1987
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const
fetch a block blob by height
Definition: db_lmdb.cpp:1778
POD_CLASS hash
Definition: hash.h:46
bool m_rf_tx_indices
Definition: db_lmdb.h:90
uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const
Definition: db_lmdb.cpp:582
std::pair< crypto::hash, uint64_t > tx_out_index
Definition: blockchain_db.h:105
virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t &meta)
add a txpool transaction
Definition: db_lmdb.cpp:1493
virtual bool can_thread_bulk_indices() const
Definition: db_lmdb.h:289
uint64_t num_active_tx() const
Definition: db_lmdb.cpp:370
MDB_env * m_env
Definition: db_lmdb.h:388
bool m_rf_validators
Definition: db_lmdb.h:96
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const
fetch a transaction&#39;s unlock time/height
Definition: db_lmdb.cpp:2168
Definition: cryptonote_basic.h:149
bool m_rf_tx_outputs
Definition: db_lmdb.h:91
struct cryptonote::mdb_txn_cursors mdb_txn_cursors
static void prevent_new_txns()
Definition: db_lmdb.cpp:375
virtual void block_txn_stop()
Definition: db_lmdb.cpp:2875
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:2638
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:1564
virtual void remove_block()
remove data about the top block
Definition: db_lmdb.cpp:720
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:2010
MDB_cursor * m_txc_block_heights
Definition: db_lmdb.h:47
void migrate_0_1()
Definition: db_lmdb.cpp:3316
virtual bool lock()
acquires the BlockchainDB lock
Definition: db_lmdb.cpp:1428
a struct containing output metadata
Definition: blockchain_db.h:116
struct cryptonote::mdb_threadinfo mdb_threadinfo
bool m_rf_spent_keys
Definition: db_lmdb.h:92
Definition: cryptonote_basic.h:198
Definition: db_lmdb.h:161
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:414
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const
fetch a block&#39;s cumulative difficulty
Definition: db_lmdb.cpp:1924
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:2189
MDB_dbi m_properties
Definition: db_lmdb.h:411
static std::atomic< uint64_t > num_active_txns
Definition: db_lmdb.h:141
static void allow_new_txns()
Definition: db_lmdb.cpp:385
MDB_dbi m_hf_starting_heights
Definition: db_lmdb.h:406