28#ifndef BLOCKCHAIN_DB_H
29#define BLOCKCHAIN_DB_H
35#include <boost/program_options.hpp>
195#define DBF_SALVAGE 0x10
215 const char*
what()
const throw()
594 static void init_options(boost::program_options::options_description& desc);
632 virtual void open(
const std::string& filename,
const int db_flags = 0) = 0;
860 ,
const std::vector<std::pair<transaction, blobdata>>& txs
1186 virtual void pop_block(
block& blk, std::vector<transaction>& txs);
1328 virtual bool get_blocks_from(
uint64_t start_height,
size_t min_block_count,
size_t max_block_count,
size_t max_tx_count,
size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>&
blocks,
bool pruned,
bool skip_coinbase,
bool get_miner_tx_hash)
const = 0;
1383 virtual std::vector<transaction>
get_tx_list(
const std::vector<crypto::hash>& hlist)
const = 0;
1829 virtual std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>>
get_output_histogram(
const std::vector<uint64_t> &amounts,
bool unlocked,
uint64_t recent_cutoff,
uint64_t min_count)
const = 0;
1852 virtual void fixup();
1880 db->block_wtxn_start();
1893 db->block_rtxn_stop();
1895 db->block_wtxn_stop();
1902 db->block_rtxn_abort();
1904 db->block_wtxn_abort();
#define s(x, c)
Definition aesb.c:47
static uint64_t db_flags
Definition blockchain_blackball.cpp:52
static uint64_t h
Definition blockchain_stats.cpp:55
BLOCK_DNE(const char *s)
Definition blockchain_db.h:278
BLOCK_DNE()
Definition blockchain_db.h:277
BLOCK_EXISTS()
Definition blockchain_db.h:297
BLOCK_EXISTS(const char *s)
Definition blockchain_db.h:298
BLOCK_INVALID(const char *s)
Definition blockchain_db.h:308
BLOCK_INVALID()
Definition blockchain_db.h:307
BLOCK_PARENT_DNE()
Definition blockchain_db.h:287
BLOCK_PARENT_DNE(const char *s)
Definition blockchain_db.h:288
The BlockchainDB backing store interface declaration/contract.
Definition blockchain_db.h:379
virtual void add_block(const block &blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, uint64_t num_rct_outs, const crypto::hash &blk_hash)=0
add the block and metadata to the db
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const =0
fetch a block blob by height
virtual ~BlockchainDB()
An empty destructor.
Definition blockchain_db.h:589
virtual std::vector< std::vector< uint64_t > > get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes=1) const =0
gets output indices (amount-specific) for a transaction's outputs
virtual uint64_t get_max_block_size()=0
get the max block size
virtual bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const =0
runs a function over all key images stored
BlockchainDB()
An empty constructor.
Definition blockchain_db.h:584
HardFork * m_hardfork
Definition blockchain_db.h:577
virtual bool get_pruned_tx_blobs_from(const crypto::hash &h, size_t count, std::vector< cryptonote::blobdata > &bd) const =0
fetches a number of pruned transaction blob from the given hash, in canonical blockchain order
virtual uint8_t get_hard_fork_version(uint64_t height) const =0
checks which hardfork version a height is on
virtual void prune_outputs(uint64_t amount)=0
prune output data for the given amount
virtual void remove_spent_key(const crypto::key_image &k_image)=0
remove a spent key
virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const =0
check whether a txid is in the txpool and meets tx_category requirements
void add_transaction(const crypto::hash &blk_hash, const std::pair< transaction, blobdata_ref > &tx, const crypto::hash *tx_hash_ptr=NULL, const crypto::hash *tx_prunable_hash_ptr=NULL)
helper function for add_transactions, to add each individual transaction
Definition blockchain_db.cpp:182
virtual crypto::hash top_block_hash(uint64_t *block_height=NULL) const =0
fetch the top block's hash
virtual bool get_prunable_tx_hash(const crypto::hash &tx_hash, crypto::hash &prunable_hash) const =0
fetches the prunable transaction hash
virtual bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd, relay_category tx_category) const =0
get a txpool transaction's blob
virtual block_header get_block_header(const crypto::hash &h) const =0
fetch a block header
virtual void set_hard_fork(HardFork *hf)
Definition blockchain_db.cpp:310
virtual block get_block_from_height(const uint64_t &height) const
fetch a block by height
Definition blockchain_db.cpp:353
virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob)=0
add a new alternative block
virtual bool for_all_alt_blocks(std::function< bool(const crypto::hash &blkid, const alt_block_data_t &data, const cryptonote::blobdata_ref *blob)> f, bool include_blob=false) const =0
runs a function over all alternative blocks stored
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t &index) const =0
gets an output's tx hash and index
virtual uint64_t get_database_size() const =0
get disk space requirements
static void init_options(boost::program_options::options_description &desc)
init command line options
Definition blockchain_db.cpp:169
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
uint64_t time_blk_hash
a performance metric
Definition blockchain_db.h:553
virtual void get_output_key(const epee::span< const uint64_t > &amounts, const std::vector< uint64_t > &offsets, std::vector< output_data_t > &outputs, bool allow_partial=false) const =0
gets outputs' data
bool txpool_tx_matches_category(const crypto::hash &tx_hash, relay_category category)
Check if tx_hash relay status is in category.
Definition blockchain_db.cpp:1027
void remove_transaction(const crypto::hash &tx_hash)
helper function to remove transaction from the blockchain
Definition blockchain_db.cpp:337
virtual void close()=0
close the BlockchainDB
virtual void remove_transaction_data(const crypto::hash &tx_hash, const transaction &tx)=0
remove data about a transaction
virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &details)=0
update a txpool transaction's metadata
virtual uint64_t get_tx_count() const =0
fetches the total number of transactions ever
virtual void batch_stop()=0
ends a batch transaction
virtual void get_output_tx_and_index(const uint64_t &amount, const std::vector< uint64_t > &offsets, std::vector< tx_out_index > &indices) const =0
gets some outputs' tx hashes and indices
virtual uint64_t get_block_height(const crypto::hash &h) const =0
gets the height of the block with a given hash
virtual void block_wtxn_stop()=0
virtual std::vector< uint64_t > get_long_term_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' long term weights
bool m_open
Whether or not the BlockchainDB is open/ready for use.
Definition blockchain_db.h:1864
virtual uint64_t add_output(const crypto::hash &tx_hash, const tx_out &tx_output, const uint64_t &local_index, const uint64_t unlock_time, const rct::key *commitment)=0
store an output
virtual std::vector< crypto::hash > get_hashes_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of block hashes
virtual void remove_alt_block(const crypto::hash &blkid)=0
remove an alternative block
virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t &details)=0
add a txpool transaction
uint64_t time_add_transaction
a performance metric
Definition blockchain_db.h:555
virtual block get_block(const crypto::hash &h) const
fetches the block with the given hash
Definition blockchain_db.cpp:363
void show_stats()
show profiling stats
Definition blockchain_db.cpp:421
virtual bool block_rtxn_start() const =0
void set_auto_remove_logs(bool auto_remove)
set whether or not to automatically remove logs
Definition blockchain_db.h:1862
virtual uint64_t get_tx_block_height(const crypto::hash &h) const =0
fetches the height of a transaction's block
virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob)=0
get an alternative block by hash
virtual void correct_block_cumulative_difficulties(const uint64_t &start_height, const std::vector< difficulty_type > &new_cumulative_difficulties)=0
correct blocks cumulative difficulties that were incorrectly calculated due to the 'difficulty drift'...
uint64_t time_add_block1
a performance metric
Definition blockchain_db.h:554
virtual std::map< uint64_t, std::tuple< uint64_t, uint64_t, uint64_t > > get_output_histogram(const std::vector< uint64_t > &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const =0
return a histogram of outputs on the blockchain
virtual uint64_t add_transaction_data(const crypto::hash &blk_hash, const std::pair< transaction, blobdata_ref > &tx, const crypto::hash &tx_hash, const crypto::hash &tx_prunable_hash)=0
store the transaction and its metadata
virtual bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const =0
get a txpool transaction's metadata
virtual bool get_blocks_from(uint64_t start_height, size_t min_block_count, size_t max_block_count, size_t max_tx_count, size_t max_size, std::vector< std::pair< std::pair< cryptonote::blobdata, crypto::hash >, std::vector< std::pair< crypto::hash, cryptonote::blobdata > > > > &blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const =0
fetches a variable number of blocks and transactions from the given height, in canonical blockchain o...
virtual bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector< uint64_t > &distribution, uint64_t &base) const =0
virtual bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>, bool pruned) const =0
runs a function over all transactions stored
bool m_auto_remove_logs
whether or not to automatically remove old logs
Definition blockchain_db.h:575
virtual transaction get_pruned_tx(const crypto::hash &h) const
fetches the transaction base with the given hash
Definition blockchain_db.cpp:403
virtual bool is_read_only() const =0
is BlockchainDB in read-only mode?
virtual void block_wtxn_start()=0
virtual uint64_t height() const =0
fetch the current blockchain height
virtual bool can_thread_bulk_indices() const =0
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB's file(s) should be in
epee::critical_section m_synchronization_lock
A lock, currently for when BlockchainLMDB needs to resize the backing db file.
Definition blockchain_db.h:1865
virtual uint64_t get_top_block_timestamp() const =0
fetch the top block's timestamp
virtual void safesyncmode(const bool onoff)=0
toggle safe syncs for the DB
virtual void remove_txpool_tx(const crypto::hash &txid)=0
remove a txpool transaction
virtual block get_top_block() const =0
fetch the top block
virtual bool tx_exists(const crypto::hash &h, uint64_t &tx_id) const =0
virtual uint64_t get_num_outputs(const uint64_t &amount) const =0
fetches the number of outputs of a given amount
virtual uint64_t get_block_already_generated_coins(const uint64_t &height) const =0
fetch a block's already generated coins
virtual uint32_t get_blockchain_pruning_seed() const =0
get the blockchain pruning seed
virtual tx_out_index get_output_tx_and_index(const uint64_t &amount, const uint64_t &index) const =0
gets an output's tx hash and index
virtual bool remove_data_file(const std::string &folder) const =0
remove file(s) storing the database
virtual void sync()=0
sync the BlockchainDB with disk
virtual void drop_alt_blocks()=0
drop all alternative blocks
virtual bool for_all_outputs(uint64_t amount, const std::function< bool(uint64_t height)> &f) const =0
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t &height) const =0
fetch a block's cumulative difficulty
bool is_open() const
Gets the current open/ready state of the BlockchainDB.
Definition blockchain_db.cpp:332
virtual void fixup()
fix up anything that may be wrong due to past bugs
Definition blockchain_db.cpp:443
virtual bool has_key_image(const crypto::key_image &img) const =0
check if a key image is stored as spent
virtual bool tx_exists(const crypto::hash &h) const =0
check if a transaction with a given hash exists
virtual size_t get_block_weight(const uint64_t &height) const =0
fetch a block's weight
virtual std::vector< transaction > get_tx_list(const std::vector< crypto::hash > &hlist) const =0
fetches a list of transactions based on their hashes
virtual std::vector< uint64_t > get_block_cumulative_rct_outputs(const std::vector< uint64_t > &heights) const =0
fetch a block's cumulative number of rct outputs
virtual void drop_hard_fork_info()=0
delete hard fork info from database
virtual void add_max_block_size(uint64_t sz)=0
add a new max block size
virtual uint64_t get_tx_unlock_time(const crypto::hash &h) const =0
fetch a transaction's unlock time/height
virtual void reset()=0
Remove everything from the BlockchainDB.
virtual std::vector< block > get_blocks_range(const uint64_t &h1, const uint64_t &h2) const =0
fetch a list of blocks
virtual void check_hard_fork_info()=0
verify hard fork info in database
virtual cryptonote::blobdata get_block_blob(const crypto::hash &h) const =0
fetches the block with the given hash
virtual bool block_exists(const crypto::hash &h, uint64_t *height=NULL) const =0
checks if a block exists
virtual bool check_pruning()=0
checks pruning was done correctly, iff enabled
virtual void batch_abort()=0
aborts a batch transaction
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0)=0
tells the BlockchainDB to start a new "batch" of blocks
virtual uint64_t get_indexing_base() const
return index of the first element (should be hidden, but isn't)
Definition blockchain_db.h:1421
virtual bool for_blocks_range(const uint64_t &h1, const uint64_t &h2, std::function< bool(uint64_t, const crypto::hash &, const cryptonote::block &)>) const =0
runs a function over a range of blocks
virtual bool get_pruned_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the pruned transaction blob with the given hash
virtual bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata_ref *)>, bool include_blob=false, relay_category category=relay_category::broadcasted) const =0
runs a function over all txpool transactions
void reset_stats()
reset profiling stats
Definition blockchain_db.cpp:411
virtual bool for_all_outputs(std::function< bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)> f) const =0
runs a function over all outputs stored
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index, bool include_commitmemt=true) const =0
get some of an output's data
virtual uint64_t get_alt_block_count()=0
get the number of alternative blocks stored
virtual bool lock()=0
acquires the BlockchainDB lock
virtual void add_spent_key(const crypto::key_image &k_image)=0
store a spent key
uint64_t num_calls
a performance metric
Definition blockchain_db.h:552
virtual difficulty_type get_block_difficulty(const uint64_t &height) const =0
fetch a block's difficulty
virtual uint64_t get_block_long_term_weight(const uint64_t &height) const =0
fetch a block's long term weight
virtual bool update_pruning()=0
prunes recent blockchain changes as needed, iff pruning is enabled
virtual void block_rtxn_stop() const =0
virtual uint64_t get_txpool_tx_count(relay_category tx_category=relay_category::broadcasted) const =0
get the number of transactions in the txpool
virtual void unlock()=0
This function releases the BlockchainDB lock.
virtual void block_rtxn_abort() const =0
virtual uint64_t get_block_timestamp(const uint64_t &height) const =0
fetch a block's timestamp
virtual bool prune_blockchain(uint32_t pruning_seed=0)=0
prunes the blockchain
virtual std::vector< uint64_t > get_block_weights(uint64_t start_height, size_t count) const =0
fetch the last N blocks' weights
virtual void set_batch_transactions(bool)=0
sets whether or not to batch transactions
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const =0
fetch a block's hash
virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector< uint64_t > &amount_output_indices)=0
store amount output indices for a tx's outputs
virtual std::vector< std::string > get_filenames() const =0
get all files used by the BlockchainDB (if any)
virtual transaction get_tx(const crypto::hash &h) const
fetches the transaction with the given hash
Definition blockchain_db.cpp:395
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
virtual void block_wtxn_abort()=0
virtual bool get_prunable_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the prunable transaction blob with the given hash
virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash &txid, relay_category tx_category) const =0
get a txpool transaction's blob
void pop_block()
private version of pop_block, for undoing if an add_block fails
Definition blockchain_db.cpp:175
uint64_t time_tx_exists
a performance metric
Definition blockchain_db.h:573
uint64_t time_commit1
a performance metric
Definition blockchain_db.h:574
virtual void remove_block()=0
remove data about the top block
DB_CREATE_FAILURE()
Definition blockchain_db.h:257
DB_CREATE_FAILURE(const char *s)
Definition blockchain_db.h:258
DB_ERROR_TXN_START(const char *s)
Definition blockchain_db.h:238
DB_ERROR_TXN_START()
Definition blockchain_db.h:237
DB_ERROR()
Definition blockchain_db.h:227
DB_ERROR(const char *s)
Definition blockchain_db.h:228
virtual ~DB_EXCEPTION()
Definition blockchain_db.h:213
std::string m
Definition blockchain_db.h:207
DB_EXCEPTION(const char *s)
Definition blockchain_db.h:210
const char * what() const
Definition blockchain_db.h:215
DB_OPEN_FAILURE()
Definition blockchain_db.h:247
DB_OPEN_FAILURE(const char *s)
Definition blockchain_db.h:248
DB_SYNC_FAILURE(const char *s)
Definition blockchain_db.h:268
DB_SYNC_FAILURE()
Definition blockchain_db.h:267
KEY_IMAGE_EXISTS(const char *s)
Definition blockchain_db.h:358
KEY_IMAGE_EXISTS()
Definition blockchain_db.h:357
OUTPUT_DNE()
Definition blockchain_db.h:337
OUTPUT_DNE(const char *s)
Definition blockchain_db.h:338
OUTPUT_EXISTS()
Definition blockchain_db.h:347
OUTPUT_EXISTS(const char *s)
Definition blockchain_db.h:348
TX_DNE()
Definition blockchain_db.h:317
TX_DNE(const char *s)
Definition blockchain_db.h:318
TX_EXISTS(const char *s)
Definition blockchain_db.h:328
TX_EXISTS()
Definition blockchain_db.h:327
db_rtxn_guard(BlockchainDB *db)
Definition blockchain_db.h:1914
BlockchainDB * db
Definition blockchain_db.h:1909
virtual ~db_txn_guard()
Definition blockchain_db.h:1884
bool readonly
Definition blockchain_db.h:1910
bool active
Definition blockchain_db.h:1911
void abort()
Definition blockchain_db.h:1899
db_txn_guard(BlockchainDB *db, bool readonly)
Definition blockchain_db.h:1872
void stop()
Definition blockchain_db.h:1888
db_wtxn_guard(BlockchainDB *db)
Definition blockchain_db.h:1915
Definition cryptonote_basic.h:205
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
std::tuple< uint64_t, uint64_t, std::vector< tools::wallet2::transfer_details > > outputs
Definition cold-outputs.cpp:53
#define const
Definition ipfrdr.c:80
static int version
Definition mdb_load.c:29
Definition block_weight.py:1
POD_CLASS public_key
Definition crypto.h:64
POD_CLASS key_image
Definition crypto.h:95
POD_CLASS hash
Definition hash.h:49
Holds cryptonote related classes and helpers.
Definition blockchain_db.cpp:45
boost::multiprecision::uint128_t difficulty_type
Definition difficulty.h:41
const command_line::arg_descriptor< bool > arg_db_salvage
Definition blockchain_db.cpp:158
relay_category
Definition blockchain_db.h:110
@ legacy
relay_category::broadcasted + relay_method::none for rpc relay requests or historical reasons
Definition blockchain_db.h:113
@ relayable
Every tx not marked relay_method::none.
Definition blockchain_db.h:112
@ all
Everything in the db.
Definition blockchain_db.h:114
@ broadcasted
Public txes received via block/fluff.
Definition blockchain_db.h:111
relay_method
Methods tracking how a tx was received and relayed.
Definition enums.h:37
BlockchainDB * new_db()
Definition blockchain_db.cpp:164
bool matches_category(relay_method method, relay_category category) noexcept
Definition blockchain_db.cpp:47
std::string blobdata
Definition blobdatatype.h:39
std::pair< crypto::hash, uint64_t > tx_out_index
Definition blockchain_db.h:104
const command_line::arg_descriptor< std::string > arg_db_sync_mode
Definition blockchain_db.cpp:153
boost::string_ref blobdata_ref
Definition blobdatatype.h:40
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
Definition command_line.h:53
Definition blockchain_db.h:143
uint64_t cumulative_difficulty_high
Definition blockchain_db.h:147
uint64_t cumulative_difficulty_low
Definition blockchain_db.h:146
uint64_t cumulative_weight
Definition blockchain_db.h:145
uint64_t already_generated_coins
Definition blockchain_db.h:148
uint64_t height
Definition blockchain_db.h:144
Definition cryptonote_basic.h:475
a struct containing output metadata
Definition blockchain_db.h:125
uint64_t unlock_time
the output's unlock time (or height)
Definition blockchain_db.h:127
uint64_t height
the height of the block which created the output
Definition blockchain_db.h:128
rct::key commitment
the output's amount commitment (for spend verification)
Definition blockchain_db.h:129
crypto::public_key pubkey
the output's public key (for spend verification)
Definition blockchain_db.h:126
Definition blockchain_db.h:135
uint64_t block_id
Definition blockchain_db.h:138
uint64_t unlock_time
Definition blockchain_db.h:137
uint64_t tx_id
Definition blockchain_db.h:136
Definition cryptonote_basic.h:158
std::string data
Definition base58.cpp:37
cryptonote::transaction tx
Definition transaction.cpp:40