Monero
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 
100 namespace cryptonote
101 {
102 
104 typedef std::pair<crypto::hash, uint64_t> tx_out_index;
105 
108 
110 {
111  broadcasted = 0,
112  relayable,
113  legacy,
114  all
115 };
116 
117 bool matches_category(relay_method method, relay_category category) noexcept;
118 
119 #pragma pack(push, 1)
120 
125 {
130 };
131 #pragma pack(pop)
132 
133 #pragma pack(push, 1)
134 struct tx_data_t
135 {
139 };
140 #pragma pack(pop)
141 
143 {
149 };
150 
155 {
164  // 112 bytes
174 
175  uint8_t padding[76]; // till 192 bytes
176 
177  void set_relay_method(relay_method method) noexcept;
179 
181  bool upgrade_relay_method(relay_method method) noexcept;
182 
184  bool matches(const relay_category category) const noexcept
185  {
186  return matches_category(get_relay_method(), category);
187  }
188 };
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 
204 class DB_EXCEPTION : public std::exception
205 {
206  private:
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 
224 class 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 
274 class BLOCK_DNE : public DB_EXCEPTION
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 
314 class 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 
324 class TX_EXISTS : public DB_EXCEPTION
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 
334 class OUTPUT_DNE : public DB_EXCEPTION
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 {
380 private:
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 
558 protected:
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 
573  mutable uint64_t time_tx_exists = 0;
575  bool m_auto_remove_logs = true;
576 
578 
579 public:
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 
884  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const = 0;
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 
987  virtual uint64_t get_top_block_timestamp() const = 0;
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 
1040  virtual difficulty_type get_block_difficulty(const uint64_t& height) const = 0;
1041 
1050  virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector<difficulty_type>& new_cumulative_difficulties) = 0;
1051 
1064  virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const = 0;
1065 
1075  virtual uint64_t get_block_long_term_weight(const uint64_t& height) const = 0;
1076 
1086  virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0;
1087 
1100  virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const = 0;
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 
1343  virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
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 
1451  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const = 0;
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 
1608  virtual uint32_t get_blockchain_pruning_seed() const = 0;
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 
1632  virtual uint64_t get_max_block_size() = 0;
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 
1661  virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob) = 0;
1662 
1668  virtual void remove_alt_block(const crypto::hash &blkid) = 0;
1669 
1673  virtual uint64_t get_alt_block_count() = 0;
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 
1807  virtual uint8_t get_hard_fork_version(uint64_t height) const = 0;
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 {
1871 public:
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  }
1884  virtual ~db_txn_guard()
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 
1908 private:
1910  bool readonly;
1911  bool active;
1912 };
1913 
1914 class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} };
1915 class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} };
1916 
1917 BlockchainDB *new_db();
1918 
1919 } // namespace cryptonote
1920 
1921 #endif // BLOCKCHAIN_DB_H
DB_SYNC_FAILURE(const char *s)
Definition: blockchain_db.h:268
thrown when creating the BlockchainDB fails
Definition: blockchain_db.h:254
uint64_t tx_id
Definition: blockchain_db.h:136
DB_CREATE_FAILURE(const char *s)
Definition: blockchain_db.h:258
virtual void reset()=0
Remove everything from the BlockchainDB.
virtual void block_wtxn_stop()=0
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB&#39;s file(s) should be in
virtual void block_wtxn_start()=0
Definition: blockchain_db.h:134
Definition: blockchain_db.h:142
void reset_stats()
reset profiling stats
Definition: blockchain_db.cpp:411
OUTPUT_EXISTS(const char *s)
Definition: blockchain_db.h:348
virtual uint64_t get_indexing_base() const
return index of the first element (should be hidden, but isn&#39;t)
Definition: blockchain_db.h:1421
const char * what() const
Definition: blockchain_db.h:215
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
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
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition: blockchain_db.cpp:175
thrown when a transaction exists, but shouldn&#39;t, namely when adding a block
Definition: blockchain_db.h:324
virtual bool has_key_image(const crypto::key_image &img) const =0
check if a key image is stored as spent
uint8_t padding[76]
Definition: blockchain_db.h:175
thrown when opening the BlockchainDB fails
Definition: blockchain_db.h:244
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
virtual void drop_hard_fork_info()=0
delete hard fork info from database
DB_ERROR()
Definition: blockchain_db.h:227
thrown when a spent key image exists, but shouldn&#39;t, namely when adding a block
Definition: blockchain_db.h:354
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const =0
fetch a block&#39;s hash
int * count
Definition: gmock_stress_test.cc:176
crypto::hash max_used_block_id
Definition: blockchain_db.h:156
uint8_t dandelionpp_stem
Definition: blockchain_db.h:171
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
db_wtxn_guard(BlockchainDB *db)
Definition: blockchain_db.h:1915
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
DB_ERROR(const char *s)
Definition: blockchain_db.h:228
virtual uint64_t get_tx_count() const =0
fetches the total number of transactions ever
::std::string string
Definition: gtest-port.h:1097
virtual uint64_t get_tx_block_height(const crypto::hash &h) const =0
fetches the height of a transaction&#39;s block
virtual bool check_pruning()=0
checks pruning was done correctly, iff enabled
virtual uint64_t get_block_height(const crypto::hash &h) const =0
gets the height of the block with a given hash
Definition: cryptonote_basic.h:474
virtual void block_rtxn_stop() const =0
Definition: cryptonote_basic.h:457
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&#39;s outputs
A base class for BlockchainDB exceptions.
Definition: blockchain_db.h:204
virtual void block_wtxn_abort()=0
virtual std::vector< transaction > get_tx_list(const std::vector< crypto::hash > &hlist) const =0
fetches a list of transactions based on their hashes
Definition: blockchain_db.h:1869
bool readonly
Definition: blockchain_db.h:1910
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const =0
fetch a block&#39;s cumulative difficulty
virtual void safesyncmode(const bool onoff)=0
toggle safe syncs for the DB
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 std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks&#39; weights
std::string data
Definition: base58.cpp:37
virtual transaction get_pruned_tx(const crypto::hash &h) const
fetches the transaction base with the given hash
Definition: blockchain_db.cpp:403
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks&#39; long term weights
thrown when a requested block does not exist
Definition: blockchain_db.h:274
const char * s
Definition: minissdp.c:596
const command_line::arg_descriptor< std::string > arg_db_sync_mode
Definition: blockchain_db.cpp:153
uint8_t do_not_relay
Definition: blockchain_db.h:167
virtual void unlock()=0
This function releases the BlockchainDB lock.
virtual uint32_t get_blockchain_pruning_seed() const =0
get the blockchain pruning seed
Definition: hardfork.h:39
virtual uint64_t get_max_block_size()=0
get the max block size
DB_ERROR_TXN_START(const char *s)
Definition: blockchain_db.h:238
Non-owning sequence of data. Does not deep copy.
Definition: span.h:54
BLOCK_EXISTS(const char *s)
Definition: blockchain_db.h:298
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
std::tuple< uint64_t, uint64_t, std::vector< tools::wallet2::transfer_details > > outputs
Definition: cold-outputs.cpp:53
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 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 &#39;difficulty drift&#39;...
virtual void add_spent_key(const crypto::key_image &k_image)=0
store a spent key
virtual void add_max_block_size(uint64_t sz)=0
add a new max block size
BlockchainDB * new_db()
Definition: blockchain_db.cpp:164
thrown when an output exists, but shouldn&#39;t, namely when adding a block
Definition: blockchain_db.h:344
Definition: blocks.cpp:12
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)=0
remove data about a transaction
unsigned char uint8_t
Definition: stdint.h:124
BLOCK_DNE()
Definition: blockchain_db.h:277
virtual transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
Definition: blockchain_db.cpp:395
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const =0
fetch a block&#39;s cumulative number of rct outputs
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
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
uint64_t block_id
Definition: blockchain_db.h:138
virtual void sync()=0
sync the BlockchainDB with disk
virtual void remove_spent_key(const crypto::key_image &k_image)=0
remove a spent key
BLOCK_PARENT_DNE()
Definition: blockchain_db.h:287
uint8_t is_local
Definition: blockchain_db.h:170
virtual void batch_abort()=0
aborts a batch transaction
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
desc
Definition: pymoduletest.py:79
A generic BlockchainDB exception.
Definition: blockchain_db.h:224
uint64_t receive_time
Definition: blockchain_db.h:162
virtual std::vector< crypto::hash > get_hashes_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of block hashes
uint64_t num_calls
a performance metric
Definition: blockchain_db.h:552
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 fixup()
fix up anything that may be wrong due to past bugs
Definition: blockchain_db.cpp:443
boost::string_ref blobdata_ref
Definition: blobdatatype.h:40
virtual bool lock()=0
acquires the BlockchainDB lock
uint64_t already_generated_coins
Definition: blockchain_db.h:148
KEY_IMAGE_EXISTS()
Definition: blockchain_db.h:357
TX_EXISTS()
Definition: blockchain_db.h:327
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:44
uint8_t pruned
Definition: blockchain_db.h:169
db_rtxn_guard(BlockchainDB *db)
Definition: blockchain_db.h:1914
void set_relay_method(relay_method method) noexcept
Definition: blockchain_db.cpp:78
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const =0
fetch a block&#39;s already generated coins
uint64_t fee
Definition: blockchain_db.h:159
virtual bool get_pruned_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the pruned transaction blob with the given hash
bool upgrade_relay_method(relay_method method) noexcept
Definition: blockchain_db.cpp:137
Definition: syncobj.h:81
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&#39;s data
virtual bool remove_data_file(const std::string &folder) const =0
remove file(s) storing the database
uint64_t cumulative_difficulty_low
Definition: blockchain_db.h:146
virtual void set_hard_fork(HardFork *hf)
Definition: blockchain_db.cpp:310
BLOCK_INVALID()
Definition: blockchain_db.h:307
virtual uint64_t get_top_block_timestamp() const =0
fetch the top block&#39;s timestamp
virtual uint64_t height() const =0
fetch the current blockchain height
bool active
Definition: blockchain_db.h:1911
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&#39;s outputs
unsigned int uint32_t
Definition: stdint.h:126
virtual ~BlockchainDB()
An empty destructor.
Definition: blockchain_db.h:589
virtual void close()=0
close the BlockchainDB
virtual void remove_txpool_tx(const crypto::hash &txid)=0
remove a txpool transaction
void stop()
Definition: blockchain_db.h:1888
uint64_t time_add_transaction
a performance metric
Definition: blockchain_db.h:555
virtual bool tx_exists(const crypto::hash &h) const =0
check if a transaction with a given hash exists
uint64_t weight
Definition: blockchain_db.h:158
bool matches(const relay_category category) const noexcept
See relay_category description.
Definition: blockchain_db.h:184
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const =0
fetches the block with the given hash
DB_OPEN_FAILURE(const char *s)
Definition: blockchain_db.h:248
virtual void set_batch_transactions(bool)=0
sets whether or not to batch transactions
bool is_open() const
Gets the current open/ready state of the BlockchainDB.
Definition: blockchain_db.cpp:332
virtual bool block_rtxn_start() const =0
crypto::public_key pubkey
the output&#39;s public key (for spend verification)
Definition: blockchain_db.h:126
uint64_t last_failed_height
Definition: blockchain_db.h:161
void set_auto_remove_logs(bool auto_remove)
set whether or not to automatically remove logs
Definition: blockchain_db.h:1862
relay_category::broadcasted + relay_method::none for rpc relay requests or historical reasons ...
Definition: rctTypes.h:79
Definition: blockchain_db.h:1914
unsigned __int64 uint64_t
Definition: stdint.h:136
virtual bool get_prunable_tx_hash(const crypto::hash &tx_hash, crypto::hash &prunable_hash) const =0
fetches the prunable transaction hash
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 unlock_time
Definition: blockchain_db.h:137
uint64_t height
the height of the block which created the output
Definition: blockchain_db.h:128
std::string m
Definition: blockchain_db.h:207
Everything in the db.
virtual uint64_t get_block_timestamp(const uint64_t &height) const =0
fetch a block&#39;s timestamp
thrown when there is an error starting a DB transaction
Definition: blockchain_db.h:234
BlockchainDB * db
Definition: blockchain_db.h:1909
virtual block get_block_from_height(const uint64_t &height) const
fetch a block by height
Definition: blockchain_db.cpp:353
#define false
Definition: stdbool.h:37
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 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
POD_CLASS public_key
Definition: crypto.h:61
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const =0
fetch a transaction&#39;s unlock time/height
version
Supported socks variants.
Definition: socks.h:57
virtual uint64_t get_num_outputs(const uint64_t &amount) const =0
fetches the number of outputs of a given amount
rct::key commitment
the output&#39;s amount commitment (for spend verification)
Definition: blockchain_db.h:129
virtual void batch_stop()=0
ends a batch transaction
DB_OPEN_FAILURE()
Definition: blockchain_db.h:247
virtual void prune_outputs(uint64_t amount)=0
prune output data for the given amount
uint8_t bf_padding
Definition: blockchain_db.h:173
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 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
virtual size_t get_block_weight(const uint64_t &height) const =0
fetch a block&#39;s weight
Every tx not marked relay_method::none
BlockchainDB()
An empty constructor.
Definition: blockchain_db.h:584
virtual ~DB_EXCEPTION()
Definition: blockchain_db.h:213
std::string blobdata
Definition: blobdatatype.h:39
thrown when a requested transaction does not exist
Definition: blockchain_db.h:314
uint64_t max_used_block_height
Definition: blockchain_db.h:160
BLOCK_DNE(const char *s)
Definition: blockchain_db.h:278
Definition: base.py:1
Definition: block_weight.py:1
virtual void block_rtxn_abort() const =0
OUTPUT_EXISTS()
Definition: blockchain_db.h:347
virtual bool can_thread_bulk_indices() const =0
OUTPUT_DNE(const char *s)
Definition: blockchain_db.h:338
DB_EXCEPTION(const char *s)
Definition: blockchain_db.h:210
uint64_t cumulative_difficulty_high
Definition: blockchain_db.h:147
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd, relay_category tx_category) const =0
get a txpool transaction&#39;s blob
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
KEY_IMAGE_EXISTS(const char *s)
Definition: blockchain_db.h:358
virtual void remove_block()=0
remove data about the top block
BLOCK_INVALID(const char *s)
Definition: blockchain_db.h:308
void remove_transaction(const crypto::hash &tx_hash)
helper function to remove transaction from the blockchain
Definition: blockchain_db.cpp:337
virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob)=0
get an alternative block by hash
static void init_options(boost::program_options::options_description &desc)
init command line options
Definition: blockchain_db.cpp:169
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:41
uint8_t double_spend_seen
Definition: blockchain_db.h:168
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
POD_CLASS key_image
Definition: crypto.h:92
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
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:154
DB_SYNC_FAILURE()
Definition: blockchain_db.h:267
virtual bool update_pruning()=0
prunes recent blockchain changes as needed, iff pruning is enabled
TX_DNE(const char *s)
Definition: blockchain_db.h:318
TX_DNE()
Definition: blockchain_db.h:317
thrown when a block&#39;s parent does not exist (and it needed to)
Definition: blockchain_db.h:284
crypto::hash last_failed_id
Definition: blockchain_db.h:157
void show_stats()
show profiling stats
Definition: blockchain_db.cpp:421
uint8_t is_forwarding
Definition: blockchain_db.h:172
bool m_auto_remove_logs
whether or not to automatically remove old logs
Definition: blockchain_db.h:575
virtual void remove_alt_block(const crypto::hash &blkid)=0
remove an alternative block
DB_CREATE_FAILURE()
Definition: blockchain_db.h:257
uint64_t cumulative_weight
Definition: blockchain_db.h:145
static uint64_t db_flags
Definition: blockchain_blackball.cpp:52
TX_EXISTS(const char *s)
Definition: blockchain_db.h:328
const command_line::arg_descriptor< bool > arg_db_salvage
Definition: blockchain_db.cpp:158
cryptonote::transaction tx
Definition: transaction.cpp:40
virtual void check_hard_fork_info()=0
verify hard fork info in database
epee::critical_section m_synchronization_lock
A lock, currently for when BlockchainLMDB needs to resize the backing db file.
Definition: blockchain_db.h:1865
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:378
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 bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const =0
runs a function over all key images stored
virtual block_header get_block_header(const crypto::hash &h) const =0
fetch a block header
HardFork * m_hardfork
Definition: blockchain_db.h:577
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const =0
get a txpool transaction&#39;s metadata
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 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
uint64_t time_tx_exists
a performance metric
Definition: blockchain_db.h:573
uint8_t kept_by_block
Definition: blockchain_db.h:165
virtual difficulty_type get_block_difficulty(const uint64_t &height) const =0
fetch a block&#39;s difficulty
bool matches_category(relay_method method, relay_category category) noexcept
Definition: blockchain_db.cpp:47
uint64_t time_blk_hash
a performance metric
Definition: blockchain_db.h:553
relay_method
Methods tracking how a tx was received and relayed.
Definition: enums.h:36
POD_CLASS hash
Definition: hash.h:49
virtual void drop_alt_blocks()=0
drop all alternative blocks
virtual block get_block(const crypto::hash &h) const
fetches the block with the given hash
Definition: blockchain_db.cpp:363
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const =0
fetch a block blob by height
relay_method get_relay_method() const noexcept
Definition: blockchain_db.cpp:109
std::pair< crypto::hash, uint64_t > tx_out_index
Definition: blockchain_db.h:104
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const =0
checks if a block exists
uint64_t time_add_block1
a performance metric
Definition: blockchain_db.h:554
uint8_t relayed
Definition: blockchain_db.h:166
virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &details)=0
update a txpool transaction&#39;s metadata
virtual uint64_t get_alt_block_count()=0
get the number of alternative blocks stored
virtual std::vector< std::string > get_filenames() const =0
get all files used by the BlockchainDB (if any)
OUTPUT_DNE()
Definition: blockchain_db.h:337
Definition: cryptonote_basic.h:157
BLOCK_PARENT_DNE(const char *s)
Definition: blockchain_db.h:288
uint64_t unlock_time
the output&#39;s unlock time (or height)
Definition: blockchain_db.h:127
void abort()
Definition: blockchain_db.h:1899
virtual uint64_t get_database_size() const =0
get disk space requirements
relay_category
Definition: blockchain_db.h:109
thrown when a requested output does not exist
Definition: blockchain_db.h:334
db_txn_guard(BlockchainDB *db, bool readonly)
Definition: blockchain_db.h:1872
thrown when a block exists, but shouldn&#39;t, namely when adding a block
Definition: blockchain_db.h:294
thrown when something is wrong with the block to be added
Definition: blockchain_db.h:304
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t &index) const =0
gets an output&#39;s tx hash and index
#define const
Definition: ipfrdr.c:80
thrown when synchronizing the BlockchainDB to disk fails
Definition: blockchain_db.h:264
virtual block get_top_block() const =0
fetch the top block
a struct containing output metadata
Definition: blockchain_db.h:124
static uint64_t h
Definition: blockchain_stats.cpp:55
Definition: cryptonote_basic.h:204
Public txes received via block/fluff.
virtual uint64_t get_block_long_term_weight(const uint64_t &height) const =0
fetch a block&#39;s long term weight
virtual bool is_read_only() const =0
is BlockchainDB in read-only mode?
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of blocks
virtual crypto::hash top_block_hash(uint64_t *block_height=NULL) const =0
fetch the top block&#39;s hash
Definition: blockchain_db.h:1915
virtual tx_out_index get_output_tx_and_index(const uint64_t &amount, const uint64_t &index) const =0
gets an output&#39;s tx hash and index
uint64_t height
Definition: blockchain_db.h:144
bool m_open
Whether or not the BlockchainDB is open/ready for use.
Definition: blockchain_db.h:1864
BLOCK_EXISTS()
Definition: blockchain_db.h:297
uint64_t time_commit1
a performance metric
Definition: blockchain_db.h:574
virtual bool prune_blockchain(uint32_t pruning_seed=0)=0
prunes the blockchain
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 uint8_t get_hard_fork_version(uint64_t height) const =0
checks which hardfork version a height is on
DB_ERROR_TXN_START()
Definition: blockchain_db.h:237
virtual ~db_txn_guard()
Definition: blockchain_db.h:1884