Electroneum
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
cryptonote::BlockchainBDB Class Reference

#include <db_bdb.h>

Inheritance diagram for cryptonote::BlockchainBDB:
Inheritance graph
[legend]
Collaboration diagram for cryptonote::BlockchainBDB:
Collaboration graph
[legend]

Public Member Functions

 BlockchainBDB (bool batch_transactions=false)
 
 ~BlockchainBDB ()
 
virtual void open (const std::string &filename, const int db_flags)
 open a db, or create it if necessary. More...
 
virtual void close ()
 close the BlockchainDB More...
 
virtual void sync ()
 sync the BlockchainDB with disk More...
 
virtual void reset ()
 Remove everything from the BlockchainDB. More...
 
virtual std::vector< std::string > get_filenames () const
 get all files used by the BlockchainDB (if any) More...
 
virtual bool remove_data_file (const std::string &folder)
 
virtual std::string get_db_name () const
 gets the name of the folder the BlockchainDB's file(s) should be in More...
 
virtual bool lock ()
 acquires the BlockchainDB lock More...
 
virtual void unlock ()
 This function releases the BlockchainDB lock. More...
 
virtual bool block_exists (const crypto::hash &h, uint64_t *height=NULL) const
 checks if a block exists More...
 
virtual block get_block (const crypto::hash &h) const
 fetches the block with the given hash More...
 
virtual uint64_t get_block_height (const crypto::hash &h) const
 gets the height of the block with a given hash More...
 
virtual block_header get_block_header (const crypto::hash &h) const
 fetch a block header More...
 
virtual block get_block_from_height (const uint64_t &height) const
 fetch a block by height More...
 
virtual uint64_t get_block_timestamp (const uint64_t &height) const
 fetch a block's timestamp More...
 
virtual uint64_t get_top_block_timestamp () const
 fetch the top block's timestamp More...
 
virtual size_t get_block_weight (const uint64_t &height) const
 fetch a block's weight More...
 
virtual void set_block_cumulative_difficulty (uint64_t height, difficulty_type diff)
 sets a block's cumulative difficulty More...
 
virtual difficulty_type get_block_cumulative_difficulty (const uint64_t &height) const
 fetch a block's cumulative difficulty More...
 
virtual difficulty_type get_block_difficulty (const uint64_t &height) const
 fetch a block's difficulty More...
 
virtual uint64_t get_block_already_generated_coins (const uint64_t &height) const
 fetch a block's already generated coins More...
 
virtual crypto::hash get_block_hash_from_height (const uint64_t &height) const
 fetch a block's hash More...
 
virtual std::vector< blockget_blocks_range (const uint64_t &h1, const uint64_t &h2) const
 fetch a list of blocks More...
 
virtual std::vector< crypto::hashget_hashes_range (const uint64_t &h1, const uint64_t &h2) const
 fetch a list of block hashes More...
 
virtual crypto::hash top_block_hash () const
 
virtual block get_top_block () const
 fetch the top block More...
 
virtual uint64_t height () const
 fetch the current blockchain height More...
 
virtual bool tx_exists (const crypto::hash &h) const
 check if a transaction with a given hash exists More...
 
virtual uint64_t get_tx_unlock_time (const crypto::hash &h) const
 fetch a transaction's unlock time/height More...
 
virtual transaction get_tx (const crypto::hash &h) const
 fetches the transaction with the given hash More...
 
virtual uint64_t get_tx_count () const
 fetches the total number of transactions ever More...
 
virtual std::vector< transactionget_tx_list (const std::vector< crypto::hash > &hlist) const
 fetches a list of transactions based on their hashes More...
 
virtual uint64_t get_tx_block_height (const crypto::hash &h) const
 fetches the height of a transaction's block More...
 
virtual uint64_t get_num_outputs (const uint64_t &amount) const
 fetches the number of outputs of a given amount More...
 
