Monero
Loading...
Searching...
No Matches
db_lmdb.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, 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
40namespace cryptonote
41{
42
47
76
77#define m_cur_blocks m_cursors->m_txc_blocks
78#define m_cur_block_heights m_cursors->m_txc_block_heights
79#define m_cur_block_info m_cursors->m_txc_block_info
80#define m_cur_output_txs m_cursors->m_txc_output_txs
81#define m_cur_output_amounts m_cursors->m_txc_output_amounts
82#define m_cur_txs m_cursors->m_txc_txs
83#define m_cur_txs_pruned m_cursors->m_txc_txs_pruned
84#define m_cur_txs_prunable m_cursors->m_txc_txs_prunable
85#define m_cur_txs_prunable_hash m_cursors->m_txc_txs_prunable_hash
86#define m_cur_txs_prunable_tip m_cursors->m_txc_txs_prunable_tip
87#define m_cur_tx_indices m_cursors->m_txc_tx_indices
88#define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
89#define m_cur_spent_keys m_cursors->m_txc_spent_keys
90#define m_cur_txpool_meta m_cursors->m_txc_txpool_meta
91#define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
92#define m_cur_alt_blocks m_cursors->m_txc_alt_blocks
93#define m_cur_hf_versions m_cursors->m_txc_hf_versions
94#define m_cur_properties m_cursors->m_txc_properties
95
118
119typedef struct mdb_threadinfo
120{
121 MDB_txn *m_ti_rtxn; // per-thread read txn
122 mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
123 mdb_rflags m_ti_rflags; // per-thread read state
124
127
129{
130 mdb_txn_safe(const bool check=true);
132
133 void commit(std::string message = "");
134
135 // This should only be needed for batch transaction which must be ensured to
136 // be aborted before mdb_env_close, not after. So we can't rely on
137 // BlockchainLMDB destructor to call mdb_txn_safe destructor, as that's too late
138 // to properly abort, since mdb_env_close would have been called earlier.
139 void abort();
140 void uncheck();
141
142 operator MDB_txn*()
143 {
144 return m_txn;
145 }
146
147 operator MDB_txn**()
148 {
149 return &m_txn;
150 }
151
152 uint64_t num_active_tx() const;
153
154 static void prevent_new_txns();
155 static void wait_no_active_txns();
156 static void allow_new_txns();
157 static void increment_txns(int);
158
161 bool m_batch_txn = false;
163 static std::atomic<uint64_t> num_active_txns;
165
166 // could use a mutex here, but this should be sufficient.
167 static std::atomic_flag creation_gate;
168};
169
170
171// If m_batch_active is set, a batch transaction exists beyond this class, such
172// as a batch import with verification enabled, or possibly (later) a batch
173// network sync.
174//
175// For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
176// and get_tx(), when m_batch_active is set, the lookup uses the batch
177// transaction. This isn't only because the transaction is available, but it's
178// necessary so that lookups include the database updates only present in the
179// current batch write.
180//
181// A regular network sync without batch writes is expected to open a new read
182// transaction, as those lookups are part of the validation done prior to the
183// write for block and tx data, so no write transaction is open at the time.
185{
186public:
187 BlockchainLMDB(bool batch_transactions=true);
189
190 virtual void open(const std::string& filename, const int mdb_flags=0);
191
192 virtual void close();
193
194 virtual void sync();
195
196 virtual void safesyncmode(const bool onoff);
197
198 virtual void reset();
199
200 virtual std::vector<std::string> get_filenames() const;
201
202 virtual bool remove_data_file(const std::string& folder) const;
203
204 virtual std::string get_db_name() const;
205
206 virtual bool lock();
207
208 virtual void unlock();
209
210 virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
211
212 virtual uint64_t get_block_height(const crypto::hash& h) const;
213
214 virtual block_header get_block_header(const crypto::hash& h) const;
215
216 virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
217
219
220 virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const;
221
222 virtual uint64_t get_block_timestamp(const uint64_t& height) const;
223
224 virtual uint64_t get_top_block_timestamp() const;
225
226 virtual size_t get_block_weight(const uint64_t& height) const;
227
228 virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const;
229
231
233
234 virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector<difficulty_type>& new_cumulative_difficulties);
235
237
239
240 virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const;
241
243
244 virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
245
246 virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
247
248 virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const;
249
250 virtual block get_top_block() const;
251
252 virtual uint64_t height() const;
253
254 virtual bool tx_exists(const crypto::hash& h) const;
255 virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
256
257 virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
258
259 virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
260 virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
261 virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const;
262 virtual bool get_blocks_from(uint64_t start_height, size_t min_block_count, size_t max_block_count, size_t max_tx_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const;
263 virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
264 virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const;
265
266 virtual uint64_t get_tx_count() const;
267
268 virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
269
270 virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
271
272 virtual uint64_t get_num_outputs(const uint64_t& amount) const;
273
274 virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt) const;
275 virtual void get_output_key(const epee::span<const uint64_t> &amounts, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs, bool allow_partial = false) const;
276
278 virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
279 std::vector<tx_out_index> &tx_out_indices) const;
280
281 virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
282 virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
283
284 virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes) const;
285
286 virtual bool has_key_image(const crypto::key_image& img) const;
287
288 virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t& meta);
289 virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
291 virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const;
292 virtual void remove_txpool_tx(const crypto::hash& txid);
293 virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
294 virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata& bd, relay_category tx_category) const;
295 virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid, relay_category tx_category) const;
297 virtual bool prune_blockchain(uint32_t pruning_seed = 0);
298 virtual bool update_pruning();
299 virtual bool check_pruning();
300
301 virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob);
302 virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob);
303 virtual void remove_alt_block(const crypto::hash &blkid);
305 virtual void drop_alt_blocks();
306
307 virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata_ref*)> f, bool include_blob = false, relay_category category = relay_category::broadcasted) const;
308
309 virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
310 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;
311 virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
312 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;
313 virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const;
314 virtual bool for_all_alt_blocks(std::function<bool(const crypto::hash &blkid, const alt_block_data_t &data, const cryptonote::blobdata_ref *blob)> f, bool include_blob = false) const;
315
316 virtual uint64_t add_block( const std::pair<block, blobdata>& blk
317 , size_t block_weight
318 , uint64_t long_term_block_weight
319 , const difficulty_type& cumulative_difficulty
320 , const uint64_t& coins_generated
321 , const std::vector<std::pair<transaction, blobdata>>& txs
322 );
323
324 virtual void set_batch_transactions(bool batch_transactions);
325 virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
326 virtual void batch_commit();
327 virtual void batch_stop();
328 virtual void batch_abort();
329
330 virtual void block_wtxn_start();
331 virtual void block_wtxn_stop();
332 virtual void block_wtxn_abort();
333 virtual bool block_rtxn_start() const;
334 virtual void block_rtxn_stop() const;
335 virtual void block_rtxn_abort() const;
336
337 bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
338
339 virtual void pop_block(block& blk, std::vector<transaction>& txs);
340
341 virtual bool can_thread_bulk_indices() const { return true; }
342
353 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;
354
355 bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const;
356
357 // helper functions
358 static int compare_uint64(const MDB_val *a, const MDB_val *b);
359 static int compare_hash32(const MDB_val *a, const MDB_val *b);
360 static int compare_string(const MDB_val *a, const MDB_val *b);
361
362private:
363 void do_resize(uint64_t size_increase=0);
364
365 bool need_resize(uint64_t threshold_size=0) const;
366 void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes);
367 uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;
368
369 virtual void add_block( const block& blk
370 , size_t block_weight
371 , uint64_t long_term_block_weight
372 , const difficulty_type& cumulative_difficulty
373 , const uint64_t& coins_generated
374 , uint64_t num_rct_outs
375 , const crypto::hash& block_hash
376 );
377
378 virtual void remove_block();
379
380 virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash);
381
382 virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
383
384 virtual uint64_t add_output(const crypto::hash& tx_hash,
385 const tx_out& tx_output,
386 const uint64_t& local_index,
387 const uint64_t unlock_time,
388 const rct::key *commitment
389 );
390
391 virtual void add_tx_amount_output_indices(const uint64_t tx_id,
392 const std::vector<uint64_t>& amount_output_indices
393 );
394
395 void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
396
397 void remove_output(const uint64_t amount, const uint64_t& out_index);
398
399 virtual void prune_outputs(uint64_t amount);
400
401 virtual void add_spent_key(const crypto::key_image& k_image);
402
403 virtual void remove_spent_key(const crypto::key_image& k_image);
404
405 uint64_t num_outputs() const;
406
407 // Hard fork
408 virtual void set_hard_fork_version(uint64_t height, uint8_t version);
410 virtual void check_hard_fork_info();
411 virtual void drop_hard_fork_info();
412
413 inline void check_open() const;
414
415 bool prune_worker(int mode, uint32_t pruning_seed);
416
417 virtual bool is_read_only() const;
418
419 virtual uint64_t get_database_size() const;
420
421 std::vector<uint64_t> get_block_info_64bit_fields(uint64_t start_height, size_t count, off_t offset) const;
422
425
426 // fix up anything that may be wrong due to past bugs
427 virtual void fixup();
428
429 // migrate from older DB version to current
430 void migrate(const uint32_t oldversion);
431
432 // migrate from DB version 0 to 1
433 void migrate_0_1();
434
435 // migrate from DB version 1 to 2
436 void migrate_1_2();
437
438 // migrate from DB version 2 to 3
439 void migrate_2_3();
440
441 // migrate from DB version 3 to 4
442 void migrate_3_4();
443
444 // migrate from DB version 4 to 5
445 void migrate_4_5();
446
447 void cleanup_batch();
448
449private:
451
455
463
466
468
471
473
476
478
479 mutable uint64_t m_cum_size; // used in batch size estimation
480 mutable unsigned int m_cum_count;
481 std::string m_folder;
482 mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
483 mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
484 boost::thread::id m_writer;
485
486 bool m_batch_transactions; // support for batch transactions
487 bool m_batch_active; // whether batch transaction is in progress
488
490 mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
491
492#if defined(__arm__)
493 // force a value so it can compile with 32-bit ARM
494 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
495#else
496#if defined(ENABLE_AUTO_RESIZE)
497 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
498#else
499 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
500#endif
501#endif
502
503 constexpr static float RESIZE_PERCENT = 0.9f;
504};
505
506} // namespace cryptonote
cryptonote::block b
Definition block.cpp:40
int compare_uint64(const MDB_val *a, const MDB_val *b)
Definition blockchain_blackball.cpp:160
BlockchainDB()
An empty constructor.
Definition blockchain_db.h:584
boost::thread_specific_ptr< mdb_threadinfo > m_tinfo
Definition db_lmdb.h:490
virtual bool update_pruning()
prunes recent blockchain changes as needed, iff pruning is enabled
Definition db_lmdb.cpp:2338
MDB_dbi m_txs
Definition db_lmdb.h:456
virtual size_t get_block_weight(const uint64_t &height) const
fetch a block's weight
Definition db_lmdb.cpp:2634
static int compare_string(const MDB_val *a, const MDB_val *b)
Definition db_lmdb.cpp:166
virtual uint64_t get_alt_block_count()
get the number of alternative blocks stored
Definition db_lmdb.cpp:4517
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:3614
virtual uint32_t get_blockchain_pruning_seed() const
get the blockchain pruning seed
Definition db_lmdb.cpp:2015
MDB_dbi m_output_txs
Definition db_lmdb.h:464
virtual void pop_block(block &blk, std::vector< transaction > &txs)
pops the top block off the blockchain
Definition db_lmdb.cpp:4112
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const
fetch a block's cumulative number of rct outputs
Definition db_lmdb.cpp:2557
virtual void batch_stop()
ends a batch transaction
Definition db_lmdb.cpp:3883
MDB_dbi m_txs_prunable_tip
Definition db_lmdb.h:460
virtual void open(const std::string &filename, const int mdb_flags=0)
open a db, or create it if necessary.
Definition db_lmdb.cpp:1376
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:3485
MDB_dbi m_output_amounts
Definition db_lmdb.h:465
virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const
check whether a txid is in the txpool and meets tx_category requirements
Definition db_lmdb.cpp:1893
virtual void set_batch_transactions(bool batch_transactions)
sets whether or not to batch transactions
Definition db_lmdb.cpp:3935
MDB_env * m_env
Definition db_lmdb.h:450
static constexpr float RESIZE_PERCENT
Definition db_lmdb.h:503
MDB_dbi m_properties
Definition db_lmdb.h:477
virtual void reset()
Remove everything from the BlockchainDB.
Definition db_lmdb.cpp:1667
virtual void unlock()
This function releases the BlockchainDB lock.
Definition db_lmdb.cpp:1764
bool prune_worker(int mode, uint32_t pruning_seed)
Definition db_lmdb.cpp:2050
virtual block get_top_block() const
fetch the top block
Definition db_lmdb.cpp:2965
virtual void block_rtxn_stop() const
Definition db_lmdb.cpp:3984
MDB_dbi m_spent_keys
Definition db_lmdb.h:467
virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t &meta)
add a txpool transaction
Definition db_lmdb.cpp:1796
MDB_dbi m_txpool_meta
Definition db_lmdb.h:469
MDB_dbi m_block_heights
Definition db_lmdb.h:453
virtual difficulty_type get_block_difficulty(const uint64_t &height) const
fetch a block's difficulty
Definition db_lmdb.cpp:2798
virtual std::vector< std::vector< uint64_t > > get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes) const
gets output indices (amount-specific) for a transaction's outputs
Definition db_lmdb.cpp:3521
virtual void prune_outputs(uint64_t amount)
prune output data for the given amount
Definition db_lmdb.cpp:1204
virtual block_header get_block_header(const crypto::hash &h) const
fetch a block header
Definition db_lmdb.cpp:2499
mdb_txn_safe * m_write_batch_txn
Definition db_lmdb.h:483
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:4250
bool m_batch_transactions
Definition db_lmdb.h:486
mdb_txn_cursors m_wcursors
Definition db_lmdb.h:489
virtual void batch_abort()
aborts a batch transaction
Definition db_lmdb.cpp:3912
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const
get a txpool transaction's metadata
Definition db_lmdb.cpp:1949
virtual bool has_key_image(const crypto::key_image &img) const
check if a key image is stored as spent
Definition db_lmdb.cpp:3564
virtual void correct_block_cumulative_difficulties(const uint64_t &start_height, const std::vector< difficulty_type > &new_cumulative_difficulties)
correct blocks cumulative difficulties that were incorrectly calculated due to the 'difficulty drift'...
Definition db_lmdb.cpp:2815
virtual uint64_t add_transaction_data(const crypto::hash &blk_hash, const std::pair< transaction, blobdata_ref > &tx, const crypto::hash &tx_hash, const crypto::hash &tx_prunable_hash)
store the transaction and its metadata
Definition db_lmdb.cpp:890
unsigned int m_cum_count
Definition db_lmdb.h:480
virtual crypto::hash top_block_hash(uint64_t *block_height=NULL) const
fetch the top block's hash
Definition db_lmdb.cpp:2950
bool m_batch_active
Definition db_lmdb.h:487
virtual void block_wtxn_abort()
Definition db_lmdb.cpp:4060
std::string m_folder
Definition db_lmdb.h:481
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const
fetch a block's already generated coins
Definition db_lmdb.cpp:2853
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const
fetch the last N blocks' long term weights
Definition db_lmdb.cpp:2768
virtual uint64_t get_tx_count() const
fetches the total number of transactions ever
Definition db_lmdb.cpp:3373
MDB_dbi m_tx_outputs
Definition db_lmdb.h:462
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const
checks if a block exists
Definition db_lmdb.cpp:2439
virtual bool get_blocks_from(uint64_t start_height, size_t min_block_count, size_t max_block_count, size_t max_tx_count, size_t max_size, std::vector< std::pair< std::pair< cryptonote::blobdata, crypto::hash >, std::vector< std::pair< crypto::hash, cryptonote::blobdata > > > > &blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const
Definition db_lmdb.cpp:3211
virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob)
add a new alternative block
Definition db_lmdb.cpp:4448
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:3139
virtual uint64_t get_num_outputs(const uint64_t &amount) const
fetches the number of outputs of a given amount
Definition db_lmdb.cpp:3427
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:2478
void migrate_2_3()
Definition db_lmdb.cpp:5287
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:2922
virtual bool tx_exists(const crypto::hash &h) const
check if a transaction with a given hash exists
Definition db_lmdb.cpp:3016
virtual bool block_rtxn_start() const
Definition db_lmdb.cpp:3993
virtual void block_rtxn_abort() const
Definition db_lmdb.cpp:4076
void do_resize(uint64_t size_increase=0)
Definition db_lmdb.cpp:558
virtual std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const
fetch the last N blocks' weights
Definition db_lmdb.cpp:2763
BlockchainLMDB(bool batch_transactions=true)
Definition db_lmdb.cpp:1309
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:3390
MDB_dbi m_alt_blocks
Definition db_lmdb.h:472
virtual uint64_t add_block(const std::pair< block, blobdata > &blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const std::vector< std::pair< transaction, blobdata > > &txs)
handles the addition of a new block to BlockchainDB
Definition db_lmdb.cpp:4083
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:3803
uint64_t get_max_block_size()
get the max block size
Definition db_lmdb.cpp:2711
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)
remove data about a transaction
Definition db_lmdb.cpp:977
virtual void remove_txpool_tx(const crypto::hash &txid)
remove a txpool transaction
Definition db_lmdb.cpp:1919
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:3664
virtual uint64_t get_block_long_term_weight(const uint64_t &height) const
fetch a block's long term weight
Definition db_lmdb.cpp:2876
static constexpr uint64_t DEFAULT_MAPSIZE
Definition db_lmdb.h:497
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const
fetch a block's cumulative difficulty
Definition db_lmdb.cpp:2773
void migrate_4_5()
Definition db_lmdb.cpp:5581
MDB_dbi m_txs_prunable
Definition db_lmdb.h:458
void cleanup_batch()
Definition db_lmdb.cpp:3873
void remove_tx_outputs(const uint64_t tx_id, const transaction &tx)
Definition db_lmdb.cpp:1143
virtual bool check_pruning()
checks pruning was done correctly, iff enabled
Definition db_lmdb.cpp:2343
virtual void batch_commit()
Definition db_lmdb.cpp:3846
virtual uint64_t get_block_timestamp(const uint64_t &height) const
fetch a block's timestamp
Definition db_lmdb.cpp:2534
std::vector< uint64_t > get_block_info_64bit_fields(uint64_t start_height, size_t count, off_t offset) const
Definition db_lmdb.cpp:2657
void migrate_0_1()
Definition db_lmdb.cpp:4605
void remove_output(const uint64_t amount, const uint64_t &out_index)
Definition db_lmdb.cpp:1166
uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const
Definition db_lmdb.cpp:701
virtual bool lock()
acquires the BlockchainDB lock
Definition db_lmdb.cpp:1756
virtual uint8_t get_hard_fork_version(uint64_t height) const
checks which hardfork version a height is on
Definition db_lmdb.cpp:4429
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:3103
MDB_dbi m_txs_prunable_hash
Definition db_lmdb.h:459
virtual void add_spent_key(const crypto::key_image &k_image)
store a spent key
Definition db_lmdb.cpp:1258
virtual bool get_prunable_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const
fetches the prunable transaction blob with the given hash
Definition db_lmdb.cpp:3313
virtual void set_hard_fork_version(uint64_t height, uint8_t version)
sets which hardfork version a height is on
Definition db_lmdb.cpp:4410
virtual void remove_spent_key(const crypto::key_image &k_image)
remove a spent key
Definition db_lmdb.cpp:1275
uint64_t num_outputs() const
Definition db_lmdb.cpp:2994
MDB_dbi m_txpool_blob
Definition db_lmdb.h:470
boost::thread::id m_writer
Definition db_lmdb.h:484
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index, bool include_commitmemt) const
get some of an output's data
Definition db_lmdb.cpp:3451
virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob)
get an alternative block by hash
Definition db_lmdb.cpp:4470
virtual void check_hard_fork_info()
verify hard fork info in database
Definition db_lmdb.cpp:4389
virtual uint64_t get_top_block_timestamp() const
fetch the top block's timestamp
Definition db_lmdb.cpp:2619
virtual void safesyncmode(const bool onoff)
toggle safe syncs for the DB
Definition db_lmdb.cpp:1661
virtual void remove_alt_block(const crypto::hash &blkid)
remove an alternative block
Definition db_lmdb.cpp:4499
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:1822
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:3343
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:1748
virtual bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata_ref *)> f, bool include_blob=false, relay_category category=relay_category::broadcasted) const
runs a function over all txpool transactions
Definition db_lmdb.cpp:2348
void check_open() const
Definition db_lmdb.cpp:552
virtual void block_wtxn_start()
Definition db_lmdb.cpp:4005
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:3508
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const
fetches the block with the given hash
Definition db_lmdb.cpp:2470
MDB_dbi m_tx_indices
Definition db_lmdb.h:461
virtual bool is_read_only() const
is BlockchainDB in read-only mode?
Definition db_lmdb.cpp:4552
virtual void close()
close the BlockchainDB
Definition db_lmdb.cpp:1629
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const
fetch a block blob by height
Definition db_lmdb.cpp:2508
void migrate_1_2()
Definition db_lmdb.cpp:5147
virtual uint64_t height() const
fetch the current blockchain height
Definition db_lmdb.cpp:2980
virtual void block_wtxn_stop()
Definition db_lmdb.cpp:4038
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:3727
mdb_txn_safe * m_write_txn
Definition db_lmdb.h:482
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:1120
MDB_dbi m_hf_versions
Definition db_lmdb.h:475
virtual uint64_t get_database_size() const
get disk space requirements
Definition db_lmdb.cpp:4565
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:2936
virtual uint64_t get_txpool_tx_count(relay_category category=relay_category::broadcasted) const
get the number of transactions in the txpool
Definition db_lmdb.cpp:1848
MDB_dbi m_blocks
Definition db_lmdb.h:452
virtual void remove_block()
remove data about the top block
Definition db_lmdb.cpp:853
virtual void fixup()
fix up anything that may be wrong due to past bugs
Definition db_lmdb.cpp:4574
virtual void drop_alt_blocks()
drop all alternative blocks
Definition db_lmdb.cpp:4538
virtual std::vector< std::string > get_filenames() const
get all files used by the BlockchainDB (if any)
Definition db_lmdb.cpp:1717
void add_max_block_size(uint64_t sz)
add a new max block size
Definition db_lmdb.cpp:2733
MDB_dbi m_block_info
Definition db_lmdb.h:454
MDB_dbi m_txs_pruned
Definition db_lmdb.h:457
uint64_t m_cum_size
Definition db_lmdb.h:479
void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes)
Definition db_lmdb.cpp:669
void migrate(const uint32_t oldversion)
Definition db_lmdb.cpp:5707
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:1053
virtual bool prune_blockchain(uint32_t pruning_seed=0)
prunes the blockchain
Definition db_lmdb.cpp:2333
virtual bool get_pruned_tx_blobs_from(const crypto::hash &h, size_t count, std::vector< cryptonote::blobdata > &bd) const
fetches a number of pruned transaction blob from the given hash, in canonical blockchain order
Definition db_lmdb.cpp:3169
virtual bool can_thread_bulk_indices() const
Definition db_lmdb.h:341
bool need_resize(uint64_t threshold_size=0) const
Definition db_lmdb.cpp:627
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const
fetch a block's hash
Definition db_lmdb.cpp:2899
static int compare_hash32(const MDB_val *a, const MDB_val *b)
Definition db_lmdb.cpp:152
void migrate_3_4()
Definition db_lmdb.cpp:5422
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:3581
virtual uint64_t get_tx_block_height(const crypto::hash &h) const
fetches the height of a transaction's block
Definition db_lmdb.cpp:3404
virtual void sync()
sync the BlockchainDB with disk
Definition db_lmdb.cpp:1645
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const
fetch a transaction's unlock time/height
Definition db_lmdb.cpp:3082
virtual void drop_hard_fork_info()
delete hard fork info from database
Definition db_lmdb.cpp:4393
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd, relay_category tx_category) const
get a txpool transaction's blob
Definition db_lmdb.cpp:1970
virtual bool remove_data_file(const std::string &folder) const
remove file(s) storing the database
Definition db_lmdb.cpp:1733
MDB_dbi m_hf_starting_heights
Definition db_lmdb.h:474
virtual bool for_all_alt_blocks(std::function< bool(const crypto::hash &blkid, const alt_block_data_t &data, const cryptonote::blobdata_ref *blob)> f, bool include_blob=false) const
runs a function over all alternative blocks stored
Definition db_lmdb.cpp:2397
Definition cryptonote_basic.h:307
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
std::tuple< uint64_t, uint64_t, std::vector< tools::wallet2::transfer_details > > outputs
Definition cold-outputs.cpp:53
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition lmdb.h:270
Lightning memory-mapped database library.
Definition base.py:1
Definition block_weight.py:1
Definition blocks.cpp:13
Definition check.py:1
POD_CLASS key_image
Definition crypto.h:95
POD_CLASS hash
Definition hash.h:49
Holds cryptonote related classes and helpers.
Definition blockchain_db.cpp:45
boost::multiprecision::uint128_t difficulty_type
Definition difficulty.h:41
relay_category
Definition blockchain_db.h:110
@ broadcasted
Public txes received via block/fluff.
Definition blockchain_db.h:111
struct cryptonote::mdb_threadinfo mdb_threadinfo
struct cryptonote::txindex txindex
std::string blobdata
Definition blobdatatype.h:39
std::pair< crypto::hash, uint64_t > tx_out_index
Definition blockchain_db.h:104
struct cryptonote::mdb_rflags mdb_rflags
struct cryptonote::mdb_txn_cursors mdb_txn_cursors
boost::string_ref blobdata_ref
Definition blobdatatype.h:40
Definition get_output_distribution.py:1
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
Definition mdb.c:1372
Definition mdb.c:1461
Definition mdb.c:1254
Generic structure used for passing keys and data in and out of the database.
Definition lmdb.h:286
Definition blockchain_db.h:143
Definition cryptonote_basic.h:560
Definition cryptonote_basic.h:577
Definition db_lmdb.h:97
bool m_rf_properties
Definition db_lmdb.h:116
bool m_rf_alt_blocks
Definition db_lmdb.h:114
bool m_rf_txs_pruned
Definition db_lmdb.h:105
bool m_rf_txs
Definition db_lmdb.h:104
bool m_rf_block_info
Definition db_lmdb.h:101
bool m_rf_txpool_meta
Definition db_lmdb.h:112
bool m_rf_blocks
Definition db_lmdb.h:99
bool m_rf_txs_prunable
Definition db_lmdb.h:106
bool m_rf_txs_prunable_tip
Definition db_lmdb.h:108
bool m_rf_tx_indices
Definition db_lmdb.h:109
bool m_rf_output_txs
Definition db_lmdb.h:102
bool m_rf_hf_versions
Definition db_lmdb.h:115
bool m_rf_txn
Definition db_lmdb.h:98
bool m_rf_block_heights
Definition db_lmdb.h:100
bool m_rf_txpool_blob
Definition db_lmdb.h:113
bool m_rf_spent_keys
Definition db_lmdb.h:111
bool m_rf_output_amounts
Definition db_lmdb.h:103
bool m_rf_txs_prunable_hash
Definition db_lmdb.h:107
bool m_rf_tx_outputs
Definition db_lmdb.h:110
Definition db_lmdb.h:120
MDB_txn * m_ti_rtxn
Definition db_lmdb.h:121
mdb_txn_cursors m_ti_rcursors
Definition db_lmdb.h:122
mdb_rflags m_ti_rflags
Definition db_lmdb.h:123
Definition db_lmdb.h:49
MDB_cursor * m_txc_tx_outputs
Definition db_lmdb.h:63
MDB_cursor * m_txc_txs_prunable
Definition db_lmdb.h:59
MDB_cursor * m_txc_hf_versions
Definition db_lmdb.h:72
MDB_cursor * m_txc_alt_blocks
Definition db_lmdb.h:70
MDB_cursor * m_txc_tx_indices
Definition db_lmdb.h:62
MDB_cursor * m_txc_blocks
Definition db_lmdb.h:50
MDB_cursor * m_txc_txpool_meta
Definition db_lmdb.h:67
MDB_cursor * m_txc_txs
Definition db_lmdb.h:57
MDB_cursor * m_txc_output_amounts
Definition db_lmdb.h:55
MDB_cursor * m_txc_output_txs
Definition db_lmdb.h:54
MDB_cursor * m_txc_txs_pruned
Definition db_lmdb.h:58
MDB_cursor * m_txc_block_heights
Definition db_lmdb.h:51
MDB_cursor * m_txc_block_info
Definition db_lmdb.h:52
MDB_cursor * m_txc_txs_prunable_tip
Definition db_lmdb.h:61
MDB_cursor * m_txc_properties
Definition db_lmdb.h:74
MDB_cursor * m_txc_txpool_blob
Definition db_lmdb.h:68
MDB_cursor * m_txc_spent_keys
Definition db_lmdb.h:65
MDB_cursor * m_txc_txs_prunable_hash
Definition db_lmdb.h:60
Definition db_lmdb.h:129
static void increment_txns(int)
Definition db_lmdb.cpp:471
static void prevent_new_txns()
Definition db_lmdb.cpp:456
bool m_batch_txn
Definition db_lmdb.h:161
void abort()
Definition db_lmdb.cpp:437
MDB_txn * m_txn
Definition db_lmdb.h:160
mdb_threadinfo * m_tinfo
Definition db_lmdb.h:159
static void allow_new_txns()
Definition db_lmdb.cpp:466
uint64_t num_active_tx() const
Definition db_lmdb.cpp:451
static void wait_no_active_txns()
Definition db_lmdb.cpp:461
static std::atomic< uint64_t > num_active_txns
Definition db_lmdb.h:163
mdb_txn_safe(const bool check=true)
Definition db_lmdb.cpp:373
bool m_check
Definition db_lmdb.h:162
void uncheck()
Definition db_lmdb.cpp:416
void commit(std::string message="")
Definition db_lmdb.cpp:422
static std::atomic_flag creation_gate
Definition db_lmdb.h:167
static uint64_t num_active_txns_per_thread
Definition db_lmdb.h:164
a struct containing output metadata
Definition blockchain_db.h:125
Definition blockchain_db.h:135
Definition cryptonote_basic.h:162
Definition db_lmdb.h:43
crypto::hash key
Definition db_lmdb.h:44
tx_data_t data
Definition db_lmdb.h:45
a struct containing txpool per transaction metadata
Definition blockchain_db.h:155
Definition rctTypes.h:79
std::string data
Definition base58.cpp:37
cryptonote::transaction tx
Definition transaction.cpp:40