Electroneum
blockchain_db.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, 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 
102 namespace cryptonote
103 {
104 
106 typedef std::pair<crypto::hash, uint64_t> tx_out_index;
107 
111 
112 #pragma pack(push, 1)
113 
118 {
123 };
124 #pragma pack(pop)
125 
126 #pragma pack(push, 1)
127 struct tx_data_t
128 {
132 };
133 #pragma pack(pop)
134 
139 {
148  // 112 bytes
155 
156  uint8_t padding[68]; // till 192 bytes
157 };
158 
160 {
163 };
164 
166 {
171 };
172 
174 {
177 
178  tx_input_t() : tx_hash(crypto::null_hash), in_index(0) { }
179 };
180 
181 #define DBF_SAFE 1
182 #define DBF_FAST 2
183 #define DBF_FASTEST 4
184 #define DBF_RDONLY 8
185 #define DBF_SALVAGE 0x10
186 
187 /***********************************
188  * Exception Definitions
189  ***********************************/
190 
194 class DB_EXCEPTION : public std::exception
195 {
196  private:
197  std::string m;
198 
199  protected:
200  DB_EXCEPTION(const char *s) : m(s) { }
201 
202  public:
203  virtual ~DB_EXCEPTION() { }
204 
205  const char* what() const throw()
206  {
207  return m.c_str();
208  }
209 };
210 
214 class DB_ERROR : public DB_EXCEPTION
215 {
216  public:
217  DB_ERROR() : DB_EXCEPTION("Generic DB Error") { }
218  DB_ERROR(const char* s) : DB_EXCEPTION(s) { }
219 };
220 
225 {
226  public:
227  DB_ERROR_TXN_START() : DB_EXCEPTION("DB Error in starting txn") { }
228  DB_ERROR_TXN_START(const char* s) : DB_EXCEPTION(s) { }
229 };
230 
235 {
236  public:
237  DB_OPEN_FAILURE() : DB_EXCEPTION("Failed to open the db") { }
238  DB_OPEN_FAILURE(const char* s) : DB_EXCEPTION(s) { }
239 };
240 
245 {
246  public:
247  DB_CREATE_FAILURE() : DB_EXCEPTION("Failed to create the db") { }
248  DB_CREATE_FAILURE(const char* s) : DB_EXCEPTION(s) { }
249 };
250 
255 {
256  public:
257  DB_SYNC_FAILURE() : DB_EXCEPTION("Failed to sync the db") { }
258  DB_SYNC_FAILURE(const char* s) : DB_EXCEPTION(s) { }
259 };
260 
264 class BLOCK_DNE : public DB_EXCEPTION
265 {
266  public:
267  BLOCK_DNE() : DB_EXCEPTION("The block requested does not exist") { }
268  BLOCK_DNE(const char* s) : DB_EXCEPTION(s) { }
269 };
270 
275 {
276  public:
277  BLOCK_PARENT_DNE() : DB_EXCEPTION("The parent of the block does not exist") { }
278  BLOCK_PARENT_DNE(const char* s) : DB_EXCEPTION(s) { }
279 };
280 
285 {
286  public:
287  BLOCK_EXISTS() : DB_EXCEPTION("The block to be added already exists!") { }
288  BLOCK_EXISTS(const char* s) : DB_EXCEPTION(s) { }
289 };
290 
295 {
296  public:
297  BLOCK_INVALID() : DB_EXCEPTION("The block to be added did not pass validation!") { }
298  BLOCK_INVALID(const char* s) : DB_EXCEPTION(s) { }
299 };
300 
304 class TX_DNE : public DB_EXCEPTION
305 {
306  public:
307  TX_DNE() : DB_EXCEPTION("The transaction requested does not exist") { }
308  TX_DNE(const char* s) : DB_EXCEPTION(s) { }
309 };
310 
314 class TX_EXISTS : public DB_EXCEPTION
315 {
316  public:
317  TX_EXISTS() : DB_EXCEPTION("The transaction to be added already exists!") { }
318  TX_EXISTS(const char* s) : DB_EXCEPTION(s) { }
319 };
320 
324 class OUTPUT_DNE : public DB_EXCEPTION
325 {
326  public:
327  OUTPUT_DNE() : DB_EXCEPTION("The output requested does not exist!") { }
328  OUTPUT_DNE(const char* s) : DB_EXCEPTION(s) { }
329 };
330 
335 {
336  public:
337  OUTPUT_EXISTS() : DB_EXCEPTION("The output to be added already exists!") { }
338  OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
339 };
340 
345 {
346  public:
347  KEY_IMAGE_EXISTS() : DB_EXCEPTION("The spent key image to be added already exists!") { }
348  KEY_IMAGE_EXISTS(const char* s) : DB_EXCEPTION(s) { }
349 };
350 
354 class UTXO_EXISTS : public DB_EXCEPTION
355 {
356 public:
357  UTXO_EXISTS() : DB_EXCEPTION("The utxo to be added already exists!") { }
358  UTXO_EXISTS(const char* s) : DB_EXCEPTION(s) { }
359 };
360 
365 {
366 public:
367  ADDR_OUTPUT_EXISTS() : DB_EXCEPTION("The output record for this address already exists!") { }
368  ADDR_OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
369 };
370 
371 /***********************************
372  * End of Exception Definitions
373  ***********************************/
374 
375 
389 {
390 private:
391  /*********************************************************************
392  * private virtual members
393  *********************************************************************/
394 
412  virtual void add_block( const block& blk
413  , size_t block_weight
414  , uint64_t long_term_block_weight
415  , const difficulty_type& cumulative_difficulty
416  , const uint64_t& coins_generated
417  , uint64_t num_rct_outs
418  , const crypto::hash& blk_hash
419  ) = 0;
420 
431  virtual void remove_block() = 0;
432 
453  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;
454 
471  virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0;
472 
499  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;
500 
514  virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector<uint64_t>& amount_output_indices) = 0;
515 
516  virtual void add_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key combined_key, uint64_t amount, uint64_t unlock_time, bool is_coinbase = false) = 0;
517  virtual void remove_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
518  virtual void add_addr_output(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key& combined_key, uint64_t amount, uint64_t unlock_time) = 0;
519  virtual void remove_addr_output(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::public_key& combined_key, uint64_t amount, uint64_t unlock_time) = 0;
520  virtual void add_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index, const crypto::hash parent_tx_hash, const uint64_t in_index) = 0;
521  virtual void remove_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
522 
533  virtual void add_spent_key(const crypto::key_image& k_image) = 0;
534 
545  virtual void remove_spent_key(const crypto::key_image& k_image) = 0;
546 
547 
548  /*********************************************************************
549  * private concrete members
550  *********************************************************************/
557  void pop_block();
558 
559  // helper function to remove transaction from blockchain
567  void remove_transaction(const crypto::hash& tx_hash);
568 
569  uint64_t num_calls = 0;
570  uint64_t time_blk_hash = 0;
571  uint64_t time_add_block1 = 0;
572  uint64_t time_add_transaction = 0;
573 
574 
575 protected:
576 
588  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);
589 
590  mutable uint64_t time_tx_exists = 0;
592  bool m_auto_remove_logs = true;
593 
595 
596 public:
597 
602 
606  virtual ~BlockchainDB() { };
607 
611  static void init_options(boost::program_options::options_description& desc);
612 
616  void reset_stats();
617 
624  void show_stats();
625 
649  virtual void open(const std::string& filename, const int db_flags = 0) = 0;
650 
656  bool is_open() const;
657 
668  virtual void close() = 0;
669 
681  virtual void sync() = 0;
682 
688  virtual void safesyncmode(const bool onoff) = 0;
689 
700  virtual void reset() = 0;
701 
711  virtual std::vector<std::string> get_filenames() const = 0;
712 
725  virtual bool remove_data_file(const std::string& folder) const = 0;
726 
727  // return the name of the folder the db's file(s) should reside in
736  virtual std::string get_db_name() const = 0;
737 
738 
739  // FIXME: these are just for functionality mocking, need to implement
740  // RAII-friendly and multi-read one-write friendly locking mechanism
741  //
742  // acquire db lock
758  virtual bool lock() = 0;
759 
760  // release db lock
771  virtual void unlock() = 0;
772 
792  virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0) = 0;
793 
807  virtual void batch_stop() = 0;
808 
822  virtual void batch_abort() = 0;
823 
839  virtual void set_batch_transactions(bool) = 0;
840 
841  virtual void block_wtxn_start() = 0;
842  virtual void block_wtxn_stop() = 0;
843  virtual void block_wtxn_abort() = 0;
844  virtual bool block_rtxn_start() const = 0;
845  virtual void block_rtxn_stop() const = 0;
846  virtual void block_rtxn_abort() const = 0;
847 
848  virtual void set_hard_fork(HardFork* hf);
849 
850  // adds a block with the given metadata to the top of the blockchain, returns the new height
872  virtual uint64_t add_block( const std::pair<block, blobdata>& blk
873  , size_t block_weight
874  , uint64_t long_term_block_weight
875  , const difficulty_type& cumulative_difficulty
876  , const uint64_t& coins_generated
877  , const std::vector<std::pair<transaction, blobdata>>& txs
878  );
879 
888  virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const = 0;
889 
901  virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const = 0;
902 
914  virtual block get_block(const crypto::hash& h) const;
915 
927  virtual uint64_t get_block_height(const crypto::hash& h) const = 0;
928 
941  virtual block_header get_block_header(const crypto::hash& h) const = 0;
942 
956 
967  virtual block get_block_from_height(const uint64_t& height) const;
968 
981  virtual uint64_t get_block_timestamp(const uint64_t& height) const = 0;
982 
995  virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const = 0;
996 
1004  virtual uint64_t get_top_block_timestamp() const = 0;
1005 
1018  virtual size_t get_block_weight(const uint64_t& height) const = 0;
1019 
1029  virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const = 0;
1030 
1044 
1058 
1072 
1086 
1097 
1107  virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0;
1108 
1122 
1138  virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1139 
1155  virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1156 
1166  virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const = 0;
1167 
1175  virtual block get_top_block() const = 0;
1176 
1184  virtual uint64_t height() const = 0;
1185 
1186 
1207  virtual void pop_block(block& blk, std::vector<transaction>& txs);
1208 
1209 
1221  virtual bool tx_exists(const crypto::hash& h) const = 0;
1222  virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_id) const = 0;
1223 
1224  // return unlock time of tx with hash <h>
1237  virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const = 0;
1238 
1239  // return tx with hash <h>
1240  // throw if no such tx exists
1250  virtual transaction get_tx(const crypto::hash& h) const;
1251 
1261  virtual transaction get_pruned_tx(const crypto::hash& h) const;
1262 
1272  virtual bool get_tx(const crypto::hash& h, transaction &tx) const;
1273 
1283  virtual bool get_pruned_tx(const crypto::hash& h, transaction &tx) const;
1284 
1297  virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1298 
1311  virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1312 
1326  virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1327 
1339  virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const = 0;
1340 
1349  virtual uint64_t get_tx_count() const = 0;
1350 
1366  virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const = 0;
1367 
1368  // returns height of block that contains transaction with hash <h>
1381  virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
1382 
1383  // returns the total number of outputs of amount <amount>
1397  virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
1398 
1404  virtual uint64_t get_indexing_base() const { return 0; }
1405 
1422  virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt = true) const = 0;
1423 
1435 
1448  virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const = 0;
1449 
1461  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;
1462 
1474  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;
1475 
1476  /*
1477  * FIXME: Need to check with git blame and ask what this does to
1478  * document it
1479  */
1480  virtual bool can_thread_bulk_indices() const = 0;
1481 
1497  virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes = 1) const = 0;
1498 
1506  virtual bool has_key_image(const crypto::key_image& img) const = 0;
1507 
1513  virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t& details) = 0;
1514 
1521  virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) = 0;
1522 
1526  virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const = 0;
1527 
1531  virtual bool txpool_has_tx(const crypto::hash &txid) const = 0;
1532 
1538  virtual void remove_txpool_tx(const crypto::hash& txid) = 0;
1539 
1548  virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const = 0;
1549 
1558  virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const = 0;
1559 
1567  virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const = 0;
1568 
1574  virtual void prune_outputs(uint64_t amount) = 0;
1575 
1581 
1587  virtual bool prune_blockchain(uint32_t pruning_seed = 0) = 0;
1588 
1593  virtual bool update_pruning() = 0;
1594 
1599  virtual bool check_pruning() = 0;
1600 
1605 
1614  virtual void add_max_block_size(uint64_t sz) = 0;
1628  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;
1629 
1643  virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0;
1644 
1663  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;
1664 
1682  virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const = 0;
1683 
1701  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;
1702  virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const = 0;
1703 
1704 
1705 
1706  //
1707  // Hard fork related storage
1708  //
1709 
1717 
1726 
1727  virtual void set_validator_list(std::string, uint32_t expiration_date) = 0;
1728 
1729  virtual std::string get_validator_list() const = 0;
1730 
1734  virtual void check_hard_fork_info() = 0;
1735 
1739  virtual void drop_hard_fork_info() = 0;
1740 
1751  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;
1752 
1753  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;
1754 
1760  virtual bool is_read_only() const = 0;
1761 
1767  virtual uint64_t get_database_size() const = 0;
1768 
1769  // TODO: this should perhaps be (or call) a series of functions which
1770  // progressively update through version updates
1774  virtual void fixup();
1775 
1784  void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; }
1785 
1786  virtual bool check_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1787  virtual uint64_t get_utxo_unlock_time(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1788  bool m_open;
1790 
1791  virtual std::vector<address_outputs> get_addr_output_all(const crypto::public_key& combined_key) = 0;
1792  virtual std::vector<address_outputs> get_addr_output_batch(const crypto::public_key& combined_key, uint64_t start_db_index = 0, uint64_t batch_size = 100, bool desc = false) = 0;
1793  virtual uint64_t get_balance(const crypto::public_key& combined_key) = 0;
1794  virtual tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1795 
1796 }; // class BlockchainDB
1797 
1799 {
1800 public:
1801  db_txn_guard(BlockchainDB *db, bool readonly): db(db), readonly(readonly), active(false)
1802  {
1803  if (readonly)
1804  {
1805  active = db->block_rtxn_start();
1806  }
1807  else
1808  {
1809  db->block_wtxn_start();
1810  active = true;
1811  }
1812  }
1813  virtual ~db_txn_guard()
1814  {
1815  if (active)
1816  stop();
1817  }
1818  void stop()
1819  {
1820  if (readonly)
1821  db->block_rtxn_stop();
1822  else
1823  db->block_wtxn_stop();
1824  active = false;
1825  }
1826  void abort()
1827  {
1828  if (readonly)
1829  db->block_rtxn_abort();
1830  else
1831  db->block_wtxn_abort();
1832  active = false;
1833  }
1834 
1835 private:
1836  BlockchainDB *db;
1837  bool readonly;
1838  bool active;
1839 };
1840 
1841 class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} };
1842 class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} };
1843 
1844 BlockchainDB *new_db(const std::string& db_type);
1845 
1846 } // namespace cryptonote
1847 
1848 #endif // BLOCKCHAIN_DB_H
thrown when an output record for an address already exists, but shouldn't, namely when adding a block
thrown when a requested block does not exist
BLOCK_DNE(const char *s)
thrown when a block exists, but shouldn't, namely when adding a block
BLOCK_EXISTS(const char *s)
thrown when something is wrong with the block to be added
BLOCK_INVALID(const char *s)
thrown when a block's parent does not exist (and it needed to)
The BlockchainDB backing store interface declaration/contract.
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.
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.
virtual bool check_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index)=0
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 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)
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
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
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
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
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 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.
virtual block get_block(const crypto::hash &h) const
fetches the block with the given hash
void show_stats()
show profiling stats
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
virtual std::vector< address_outputs > get_addr_output_batch(const crypto::public_key &combined_key, uint64_t start_db_index=0, uint64_t batch_size=100, bool desc=false)=0
void set_auto_remove_logs(bool auto_remove)
set whether or not to automatically remove logs
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
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
virtual transaction get_pruned_tx(const crypto::hash &h) const
fetches the transaction base with the given hash
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 tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index)=0
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.
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 std::vector< address_outputs > get_addr_output_all(const crypto::public_key &combined_key)=0
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
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.
virtual void fixup()
fix up anything that may be wrong due to past bugs
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)
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
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 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 transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
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 uint64_t get_utxo_unlock_time(const crypto::hash tx_hash, const uint32_t relative_out_index)=0
uint64_t time_tx_exists
a performance metric
uint64_t time_commit1
a performance metric
virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash &txid) const =0
get a txpool transaction's blob
virtual uint64_t get_balance(const crypto::public_key &combined_key)=0
thrown when creating the BlockchainDB fails
thrown when there is an error starting a DB transaction
A generic BlockchainDB exception.
DB_ERROR(const char *s)
A base class for BlockchainDB exceptions.
const char * what() const
DB_EXCEPTION(const char *s)
thrown when opening the BlockchainDB fails
DB_OPEN_FAILURE(const char *s)
thrown when synchronizing the BlockchainDB to disk fails
DB_SYNC_FAILURE(const char *s)
thrown when a spent key image exists, but shouldn't, namely when adding a block
thrown when a requested output does not exist
OUTPUT_DNE(const char *s)
thrown when an output exists, but shouldn't, namely when adding a block
OUTPUT_EXISTS(const char *s)
thrown when a requested transaction does not exist
TX_DNE(const char *s)
thrown when a transaction exists, but shouldn't, namely when adding a block
TX_EXISTS(const char *s)
thrown when a utxo already exists, but shouldn't, namely when adding a block
UTXO_EXISTS(const char *s)
db_rtxn_guard(BlockchainDB *db)
db_txn_guard(BlockchainDB *db, bool readonly)
db_wtxn_guard(BlockchainDB *db)
Non-owning sequence of data. Does not deep copy.
Definition: span.h:57
crypto namespace.
Definition: crypto.cpp:58
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: ban.cpp:40
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
const command_line::arg_descriptor< bool > arg_db_salvage
BlockchainDB * new_db(const std::string &db_type)
const command_line::arg_descriptor< std::string > arg_db_type
bool is_coinbase(const transaction &tx)
std::string blobdata
Definition: blobdatatype.h:39
std::pair< crypto::hash, uint64_t > tx_out_index
const command_line::arg_descriptor< std::string > arg_db_sync_mode
mdb_size_t count(MDB_cursor *cur)
version
Supported socks variants.
Definition: socks.h:58
::std::string string
Definition: gtest-port.h:1097
#define false
Definition: stdbool.h:38
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136
crypto::public_key combined_key
a struct containing output metadata
uint64_t unlock_time
the output's unlock time (or height)
uint64_t height
the height of the block which created the output
rct::key commitment
the output's amount commitment (for spend verification)
crypto::public_key pubkey
the output's public key (for spend verification)
a struct containing txpool per transaction metadata