virtual uint64_t get_indexing_base () const
 return index of the first element (should be hidden, but isn't) More...
 
virtual output_data_t get_output_key (const uint64_t &amount, const uint64_t &index)
 
virtual void get_output_key (const uint64_t &amount, const std::vector< uint64_t > &offsets, std::vector< output_data_t > &outputs)
 
virtual tx_out_index get_output_tx_and_index_from_global (const uint64_t &index) const
 gets an output's tx hash and index More...
 
virtual void get_output_tx_and_index_from_global (const std::vector< uint64_t > &global_indices, std::vector< tx_out_index > &tx_out_indices) const
 
virtual tx_out_index get_output_tx_and_index (const uint64_t &amount, const uint64_t &index)
 
virtual void get_output_tx_and_index (const uint64_t &amount, const std::vector< uint64_t > &offsets, std::vector< tx_out_index > &indices)
 
virtual std::vector< uint64_t > get_tx_amount_output_indices (const crypto::hash &h) const
 
virtual bool has_key_image (const crypto::key_image &img) const
 check if a key image is stored as spent More...
 
virtual uint64_t add_block (const block &blk, size_t block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const std::vector< transaction > &txs)
 
virtual void set_batch_transactions (bool batch_transactions)
 sets whether or not to batch transactions More...
 
virtual bool batch_start (uint64_t batch_num_blocks=0)
 
virtual void batch_commit ()
 
virtual void batch_stop ()
 ends a batch transaction More...
 
virtual void batch_abort ()
 aborts a batch transaction More...
 
virtual void block_txn_start (bool readonly)
 
virtual void block_txn_stop ()
 
virtual void block_txn_abort ()
 
virtual void pop_block (block &blk, std::vector< transaction > &txs)
 pops the top block off the blockchain More...
 
virtual bool can_thread_bulk_indices () const
 
std::map< uint64_t, uint64_t > get_output_histogram (const std::vector< uint64_t > &amounts) const
 return a histogram of outputs on the blockchain More...
 
- Public Member Functions inherited from cryptonote::BlockchainDB
 BlockchainDB ()
 An empty constructor. More...
 
virtual ~BlockchainDB ()
 An empty destructor. More...
 
void reset_stats ()
 reset profiling stats More...
 
void show_stats ()
 show profiling stats More...
 
bool is_open () const
 Gets the current open/ready state of the BlockchainDB. More...
 
virtual void safesyncmode (const bool onoff)=0
 toggle safe syncs for the DB More...
 
virtual bool remove_data_file (const std::string &folder) const =0
 remove file(s) storing the database More...
 
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 More...
 
virtual void block_wtxn_start ()=0
 
virtual void block_wtxn_stop ()=0
 
virtual void block_wtxn_abort ()=0
 
virtual bool block_rtxn_start () const =0
 
virtual void block_rtxn_stop () const =0
 
virtual void block_rtxn_abort () const =0
 
virtual void set_hard_fork (HardFork *hf)
 
virtual uint64_t add_block (const std::pair< block, blobdata > &blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const std::vector< std::pair< transaction, blobdata >> &txs)
 handles the addition of a new block to BlockchainDB More...
 
virtual cryptonote::blobdata get_block_blob (const crypto::hash &h) const =0
 fetches the block with the given hash More...
 
virtual cryptonote::blobdata get_block_blob_from_height (const uint64_t &height) const =0
 fetch a block blob by height More...
 
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 More...
 
virtual std::vector< uint64_t > get_block_weights (uint64_t start_height, size_t count) const =0
 fetch the last N blocks' weights More...
 
virtual uint64_t get_block_long_term_weight (const uint64_t &height) const =0
 fetch a block's long term weight More...
 
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 More...
 
virtual crypto::hash top_block_hash (uint64_t *block_height=NULL) const =0
 fetch the top block's hash More...
 
virtual bool tx_exists (const crypto::hash &h, uint64_t &tx_id) const =0
 
virtual transaction get_pruned_tx (const crypto::hash &h) const
 fetches the transaction base with the given hash More...
 
virtual bool get_tx (const crypto::hash &h, transaction &tx) const
 fetches the transaction with the given hash More...
 
virtual bool get_pruned_tx (const crypto::hash &h, transaction &tx) const
 fetches the transaction base with the given hash More...
 
virtual bool get_tx_blob (const crypto::hash &h, cryptonote::blobdata &tx) const =0
 fetches the transaction blob with the given hash More...
 
virtual bool get_pruned_tx_blob (const crypto::hash &h, cryptonote::blobdata &tx) const =0
 fetches the pruned transaction blob with the given hash More...
 
virtual bool get_prunable_tx_blob (const crypto::hash &h, cryptonote::blobdata &tx) const =0
 fetches the prunable transaction blob with the given hash More...
 
virtual bool get_prunable_tx_hash (const crypto::hash &tx_hash, crypto::hash &prunable_hash) const =0
 fetches the prunable transaction hash More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
virtual void add_txpool_tx (const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &details)=0
 add a txpool transaction More...
 
virtual void update_txpool_tx (const crypto::hash &txid, const txpool_tx_meta_t &details)=0
 update a txpool transaction's metadata More...
 
virtual uint64_t get_txpool_tx_count (bool include_unrelayed_txes=true) const =0
 get the number of transactions in the txpool More...
 
virtual bool txpool_has_tx (const crypto::hash &txid) const =0
 check whether a txid is in the txpool More...
 
virtual void remove_txpool_tx (const crypto::hash &txid)=0
 remove a txpool transaction More...
 
virtual bool get_txpool_tx_meta (const crypto::hash &txid, txpool_tx_meta_t &meta) const =0
 get a txpool transaction's metadata More...
 
virtual bool get_txpool_tx_blob (const crypto::hash &txid, cryptonote::blobdata &bd) const =0
 get a txpool transaction's blob More...
 
virtual cryptonote::blobdata get_txpool_tx_blob (const crypto::hash &txid) const =0
 get a txpool transaction's blob More...
 
virtual void prune_outputs (uint64_t amount)=0
 prune output data for the given amount More...
 
virtual uint32_t get_blockchain_pruning_seed () const =0
 get the blockchain pruning seed More...
 
virtual bool prune_blockchain (uint32_t pruning_seed=0)=0
 prunes the blockchain More...
 
virtual bool update_pruning ()=0
 prunes recent blockchain changes as needed, iff pruning is enabled More...
 
virtual bool check_pruning ()=0
 checks pruning was done correctly, iff enabled More...
 
virtual uint64_t get_max_block_size ()=0
 get the max block size More...
 
virtual void add_max_block_size (uint64_t sz)=0
 add a new max block size More...
 
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 More...
 
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 More...
 
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 More...
 
virtual bool for_all_outputs (uint64_t amount, const std::function< bool(uint64_t height)> &f) const =0
 
virtual void set_validator_list (std::string, uint32_t expiration_date)=0
 
virtual std::string get_validator_list () const =0
 
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 More...
 
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 uint64_t get_database_size () const =0
 get disk space requirements More...
 
virtual void fixup ()
 fix up anything that may be wrong due to past bugs More...
 
void set_auto_remove_logs (bool auto_remove)
 set whether or not to automatically remove logs More...
 

Private Types

typedef bdb_safe_buffer< void * > bdb_safe_buffer_t
 

Private Member Functions

virtual void add_block (const block &blk, size_t block_weight, const difficulty_type &cumulative_difficulty, const uint64_t &coins_generated, const crypto::hash &block_hash)
 
virtual void remove_block ()
 remove data about the top block More...
 
virtual void add_transaction_data (const crypto::hash &blk_hash, const transaction &tx, const crypto::hash &tx_hash, const crypto::hash &tx_prunable_hash)
 
virtual void remove_transaction_data (const crypto::hash &tx_hash, const transaction &tx)
 remove data about a transaction More...
 
virtual void 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)
 store an output More...
 
