Electroneum
Loading...
Searching...
No Matches
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
103namespace cryptonote
104{
105
107typedef std::pair<crypto::hash, uint64_t> tx_out_index;
108
113
114#pragma pack(push, 1)
115
126#pragma pack(pop)
127
128#pragma pack(push, 1)
135#pragma pack(pop)
136
160
166
174
176{
179
180 tx_input_t() : tx_hash(crypto::null_hash), in_index(0) {}
181
182 bool is_empty() const {
183 return tx_hash == crypto::null_hash && in_index == 0;
184 }
185};
186
187#define DBF_SAFE 1
188#define DBF_FAST 2
189#define DBF_FASTEST 4
190#define DBF_RDONLY 8
191#define DBF_SALVAGE 0x10
192#define DBF_ADDR_TX_SALVAGE 0x20
193
194/***********************************
195 * Exception Definitions
196 ***********************************/
197
201class DB_EXCEPTION : public std::exception
202{
203 private:
204 std::string m;
205
206 protected:
207 DB_EXCEPTION(const char *s) : m(s) { }
208
209 public:
210 virtual ~DB_EXCEPTION() { }
211
212 const char* what() const throw()
213 {
214 return m.c_str();
215 }
216};
217
221class DB_ERROR : public DB_EXCEPTION
222{
223 public:
224 DB_ERROR() : DB_EXCEPTION("Generic DB Error") { }
225 DB_ERROR(const char* s) : DB_EXCEPTION(s) { }
226};
227
232{
233 public:
234 DB_ERROR_TXN_START() : DB_EXCEPTION("DB Error in starting txn") { }
235 DB_ERROR_TXN_START(const char* s) : DB_EXCEPTION(s) { }
236};
237
242{
243 public:
244 DB_OPEN_FAILURE() : DB_EXCEPTION("Failed to open the db") { }
245 DB_OPEN_FAILURE(const char* s) : DB_EXCEPTION(s) { }
246};
247
252{
253 public:
254 DB_CREATE_FAILURE() : DB_EXCEPTION("Failed to create the db") { }
255 DB_CREATE_FAILURE(const char* s) : DB_EXCEPTION(s) { }
256};
257
262{
263 public:
264 DB_SYNC_FAILURE() : DB_EXCEPTION("Failed to sync the db") { }
265 DB_SYNC_FAILURE(const char* s) : DB_EXCEPTION(s) { }
266};
267
272{
273 public:
274 BLOCK_DNE() : DB_EXCEPTION("The block requested does not exist") { }
275 BLOCK_DNE(const char* s) : DB_EXCEPTION(s) { }
276};
277
282{
283 public:
284 BLOCK_PARENT_DNE() : DB_EXCEPTION("The parent of the block does not exist") { }
285 BLOCK_PARENT_DNE(const char* s) : DB_EXCEPTION(s) { }
286};
287
292{
293 public:
294 BLOCK_EXISTS() : DB_EXCEPTION("The block to be added already exists!") { }
295 BLOCK_EXISTS(const char* s) : DB_EXCEPTION(s) { }
296};
297
302{
303 public:
304 BLOCK_INVALID() : DB_EXCEPTION("The block to be added did not pass validation!") { }
305 BLOCK_INVALID(const char* s) : DB_EXCEPTION(s) { }
306};
307
311class TX_DNE : public DB_EXCEPTION
312{
313 public:
314 TX_DNE() : DB_EXCEPTION("The transaction requested does not exist") { }
315 TX_DNE(const char* s) : DB_EXCEPTION(s) { }
316};
317
322{
323 public:
324 TX_EXISTS() : DB_EXCEPTION("The transaction to be added already exists!") { }
325 TX_EXISTS(const char* s) : DB_EXCEPTION(s) { }
326};
327
332{
333 public:
334 OUTPUT_DNE() : DB_EXCEPTION("The output requested does not exist!") { }
335 OUTPUT_DNE(const char* s) : DB_EXCEPTION(s) { }
336};
337
342{
343 public:
344 OUTPUT_EXISTS() : DB_EXCEPTION("The output to be added already exists!") { }
345 OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
346};
347
352{
353 public:
354 KEY_IMAGE_EXISTS() : DB_EXCEPTION("The spent key image to be added already exists!") { }
355 KEY_IMAGE_EXISTS(const char* s) : DB_EXCEPTION(s) { }
356};
357
362{
363public:
364 UTXO_EXISTS() : DB_EXCEPTION("The utxo to be added already exists!") { }
365 UTXO_EXISTS(const char* s) : DB_EXCEPTION(s) { }
366};
367
372{
373public:
374 ADDR_OUTPUT_EXISTS() : DB_EXCEPTION("The output record for this address already exists!") { }
375 ADDR_OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { }
376};
377
379{
380public:
381 ADDR_TX_EXISTS() : DB_EXCEPTION("The tx record for this address already exists!") { }
382 ADDR_TX_EXISTS(const char* s) : DB_EXCEPTION(s) { }
383};
384
385/***********************************
386 * End of Exception Definitions
387 ***********************************/
388
389
403{
404private:
405 /*********************************************************************
406 * private virtual members
407 *********************************************************************/
408
426 virtual void add_block( const block& blk
427 , size_t block_weight
428 , uint64_t long_term_block_weight
429 , const difficulty_type& cumulative_difficulty
430 , const uint64_t& coins_generated
431 , uint64_t num_rct_outs
432 , const crypto::hash& blk_hash
433 ) = 0;
434
445 virtual void remove_block() = 0;
446
467 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;
468
485 virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0;
486
513 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;
514
528 virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector<uint64_t>& amount_output_indices) = 0;
529
530 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;
531 virtual void remove_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
532 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;
533 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;
534 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;
535 virtual void remove_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
536
547 virtual void add_spent_key(const crypto::key_image& k_image) = 0;
548
559 virtual void remove_spent_key(const crypto::key_image& k_image) = 0;
560
561
562 /*********************************************************************
563 * private concrete members
564 *********************************************************************/
571 void pop_block();
572
573 // helper function to remove transaction from blockchain
581 void remove_transaction(const crypto::hash& tx_hash);
582
583 uint64_t num_calls = 0;
584 uint64_t time_blk_hash = 0;
585 uint64_t time_add_block1 = 0;
586 uint64_t time_add_transaction = 0;
587
588
589protected:
590
602 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);
603
606 bool m_auto_remove_logs = true;
607
609
610public:
611
612 virtual void add_addr_tx(const crypto::hash tx_hash, const crypto::public_key& combined_key) = 0;
613 virtual void remove_addr_tx(const crypto::hash tx_hash, const crypto::public_key& combined_key) = 0;
618
622 virtual ~BlockchainDB() { };
623
627 static void init_options(boost::program_options::options_description& desc);
628
632 void reset_stats();
633
640 void show_stats();
641
665 virtual void open(const std::string& filename, const int db_flags = 0) = 0;
666
672 bool is_open() const;
673
684 virtual void close() = 0;
685
697 virtual void sync() = 0;
698
704 virtual void safesyncmode(const bool onoff) = 0;
705
716 virtual void reset() = 0;
717
727 virtual std::vector<std::string> get_filenames() const = 0;
728
741 virtual bool remove_data_file(const std::string& folder) const = 0;
742
743 // return the name of the folder the db's file(s) should reside in
752 virtual std::string get_db_name() const = 0;
753
754
755 // FIXME: these are just for functionality mocking, need to implement
756 // RAII-friendly and multi-read one-write friendly locking mechanism
757 //
758 // acquire db lock
774 virtual bool lock() = 0;
775
776 // release db lock
787 virtual void unlock() = 0;
788
808 virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0) = 0;
809
823 virtual void batch_stop() = 0;
824
838 virtual void batch_abort() = 0;
839
855 virtual void set_batch_transactions(bool) = 0;
856
857 virtual void block_wtxn_start() = 0;
858 virtual void block_wtxn_stop() = 0;
859 virtual void block_wtxn_abort() = 0;
860 virtual bool block_rtxn_start() const = 0;
861 virtual void block_rtxn_stop() const = 0;
862 virtual void block_rtxn_abort() const = 0;
863
864 virtual void set_hard_fork(HardFork* hf);
865
866 // adds a block with the given metadata to the top of the blockchain, returns the new height
888 virtual uint64_t add_block( const std::pair<block, blobdata>& blk
889 , size_t block_weight
890 , uint64_t long_term_block_weight
891 , const difficulty_type& cumulative_difficulty
892 , const uint64_t& coins_generated
893 , const std::vector<std::pair<transaction, blobdata>>& txs
894 );
895
904 virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const = 0;
905
918
930 virtual block get_block(const crypto::hash& h) const;
931
943 virtual uint64_t get_block_height(const crypto::hash& h) const = 0;
944
957 virtual block_header get_block_header(const crypto::hash& h) const = 0;
958
972
983 virtual block get_block_from_height(const uint64_t& height) const;
984
997 virtual uint64_t get_block_timestamp(const uint64_t& height) const = 0;
998
1011 virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const = 0;
1012
1021
1034 virtual size_t get_block_weight(const uint64_t& height) const = 0;
1035
1045 virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const = 0;
1046
1060
1074
1088
1102
1113
1123 virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0;
1124
1138
1154 virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1155
1171 virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const = 0;
1172
1182 virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const = 0;
1183
1191 virtual block get_top_block() const = 0;
1192
1200 virtual uint64_t height() const = 0;
1201
1202
1223 virtual void pop_block(block& blk, std::vector<transaction>& txs);
1224
1225
1237 virtual bool tx_exists(const crypto::hash& h) const = 0;
1238 virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_id) const = 0;
1239
1240 // return unlock time of tx with hash <h>
1253 virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const = 0;
1254
1255 // return tx with hash <h>
1256 // throw if no such tx exists
1266 virtual transaction get_tx(const crypto::hash& h) const;
1267
1277 virtual transaction get_pruned_tx(const crypto::hash& h) const;
1278
1288 virtual bool get_tx(const crypto::hash& h, transaction &tx) const;
1289
1299 virtual bool get_pruned_tx(const crypto::hash& h, transaction &tx) const;
1300
1313 virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1314
1327 virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1328
1342 virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0;
1343
1355 virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const = 0;
1356
1365 virtual uint64_t get_tx_count() const = 0;
1366
1382 virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const = 0;
1383
1384 // returns height of block that contains transaction with hash <h>
1397 virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
1398
1399 // returns the total number of outputs of amount <amount>
1413 virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
1414
1420 virtual uint64_t get_indexing_base() const { return 0; }
1421
1438 virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt = true) const = 0;
1439
1451
1464 virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const = 0;
1465
1477 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;
1478
1490 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;
1491
1492 /*
1493 * FIXME: Need to check with git blame and ask what this does to
1494 * document it
1495 */
1496 virtual bool can_thread_bulk_indices() const = 0;
1497
1513 virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes = 1) const = 0;
1514
1522 virtual bool has_key_image(const crypto::key_image& img) const = 0;
1523
1529 virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t& details) = 0;
1530
1537 virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) = 0;
1538
1542 virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const = 0;
1543
1547 virtual bool txpool_has_tx(const crypto::hash &txid) const = 0;
1548
1554 virtual void remove_txpool_tx(const crypto::hash& txid) = 0;
1555
1564 virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const = 0;
1565
1574 virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const = 0;
1575
1584
1590 virtual void prune_outputs(uint64_t amount) = 0;
1591
1597
1603 virtual bool prune_blockchain(uint32_t pruning_seed = 0) = 0;
1604
1609 virtual bool update_pruning() = 0;
1610
1615 virtual bool check_pruning() = 0;
1616
1621
1629
1630 virtual void add_max_block_size(uint64_t sz) = 0;
1644 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;
1645
1659 virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0;
1660
1679 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;
1680
1698 virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const = 0;
1699
1717 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;
1718 virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const = 0;
1719
1720
1721
1722 //
1723 // Hard fork related storage
1724 //
1725
1733
1742
1743 virtual void set_validator_list(std::string, uint32_t expiration_date) = 0;
1744
1745 virtual std::string get_validator_list() const = 0;
1746
1750 virtual void check_hard_fork_info() = 0;
1751
1755 virtual void drop_hard_fork_info() = 0;
1756
1767 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;
1768
1769 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;
1770
1776 virtual bool is_read_only() const = 0;
1777
1783 virtual uint64_t get_database_size() const = 0;
1784
1785 // TODO: this should perhaps be (or call) a series of functions which
1786 // progressively update through version updates
1790 virtual void fixup();
1791
1800 void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; }
1801
1802 virtual bool check_chainstate_utxo(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1803 virtual uint64_t get_utxo_unlock_time(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1804 bool m_open;
1806
1807 virtual std::vector<address_outputs> get_addr_output_all(const crypto::public_key& combined_key) = 0;
1808 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;
1809 virtual std::vector<address_txs> get_addr_tx_all(const crypto::public_key& combined_key) = 0;
1810 virtual std::vector<address_txs> get_addr_tx_batch(const crypto::public_key& combined_key, uint64_t start_db_index = 0, uint64_t batch_size = 100, bool desc = false) = 0;
1811 virtual uint64_t get_balance(const crypto::public_key& combined_key) = 0;
1812 virtual tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index) = 0;
1813
1814}; // class BlockchainDB
1815
1817{
1818public:
1819 db_txn_guard(BlockchainDB *db, bool readonly): db(db), readonly(readonly), active(false)
1820 {
1821 if (readonly)
1822 {
1823 active = db->block_rtxn_start();
1824 }
1825 else
1826 {
1827 db->block_wtxn_start();
1828 active = true;
1829 }
1830 }
1832 {
1833 if (active)
1834 stop();
1835 }
1836 void stop()
1837 {
1838 if (readonly)
1839 db->block_rtxn_stop();
1840 else
1841 db->block_wtxn_stop();
1842 active = false;
1843 }
1844 void abort()
1845 {
1846 if (readonly)
1847 db->block_rtxn_abort();
1848 else
1849 db->block_wtxn_abort();
1850 active = false;
1851 }
1852
1853private:
1854 BlockchainDB *db;
1855 bool readonly;
1856 bool active;
1857};
1858
1859class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} };
1860class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} };
1861
1862BlockchainDB *new_db(const std::string& db_type);
1863
1864} // namespace cryptonote
1865
1866#endif // BLOCKCHAIN_DB_H
uint8_t version
BLOCK_DNE(const char *s)
BLOCK_EXISTS(const char *s)
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 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 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< address_txs > get_addr_tx_batch(const crypto::public_key &combined_key, uint64_t start_db_index=0, uint64_t batch_size=100, bool desc=false)=0
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 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
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' long term weights
bool m_open
Whether or not the BlockchainDB is open/ready for use.
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 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
void set_auto_remove_logs(bool auto_remove)
set whether or not to automatically remove logs
virtual uint64_t get_tx_block_height(const crypto::hash &h) const =0
fetches the height of a transaction's 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
virtual std::vector< address_outputs > get_addr_output_all(const crypto::public_key &combined_key)=0
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const =0
get a txpool transaction's metadata
virtual void add_addr_tx(const crypto::hash tx_hash, const crypto::public_key &combined_key)=0
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 bool is_read_only() const =0
is BlockchainDB in read-only mode?
virtual void block_wtxn_start()=0
virtual tx_input_t get_tx_input(const crypto::hash tx_hash, const uint32_t relative_out_index)=0
virtual std::vector< address_txs > get_addr_tx_all(const crypto::public_key &combined_key)=0
virtual uint64_t height() const =0
fetch the current blockchain height
virtual bool can_thread_bulk_indices() const =0
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB's file(s) should be in
epee::critical_section m_synchronization_lock
A lock, currently for when BlockchainLMDB needs to resize the backing db file.
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 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
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
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 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< transaction > get_tx_list(const std::vector< crypto::hash > &hlist) const =0
fetches a list of transactions based on their hashes
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const =0
fetch a block's cumulative number of rct outputs
virtual void drop_hard_fork_info()=0
delete hard fork info from database
virtual void add_max_block_size(uint64_t sz)=0
add a new max block size
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const =0
fetch a transaction's unlock time/height
virtual void reset()=0
Remove everything from the BlockchainDB.
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of blocks
virtual void check_hard_fork_info()=0
verify hard fork info in database
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const =0
fetches the block with the given hash
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const =0
checks if a block exists
virtual bool check_pruning()=0
checks pruning was done correctly, iff enabled
virtual void batch_abort()=0
aborts a batch transaction
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0)=0
tells the BlockchainDB to start a new "batch" of blocks
virtual uint64_t get_indexing_base() const
return index of the first element (should be hidden, but isn't)
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 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 std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' weights
virtual void set_batch_transactions(bool)=0
sets whether or not to batch transactions
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const =0
fetch a block's hash
virtual std::vector< std::string > get_filenames() const =0
get all files used by the BlockchainDB (if any)
virtual transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
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
virtual void remove_addr_tx(const crypto::hash tx_hash, const crypto::public_key &combined_key)=0
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
DB_ERROR(const char *s)
DB_EXCEPTION(const char *s)
const char * what() const
OUTPUT_DNE(const char *s)
TX_DNE(const char *s)
TX_EXISTS(const char *s)
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:79
POD_CLASS key_image
Definition crypto.h:105
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
const command_line::arg_descriptor< bool > arg_addr_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
std::pair< crypto::hash, uint64_t > tx_out_index
const command_line::arg_descriptor< std::string > arg_db_sync_mode
#define false
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