Monero
Loading...
Searching...
No Matches
blockchain_db.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, The Monero Project
2//
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#ifndef BLOCKCHAIN_DB_H
29#define BLOCKCHAIN_DB_H
30
31#pragma once
32
33#include <string>
34#include <exception>
35#include <boost/program_options.hpp>
36#include "common/command_line.h"
37#include "crypto/hash.h"
43
99
100namespace cryptonote
101{
102
104typedef std::pair<crypto::hash, uint64_t> tx_out_index;
105
108
116
117bool matches_category(relay_method method, relay_category category) noexcept;
118
119#pragma pack(push, 1)
120
131#pragma pack(pop)
132
133#pragma pack(push, 1)
140#pragma pack(pop)
141
150
189
190
191#define DBF_SAFE 1
192#define DBF_FAST 2
193#define DBF_FASTEST 4
194#define DBF_RDONLY 8
195#define DBF_SALVAGE 0x10
196
197/***********************************
198 * Exception Definitions
199 ***********************************/
200
204class DB_EXCEPTION : public std::exception
205{
206 private:
207 std::string m;
208
209 protected:
210 DB_EXCEPTION(const char *s) : m(s) { }
211
212 public:
213 virtual ~DB_EXCEPTION() { }
214
215 const char* what() const throw()
216 {
217 return m.c_str();
218 }
219};
220
224class DB_ERROR : public DB_EXCEPTION
225{
226 public:
227 DB_ERROR() : DB_EXCEPTION("Generic DB Error") { }
228 DB_ERROR(const char* s) : DB_EXCEPTION(s) { }
229};
230
235{
236 public:
237 DB_ERROR_TXN_START() : DB_EXCEPTION("DB Error in starting txn") { }
238 DB_ERROR_TXN_START(const char* s) : DB_EXCEPTION(s) { }
239};
240
245{
246 public:
247 DB_OPEN_FAILURE() : DB_EXCEPTION("Failed to open the db") { }
248 DB_OPEN_FAILURE(const char* s) : DB_EXCEPTION(s) { }
249};
250
255{
256 public:
257 DB_CREATE_FAILURE() : DB_EXCEPTION("Failed to create the db") { }
258 DB_CREATE_FAILURE(const char* s) : DB_EXCEPTION(s) { }
259};
260
265{
266 public:
267 DB_SYNC_FAILURE() : DB_EXCEPTION("Failed to sync the db") { }
268 DB_SYNC_FAILURE(const char* s) : DB_EXCEPTION(s) { }
269};
270
275{
276 public:
277 BLOCK_DNE() : DB_EXCEPTION("The block requested does not exist") { }
278 BLOCK_DNE(const char* s) : DB_EXCEPTION(s) { }
279};
280
285{
286 public:
287 BLOCK_PARENT_DNE() : DB_EXCEPTION("The parent of the block does not exist") { }
288 BLOCK_PARENT_DNE(const char* s) : DB_EXCEPTION(s) { }
289};
290
295{
296 public:
297 BLOCK_EXISTS() : DB_EXCEPTION("The block to be added already exists!") { }
298 BLOCK_EXISTS(const char* s) : DB_EXCEPTION(s) { }
299};
300
305{
306 public:
307 BLOCK_INVALID() : DB_EXCEPTION("The block to be added did not pass validation!") { }
308 BLOCK_INVALID(const char* s) : DB_EXCEPTION(s) { }
309};
310
314class TX_DNE : public DB_EXCEPTION
315{
316 public:
317 TX_DNE() : DB_EXCEPTION("The transaction requested does not exist") { }
318 TX_DNE(const char* s) : DB_EXCEPTION(s) { }
319};
320
325{
326 public:
327 TX_EXISTS() : DB_EXCEPTION("The transaction to be added already exists!") { }
328 TX_EXISTS(const char* s) : DB_EXCEPTION(s) { }
329};
330
335{
336 public:
337 OUTPUT_DNE() : DB_EXCEPTION("The output requested does not exist!") { }
338 OUTPUT_DNE(const char* s) : DB_EXCEPTION(s) { }
339};
340
345{
346 public:
347 OUTPUT_EXISTS() : DB_EXCEPTION("The output to be added already exists!") { }
348 OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
349};
350
355{
356 public:
357 KEY_IMAGE_EXISTS() : DB_EXCEPTION("The spent key image to be added already exists!") { }
358 KEY_IMAGE_EXISTS(const char* s) : DB_EXCEPTION(s) { }
359};
360
361/***********************************
362 * End of Exception Definitions
363 ***********************************/
364
365
379{
380private:
381 /*********************************************************************
382 * private virtual members
383 *********************************************************************/
384
402 virtual void add_block( const block& blk
403 , size_t block_weight
404 , uint64_t long_term_block_weight
405 , const difficulty_type& cumulative_difficulty
406 , const uint64_t& coins_generated
407 , uint64_t num_rct_outs
408 , const crypto::hash& blk_hash
409 ) = 0;
410
421 virtual void remove_block() = 0;
422
443 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) = 0;
444
461 virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0;
462
489 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) = 0;
490
504 virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector<uint64_t>& amount_output_indices) = 0;
505
516 virtual void add_spent_key(const crypto::key_image& k_image) = 0;
517
528 virtual void remove_spent_key(const crypto::key_image& k_image) = 0;
529
530
531 /*********************************************************************
532 * private concrete members
533 *********************************************************************/
540 void pop_block();
541
542 // helper function to remove transaction from blockchain
550 void remove_transaction(const crypto::hash& tx_hash);
551
556
557
558protected:
559
571 void add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash* tx_hash_ptr = NULL, const crypto::hash* tx_prunable_hash_ptr = NULL);
572
575 bool m_auto_remove_logs = true;
576
578
579public:
580
585
589 virtual ~BlockchainDB() { };
590
594 static void init_options(boost::program_options::options_description& desc);
595
599 void reset_stats();
600
607 void show_stats();
608
632 virtual void open(const std::string& filename, const int db_flags = 0) = 0;
633
639 bool is_open() const;
640
651 virtual void close() = 0;
652
664 virtual void sync() = 0;
665
671 virtual void safesyncmode(const bool onoff) = 0;
672
683 virtual void reset() = 0;
684
694 virtual std::vector<std::string> get_filenames() const = 0;
695
708 virtual bool remove_data_file(const std::string& folder) const = 0;
709
710 // return the name of the folder the db's file(s) should reside in
719 virtual std::string get_db_name() const = 0;
720
721
722 // FIXME: these are just for functionality mocking, need to implement
723 // RAII-friendly and multi-read one-write friendly locking mechanism
724 //
725 // acquire db lock
741 virtual bool lock() = 0;
742
743 // release db lock
754 virtual void unlock() = 0;
755
775 virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0) = 0;
776
790 virtual void batch_stop() = 0;
791
805 virtual void batch_abort() = 0;
806
822 virtual void set_batch_transactions(bool) = 0;
823
824 virtual void block_wtxn_start() = 0;
825 virtual void block_wtxn_stop() = 0;
826 virtual void block_wtxn_abort() = 0;
827 virtual bool block_rtxn_start() const = 0;
828 virtual void block_rtxn_stop() const = 0;
829 virtual void block_rtxn_abort() const = 0;
830
831 virtual void set_hard_fork(HardFork* hf);
832
833 // adds a block with the given metadata to the top of the blockchain, returns the new height
855 virtual uint64_t add_block( const std::pair<block, blobdata>& blk
856 , size_t block_weight
857 , uint64_t long_term_block_weight
858 , const difficulty_type& cumulative_difficulty
859 , const uint64_t& coins_generated
860 , const std::vector<std::pair<transaction, blobdata>>& txs
861 );
862
871 virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const = 0;
872
885
897 virtual block get_block(const crypto::hash& h) const;
898
910 virtual uint64_t get_block_height(const crypto::hash& h) const = 0;
911
924 virtual block_header get_block_header(const crypto::hash& h) const = 0;
925
939
950 virtual block get_block_from_height(const uint64_t& height) const;
951
964 virtual uint64_t get_block_timestamp(const uint64_t& height) const = 0;
965
978 virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const = 0;
979
988
1001 virtual size_t get_block_weight(const uint64_t& height) const = 0;
1002
1012 virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const = 0;
1013
1027
1041
1050 virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector<difficulty_type>& new_cumulative_difficulties) = 0;
1051
1065
1076
1086 virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0;
1087
1101
1117 virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1118
1134 virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1135
1145 virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const = 0;
1146
1154 virtual block get_top_block() const = 0;
1155
1163 virtual uint64_t height() const = 0;
1164
1165
1186 virtual void pop_block(block& blk, std::vector<transaction>& txs);
1187
1188
1200 virtual bool tx_exists(const crypto::hash& h) const = 0;
1201 virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_id) const = 0;
1202
1203 // return unlock time of tx with hash <h>
1216 virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const = 0;
1217
1218 // return tx with hash <h>
1219 // throw if no such tx exists
1229 virtual transaction get_tx(const crypto::hash& h) const;
1230
1240 virtual transaction get_pruned_tx(const crypto::hash& h) const;
1241
1251 virtual bool get_tx(const crypto::hash& h, transaction &tx) const;
1252
1262 virtual bool get_pruned_tx(const crypto::hash& h, transaction &tx) const;
1263
1276 virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1277
1290 virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1291
1306 virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const = 0;
1307
1328 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 = 0;
1329
1344
1356 virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const = 0;
1357
1366 virtual uint64_t get_tx_count() const = 0;
1367
1383 virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const = 0;
1384
1385 // returns height of block that contains transaction with hash <h>
1398 virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
1399
1400 // returns the total number of outputs of amount <amount>
1414 virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
1415
1421 virtual uint64_t get_indexing_base() const { return 0; }
1422
1439 virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt = true) const = 0;
1440
1452
1465 virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const = 0;
1466
1478 virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const = 0;
1479
1491 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 = 0;
1492
1493 /*
1494 * FIXME: Need to check with git blame and ask what this does to
1495 * document it
1496 */
1497 virtual bool can_thread_bulk_indices() const = 0;
1498
1514 virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes = 1) const = 0;
1515
1523 virtual bool has_key_image(const crypto::key_image& img) const = 0;
1524
1530 virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t& details) = 0;
1531
1538 virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) = 0;
1539
1544
1548 virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const = 0;
1549
1555 virtual void remove_txpool_tx(const crypto::hash& txid) = 0;
1556
1565 virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const = 0;
1566
1576 virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd, relay_category tx_category) const = 0;
1577
1585 virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid, relay_category tx_category) const = 0;
1586
1595 bool txpool_tx_matches_category(const crypto::hash& tx_hash, relay_category category);
1596
1602 virtual void prune_outputs(uint64_t amount) = 0;
1603
1609
1615 virtual bool prune_blockchain(uint32_t pruning_seed = 0) = 0;
1616
1621 virtual bool update_pruning() = 0;
1622
1627 virtual bool check_pruning() = 0;
1628
1633
1641 virtual void add_max_block_size(uint64_t sz) = 0;
1642
1650 virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob) = 0;
1651
1662
1668 virtual void remove_alt_block(const crypto::hash &blkid) = 0;
1669
1674
1678 virtual void drop_alt_blocks() = 0;
1679
1693 virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata_ref*)>, bool include_blob = false, relay_category category = relay_category::broadcasted) const = 0;
1694
1708 virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0;
1709
1728 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 = 0;
1729
1747 virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const = 0;
1748
1766 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 = 0;
1767 virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const = 0;
1768
1785 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 = 0;
1786
1787
1788 //
1789 // Hard fork related storage
1790 //
1791
1799
1808
1812 virtual void check_hard_fork_info() = 0;
1813
1817 virtual void drop_hard_fork_info() = 0;
1818
1829 virtual 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 = 0;
1830
1831 virtual bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const = 0;
1832
1838 virtual bool is_read_only() const = 0;
1839
1845 virtual uint64_t get_database_size() const = 0;
1846
1847 // TODO: this should perhaps be (or call) a series of functions which
1848 // progressively update through version updates
1852 virtual void fixup();
1853
1862 void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; }
1863
1864 bool m_open;
1866
1867}; // class BlockchainDB
1868
1870{
1871public:
1873 {
1874 if (readonly)
1875 {
1876 active = db->block_rtxn_start();
1877 }
1878 else
1879 {
1880 db->block_wtxn_start();
1881 active = true;
1882 }
1883 }
1885 {
1886 stop();
1887 }
1888 void stop()
1889 {
1890 if (active)
1891 {
1892 if (readonly)
1893 db->block_rtxn_stop();
1894 else
1895 db->block_wtxn_stop();
1896 active = false;
1897 }
1898 }
1899 void abort()
1900 {
1901 if (readonly)
1902 db->block_rtxn_abort();
1903 else
1904 db->block_wtxn_abort();
1905 active = false;
1906 }
1907
1908private:
1912};
1913
1916
1918
1919} // namespace cryptonote
1920
1921#endif // BLOCKCHAIN_DB_H
#define s(x, c)
Definition aesb.c:47
static uint64_t db_flags
Definition blockchain_blackball.cpp:52
static uint64_t h
Definition blockchain_stats.cpp:55
BLOCK_DNE(const char *s)
Definition blockchain_db.h:278
BLOCK_DNE()
Definition blockchain_db.h:277
BLOCK_EXISTS()
Definition blockchain_db.h:297
BLOCK_EXISTS(const char *s)
Definition blockchain_db.h:298
BLOCK_INVALID(const char *s)
Definition blockchain_db.h:308
BLOCK_INVALID()
Definition blockchain_db.h:307
BLOCK_PARENT_DNE()
Definition blockchain_db.h:287
BLOCK_PARENT_DNE(const char *s)
Definition blockchain_db.h:288
The BlockchainDB backing store interface declaration/contract.
Definition blockchain_db.h:379
virtual void add_block(const block &blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, uint64_t num_rct_outs, const crypto::hash &blk_hash)=0
add the block and metadata to the db
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const =0
fetch a block blob by height
virtual ~BlockchainDB()
An empty destructor.
Definition blockchain_db.h:589
virtual std::vector< std::vector< uint64_t > > get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes=1) const =0
gets output indices (amount-specific) for a transaction's outputs
virtual uint64_t get_max_block_size()=0
get the max block size
virtual bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const =0
runs a function over all key images stored
BlockchainDB()
An empty constructor.
Definition blockchain_db.h:584
HardFork * m_hardfork
Definition blockchain_db.h:577
virtual bool get_pruned_tx_blobs_from(const crypto::hash &h, size_t count, std::vector< cryptonote::blobdata > &bd) const =0
fetches a number of pruned transaction blob from the given hash, in canonical blockchain order
virtual uint8_t get_hard_fork_version(uint64_t height) const =0
checks which hardfork version a height is on
virtual void prune_outputs(uint64_t amount)=0
prune output data for the given amount
virtual void remove_spent_key(const crypto::key_image &k_image)=0
remove a spent key
virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const =0
check whether a txid is in the txpool and meets tx_category requirements
void add_transaction(const crypto::hash &blk_hash, const std::pair< transaction, blobdata_ref > &tx, const crypto::hash *tx_hash_ptr=NULL, const crypto::hash *tx_prunable_hash_ptr=NULL)
helper function for add_transactions, to add each individual transaction
Definition blockchain_db.cpp:182
virtual crypto::hash top_block_hash(uint64_t *block_height=NULL) const =0
fetch the top block's hash
virtual bool get_prunable_tx_hash(const crypto::hash &tx_hash, crypto::hash &prunable_hash) const =0
fetches the prunable transaction hash
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd, relay_category tx_category) const =0
get a txpool transaction's blob
virtual block_header get_block_header(const crypto::hash &h) const =0
fetch a block header
virtual void set_hard_fork(HardFork *hf)
Definition blockchain_db.cpp:310
virtual block get_block_from_height(const uint64_t &height) const
fetch a block by height
Definition blockchain_db.cpp:353
virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob)=0
add a new alternative block
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 =0
runs a function over all alternative blocks stored
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t &index) const =0
gets an output's tx hash and index
virtual uint64_t get_database_size() const =0
get disk space requirements
static void init_options(boost::program_options::options_description &desc)
init command line options
Definition blockchain_db.cpp:169
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
uint64_t time_blk_hash
a performance metric
Definition blockchain_db.h:553
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 =0
gets outputs' data
bool txpool_tx_matches_category(const crypto::hash &tx_hash, relay_category category)
Check if tx_hash relay status is in category.
Definition blockchain_db.cpp:1027
void remove_transaction(const crypto::hash &tx_hash)
helper function to remove transaction from the blockchain
Definition blockchain_db.cpp:337
virtual void close()=0
close the BlockchainDB
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)=0
remove data about a transaction
virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &details)=0
update a txpool transaction's metadata
virtual uint64_t get_tx_count() const =0
fetches the total number of transactions ever
virtual void batch_stop()=0
ends a batch transaction
virtual void get_output_tx_and_index(const uint64_t &amount, const std::vector< uint64_t > &offsets, std::vector< tx_out_index > &indices) const =0
gets some outputs' tx hashes and indices
virtual uint64_t get_block_height(const crypto::hash &h) const =0
gets the height of the block with a given hash
virtual void block_wtxn_stop()=0
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' long term weights
bool m_open
Whether or not the BlockchainDB is open/ready for use.
Definition blockchain_db.h:1864
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)=0
store an output
virtual std::vector< crypto::hash > get_hashes_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of block hashes
virtual void remove_alt_block(const crypto::hash &blkid)=0
remove an alternative block
virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t &details)=0
add a txpool transaction
uint64_t time_add_transaction
a performance metric
Definition blockchain_db.h:555
virtual block get_block(const crypto::hash &h) const
fetches the block with the given hash
Definition blockchain_db.cpp:363
void show_stats()
show profiling stats
Definition blockchain_db.cpp:421
virtual bool block_rtxn_start() const =0
void set_auto_remove_logs(bool auto_remove)
set whether or not to automatically remove logs
Definition blockchain_db.h:1862
virtual uint64_t get_tx_block_height(const crypto::hash &h) const =0
fetches the height of a transaction's block
virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob)=0
get an alternative block by hash
virtual void correct_block_cumulative_difficulties(const uint64_t &start_height, const std::vector< difficulty_type > &new_cumulative_difficulties)=0
correct blocks cumulative difficulties that were incorrectly calculated due to the 'difficulty drift'...
uint64_t time_add_block1
a performance metric
Definition blockchain_db.h:554
virtual 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 =0
return a histogram of outputs on the blockchain
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)=0
store the transaction and its metadata
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const =0
get a txpool transaction's metadata
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 =0
fetches a variable number of blocks and transactions from the given height, in canonical blockchain o...
virtual bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector< uint64_t > &distribution, uint64_t &base) const =0
virtual bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>, bool pruned) const =0
runs a function over all transactions stored
bool m_auto_remove_logs
whether or not to automatically remove old logs
Definition blockchain_db.h:575
virtual transaction get_pruned_tx(const crypto::hash &h) const
fetches the transaction base with the given hash
Definition blockchain_db.cpp:403
virtual bool is_read_only() const =0
is BlockchainDB in read-only mode?
virtual void block_wtxn_start()=0
virtual uint64_t height() const =0
fetch the current blockchain height
virtual bool can_thread_bulk_indices() const =0
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB's file(s) should be in
epee::critical_section m_synchronization_lock
A lock, currently for when BlockchainLMDB needs to resize the backing db file.
Definition blockchain_db.h:1865
virtual uint64_t get_top_block_timestamp() const =0
fetch the top block's timestamp
virtual void safesyncmode(const bool onoff)=0
toggle safe syncs for the DB
virtual void remove_txpool_tx(const crypto::hash &txid)=0
remove a txpool transaction
virtual block get_top_block() const =0
fetch the top block
virtual bool tx_exists(const crypto::hash &h, uint64_t &tx_id) const =0
virtual uint64_t get_num_outputs(const uint64_t &amount) const =0
fetches the number of outputs of a given amount
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const =0
fetch a block's already generated coins
virtual uint32_t get_blockchain_pruning_seed() const =0
get the blockchain pruning seed
virtual tx_out_index get_output_tx_and_index(const uint64_t &amount, const uint64_t &index) const =0
gets an output's tx hash and index
virtual bool remove_data_file(const std::string &folder) const =0
remove file(s) storing the database
virtual void sync()=0
sync the BlockchainDB with disk
virtual void drop_alt_blocks()=0
drop all alternative blocks
virtual bool for_all_outputs(uint64_t amount, const std::function< bool(uint64_t height)> &f) const =0
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const =0
fetch a block's cumulative difficulty
bool is_open() const
Gets the current open/ready state of the BlockchainDB.
Definition blockchain_db.cpp:332
virtual void fixup()
fix up anything that may be wrong due to past bugs
Definition blockchain_db.cpp:443
virtual bool has_key_image(const crypto::key_image &img) const =0
check if a key image is stored as spent
virtual bool tx_exists(const crypto::hash &h) const =0
check if a transaction with a given hash exists
virtual size_t get_block_weight(const uint64_t &height) const =0
fetch a block's weight
virtual std::vector< transaction > get_tx_list(const std::vector< crypto::hash > &hlist) const =0
fetches a list of transactions based on their hashes
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const =0
fetch a block's cumulative number of rct outputs
virtual void drop_hard_fork_info()=0
delete hard fork info from database
virtual void add_max_block_size(uint64_t sz)=0
add a new max block size
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const =0
fetch a transaction's unlock time/height
virtual void reset()=0
Remove everything from the BlockchainDB.
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of blocks
virtual void check_hard_fork_info()=0
verify hard fork info in database
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const =0
fetches the block with the given hash
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const =0
checks if a block exists
virtual bool check_pruning()=0
checks pruning was done correctly, iff enabled
virtual void batch_abort()=0
aborts a batch transaction
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0)=0
tells the BlockchainDB to start a new "batch" of blocks
virtual uint64_t get_indexing_base() const
return index of the first element (should be hidden, but isn't)
Definition blockchain_db.h:1421
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 =0
runs a function over a range of blocks
virtual bool get_pruned_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the pruned transaction blob with the given hash
virtual bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata_ref *)>, bool include_blob=false, relay_category category=relay_category::broadcasted) const =0
runs a function over all txpool transactions
void reset_stats()
reset profiling stats
Definition blockchain_db.cpp:411
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 =0
runs a function over all outputs stored
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index, bool include_commitmemt=true) const =0
get some of an output's data
virtual uint64_t get_alt_block_count()=0
get the number of alternative blocks stored
virtual bool lock()=0
acquires the BlockchainDB lock
virtual void add_spent_key(const crypto::key_image &k_image)=0
store a spent key
uint64_t num_calls
a performance metric
Definition blockchain_db.h:552
virtual difficulty_type get_block_difficulty(const uint64_t &height) const =0
fetch a block's difficulty
virtual uint64_t get_block_long_term_weight(const uint64_t &height) const =0
fetch a block's long term weight
virtual bool update_pruning()=0
prunes recent blockchain changes as needed, iff pruning is enabled
virtual void block_rtxn_stop() const =0
virtual uint64_t get_txpool_tx_count(relay_category tx_category=relay_category::broadcasted) const =0
get the number of transactions in the txpool
virtual void unlock()=0
This function releases the BlockchainDB lock.
virtual void block_rtxn_abort() const =0
virtual uint64_t get_block_timestamp(const uint64_t &height) const =0
fetch a block's timestamp
virtual bool prune_blockchain(uint32_t pruning_seed=0)=0
prunes the blockchain
virtual std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' weights
virtual void set_batch_transactions(bool)=0
sets whether or not to batch transactions
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const =0
fetch a block's hash
virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector< uint64_t > &amount_output_indices)=0
store amount output indices for a tx's outputs
virtual std::vector< std::string > get_filenames() const =0
get all files used by the BlockchainDB (if any)
virtual transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
Definition blockchain_db.cpp:395
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
virtual void block_wtxn_abort()=0
virtual bool get_prunable_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the prunable transaction blob with the given hash
virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash &txid, relay_category tx_category) const =0
get a txpool transaction's blob
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition blockchain_db.cpp:175
uint64_t time_tx_exists
a performance metric
Definition blockchain_db.h:573
uint64_t time_commit1
a performance metric
Definition blockchain_db.h:574
virtual void remove_block()=0
remove data about the top block
DB_CREATE_FAILURE()
Definition blockchain_db.h:257
DB_CREATE_FAILURE(const char *s)
Definition blockchain_db.h:258
DB_ERROR_TXN_START(const char *s)
Definition blockchain_db.h:238
DB_ERROR_TXN_START()
Definition blockchain_db.h:237
DB_ERROR()
Definition blockchain_db.h:227
DB_ERROR(const char *s)
Definition blockchain_db.h:228
virtual ~DB_EXCEPTION()
Definition blockchain_db.h:213
std::string m
Definition blockchain_db.h:207
DB_EXCEPTION(const char *s)
Definition blockchain_db.h:210
const char * what() const
Definition blockchain_db.h:215
DB_OPEN_FAILURE()
Definition blockchain_db.h:247
DB_OPEN_FAILURE(const char *s)
Definition blockchain_db.h:248
DB_SYNC_FAILURE(const char *s)
Definition blockchain_db.h:268
DB_SYNC_FAILURE()
Definition blockchain_db.h:267
Definition hardfork.h:40
KEY_IMAGE_EXISTS(const char *s)
Definition blockchain_db.h:358
KEY_IMAGE_EXISTS()
Definition blockchain_db.h:357
OUTPUT_DNE()
Definition blockchain_db.h:337
OUTPUT_DNE(const char *s)
Definition blockchain_db.h:338
OUTPUT_EXISTS()
Definition blockchain_db.h:347
OUTPUT_EXISTS(const char *s)
Definition blockchain_db.h:348
TX_DNE()
Definition blockchain_db.h:317
TX_DNE(const char *s)
Definition blockchain_db.h:318
TX_EXISTS(const char *s)
Definition blockchain_db.h:328
TX_EXISTS()
Definition blockchain_db.h:327
db_rtxn_guard(BlockchainDB *db)
Definition blockchain_db.h:1914
BlockchainDB * db
Definition blockchain_db.h:1909
virtual ~db_txn_guard()
Definition blockchain_db.h:1884
bool readonly
Definition blockchain_db.h:1910
bool active
Definition blockchain_db.h:1911
void abort()
Definition blockchain_db.h:1899
db_txn_guard(BlockchainDB *db, bool readonly)
Definition blockchain_db.h:1872
void stop()
Definition blockchain_db.h:1888
db_wtxn_guard(BlockchainDB *db)
Definition blockchain_db.h:1915
Definition cryptonote_basic.h:205
Definition syncobj.h:82
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
#define false
#define const
Definition ipfrdr.c:80
static int version
Definition mdb_load.c:29
Definition base.py:1
Definition block_weight.py:1
Definition blocks.cpp:13
POD_CLASS public_key
Definition crypto.h:64
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
const command_line::arg_descriptor< bool > arg_db_salvage
Definition blockchain_db.cpp:158
relay_category
Definition blockchain_db.h:110
@ legacy
relay_category::broadcasted + relay_method::none for rpc relay requests or historical reasons
Definition blockchain_db.h:113
@ relayable
Every tx not marked relay_method::none.
Definition blockchain_db.h:112
@ all
Everything in the db.
Definition blockchain_db.h:114
@ broadcasted
Public txes received via block/fluff.
Definition blockchain_db.h:111
relay_method
Methods tracking how a tx was received and relayed.
Definition enums.h:37
BlockchainDB * new_db()
Definition blockchain_db.cpp:164
bool matches_category(relay_method method, relay_category category) noexcept
Definition blockchain_db.cpp:47
std::string blobdata
Definition blobdatatype.h:39
std::pair< crypto::hash, uint64_t > tx_out_index
Definition blockchain_db.h:104
const command_line::arg_descriptor< std::string > arg_db_sync_mode
Definition blockchain_db.cpp:153
boost::string_ref blobdata_ref
Definition blobdatatype.h:40
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 command_line.h:53
Definition blockchain_db.h:143
uint64_t cumulative_difficulty_high
Definition blockchain_db.h:147
uint64_t cumulative_difficulty_low
Definition blockchain_db.h:146
uint64_t cumulative_weight
Definition blockchain_db.h:145
uint64_t already_generated_coins
Definition blockchain_db.h:148
uint64_t height
Definition blockchain_db.h:144
Definition cryptonote_basic.h:458
Definition cryptonote_basic.h:475
a struct containing output metadata
Definition blockchain_db.h:125
uint64_t unlock_time
the output's unlock time (or height)
Definition blockchain_db.h:127
uint64_t height
the height of the block which created the output
Definition blockchain_db.h:128
rct::key commitment
the output's amount commitment (for spend verification)
Definition blockchain_db.h:129
crypto::public_key pubkey
the output's public key (for spend verification)
Definition blockchain_db.h:126
Definition blockchain_db.h:135
uint64_t block_id
Definition blockchain_db.h:138
uint64_t unlock_time
Definition blockchain_db.h:137
uint64_t tx_id
Definition blockchain_db.h:136
Definition cryptonote_basic.h:158
a struct containing txpool per transaction metadata
Definition blockchain_db.h:155
uint8_t bf_padding
Definition blockchain_db.h:173
uint64_t weight
Definition blockchain_db.h:158
void set_relay_method(relay_method method) noexcept
Definition blockchain_db.cpp:78
uint8_t pruned
Definition blockchain_db.h:169
uint64_t last_failed_height
Definition blockchain_db.h:161
uint8_t do_not_relay
Definition blockchain_db.h:167
uint8_t double_spend_seen
Definition blockchain_db.h:168
relay_method get_relay_method() const noexcept
Definition blockchain_db.cpp:109
uint64_t fee
Definition blockchain_db.h:159
uint64_t receive_time
Definition blockchain_db.h:162
uint8_t kept_by_block
Definition blockchain_db.h:165
bool matches(const relay_category category) const noexcept
See relay_category description.
Definition blockchain_db.h:184
bool upgrade_relay_method(relay_method method) noexcept
Definition blockchain_db.cpp:137
uint64_t last_relayed_time
If received over i2p/tor, randomized forward time. If Dandelion++stem, randomized embargo time....
Definition blockchain_db.h:163
uint64_t max_used_block_height
Definition blockchain_db.h:160
crypto::hash max_used_block_id
Definition blockchain_db.h:156
crypto::hash last_failed_id
Definition blockchain_db.h:157
uint8_t relayed
Definition blockchain_db.h:166
uint8_t is_forwarding
Definition blockchain_db.h:172
uint8_t padding[76]
Definition blockchain_db.h:175
uint8_t is_local
Definition blockchain_db.h:170
uint8_t dandelionpp_stem
Definition blockchain_db.h:171
Definition rctTypes.h:79
std::string data
Definition base58.cpp:37
cryptonote::transaction tx
Definition transaction.cpp:40