virtual void remove_output (const tx_out &tx_output)
 
void remove_tx_outputs (const crypto::hash &tx_hash, const transaction &tx)
 
void remove_output (const uint64_t &out_index, const uint64_t amount)
 
void remove_amount_output_index (const uint64_t amount, const uint64_t global_output_index)
 
virtual void add_spent_key (const crypto::key_image &k_image)
 store a spent key More...
 
virtual void remove_spent_key (const crypto::key_image &k_image)
 remove a spent key More...
 
void get_output_global_indices (const uint64_t &amount, const std::vector< uint64_t > &offsets, std::vector< uint64_t > &global_indices)
 
virtual bool for_all_key_images (std::function< bool(const crypto::key_image &)>) const
 runs a function over all key images stored More...
 
virtual bool for_all_blocks (std::function< bool(uint64_t, const crypto::hash &, const cryptonote::block &)>) const
 
virtual bool for_all_transactions (std::function< bool(const crypto::hash &, const cryptonote::transaction &)>, bool pruned) const
 runs a function over all transactions stored More...
 
virtual bool for_all_outputs (std::function< bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const
 
virtual void set_hard_fork_version (uint64_t height, uint8_t version)
 sets which hardfork version a height is on More...
 
virtual uint8_t get_hard_fork_version (uint64_t height) const
 checks which hardfork version a height is on More...
 
virtual void check_hard_fork_info ()
 verify hard fork info in database More...
 
virtual void drop_hard_fork_info ()
 delete hard fork info from database More...
 
uint64_t get_output_global_index (const uint64_t &amount, const uint64_t &index)
 get the global index of the index-th output of the given amount More...
 
output_data_t get_output_key (const uint64_t &global_index) const
 
void checkpoint_worker () const
 
void check_open () const
 
virtual bool is_read_only () const
 is BlockchainDB in read-only mode? More...
 

Private Attributes

bool m_run_checkpoint
 
std::unique_ptr< boost::thread > m_checkpoint_thread
 
bdb_safe_buffer_t m_buffer
 
DbEnv * m_env
 
Db * m_blocks
 
Db * m_block_heights
 
Db * m_block_hashes
 
Db * m_block_timestamps
 
Db * m_block_sizes
 
Db * m_block_diffs
 
Db * m_block_coins
 
Db * m_txs
 
Db * m_tx_unlocks
 
Db * m_tx_heights
 
Db * m_tx_outputs
 
Db * m_output_txs
 
Db * m_output_indices
 
Db * m_output_amounts
 
Db * m_output_keys
 
Db * m_spent_keys
 
Db * m_hf_starting_heights
 
Db * m_hf_versions
 
Db * m_properties
 
uint64_t m_height
 
uint64_t m_num_outputs
 
std::string m_folder
 
bdb_txn_safem_write_txn
 
bool m_batch_transactions
 

Additional Inherited Members

- Static Public Member Functions inherited from cryptonote::BlockchainDB
static void init_options (boost::program_options::options_description &desc)
 init command line options More...
 
- Public Attributes inherited from cryptonote::BlockchainDB
bool m_open
 Whether or not the BlockchainDB is open/ready for use. More...
 
epee::critical_section m_synchronization_lock
 A lock, currently for when BlockchainLMDB needs to resize the backing db file. More...
 
- Protected Member Functions inherited from cryptonote::BlockchainDB
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 More...
 
- Protected Attributes inherited from cryptonote::BlockchainDB
uint64_t time_tx_exists = 0
 a performance metric More...
 
uint64_t time_commit1 = 0
 a performance metric More...
 
bool m_auto_remove_logs = true
 whether or not to automatically remove old logs More...
 
HardForkm_hardfork
 

Member Typedef Documentation

◆ bdb_safe_buffer_t

Constructor & Destructor Documentation

◆ BlockchainBDB()

cryptonote::BlockchainBDB::BlockchainBDB ( bool  batch_transactions = false)

◆ ~BlockchainBDB()

cryptonote::BlockchainBDB::~BlockchainBDB ( )

Member Function Documentation

◆ add_block() [1/2]

void cryptonote::BlockchainBDB::add_block ( const block blk,
size_t  block_weight,
const difficulty_type cumulative_difficulty,
const uint64_t &  coins_generated,
const crypto::hash block_hash 
)
privatevirtual

◆ add_block() [2/2]

uint64_t cryptonote::BlockchainBDB::add_block ( const block blk,
size_t  block_weight,
const difficulty_type cumulative_difficulty,
const uint64_t &  coins_generated,
const std::vector< transaction > &  txs 
)
virtual

◆ add_output()

void cryptonote::BlockchainBDB::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 
)
privatevirtual

