Electroneum
blockchain_db.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2020, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef BLOCKCHAIN_DB_H
30 #define BLOCKCHAIN_DB_H
31 
32 #pragma once
33 
34 #include <string>
35 #include <exception>
36 #include <boost/program_options.hpp>
37 #include "common/command_line.h"
38 #include "crypto/hash.h"
43 
100 namespace cryptonote
101 {
102 
104 typedef std::pair<crypto::hash, uint64_t> tx_out_index;
105 
109 
110 #pragma pack(push, 1)
111 
116 {
118  uint64_t unlock_time;
119  uint64_t height;
121 };
122 #pragma pack(pop)
123 
124 #pragma pack(push, 1)
125 struct tx_data_t
126 {
127  uint64_t tx_id;
128  uint64_t unlock_time;
129  uint64_t block_id;
130 };
131 #pragma pack(pop)
132 
137 {
140  uint64_t weight;
141  uint64_t fee;
144  uint64_t receive_time;
146  // 112 bytes
147  uint8_t kept_by_block;
148  uint8_t relayed;
149  uint8_t do_not_relay;
150  uint8_t double_spend_seen: 1;
151  uint8_t bf_padding: 7;
152 
153  uint8_t padding[76]; // till 192 bytes
154 };
155 
156 #define DBF_SAFE 1
157 #define DBF_FAST 2
158 #define DBF_FASTEST 4
159 #define DBF_RDONLY 8
160 #define DBF_SALVAGE 0x10
161 
162 /***********************************
163  * Exception Definitions
164  ***********************************/
165 
169 class DB_EXCEPTION : public std::exception
170 {
171  private:
172  std::string m;
173 
174  protected:
175  DB_EXCEPTION(const char *s) : m(s) { }
176 
177  public:
178  virtual ~DB_EXCEPTION() { }
179 
180  const char* what() const throw()
181  {
182  return m.c_str();
183  }
184 };
185 
189 class DB_ERROR : public DB_EXCEPTION
190 {
191  public:
192  DB_ERROR() : DB_EXCEPTION("Generic DB Error") { }
193  DB_ERROR(const char* s) : DB_EXCEPTION(s) { }
194 };
195 
200 {
201  public:
202  DB_ERROR_TXN_START() : DB_EXCEPTION("DB Error in starting txn") { }
203  DB_ERROR_TXN_START(const char* s) : DB_EXCEPTION(s) { }
204 };
205 
210 {
211  public:
212  DB_OPEN_FAILURE() : DB_EXCEPTION("Failed to open the db") { }
213  DB_OPEN_FAILURE(const char* s) : DB_EXCEPTION(s) { }
214 };
215 
220 {
221  public:
222  DB_CREATE_FAILURE() : DB_EXCEPTION("Failed to create the db") { }
223  DB_CREATE_FAILURE(const char* s) : DB_EXCEPTION(s) { }
224 };
225 
230 {
231  public:
232  DB_SYNC_FAILURE() : DB_EXCEPTION("Failed to sync the db") { }
233  DB_SYNC_FAILURE(const char* s) : DB_EXCEPTION(s) { }
234 };
235 
239 class BLOCK_DNE : public DB_EXCEPTION
240 {
241  public:
242  BLOCK_DNE() : DB_EXCEPTION("The block requested does not exist") { }
243  BLOCK_DNE(const char* s) : DB_EXCEPTION(s) { }
244 };
245 
250 {
251  public:
252  BLOCK_PARENT_DNE() : DB_EXCEPTION("The parent of the block does not exist") { }
253  BLOCK_PARENT_DNE(const char* s) : DB_EXCEPTION(s) { }
254 };
255 
260 {
261  public:
262  BLOCK_EXISTS() : DB_EXCEPTION("The block to be added already exists!") { }
263  BLOCK_EXISTS(const char* s) : DB_EXCEPTION(s) { }
264 };
265 
270 {
271  public:
272  BLOCK_INVALID() : DB_EXCEPTION("The block to be added did not pass validation!") { }
273  BLOCK_INVALID(const char* s) : DB_EXCEPTION(s) { }
274 };
275 
279 class TX_DNE : public DB_EXCEPTION
280 {
281  public:
282  TX_DNE() : DB_EXCEPTION("The transaction requested does not exist") { }
283  TX_DNE(const char* s) : DB_EXCEPTION(s) { }
284 };
285 
289 class TX_EXISTS : public DB_EXCEPTION
290 {
291  public:
292  TX_EXISTS() : DB_EXCEPTION("The transaction to be added already exists!") { }
293  TX_EXISTS(const char* s) : DB_EXCEPTION(s) { }
294 };
295 
299 class OUTPUT_DNE : public DB_EXCEPTION
300 {
301  public:
302  OUTPUT_DNE() : DB_EXCEPTION("The output requested does not exist!") { }
303  OUTPUT_DNE(const char* s) : DB_EXCEPTION(s) { }
304 };
305 
310 {
311  public:
312  OUTPUT_EXISTS() : DB_EXCEPTION("The output to be added already exists!") { }
313  OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
314 };
315 
320 {
321  public:
322  KEY_IMAGE_EXISTS() : DB_EXCEPTION("The spent key image to be added already exists!") { }
323  KEY_IMAGE_EXISTS(const char* s) : DB_EXCEPTION(s) { }
324 };
325 
326 /***********************************
327  * End of Exception Definitions
328  ***********************************/
329 
330 
344 {
345 private:
346  /*********************************************************************
347  * private virtual members
348  *********************************************************************/
349 
367  virtual void add_block( const block& blk
368  , size_t block_weight
369  , uint64_t long_term_block_weight
370  , const difficulty_type& cumulative_difficulty
371  , const uint64_t& coins_generated
372  , uint64_t num_rct_outs
373  , const crypto::hash& blk_hash
374  ) = 0;
375 
386  virtual void remove_block() = 0;
387 
408  virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) = 0;
409 
426  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0;
427 
454  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;
455 
469  virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector<uint64_t>& amount_output_indices) = 0;
470 
481  virtual void add_spent_key(const crypto::key_image& k_image) = 0;
482 
493  virtual void remove_spent_key(const crypto::key_image& k_image) = 0;
494 
495 
496  /*********************************************************************
497  * private concrete members
498  *********************************************************************/
505  void pop_block();
506 
507  // helper function to remove transaction from blockchain
515  void remove_transaction(const crypto::hash& tx_hash);
516 
517  uint64_t num_calls = 0;
518  uint64_t time_blk_hash = 0;
519  uint64_t time_add_block1 = 0;
520  uint64_t time_add_transaction = 0;
521 
522 
523 protected:
524 
536  void add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata>& tx, const crypto::hash* tx_hash_ptr = NULL, const crypto::hash* tx_prunable_hash_ptr = NULL);
537 
538  mutable uint64_t time_tx_exists = 0;
539  uint64_t time_commit1 = 0;
540  bool m_auto_remove_logs = true;
541 
543 
544 public:
545 
550 
554  virtual ~BlockchainDB() { };
555 
559  static void init_options(boost::program_options::options_description& desc);
560 
564  void reset_stats();
565 
572  void show_stats();
573 
597  virtual void open(const std::string& filename, const int db_flags = 0) = 0;
598 
604  bool is_open() const;
605 
616  virtual void close() = 0;
617 
629  virtual void sync() = 0;
630 
636  virtual void safesyncmode(const bool onoff) = 0;
637 
648  virtual void reset() = 0;
649 
659  virtual std::vector<std::string> get_filenames() const = 0;
660 
673  virtual bool remove_data_file(const std::string& folder) const = 0;
674 
675  // return the name of the folder the db's file(s) should reside in
684  virtual std::string get_db_name() const = 0;
685 
686 
687  // FIXME: these are just for functionality mocking, need to implement
688  // RAII-friendly and multi-read one-write friendly locking mechanism
689  //
690  // acquire db lock
706  virtual bool lock() = 0;
707 
708  // release db lock
719  virtual void unlock() = 0;
720 
740  virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0) = 0;
741 
755  virtual void batch_stop() = 0;
756 
770  virtual void batch_abort() = 0;
771 
787  virtual void set_batch_transactions(bool) = 0;
788 
789  virtual void block_wtxn_start() = 0;
790  virtual void block_wtxn_stop() = 0;
791  virtual void block_wtxn_abort() = 0;
792  virtual bool block_rtxn_start() const = 0;
793  virtual void block_rtxn_stop() const = 0;
794  virtual void block_rtxn_abort() const = 0;
795 
796  virtual void set_hard_fork(HardFork* hf);
797 
798  // adds a block with the given metadata to the top of the blockchain, returns the new height
820  virtual uint64_t add_block( const std::pair<block, blobdata>& blk
821  , size_t block_weight
822  , uint64_t long_term_block_weight
823  , const difficulty_type& cumulative_difficulty
824  , const uint64_t& coins_generated
825  , const std::vector<std::pair<transaction, blobdata>>& txs
826  );
827 
836  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const = 0;
837 
849  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const = 0;
850 
862  virtual block get_block(const crypto::hash& h) const;
863 
875  virtual uint64_t get_block_height(const crypto::hash& h) const = 0;
876 
889  virtual block_header get_block_header(const crypto::hash& h) const = 0;
890 
903  virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const = 0;
904 
915  virtual block get_block_from_height(const uint64_t& height) const;
916 
929  virtual uint64_t get_block_timestamp(const uint64_t& height) const = 0;
930 
943  virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const = 0;
944 
952  virtual uint64_t get_top_block_timestamp() const = 0;
953 
966  virtual size_t get_block_weight(const uint64_t& height) const = 0;
967 
977  virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const = 0;
978 
991  virtual void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff) = 0;
992 
1005  virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const = 0;
1006 
1019  virtual difficulty_type get_block_difficulty(const uint64_t& height) const = 0;
1020 
1033  virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const = 0;
1034 
1044  virtual uint64_t get_block_long_term_weight(const uint64_t& height) const = 0;
1045 
1055  virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0;
1056 
1069  virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const = 0;
1070 
1086  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1087 
1103  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1104 
1114  virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const = 0;
1115 
1123  virtual block get_top_block() const = 0;
1124 
1132  virtual uint64_t height() const = 0;
1133 
1134 
1155  virtual void pop_block(block& blk, std::vector<transaction>& txs);
1156 
1157 
1169  virtual bool tx_exists(const crypto::hash& h) const = 0;
1170  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_id) const = 0;
1171 
1172  // return unlock time of tx with hash <h>
1185  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const = 0;
1186 
1187  // return tx with hash <h>
1188  // throw if no such tx exists
1198  virtual transaction get_tx(const crypto::hash& h) const;
1199 
1209  virtual transaction get_pruned_tx(const crypto::hash& h) const;
1210 
1220  virtual bool get_tx(const crypto::hash& h, transaction &tx) const;
1221 
1231  virtual bool get_pruned_tx(const crypto::hash& h, transaction &tx) const;
1232 
1245  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1246 
1259  virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1260 
1274  virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1275 
1287  virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const = 0;
1288 
1297  virtual uint64_t get_tx_count() const = 0;
1298 
1314  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const = 0;
1315 
1316  // returns height of block that contains transaction with hash <h>
1329  virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
1330 
1331  // returns the total number of outputs of amount <amount>
1345  virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
1346 
1352  virtual uint64_t get_indexing_base() const { return 0; }
1353 
1370  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt = true) const = 0;
1371 
1382  virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const = 0;
1383 
1396  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const = 0;
1397 
1409  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;
1410 
1422  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;
1423 
1424  /*
1425  * FIXME: Need to check with git blame and ask what this does to
1426  * document it
1427  */
1428  virtual bool can_thread_bulk_indices() const = 0;
1429 
1445  virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes = 1) const = 0;
1446 
1454  virtual bool has_key_image(const crypto::key_image& img) const = 0;
1455 
1461  virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t& details) = 0;
1462 
1469  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) = 0;
1470 
1474  virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const = 0;
1475 
1479  virtual bool txpool_has_tx(const crypto::hash &txid) const = 0;
1480 
1486  virtual void remove_txpool_tx(const crypto::hash& txid) = 0;
1487 
1496  virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const = 0;
1497 
1506  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const = 0;
1507 
1515  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const = 0;
1516 
1522  virtual void prune_outputs(uint64_t amount) = 0;
1523 
1528  virtual uint32_t get_blockchain_pruning_seed() const = 0;
1529 
1535  virtual bool prune_blockchain(uint32_t pruning_seed = 0) = 0;
1536 
1541  virtual bool update_pruning() = 0;
1542 
1547  virtual bool check_pruning() = 0;
1548 
1552  virtual uint64_t get_max_block_size() = 0;
1553 
1562  virtual void add_max_block_size(uint64_t sz) = 0;
1576  virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = true) const = 0;
1577 
1591  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0;
1592 
1611  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;
1612 
1630  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const = 0;
1631 
1649  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;
1650  virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const = 0;
1651 
1652 
1653 
1654  //
1655  // Hard fork related storage
1656  //
1657 
1664  virtual void set_hard_fork_version(uint64_t height, uint8_t version) = 0;
1665 
1673  virtual uint8_t get_hard_fork_version(uint64_t height) const = 0;
1674 
1675  virtual void set_validator_list(std::string, uint32_t expiration_date) = 0;
1676 
1677  virtual std::string get_validator_list() const = 0;
1678 
1682  virtual void check_hard_fork_info() = 0;
1683 
1687  virtual void drop_hard_fork_info() = 0;
1688 
1699  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;
1700 
1701  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;
1702 
1708  virtual bool is_read_only() const = 0;
1709 
1715  virtual uint64_t get_database_size() const = 0;
1716 
1717  // TODO: this should perhaps be (or call) a series of functions which
1718  // progressively update through version updates
1722  virtual void fixup();
1723 
1732  void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; }
1733 
1734  bool m_open;
1735  mutable epee::critical_section m_synchronization_lock;
1736 
1737 }; // class BlockchainDB
1738 
1740 {
1741 public:
1743  {
1744  if (readonly)
1745  {
1746  active = db->block_rtxn_start();
1747  }
1748  else
1749  {
1750  db->block_wtxn_start();
1751  active = true;
1752  }
1753  }
1754  virtual ~db_txn_guard()
1755  {
1756  if (active)
1757  stop();
1758  }
1759  void stop()
1760  {
1761  if (readonly)
1762  db->block_rtxn_stop();
1763  else
1764  db->block_wtxn_stop();
1765  active = false;
1766  }
1767  void abort()
1768  {
1769  if (readonly)
1770  db->block_rtxn_abort();
1771  else
1772  db->block_wtxn_abort();
1773  active = false;
1774  }
1775 
1776 private:
1778  bool readonly;
1779  bool active;
1780 };
1781 
1782 class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} };
1783 class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} };
1784 
1785 BlockchainDB *new_db(const std::string& db_type);
1786 
1787 } // namespace cryptonote
1788 
1789 #endif // BLOCKCHAIN_DB_H
#define s(x, c)
Definition: aesb.c:47
uint8_t version
Definition: blockchain.cpp:90
static uint64_t db_flags
Definition: blockchain_blackball.cpp:57
thrown when a requested block does not exist
Definition: blockchain_db.h:240
BLOCK_DNE(const char *s)
Definition: blockchain_db.h:243
BLOCK_DNE()
Definition: blockchain_db.h:242
thrown when a block exists, but shouldn't, namely when adding a block
Definition: blockchain_db.h:260
BLOCK_EXISTS()
Definition: blockchain_db.h:262
BLOCK_EXISTS(const char *s)
Definition: blockchain_db.h:263
thrown when something is wrong with the block to be added
Definition: blockchain_db.h:270
BLOCK_INVALID(const char *s)
Definition: blockchain_db.h:273
BLOCK_INVALID()
Definition: blockchain_db.h:272
thrown when a block's parent does not exist (and it needed to)
Definition: blockchain_db.h:250
BLOCK_PARENT_DNE()
Definition: blockchain_db.h:252
BLOCK_PARENT_DNE(const char *s)
Definition: blockchain_db.h:253
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:344
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:554
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const =0
get a txpool transaction's blob
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:549
HardFork * m_hardfork
Definition: blockchain_db.h:542
virtual bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata *)>, bool include_blob=false, bool include_unrelayed_txes=true) const =0
runs a function over all txpool transactions
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 std::vector< crypto::hash > get_hashes_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of block hashes
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 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:259
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 block get_block_from_height(const uint64_t &height) const
fetch a block by height
Definition: blockchain_db.cpp:302
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:111
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
virtual uint64_t add_transaction_data(const crypto::hash &blk_hash, const std::pair< transaction, blobdata > &tx, const crypto::hash &tx_hash, const crypto::hash &tx_prunable_hash)=0
store the transaction and its metadata
uint64_t time_blk_hash
a performance metric
Definition: blockchain_db.h:518
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
void remove_transaction(const crypto::hash &tx_hash)
helper function to remove transaction from the blockchain
Definition: blockchain_db.cpp:286
virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes=true) const =0
get the number of transactions in the txpool
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 bool txpool_has_tx(const crypto::hash &txid) const =0
check whether a txid is in the txpool
bool m_open
Whether or not the BlockchainDB is open/ready for use.
Definition: blockchain_db.h:1734
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
uint64_t time_add_transaction
a performance metric
Definition: blockchain_db.h:520
virtual block get_block(const crypto::hash &h) const
fetches the block with the given hash
Definition: blockchain_db.cpp:312
void show_stats()
show profiling stats
Definition: blockchain_db.cpp:370
virtual void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff)=0
sets a block's cumulative difficulty
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:1732
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 uint64_t get_tx_block_height(const crypto::hash &h) const =0
fetches the height of a transaction's block
uint64_t time_add_block1
a performance metric
Definition: blockchain_db.h:519
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_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:540
virtual transaction get_pruned_tx(const crypto::hash &h) const
fetches the transaction base with the given hash
Definition: blockchain_db.cpp:352
virtual std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' weights
virtual bool is_read_only() const =0
is BlockchainDB in read-only mode?
virtual void block_wtxn_start()=0
virtual std::vector< std::string > get_filenames() const =0
get all files used by the BlockchainDB (if any)
virtual uint64_t height() const =0
fetch the current blockchain height
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of blocks
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:1735
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 void set_validator_list(std::string, uint32_t expiration_date)=0
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 bool for_all_outputs(uint64_t amount, const std::function< bool(uint64_t height)> &f) const =0
void add_transaction(const crypto::hash &blk_hash, const std::pair< transaction, blobdata > &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:125
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:281
virtual void fixup()
fix up anything that may be wrong due to past bugs
Definition: blockchain_db.cpp:392
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 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 void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &details)=0
add a txpool transaction
virtual size_t get_block_weight(const uint64_t &height) const =0
fetch a block's weight
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 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 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:1352
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
void reset_stats()
reset profiling stats
Definition: blockchain_db.cpp:360
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
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 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:517
virtual std::string get_validator_list() const =0
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 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 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 transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
Definition: blockchain_db.cpp:344
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
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition: blockchain_db.cpp:118
uint64_t time_tx_exists
a performance metric
Definition: blockchain_db.h:538
uint64_t time_commit1
a performance metric
Definition: blockchain_db.h:539
virtual void remove_block()=0
remove data about the top block
virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash &txid) const =0
get a txpool transaction's blob
thrown when creating the BlockchainDB fails
Definition: blockchain_db.h:220
DB_CREATE_FAILURE()
Definition: blockchain_db.h:222
DB_CREATE_FAILURE(const char *s)
Definition: blockchain_db.h:223
thrown when there is an error starting a DB transaction
Definition: blockchain_db.h:200
DB_ERROR_TXN_START(const char *s)
Definition: blockchain_db.h:203
DB_ERROR_TXN_START()
Definition: blockchain_db.h:202
A generic BlockchainDB exception.
Definition: blockchain_db.h:190
DB_ERROR()
Definition: blockchain_db.h:192
DB_ERROR(const char *s)
Definition: blockchain_db.h:193
A base class for BlockchainDB exceptions.
Definition: blockchain_db.h:170
virtual ~DB_EXCEPTION()
Definition: blockchain_db.h:178
std::string m
Definition: blockchain_db.h:172
const char * what() const
Definition: blockchain_db.h:180
DB_EXCEPTION(const char *s)
Definition: blockchain_db.h:175
thrown when opening the BlockchainDB fails
Definition: blockchain_db.h:210
DB_OPEN_FAILURE()
Definition: blockchain_db.h:212
DB_OPEN_FAILURE(const char *s)
Definition: blockchain_db.h:213
thrown when synchronizing the BlockchainDB to disk fails
Definition: blockchain_db.h:230
DB_SYNC_FAILURE(const char *s)
Definition: blockchain_db.h:233
DB_SYNC_FAILURE()
Definition: blockchain_db.h:232
Definition: hardfork.h:40
thrown when a spent key image exists, but shouldn't, namely when adding a block
Definition: blockchain_db.h:320
KEY_IMAGE_EXISTS(const char *s)
Definition: blockchain_db.h:323
KEY_IMAGE_EXISTS()
Definition: blockchain_db.h:322
thrown when a requested output does not exist
Definition: blockchain_db.h:300
OUTPUT_DNE()
Definition: blockchain_db.h:302
OUTPUT_DNE(const char *s)
Definition: blockchain_db.h:303
thrown when an output exists, but shouldn't, namely when adding a block
Definition: blockchain_db.h:310
OUTPUT_EXISTS()
Definition: blockchain_db.h:312
OUTPUT_EXISTS(const char *s)
Definition: blockchain_db.h:313
thrown when a requested transaction does not exist
Definition: blockchain_db.h:280
TX_DNE()
Definition: blockchain_db.h:282
TX_DNE(const char *s)
Definition: blockchain_db.h:283
thrown when a transaction exists, but shouldn't, namely when adding a block
Definition: blockchain_db.h:290
TX_EXISTS(const char *s)
Definition: blockchain_db.h:293
TX_EXISTS()
Definition: blockchain_db.h:292
Definition: blockchain_db.h:1782
db_rtxn_guard(BlockchainDB *db)
Definition: blockchain_db.h:1782
Definition: blockchain_db.h:1740
BlockchainDB * db
Definition: blockchain_db.h:1777
virtual ~db_txn_guard()
Definition: blockchain_db.h:1754
bool readonly
Definition: blockchain_db.h:1778
bool active
Definition: blockchain_db.h:1779
void abort()
Definition: blockchain_db.h:1767
db_txn_guard(BlockchainDB *db, bool readonly)
Definition: blockchain_db.h:1742
void stop()
Definition: blockchain_db.h:1759
Definition: blockchain_db.h:1783
db_wtxn_guard(BlockchainDB *db)
Definition: blockchain_db.h:1783
Definition: cryptonote_basic.h:205
Definition: base.py:1
POD_CLASS public_key
Definition: crypto.h:76
POD_CLASS key_image
Definition: crypto.h:102
POD_CLASS hash
Definition: hash.h:50
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:226
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
const command_line::arg_descriptor< bool > arg_db_salvage
Definition: blockchain_db.cpp:94
BlockchainDB * new_db(const std::string &db_type)
Definition: blockchain_db.cpp:100
const command_line::arg_descriptor< std::string > arg_db_type
Definition: blockchain_db.cpp:84
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:89
mdb_size_t count(MDB_cursor *cur)
Definition: value_stream.cpp:39
#define false
Definition: stdbool.h:38
Definition: cryptonote_basic.h:393
Definition: cryptonote_basic.h:410
a struct containing output metadata
Definition: blockchain_db.h:116
uint64_t unlock_time
the output's unlock time (or height)
Definition: blockchain_db.h:118
uint64_t height
the height of the block which created the output
Definition: blockchain_db.h:119
rct::key commitment
the output's amount commitment (for spend verification)
Definition: blockchain_db.h:120
crypto::public_key pubkey
the output's public key (for spend verification)
Definition: blockchain_db.h:117
Definition: blockchain_db.h:126
uint64_t block_id
Definition: blockchain_db.h:129
uint64_t unlock_time
Definition: blockchain_db.h:128
uint64_t tx_id
Definition: blockchain_db.h:127
Definition: cryptonote_basic.h:144
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:137
uint8_t bf_padding
Definition: blockchain_db.h:151
uint64_t weight
Definition: blockchain_db.h:140
uint64_t last_failed_height
Definition: blockchain_db.h:143
uint8_t do_not_relay
Definition: blockchain_db.h:149
uint8_t double_spend_seen
Definition: blockchain_db.h:150
uint64_t fee
Definition: blockchain_db.h:141
uint64_t receive_time
Definition: blockchain_db.h:144
uint8_t kept_by_block
Definition: blockchain_db.h:147
uint64_t last_relayed_time
Definition: blockchain_db.h:145
uint64_t max_used_block_height
Definition: blockchain_db.h:142
crypto::hash max_used_block_id
Definition: blockchain_db.h:138
crypto::hash last_failed_id
Definition: blockchain_db.h:139
uint8_t relayed
Definition: blockchain_db.h:148
uint8_t padding[76]
Definition: blockchain_db.h:153
Definition: rctTypes.h:78