Debian Codegraph @

src/blockchain_db/lmdb/db_lmdb.h

raw symbols SCIP
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
31 #include "blockchain_db/blockchain_db.h"
32 #include "cryptonote_basic/blobdatatype.h" // for type blobdata
33 #include "ringct/rctTypes.h"
34 #include <boost/thread/tss.hpp>
35
36 #include <lmdb.h>
37
38 #define ENABLE_AUTO_RESIZE
39
40 namespace cryptonote
41 {
42
43 typedef struct txindex {
44 crypto::hash key;
45 tx_data_t data;
46 } txindex;
47
48 typedef struct mdb_txn_cursors
49 {
50 MDB_cursor *m_txc_blocks;
51 MDB_cursor *m_txc_block_heights;
52 MDB_cursor *m_txc_block_info;
53
54 MDB_cursor *m_txc_output_txs;
55 MDB_cursor *m_txc_output_amounts;
56
57 MDB_cursor *m_txc_txs;
58 MDB_cursor *m_txc_txs_pruned;
59 MDB_cursor *m_txc_txs_prunable;
60 MDB_cursor *m_txc_txs_prunable_hash;
61 MDB_cursor *m_txc_txs_prunable_tip;
62 MDB_cursor *m_txc_tx_indices;
63 MDB_cursor *m_txc_tx_outputs;
64
65 MDB_cursor *m_txc_spent_keys;
66
67 MDB_cursor *m_txc_txpool_meta;
68 MDB_cursor *m_txc_txpool_blob;
69
70 MDB_cursor *m_txc_alt_blocks;
71
72 MDB_cursor *m_txc_hf_versions;
73
74 MDB_cursor *m_txc_properties;
75 } mdb_txn_cursors;
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
96 typedef struct mdb_rflags
97 {
98 bool m_rf_txn;
99 bool m_rf_blocks;
100 bool m_rf_block_heights;
101 bool m_rf_block_info;
102 bool m_rf_output_txs;
103 bool m_rf_output_amounts;
104 bool m_rf_txs;
105 bool m_rf_txs_pruned;
106 bool m_rf_txs_prunable;
107 bool m_rf_txs_prunable_hash;
108 bool m_rf_txs_prunable_tip;
109 bool m_rf_tx_indices;
110 bool m_rf_tx_outputs;
111 bool m_rf_spent_keys;
112 bool m_rf_txpool_meta;
113 bool m_rf_txpool_blob;
114 bool m_rf_alt_blocks;
115 bool m_rf_hf_versions;
116 bool m_rf_properties;
117 } mdb_rflags;
118
119 typedef 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
125 ~mdb_threadinfo();
126 } mdb_threadinfo;
127
128 struct mdb_txn_safe
129 {
130 mdb_txn_safe(const bool check=true);
131 ~mdb_txn_safe();
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
159 mdb_threadinfo* m_tinfo;
160 MDB_txn* m_txn;
161 bool m_batch_txn = false;
162 bool m_check;
163 static std::atomic<uint64_t> num_active_txns;
164
165 // could use a mutex here, but this should be sufficient.
166 static std::atomic_flag creation_gate;
167 };
168
169
170 // If m_batch_active is set, a batch transaction exists beyond this class, such
171 // as a batch import with verification enabled, or possibly (later) a batch
172 // network sync.
173 //
174 // For some of the lookup methods, such as get_block_timestamp(), tx_exists(),
175 // and get_tx(), when m_batch_active is set, the lookup uses the batch
176 // transaction. This isn't only because the transaction is available, but it's
177 // necessary so that lookups include the database updates only present in the
178 // current batch write.
179 //
180 // A regular network sync without batch writes is expected to open a new read
181 // transaction, as those lookups are part of the validation done prior to the
182 // write for block and tx data, so no write transaction is open at the time.
183 class BlockchainLMDB : public BlockchainDB
184 {
185 public:
186 BlockchainLMDB(bool batch_transactions=true);
187 ~BlockchainLMDB();
188
189 virtual void open(const std::string& filename, const int mdb_flags=0);
190
191 virtual void close();
192
193 virtual void sync();
194
195 virtual void safesyncmode(const bool onoff);
196
197 virtual void reset();
198
199 virtual std::vector<std::string> get_filenames() const;
200
201 virtual bool remove_data_file(const std::string& folder) const;
202
203 virtual std::string get_db_name() const;
204
205 virtual bool lock();
206
207 virtual void unlock();
208
209 virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
210
211 virtual uint64_t get_block_height(const crypto::hash& h) const;
212
213 virtual block_header get_block_header(const crypto::hash& h) const;
214
215 virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const;
216
217 virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const;
218
219 virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const;
220
221 virtual uint64_t get_block_timestamp(const uint64_t& height) const;
222
223 virtual uint64_t get_top_block_timestamp() const;
224
225 virtual size_t get_block_weight(const uint64_t& height) const;
226
227 virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const;
228
229 virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;
230
231 virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
232
233 virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector<difficulty_type>& new_cumulative_difficulties);
234
235 virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
236
237 virtual uint64_t get_block_long_term_weight(const uint64_t& height) const;
238
239 virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const;
240
241 virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const;
242
243 virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
244
245 virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
246
247 virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const;
248
249 virtual block get_top_block() const;
250
251 virtual uint64_t height() const;
252
253 virtual bool tx_exists(const crypto::hash& h) const;
254 virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const;
255
256 virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
257
258 virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
259 virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
260 virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const;
261 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;
262 virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
263 virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const;
264
265 virtual uint64_t get_tx_count() const;
266
267 virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
268
269 virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
270
271 virtual uint64_t get_num_outputs(const uint64_t& amount) const;
272
273 virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt) const;
274 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;
275
276 virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
277 virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
278 std::vector<tx_out_index> &tx_out_indices) const;
279
280 virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const;
281 virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const;
282
283 virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes) const;
284
285 virtual bool has_key_image(const crypto::key_image& img) const;
286
287 virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t& meta);
288 virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta);
289 virtual uint64_t get_txpool_tx_count(relay_category category = relay_category::broadcasted) const;
290 virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const;
291 virtual void remove_txpool_tx(const crypto::hash& txid);
292 virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
293 virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata& bd, relay_category tx_category) const;
294 virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid, relay_category tx_category) const;
295 virtual uint32_t get_blockchain_pruning_seed() const;
296 virtual bool prune_blockchain(uint32_t pruning_seed = 0);
297 virtual bool update_pruning();
298 virtual bool check_pruning();
299
300 virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob);
301 virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob);
302 virtual void remove_alt_block(const crypto::hash &blkid);
303 virtual uint64_t get_alt_block_count();
304 virtual void drop_alt_blocks();
305
306 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;
307
308 virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
309 virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const;
310 virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
311 virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)> f) const;
312 virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const;
313 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;
314
315 virtual uint64_t add_block( const std::pair<block, blobdata>& blk
316 , size_t block_weight
317 , uint64_t long_term_block_weight
318 , const difficulty_type& cumulative_difficulty
319 , const uint64_t& coins_generated
320 , const std::vector<std::pair<transaction, blobdata>>& txs
321 );
322
323 virtual void set_batch_transactions(bool batch_transactions);
324 virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
325 virtual void batch_commit();
326 virtual void batch_stop();
327 virtual void batch_abort();
328
329 virtual void block_wtxn_start();
330 virtual void block_wtxn_stop();
331 virtual void block_wtxn_abort();
332 virtual bool block_rtxn_start() const;
333 virtual void block_rtxn_stop() const;
334 virtual void block_rtxn_abort() const;
335
336 bool block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const;
337
338 virtual void pop_block(block& blk, std::vector<transaction>& txs);
339
340 virtual bool can_thread_bulk_indices() const { return true; }
341
342 /**
343 * @brief return a histogram of outputs on the blockchain
344 *
345 * @param amounts optional set of amounts to lookup
346 * @param unlocked whether to restrict count to unlocked outputs
347 * @param recent_cutoff timestamp to determine which outputs are recent
348 * @param min_count return only amounts with at least that many instances
349 *
350 * @return a set of amount/instances
351 */
352 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;
353
354 bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const;
355
356 // helper functions
357 static int compare_uint64(const MDB_val *a, const MDB_val *b);
358 static int compare_hash32(const MDB_val *a, const MDB_val *b);
359 static int compare_string(const MDB_val *a, const MDB_val *b);
360
361 private:
362 void do_resize(uint64_t size_increase=0);
363
364 bool need_resize(uint64_t threshold_size=0) const;
365 void check_and_resize_for_batch(uint64_t batch_num_blocks, uint64_t batch_bytes);
366 uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;
367
368 virtual void add_block( const block& blk
369 , size_t block_weight
370 , uint64_t long_term_block_weight
371 , const difficulty_type& cumulative_difficulty
372 , const uint64_t& coins_generated
373 , uint64_t num_rct_outs
374 , const crypto::hash& block_hash
375 );
376
377 virtual void remove_block();
378
379 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);
380
381 virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
382
383 virtual uint64_t add_output(const crypto::hash& tx_hash,
384 const tx_out& tx_output,
385 const uint64_t& local_index,
386 const uint64_t unlock_time,
387 const rct::key *commitment
388 );
389
390 virtual void add_tx_amount_output_indices(const uint64_t tx_id,
391 const std::vector<uint64_t>& amount_output_indices
392 );
393
394 void remove_tx_outputs(const uint64_t tx_id, const transaction& tx);
395
396 void remove_output(const uint64_t amount, const uint64_t& out_index);
397
398 virtual void prune_outputs(uint64_t amount);
399
400 virtual void add_spent_key(const crypto::key_image& k_image);
401
402 virtual void remove_spent_key(const crypto::key_image& k_image);
403
404 uint64_t num_outputs() const;
405
406 // Hard fork
407 virtual void set_hard_fork_version(uint64_t height, uint8_t version);
408 virtual uint8_t get_hard_fork_version(uint64_t height) const;
409 virtual void check_hard_fork_info();
410 virtual void drop_hard_fork_info();
411
412 inline void check_open() const;
413
414 bool prune_worker(int mode, uint32_t pruning_seed);
415
416 virtual bool is_read_only() const;
417
418 virtual uint64_t get_database_size() const;
419
420 std::vector<uint64_t> get_block_info_64bit_fields(uint64_t start_height, size_t count, off_t offset) const;
421
422 uint64_t get_max_block_size();
423 void add_max_block_size(uint64_t sz);
424
425 // fix up anything that may be wrong due to past bugs
426 virtual void fixup();
427
428 // migrate from older DB version to current
429 void migrate(const uint32_t oldversion);
430
431 // migrate from DB version 0 to 1
432 void migrate_0_1();
433
434 // migrate from DB version 1 to 2
435 void migrate_1_2();
436
437 // migrate from DB version 2 to 3
438 void migrate_2_3();
439
440 // migrate from DB version 3 to 4
441 void migrate_3_4();
442
443 // migrate from DB version 4 to 5
444 void migrate_4_5();
445
446 void cleanup_batch();
447
448 private:
449 MDB_env* m_env;
450
451 MDB_dbi m_blocks;
452 MDB_dbi m_block_heights;
453 MDB_dbi m_block_info;
454
455 MDB_dbi m_txs;
456 MDB_dbi m_txs_pruned;
457 MDB_dbi m_txs_prunable;
458 MDB_dbi m_txs_prunable_hash;
459 MDB_dbi m_txs_prunable_tip;
460 MDB_dbi m_tx_indices;
461 MDB_dbi m_tx_outputs;
462
463 MDB_dbi m_output_txs;
464 MDB_dbi m_output_amounts;
465
466 MDB_dbi m_spent_keys;
467
468 MDB_dbi m_txpool_meta;
469 MDB_dbi m_txpool_blob;
470
471 MDB_dbi m_alt_blocks;
472
473 MDB_dbi m_hf_starting_heights;
474 MDB_dbi m_hf_versions;
475
476 MDB_dbi m_properties;
477
478 mutable uint64_t m_cum_size; // used in batch size estimation
479 mutable unsigned int m_cum_count;
480 std::string m_folder;
481 mdb_txn_safe* m_write_txn; // may point to either a short-lived txn or a batch txn
482 mdb_txn_safe* m_write_batch_txn; // persist batch txn outside of BlockchainLMDB
483 boost::thread::id m_writer;
484
485 bool m_batch_transactions; // support for batch transactions
486 bool m_batch_active; // whether batch transaction is in progress
487
488 mdb_txn_cursors m_wcursors;
489 mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
490
491 #if defined(__arm__)
492 // force a value so it can compile with 32-bit ARM
493 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 31;
494 #else
495 #if defined(ENABLE_AUTO_RESIZE)
496 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 30;
497 #else
498 constexpr static uint64_t DEFAULT_MAPSIZE = 1LL << 33;
499 #endif
500 #endif
501
502 constexpr static float RESIZE_PERCENT = 0.9f;
503 };
504
505 } // namespace cryptonote