store an output

The subclass implementing this will add the output data passed to its backing store in a suitable manner. In addition, the subclass is responsible for keeping track of the global output count in some manner, so that outputs may be indexed by the order in which they were created. In the future, this tracking (of the number, at least) should be moved to this class, as it is necessary and the same among all BlockchainDB.

It returns an amount output index, which is the index of the output for its specified amount.

This data should be stored in such a manner that the only thing needed to reverse the process is the tx_out.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
tx_hashhash of the transaction the output was created by
tx_outputthe output
local_indexindex of the output in its transaction
unlock_timeunlock time/height of the output
commitmentthe rct commitment to the output amount
Returns
amount output index

Implements cryptonote::BlockchainDB.

◆ add_spent_key()

void cryptonote::BlockchainBDB::add_spent_key ( const crypto::key_image k_image)
privatevirtual

store a spent key

The subclass implementing this will store the spent key image.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
k_imagethe spent key image to store

Implements cryptonote::BlockchainDB.

◆ add_transaction_data()

void cryptonote::BlockchainBDB::add_transaction_data ( const crypto::hash blk_hash,
const transaction tx,
const crypto::hash tx_hash,
const crypto::hash tx_prunable_hash 
)
privatevirtual

◆ batch_abort()

void cryptonote::BlockchainBDB::batch_abort ( )
virtual

aborts a batch transaction

If the subclass implements batching, this function should abort the batch it is currently on.

If no batch is in-progress, this function should throw a DB_ERROR. This exception may change in the future if it is deemed necessary to have a more granular exception type for this scenario.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ batch_commit()

void cryptonote::BlockchainBDB::batch_commit ( )
virtual

◆ batch_start()

bool cryptonote::BlockchainBDB::batch_start ( uint64_t  batch_num_blocks = 0)
virtual

◆ batch_stop()

void cryptonote::BlockchainBDB::batch_stop ( )
virtual

ends a batch transaction

If the subclass implements batching, this function should store the batch it is currently on and mark it finished.

If no batch is in-progress, this function should throw a DB_ERROR. This exception may change in the future if it is deemed necessary to have a more granular exception type for this scenario.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ block_exists()

bool cryptonote::BlockchainBDB::block_exists ( const crypto::hash h,
uint64_t *  height = NULL 
) const
virtual

checks if a block exists

Parameters
hthe hash of the requested block
heightif non NULL, returns the block's height if found
Returns
true of the block exists, otherwise false

Implements cryptonote::BlockchainDB.

◆ block_txn_abort()

void cryptonote::BlockchainBDB::block_txn_abort ( )
virtual

◆ block_txn_start()

void cryptonote::BlockchainBDB::block_txn_start ( bool  readonly)
virtual

◆ block_txn_stop()

void cryptonote::BlockchainBDB::block_txn_stop ( )
virtual

◆ can_thread_bulk_indices()

virtual bool cryptonote::BlockchainBDB::can_thread_bulk_indices ( ) const
inlinevirtual

◆ check_hard_fork_info()

void cryptonote::BlockchainBDB::check_hard_fork_info ( )
privatevirtual

verify hard fork info in database

Implements cryptonote::BlockchainDB.

◆ check_open()

void cryptonote::BlockchainBDB::check_open ( ) const
private

◆ checkpoint_worker()

void cryptonote::BlockchainBDB::checkpoint_worker ( ) const
private

◆ close()

void cryptonote::BlockchainBDB::close ( )
virtual

close the BlockchainDB

At minimum, this call ensures that further use of the BlockchainDB instance will not have effect. In any case where it is necessary to do so, a subclass implementing this will sync with disk.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ drop_hard_fork_info()

void cryptonote::BlockchainBDB::drop_hard_fork_info ( )
privatevirtual

delete hard fork info from database

Implements cryptonote::BlockchainDB.

◆ for_all_blocks()

bool cryptonote::BlockchainBDB::for_all_blocks ( std::function< bool(uint64_t, const crypto::hash &, const cryptonote::block &)>  f) const
privatevirtual

◆ for_all_key_images()

bool cryptonote::BlockchainBDB::for_all_key_images ( std::function< bool(const crypto::key_image &)>  ) const
privatevirtual

runs a function over all key images stored

The subclass should run the passed function for each key image it has stored, passing the key image as its parameter.

If any call to the function returns false, the subclass should return false. Otherwise, the subclass returns true.

Parameters
std::functionfn the function to run
Returns
false if the function returns false for any key image, otherwise true

Implements cryptonote::BlockchainDB.

◆ for_all_outputs()

bool cryptonote::BlockchainBDB::for_all_outputs ( std::function< bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)>  f) const
privatevirtual

◆ for_all_transactions()

bool cryptonote::BlockchainBDB::for_all_transactions ( std::function< bool(const crypto::hash &, const cryptonote::transaction &)>  ,
bool  pruned 
) const
privatevirtual

runs a function over all transactions stored

The subclass should run the passed function for each transaction it has stored, passing (transaction_hash, transaction) as its parameters.

If any call to the function returns false, the subclass should return false. Otherwise, the subclass returns true.

The subclass should throw DB_ERROR if any of the expected values are not found. Current implementations simply return false.

Parameters
std::functionfn the function to run
boolpruned whether to only get pruned tx data, or the whole
Returns
false if the function returns false for any transaction, otherwise true

Implements cryptonote::BlockchainDB.

◆ get_block()

block cryptonote::BlockchainBDB::get_block ( const crypto::hash h) const
virtual

fetches the block with the given hash

Returns the requested block.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
hthe hash to look for
Returns
the block requested

Reimplemented from cryptonote::BlockchainDB.

◆ get_block_already_generated_coins()

uint64_t cryptonote::BlockchainBDB::get_block_already_generated_coins ( const uint64_t &  height) const
virtual

fetch a block's already generated coins

The subclass should return the total coins generated as of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the already generated coins

Implements cryptonote::BlockchainDB.

◆ get_block_cumulative_difficulty()

difficulty_type cryptonote::BlockchainBDB::get_block_cumulative_difficulty ( const uint64_t &  height) const
virtual

fetch a block's cumulative difficulty

The subclass should return the cumulative difficulty of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the cumulative difficulty

Implements cryptonote::BlockchainDB.

◆ get_block_difficulty()

difficulty_type cryptonote::BlockchainBDB::get_block_difficulty ( const uint64_t &  height) const
virtual

fetch a block's difficulty

The subclass should return the difficulty of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the difficulty

Implements cryptonote::BlockchainDB.

◆ get_block_from_height()

block cryptonote::BlockchainBDB::get_block_from_height ( const uint64_t &  height) const
virtual

fetch a block by height

If the block does not exist, that is to say if the blockchain is not that high, then the subclass should throw BLOCK_DNE

Parameters
heightthe height to look for
Returns
the block

Reimplemented from cryptonote::BlockchainDB.

◆ get_block_hash_from_height()

crypto::hash cryptonote::BlockchainBDB::get_block_hash_from_height ( const uint64_t &  height) const
virtual

fetch a block's hash

The subclass should return hash of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the hash

Implements cryptonote::BlockchainDB.

◆ get_block_header()

block_header cryptonote::BlockchainBDB::get_block_header ( const crypto::hash h) const
virtual

fetch a block header

The subclass should return the block header from the block with the given hash.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
hthe hash to look for
Returns
the block header

Implements cryptonote::BlockchainDB.

◆ get_block_height()

uint64_t cryptonote::BlockchainBDB::get_block_height ( const crypto::hash h) const
virtual

gets the height of the block with a given hash

The subclass should return the requested height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
hthe hash to look for
Returns
the height

Implements cryptonote::BlockchainDB.

◆ get_block_timestamp()

uint64_t cryptonote::BlockchainBDB::get_block_timestamp ( const uint64_t &  height) const
virtual

fetch a block's timestamp

The subclass should return the timestamp of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the timestamp

Implements cryptonote::BlockchainDB.

◆ get_block_weight()

size_t cryptonote::BlockchainBDB::get_block_weight ( const uint64_t &  height) const
virtual

fetch a block's weight

The subclass should return the weight of the block with the given height.

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
Returns
the weight

Implements cryptonote::BlockchainDB.

◆ get_blocks_range()

std::vector< block > cryptonote::BlockchainBDB::get_blocks_range ( const uint64_t &  h1,
const uint64_t &  h2 
) const
virtual

fetch a list of blocks

The subclass should return a vector of blocks with heights starting at h1 and ending at h2, inclusively.

If the height range requested goes past the end of the blockchain, the subclass should throw BLOCK_DNE. (current implementations simply don't catch this exception as thrown by methods called within)

Parameters
h1the start height
h2the end height
Returns
a vector of blocks

Implements cryptonote::BlockchainDB.

◆ get_db_name()

std::string cryptonote::BlockchainBDB::get_db_name ( ) const
virtual

gets the name of the folder the BlockchainDB's file(s) should be in

The subclass implementation should return the name of the folder in which it stores files, or an empty string if there is none.

Returns
the name of the folder with the BlockchainDB's files, if any.

Implements cryptonote::BlockchainDB.

◆ get_filenames()

std::vector< std::string > cryptonote::BlockchainBDB::get_filenames ( ) const
virtual

get all files used by the BlockchainDB (if any)

This function is largely for ease of automation, namely for unit tests.

The subclass implementation should return all filenames it uses.

Returns
a list of filenames

Implements cryptonote::BlockchainDB.

◆ get_hard_fork_version()

uint8_t cryptonote::BlockchainBDB::get_hard_fork_version ( uint64_t  height) const
privatevirtual

checks which hardfork version a height is on

Parameters
heightthe height
Returns
the version

Implements cryptonote::BlockchainDB.

◆ get_hashes_range()

std::vector< crypto::hash > cryptonote::BlockchainBDB::get_hashes_range ( const uint64_t &  h1,
const uint64_t &  h2 
) const
virtual

fetch a list of block hashes

The subclass should return a vector of block hashes from blocks with heights starting at h1 and ending at h2, inclusively.

If the height range requested goes past the end of the blockchain, the subclass should throw BLOCK_DNE. (current implementations simply don't catch this exception as thrown by methods called within)

Parameters
h1the start height
h2the end height
Returns
a vector of block hashes

Implements cryptonote::BlockchainDB.

◆ get_indexing_base()

virtual uint64_t cryptonote::BlockchainBDB::get_indexing_base ( ) const
inlinevirtual

return index of the first element (should be hidden, but isn't)

Returns
the index

Reimplemented from cryptonote::BlockchainDB.

◆ get_num_outputs()

uint64_t cryptonote::BlockchainBDB::get_num_outputs ( const uint64_t &  amount) const
virtual

fetches the number of outputs of a given amount

The subclass should return a count of outputs of the given amount, or zero if there are none.

Parameters
amountthe output amount being looked up
Returns
the number of outputs of the given amount

Implements cryptonote::BlockchainDB.

◆ get_output_global_index()

uint64_t cryptonote::BlockchainBDB::get_output_global_index ( const uint64_t &  amount,
const uint64_t &  index 
)
private

get the global index of the index-th output of the given amount

Parameters
amountthe output amount
indexthe index into the set of outputs of that amount
Returns
the global index of the desired output

◆ get_output_global_indices()

void cryptonote::BlockchainBDB::get_output_global_indices ( const uint64_t &  amount,
const std::vector< uint64_t > &  offsets,
std::vector< uint64_t > &  global_indices 
)
private

◆ get_output_histogram()

std::map<uint64_t, uint64_t> cryptonote::BlockchainBDB::get_output_histogram ( const std::vector< uint64_t > &  amounts) const

return a histogram of outputs on the blockchain

Parameters
amountsoptional set of amounts to lookup
Returns
a set of amount/instances

◆ get_output_key() [1/3]

void cryptonote::BlockchainBDB::get_output_key ( const uint64_t &  amount,
const std::vector< uint64_t > &  offsets,
std::vector< output_data_t > &  outputs 
)
virtual

◆ get_output_key() [2/3]

virtual output_data_t cryptonote::BlockchainBDB::get_output_key ( const uint64_t &  amount,
const uint64_t &  index 
)
virtual

◆ get_output_key() [3/3]

output_data_t cryptonote::BlockchainBDB::get_output_key ( const uint64_t &  global_index) const
private

◆ get_output_tx_and_index() [1/2]

void cryptonote::BlockchainBDB::get_output_tx_and_index ( const uint64_t &  amount,
const std::vector< uint64_t > &  offsets,
std::vector< tx_out_index > &  indices 
)
virtual

◆ get_output_tx_and_index() [2/2]

virtual tx_out_index cryptonote::BlockchainBDB::get_output_tx_and_index ( const uint64_t &  amount,
const uint64_t &  index 
)
virtual

◆ get_output_tx_and_index_from_global() [1/2]

void cryptonote::BlockchainBDB::get_output_tx_and_index_from_global ( const std::vector< uint64_t > &  global_indices,
std::vector< tx_out_index > &  tx_out_indices 
) const
virtual

◆ get_output_tx_and_index_from_global() [2/2]

tx_out_index cryptonote::BlockchainBDB::get_output_tx_and_index_from_global ( const uint64_t &  index) const
virtual

gets an output's tx hash and index

The subclass should return the hash of the transaction which created the output with the global index given, as well as its index in that transaction.

Parameters
indexan output's global index
Returns
the tx hash and output index

Implements cryptonote::BlockchainDB.

◆ get_top_block()

block cryptonote::BlockchainBDB::get_top_block ( ) const
virtual

fetch the top block

The subclass should return most recent block

Returns
the top block

Implements cryptonote::BlockchainDB.

◆ get_top_block_timestamp()

uint64_t cryptonote::BlockchainBDB::get_top_block_timestamp ( ) const
virtual

fetch the top block's timestamp

The subclass should return the timestamp of the most recent block.

Returns
the top block's timestamp

Implements cryptonote::BlockchainDB.

◆ get_tx()

transaction cryptonote::BlockchainBDB::get_tx ( const crypto::hash h) const
virtual

fetches the transaction with the given hash

If the transaction does not exist, the subclass should throw TX_DNE.

Parameters
hthe hash to look for
Returns
the transaction with the given hash

Reimplemented from cryptonote::BlockchainDB.

◆ get_tx_amount_output_indices()

std::vector< uint64_t > cryptonote::BlockchainBDB::get_tx_amount_output_indices ( const crypto::hash h) const
virtual

◆ get_tx_block_height()

uint64_t cryptonote::BlockchainBDB::get_tx_block_height ( const crypto::hash h) const
virtual

fetches the height of a transaction's block

The subclass should attempt to return the height of the block containing the transaction with the given hash.

If the transaction cannot be found, the subclass should throw TX_DNE.

Parameters
hthe hash of the transaction
Returns
the height of the transaction's block

Implements cryptonote::BlockchainDB.

◆ get_tx_count()

uint64_t cryptonote::BlockchainBDB::get_tx_count ( ) const
virtual

fetches the total number of transactions ever

The subclass should return a count of all the transactions from all blocks.

Returns
the number of transactions in the blockchain

Implements cryptonote::BlockchainDB.

◆ get_tx_list()

std::vector< transaction > cryptonote::BlockchainBDB::get_tx_list ( const std::vector< crypto::hash > &  hlist) const
virtual

fetches a list of transactions based on their hashes

The subclass should attempt to fetch each transaction referred to by the hashes passed.

Currently, if any of the transactions is not in BlockchainDB, the call to get_tx in the implementation will throw TX_DNE.

Parameters
hlista list of hashes
Returns
the list of transactions

Implements cryptonote::BlockchainDB.

◆ get_tx_unlock_time()

uint64_t cryptonote::BlockchainBDB::get_tx_unlock_time ( const crypto::hash h) const
virtual

fetch a transaction's unlock time/height

The subclass should return the stored unlock time for the transaction with the given hash.

If no such transaction exists, the subclass should throw TX_DNE.

Parameters
hthe hash of the requested transaction
Returns
the unlock time/height

Implements cryptonote::BlockchainDB.

◆ has_key_image()

bool cryptonote::BlockchainBDB::has_key_image ( const crypto::key_image img) const
virtual

check if a key image is stored as spent

Parameters
imgthe key image to check for
Returns
true if the image is present, otherwise false

Implements cryptonote::BlockchainDB.

◆ height()

uint64_t cryptonote::BlockchainBDB::height ( ) const
virtual

fetch the current blockchain height

The subclass should return the current blockchain height

Returns
the current blockchain height

Implements cryptonote::BlockchainDB.

◆ is_read_only()

bool cryptonote::BlockchainBDB::is_read_only ( ) const
privatevirtual

is BlockchainDB in read-only mode?

Returns
true if in read-only mode, otherwise false

Implements cryptonote::BlockchainDB.

◆ lock()

bool cryptonote::BlockchainBDB::lock ( )
virtual

acquires the BlockchainDB lock

This function is a stub until such a time as locking is implemented at this level.

The subclass implementation should return true unless implementing a locking scheme of some sort, in which case it should return true upon acquisition of the lock and block until then.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Returns
true, unless at a future time false makes sense (timeout, etc)

Implements cryptonote::BlockchainDB.

◆ open()

void cryptonote::BlockchainBDB::open ( const std::string &  filename,
const int  db_flags 
)
virtual

open a db, or create it if necessary.

This function opens an existing database or creates it if it does not exist.

The subclass implementing this will handle all file opening/creation, and is responsible for maintaining its state.

The parameter <filename> may not refer to a file name, necessarily, but could be an IP:PORT for a database which needs it, and so on. Calling it <filename> is convenient and should be descriptive enough, however.

For now, db_flags are specific to the subclass being instantiated. This is subject to change, and the db_flags parameter may be deprecated.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
filenamea string referring to the BlockchainDB to open
db_flagsflags relevant to how to open/use the BlockchainDB

Implements cryptonote::BlockchainDB.

◆ pop_block()

void cryptonote::BlockchainBDB::pop_block ( block blk,
std::vector< transaction > &  txs 
)
virtual

pops the top block off the blockchain

The subclass should remove the most recent block from the blockchain, along with all transactions, outputs, and other metadata created as a result of its addition to the blockchain. Most of this is handled by the concrete members of the base class provided the subclass correctly implements remove_* functions.

The subclass should return by reference the popped block and its associated transactions

Parameters
blkreturn-by-reference the block which was popped
txsreturn-by-reference the transactions from the popped block

Reimplemented from cryptonote::BlockchainDB.

◆ remove_amount_output_index()

void cryptonote::BlockchainBDB::remove_amount_output_index ( const uint64_t  amount,
const uint64_t  global_output_index 
)
private

◆ remove_block()

void cryptonote::BlockchainBDB::remove_block ( )
privatevirtual

remove data about the top block

The subclass implementing this will remove the block data from the top block in the chain. The data to be removed is that which was added in BlockchainDB::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, const crypto::hash& blk_hash)

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ remove_data_file()

bool cryptonote::BlockchainBDB::remove_data_file ( const std::string &  folder)
virtual

◆ remove_output() [1/2]

void cryptonote::BlockchainBDB::remove_output ( const tx_out tx_output)
privatevirtual

◆ remove_output() [2/2]

void cryptonote::BlockchainBDB::remove_output ( const uint64_t &  out_index,
const uint64_t  amount 
)
private

◆ remove_spent_key()

void cryptonote::BlockchainBDB::remove_spent_key ( const crypto::key_image k_image)
privatevirtual

remove a spent key

The subclass implementing this will remove the key image.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
k_imagethe spent key image to remove

Implements cryptonote::BlockchainDB.

◆ remove_transaction_data()

void cryptonote::BlockchainBDB::remove_transaction_data ( const crypto::hash tx_hash,
const transaction tx 
)
privatevirtual

remove data about a transaction

The subclass implementing this will remove the transaction data for the passed transaction. The data to be removed was added in add_transaction_data(). Additionally, current subclasses have behavior which requires the transaction itself as a parameter here. Future implementations should note that this parameter is subject to be removed at a later time.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
tx_hashthe hash of the transaction to be removed
txthe transaction

Implements cryptonote::BlockchainDB.

◆ remove_tx_outputs()

void cryptonote::BlockchainBDB::remove_tx_outputs ( const crypto::hash tx_hash,
const transaction tx 
)
private

◆ reset()

void cryptonote::BlockchainBDB::reset ( )
virtual

Remove everything from the BlockchainDB.

This function should completely remove all data from a BlockchainDB.

Use with caution!

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ set_batch_transactions()

void cryptonote::BlockchainBDB::set_batch_transactions ( bool  )
virtual

sets whether or not to batch transactions

If the subclass implements batching, this function tells it to begin batching automatically.

If the subclass implements batching and has a batch in-progress, a parameter of false should disable batching and call batch_stop() to store the current batch.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Parameters
boolbatch whether or not to use batch transactions.

Implements cryptonote::BlockchainDB.

◆ set_block_cumulative_difficulty()

virtual void cryptonote::BlockchainBDB::set_block_cumulative_difficulty ( uint64_t  height,
difficulty_type  diff 
)
virtual

sets a block's cumulative difficulty

The subclass should return true if the cumulative difficulty is set successfully

If the block does not exist, the subclass should throw BLOCK_DNE

Parameters
heightthe height requested
diffthe cumulative difficulty value to be set
Returns
true | false

Implements cryptonote::BlockchainDB.

◆ set_hard_fork_version()

void cryptonote::BlockchainBDB::set_hard_fork_version ( uint64_t  height,
uint8_t  version 
)
privatevirtual

sets which hardfork version a height is on

Parameters
heightthe height
versionthe version

Implements cryptonote::BlockchainDB.

◆ sync()

void cryptonote::BlockchainBDB::sync ( )
virtual

sync the BlockchainDB with disk

This function should write any changes to whatever permanent backing store the subclass uses. Example: a BlockchainDB instance which keeps the whole blockchain in RAM won't need to regularly access a disk, but should write out its state when this is called.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

◆ top_block_hash()

crypto::hash cryptonote::BlockchainBDB::top_block_hash ( ) const
virtual

◆ tx_exists()

bool cryptonote::BlockchainBDB::tx_exists ( const crypto::hash h) const
virtual

check if a transaction with a given hash exists

The subclass should check if a transaction is stored which has the given hash and return true if so, false otherwise.

Parameters
hthe hash to check against
tx_id(optional) returns the tx_id for the tx hash
Returns
true if the transaction exists, otherwise false

Implements cryptonote::BlockchainDB.

◆ unlock()

void cryptonote::BlockchainBDB::unlock ( )
virtual

This function releases the BlockchainDB lock.

The subclass, should it have implemented lock(), will release any lock held by the calling thread. In the case of recursive locking, it should release one instance of a lock.

If any of this cannot be done, the subclass should throw the corresponding subclass of DB_EXCEPTION

Implements cryptonote::BlockchainDB.

Member Data Documentation

◆ m_batch_transactions

bool cryptonote::BlockchainBDB::m_batch_transactions
private

◆ m_block_coins

Db* cryptonote::BlockchainBDB::m_block_coins
private

◆ m_block_diffs

Db* cryptonote::BlockchainBDB::m_block_diffs
private

◆ m_block_hashes

Db* cryptonote::BlockchainBDB::m_block_hashes
private

◆ m_block_heights

Db* cryptonote::BlockchainBDB::m_block_heights
private

◆ m_block_sizes

Db* cryptonote::BlockchainBDB::m_block_sizes
private

◆ m_block_timestamps

Db* cryptonote::BlockchainBDB::m_block_timestamps
private

◆ m_blocks

Db* cryptonote::BlockchainBDB::m_blocks
private

◆ m_buffer

bdb_safe_buffer_t cryptonote::BlockchainBDB::m_buffer
private

◆ m_checkpoint_thread

std::unique_ptr<boost::thread> cryptonote::BlockchainBDB::m_checkpoint_thread
private

◆ m_env

DbEnv* cryptonote::BlockchainBDB::m_env
private

◆ m_folder

std::string cryptonote::BlockchainBDB::m_folder
private

◆ m_height

uint64_t cryptonote::BlockchainBDB::m_height
private

◆ m_hf_starting_heights

Db* cryptonote::BlockchainBDB::m_hf_starting_heights
private

◆ m_hf_versions

Db* cryptonote::BlockchainBDB::m_hf_versions
private

◆ m_num_outputs

uint64_t cryptonote::BlockchainBDB::m_num_outputs
private

◆ m_output_amounts

Db* cryptonote::BlockchainBDB::m_output_amounts
private

◆ m_output_indices

Db* cryptonote::BlockchainBDB::m_output_indices
private

◆ m_output_keys

Db* cryptonote::BlockchainBDB::m_output_keys
private

◆ m_output_txs

Db* cryptonote::BlockchainBDB::m_output_txs
private

◆ m_properties

Db* cryptonote::BlockchainBDB::m_properties
private

◆ m_run_checkpoint

bool cryptonote::BlockchainBDB::m_run_checkpoint
private

◆ m_spent_keys

Db* cryptonote::BlockchainBDB::m_spent_keys
private

◆ m_tx_heights

Db* cryptonote::BlockchainBDB::m_tx_heights
private

◆ m_tx_outputs

Db* cryptonote::BlockchainBDB::m_tx_outputs
private

◆ m_tx_unlocks

Db* cryptonote::BlockchainBDB::m_tx_unlocks
private

◆ m_txs

Db* cryptonote::BlockchainBDB::m_txs
private

◆ m_write_txn

bdb_txn_safe* cryptonote::BlockchainBDB::m_write_txn
private

The documentation for this class was generated from the following files: