Monero
blockchain.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 #include <boost/asio/io_service.hpp>
33 #include <boost/serialization/serialization.hpp>
34 #include <boost/serialization/version.hpp>
35 #include <boost/serialization/list.hpp>
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/global_fun.hpp>
38 #include <boost/multi_index/hashed_index.hpp>
39 #include <boost/multi_index/member.hpp>
40 #include <atomic>
41 #include <unordered_map>
42 #include <unordered_set>
43 
44 #include "syncobj.h"
45 #include "string_tools.h"
47 #include "common/util.h"
51 #include "cryptonote_tx_utils.h"
53 #include "crypto/hash.h"
57 
58 namespace cryptonote
59 {
60  class tx_memory_pool;
61  struct test_options;
62 
67  {
72  };
73 
74  /************************************************************************/
75  /* */
76  /************************************************************************/
77  class Blockchain
78  {
79  public:
84  {
87  size_t m_blob_size;
88  std::vector<uint64_t> m_global_output_indexes;
89  };
90 
95  {
97  uint64_t height;
101  };
102 
108  Blockchain(tx_memory_pool& tx_pool);
109 
121  bool init(BlockchainDB* db, const network_type nettype = MAINNET, bool offline = false, const cryptonote::test_options *test_options = NULL, difficulty_type fixed_difficulty = 0);
122 
133  bool init(BlockchainDB* db, HardFork*& hf, const network_type nettype = MAINNET, bool offline = false);
134 
142  bool deinit();
143 
149  void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
150 
161  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
162 
172  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
173 
181  bool get_alternative_blocks(std::vector<block>& blocks) const;
182 
188  size_t get_alternative_blocks_count() const;
189 
198 
208  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
209 
217  bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks);
218 
226  bool cleanup_handle_incoming_blocks(bool force_sync = false);
227 
235  bool have_tx(const crypto::hash &id) const;
236 
244  bool have_tx_keyimges_as_spent(const transaction &tx) const;
245 
258  bool have_tx_keyimg_as_spent(const crypto::key_image &key_im) const;
259 
265  uint64_t get_current_blockchain_height() const;
266 
272  crypto::hash get_tail_id() const;
273 
281  crypto::hash get_tail_id(uint64_t& height) const;
282 
289 
303  bool add_new_block(const block& bl_, block_verification_context& bvc);
304 
312  bool reset_and_set_genesis_block(const block& b);
313 
326  bool create_block_template(block& b, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
327 
338  bool have_block(const crypto::hash& id) const;
339 
345  size_t get_total_transactions() const;
346 
361  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
362 
377  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height) const;
378 
391  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
392 
405  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset) const;
406 
424  bool find_blockchain_supplement(const uint64_t req_start_block, const std::list<crypto::hash>& qblock_ids, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata> > > >& blocks, uint64_t& total_height, uint64_t& start_height, bool pruned, bool get_miner_tx_hash, size_t max_count) const;
425 
439 
447  uint64_t get_num_mature_outputs(uint64_t amount) const;
448 
457  crypto::public_key get_output_key(uint64_t amount, uint64_t global_index) const;
458 
473 
483  void get_output_key_mask_unlocked(const uint64_t& amount, const uint64_t& index, crypto::public_key& key, rct::key& mask, bool& unlocked) const;
484 
495  bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, uint64_t &start_height, std::vector<uint64_t> &distribution, uint64_t &base) const;
496 
508  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
509 
518  bool store_blockchain();
519 
536  bool check_tx_inputs(transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id, tx_verification_context &tvc, bool kept_by_block = false);
537 
545  static uint64_t get_fee_quantization_mask();
546 
560  static uint64_t get_dynamic_base_fee(uint64_t block_reward, size_t median_block_weight, uint8_t version);
561 
575  uint64_t get_dynamic_base_fee_estimate(uint64_t grace_blocks) const;
576 
589  bool check_fee(size_t tx_weight, uint64_t fee) const;
590 
604 
611 
618 
626  uint64_t block_difficulty(uint64_t i) const;
627 
640  template<class t_ids_container, class t_blocks_container, class t_missed_container>
641  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const;
642 
656  template<class t_ids_container, class t_tx_container, class t_missed_container>
657  bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs, bool pruned = false) const;
658  template<class t_ids_container, class t_tx_container, class t_missed_container>
659  bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
660 
661  //debug functions
662 
674  void check_against_checkpoints(const checkpoints& points, bool enforce);
675 
681  void set_enforce_dns_checkpoints(bool enforce);
682 
691  bool update_checkpoints(const std::string& file_path, bool check_dns);
692 
693 
694  // user options, must be called before calling init()
695 
705  void set_user_options(uint64_t maxthreads, bool sync_on_blocks, uint64_t sync_threshold,
706  blockchain_db_sync_mode sync_mode, bool fast_sync);
707 
711  void safesyncmode(const bool onoff);
712 
718  void set_show_time_stats(bool stats) { m_show_time_stats = stats; }
719 
726 
732  static const std::vector<HardFork::Params>& get_hard_fork_heights(network_type nettype);
733 
740 
747 
754 
764 
772  uint8_t get_hard_fork_version(uint64_t height) const { return m_hardfork->get(height); }
773 
780 
793  bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
794 
800  uint64_t get_difficulty_target() const;
801 
809  bool flush_txes_from_pool(const std::vector<crypto::hash> &txids);
810 
821  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 = 0) const;
822 
830  bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
831 
841  bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const block&)>) const;
842 
851  bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
852 
860  bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)>) const;
861 
870  bool for_all_outputs(uint64_t amount, std::function<bool(uint64_t height)>) const;
871 
877  const BlockchainDB& get_db() const
878  {
879  return *m_db;
880  }
881 
888  {
889  return *m_db;
890  }
891 
900  void output_scan_worker(const uint64_t amount,const std::vector<uint64_t> &offsets,
901  std::vector<output_data_t> &outputs, std::unordered_map<crypto::hash,
902  cryptonote::transaction> &txs) const;
903 
911  void block_longhash_worker(uint64_t height, const std::vector<block> &blocks,
912  std::unordered_map<crypto::hash, crypto::hash> &map) const;
913 
919  std::list<std::pair<block_extended_info,std::vector<crypto::hash>>> get_alternative_chains() const;
920 
921  void add_txpool_tx(transaction &tx, const txpool_tx_meta_t &meta);
922  void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta);
923  void remove_txpool_tx(const crypto::hash &txid);
924  uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const;
925  bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
926  bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
928  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;
929 
930  bool is_within_compiled_block_hash_area(uint64_t height) const;
932  uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes);
933 
934  void lock();
935  void unlock();
936 
937  void cancel();
938 
945 
946  private:
947 
948  // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage
949  typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
950 
951  typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;
952 
953  typedef std::unordered_set<crypto::key_image> key_images_container;
954 
955  typedef std::vector<block_extended_info> blocks_container;
956 
957  typedef std::unordered_map<crypto::hash, block_extended_info> blocks_ext_by_hash;
958 
959  typedef std::unordered_map<crypto::hash, block> blocks_by_hash;
960 
961  typedef std::map<uint64_t, std::vector<std::pair<crypto::hash, size_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
962 
963 
965 
967 
968  mutable epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
969 
970  // main chain
974 
975  // metadata containers
976  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, std::vector<output_data_t>>> m_scan_table;
977  std::unordered_map<crypto::hash, crypto::hash> m_blocks_longhash_table;
978  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, bool>> m_check_txin_table;
979 
980  // SHA-3 hashes for each block and for fast pow checking
981  std::vector<crypto::hash> m_blocks_hash_of_hashes;
982  std::vector<crypto::hash> m_blocks_hash_check;
983  std::vector<crypto::hash> m_blocks_txs_check;
984 
994  uint64_t m_sync_counter;
995  uint64_t m_bytes_to_sync;
996  std::vector<uint64_t> m_timestamps;
997  std::vector<difficulty_type> m_difficulties;
999 
1000  epee::critical_section m_difficulty_lock;
1003 
1004  boost::asio::io_service m_async_service;
1005  boost::thread_group m_async_pool;
1006  std::unique_ptr<boost::asio::io_service::work> m_async_work_idle;
1007 
1008  // all alternative chains
1009  blocks_ext_by_hash m_alternative_chains; // crypto::hash -> block_extended_info
1010 
1011  // some invalid blocks
1012  blocks_ext_by_hash m_invalid_blocks; // crypto::hash -> block_extended_info
1013 
1014 
1017 
1019 
1023 
1024  std::atomic<bool> m_cancel;
1025 
1026  // block template cache
1034 
1054  template<class visitor_t>
1055  inline bool scan_outputkeys_for_indexes(size_t tx_version, const txin_to_key& tx_in_to_key, visitor_t &vis, const crypto::hash &tx_prefix_hash, uint64_t* pmax_related_block_height = NULL) const;
1056 
1077  bool check_tx_input(size_t tx_version,const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig, const rct::rctSig &rct_signatures, std::vector<rct::ctkey> &output_keys, uint64_t* pmax_related_block_height);
1078 
1099  bool check_tx_inputs(transaction& tx, tx_verification_context &tvc, uint64_t* pmax_used_block_height = NULL);
1100 
1113  bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
1114 
1121 
1135 
1150 
1165 
1174  difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::iterator>& alt_chain, block_extended_info& bei) const;
1175 
1187  bool prevalidate_miner_transaction(const block& b, uint64_t height);
1188 
1205  bool validate_miner_transaction(const block& b, size_t cumulative_block_weight, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins, bool &partial_block_reward, uint8_t version);
1206 
1219  bool rollback_blockchain_switching(std::list<block>& original_chain, uint64_t rollback_height);
1220 
1229  void get_last_n_blocks_weights(std::vector<size_t>& weights, size_t count) const;
1230 
1241  bool is_tx_spendtime_unlocked(uint64_t unlock_time) const;
1242 
1254  bool add_block_as_invalid(const block& bl, const crypto::hash& h);
1255 
1267  bool add_block_as_invalid(const block_extended_info& bei, const crypto::hash& h);
1268 
1285  bool check_block_timestamp(const block& b, uint64_t& median_ts) const;
1286  bool check_block_timestamp(const block& b) const { uint64_t median_ts; return check_block_timestamp(b, median_ts); }
1287 
1299  bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b, uint64_t& median_ts) const;
1300  bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b) const { uint64_t median_ts; return check_block_timestamp(timestamps, b, median_ts); }
1301 
1310  uint64_t get_adjusted_time() const;
1311 
1323  bool complete_timestamps_vector(uint64_t start_height, std::vector<uint64_t>& timestamps);
1324 
1331  void return_tx_to_pool(std::vector<transaction> &txs);
1332 
1341  bool check_for_double_spend(const transaction& tx, key_images_container& keys_this_block) const;
1342 
1352  void check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image,
1353  const std::vector<rct::ctkey> &pubkeys, const std::vector<crypto::signature> &sig, uint64_t &result);
1354 
1363 
1371  bool expand_transaction_2(transaction &tx, const crypto::hash &tx_prefix_hash, const std::vector<std::vector<rct::ctkey>> &pubkeys);
1372 
1377 
1383  void cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t expected_reward, uint64_t pool_cookie);
1384  };
1385 } // namespace cryptonote
static uint64_t get_dynamic_base_fee(uint64_t block_reward, size_t median_block_weight, uint8_t version)
get dynamic per kB or byte fee for a given block weight
Definition: blockchain.cpp:2893
Definition: cryptonote_protocol_defs.h:162
uint64_t m_max_prepare_blocks_threads
Definition: blockchain.h:991
bool check_block_timestamp(std::vector< uint64_t > &timestamps, const block &b) const
Definition: blockchain.h:1300
bool m_db_default_sync
Definition: blockchain.h:988
std::vector< crypto::hash > m_blocks_hash_check
Definition: blockchain.h:982
std::unordered_map< crypto::hash, std::unordered_map< crypto::key_image, std::vector< output_data_t > > > m_scan_table
Definition: blockchain.h:976
block pop_block_from_blockchain()
removes the most recent block from the blockchain
Definition: blockchain.cpp:581
crypto::hash m_difficulty_for_next_block_top_hash
Definition: blockchain.h:1001
checkpoints m_checkpoints
Definition: blockchain.h:1015
bool add_block_as_invalid(const block &bl, const crypto::hash &h)
stores an invalid block in a separate container
Definition: blockchain.cpp:2108
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
Definition: blockchain.cpp:4278
Definition: cryptonote_config.h:206
bool have_tx_keyimg_as_spent(const crypto::key_image &key_im) const
check if a key image is already spent on the blockchain
Definition: blockchain.cpp:186
uint64_t m_fake_pow_calc_time
Definition: blockchain.h:992
bool is_tx_spendtime_unlocked(uint64_t unlock_time) const
checks if a transaction is unlocked (its outputs spendable)
Definition: blockchain.cpp:3021
block bl
the block
Definition: blockchain.h:96
static uint64_t get_fee_quantization_mask()
get fee quantization mask
Definition: blockchain.cpp:2880
handle syncing calls instead of the backing db, asynchronously
Definition: blockchain.h:70
void block_longhash_worker(uint64_t height, const std::vector< block > &blocks, std::unordered_map< crypto::hash, crypto::hash > &map) const
computes the "short" and "long" hashes for a set of blocks
Definition: blockchain.cpp:3689
std::vector< uint64_t > m_timestamps
Definition: blockchain.h:996
bool check_for_double_spend(const transaction &tx, key_images_container &keys_this_block) const
make sure a transaction isn&#39;t attempting a double-spend
Definition: blockchain.cpp:2175
std::unique_ptr< boost::asio::io_service::work > m_async_work_idle
Definition: blockchain.h:1006
void set_user_options(uint64_t maxthreads, bool sync_on_blocks, uint64_t sync_threshold, blockchain_db_sync_mode sync_mode, bool fast_sync)
sets various performance options
Definition: blockchain.cpp:4283
uint64_t get_dynamic_base_fee_estimate(uint64_t grace_blocks) const
get dynamic per kB or byte fee estimate for the next few blocks
Definition: blockchain.cpp:2983
BlockchainDB & get_db()
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:887
bool m_offline
Definition: blockchain.h:1021
bool expand_transaction_2(transaction &tx, const crypto::hash &tx_prefix_hash, const std::vector< std::vector< rct::ctkey >> &pubkeys)
expands v2 transaction data from blockchain
Definition: blockchain.cpp:2400
uint64_t get_current_cumulative_block_weight_limit() const
gets the block weight limit based on recent blocks
Definition: blockchain.cpp:1199
std::unordered_set< crypto::key_image > key_images_container
Definition: blockchain.h:953
difficulty_type m_difficulty_for_next_block
Definition: blockchain.h:1002
bool m_fast_sync
Definition: blockchain.h:986
Definition: cryptonote_basic.h:366
bool create_block_template(block &b, const account_public_address &miner_address, difficulty_type &di, uint64_t &height, uint64_t &expected_reward, const blobdata &ex_nonce)
creates a new block to mine against
Definition: blockchain.cpp:1222
bool prevalidate_miner_transaction(const block &b, uint64_t height)
sanity checks a miner transaction before validating an entire block
Definition: blockchain.cpp:1098
blocks_ext_by_hash m_invalid_blocks
Definition: blockchain.h:1012
uint64_t m_db_sync_threshold
Definition: blockchain.h:990
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: blockchain.h:779
uint64_t height
Definition: blockchain.cpp:88
bool handle_alternative_block(const block &b, const crypto::hash &id, block_verification_context &bvc)
validate and add a new block to an alternate blockchain
Definition: blockchain.cpp:1424
bool validate_miner_transaction(const block &b, size_t cumulative_block_weight, uint64_t fee, uint64_t &base_reward, uint64_t already_generated_coins, bool &partial_block_reward, uint8_t version)
validates a miner (coinbase) transaction
Definition: blockchain.cpp:1125
uint64_t m_fake_scan_time
Definition: blockchain.h:993
user didn&#39;t specify, use db_async
Definition: blockchain.h:68
std::unordered_map< crypto::hash, block > blocks_by_hash
Definition: blockchain.h:959
std::unordered_map< crypto::hash, std::unordered_map< crypto::key_image, bool > > m_check_txin_table
Definition: blockchain.h:978
crypto::public_key get_output_key(uint64_t amount, uint64_t global_index) const
get the public key for an output
Definition: blockchain.cpp:1745
bool switch_to_alternative_blockchain(std::list< blocks_ext_by_hash::iterator > &alt_chain, bool discard_disconnected_chain)
performs a blockchain reorganization according to the longest chain rule
Definition: blockchain.cpp:930
bool have_tx(const crypto::hash &id) const
search the blockchain for a transaction by hash
Definition: blockchain.cpp:176
uint64_t get_difficulty_target() const
get difficulty target based on chain and hardfork version
Definition: blockchain.cpp:4352
difficulty_type get_next_difficulty_for_alternative_chain(const std::list< blocks_ext_by_hash::iterator > &alt_chain, block_extended_info &bei) const
gets the difficulty requirement for a new block on an alternate chain
Definition: blockchain.cpp:1025
bool check_tx_inputs(transaction &tx, uint64_t &pmax_used_block_height, crypto::hash &max_used_block_id, tx_verification_context &tvc, bool kept_by_block=false)
validates a transaction&#39;s inputs
Definition: blockchain.cpp:2284
static const std::vector< HardFork::Params > & get_hard_fork_heights(network_type nettype)
gets the hardfork heights of given network
Definition: blockchain.cpp:4314
Definition: core_rpc_server_commands_defs.h:722
bool m_db_sync_on_blocks
Definition: blockchain.h:989
bool get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request &req, COMMAND_RPC_GET_OUTPUTS_BIN::response &res) const
gets specific outputs to mix with
Definition: blockchain.cpp:1752
void invalidate_block_template_cache()
invalidates any cached block template
Definition: blockchain.cpp:4527
handle syncing calls instead of the backing db, synchronously
Definition: blockchain.h:69
Definition: hardfork.h:38
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=0) const
return a histogram of outputs on the blockchain
Definition: blockchain.cpp:4357
uint8_t get_next_hard_fork_version() const
returns the next hardfork version
Definition: blockchain.h:753
uint64_t m_btc_expected_reward
Definition: blockchain.h:1032
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: hardfork.cpp:380
uint64_t get_num_mature_outputs(uint64_t amount) const
get number of outputs of an amount past the minimum spendable age
Definition: blockchain.cpp:1728
bool add_new_block(const block &bl_, block_verification_context &bvc)
adds a block to the blockchain
Definition: blockchain.cpp:3575
Definition: verification_context.h:53
container for passing a block and metadata about it on the blockchain
Definition: blockchain.h:94
difficulty_type m_fixed_difficulty
Definition: blockchain.h:1022
bool m_show_time_stats
Definition: blockchain.h:987
bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, uint64_t &start_height, std::vector< uint64_t > &distribution, uint64_t &base) const
gets per block distribution of outputs of a given amount
Definition: blockchain.cpp:1787
bool get_short_chain_history(std::list< crypto::hash > &ids) const
gets the hashes for a subset of the blockchain
Definition: blockchain.cpp:696
uint64_t height
the height of the block in the blockchain
Definition: blockchain.h:97
void check_against_checkpoints(const checkpoints &points, bool enforce)
check the blockchain against a set of checkpoints
Definition: blockchain.cpp:3611
void load_compiled_in_block_hashes()
loads block hashes from compiled-in data set
bool check_tx_input(size_t tx_version, const txin_to_key &txin, const crypto::hash &tx_prefix_hash, const std::vector< crypto::signature > &sig, const rct::rctSig &rct_signatures, std::vector< rct::ctkey > &output_keys, uint64_t *pmax_related_block_height)
collect output public keys of a transaction input set
Definition: blockchain.cpp:3048
bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan=NULL) const
gets the block with a given hash
Definition: blockchain.cpp:766
std::unordered_map< crypto::hash, crypto::hash > m_blocks_longhash_table
Definition: blockchain.h:977
std::unordered_map< crypto::hash, size_t > blocks_by_id_index
Definition: blockchain.h:949
void cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t expected_reward, uint64_t pool_cookie)
stores a new cached block template
Definition: blockchain.cpp:4533
difficulty_type cumulative_difficulty
the accumulated difficulty after that block
Definition: blockchain.h:99
std::vector< difficulty_type > m_difficulties
Definition: blockchain.h:997
bool update_checkpoints(const std::string &file_path, bool check_dns)
loads new checkpoints from a file and optionally from DNS
Definition: blockchain.cpp:3648
std::unordered_map< crypto::hash, block_extended_info > blocks_ext_by_hash
Definition: blockchain.h:957
void remove_txpool_tx(const crypto::hash &txid)
Definition: blockchain.cpp:4253
void lock()
Definition: blockchain.cpp:4492
block m_btc
Definition: blockchain.h:1027
uint64_t get_txpool_tx_count(bool include_unrelayed_txes=true) const
Definition: blockchain.cpp:4258
HardFork::State get_hard_fork_state() const
gets the hardfork voting state object
Definition: blockchain.cpp:4309
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
uint64_t m_sync_counter
Definition: blockchain.h:994
Definition: verification_context.h:37
uint8_t get_next_version() const
returns the next version
Definition: hardfork.cpp:393
uint8_t get_hard_fork_version(uint64_t height) const
returns the actual hardfork version for a given block height
Definition: blockchain.h:772
void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta)
Definition: blockchain.cpp:4248
std::vector< uint64_t > m_global_output_indexes
Definition: blockchain.h:88
uint64_t m_bytes_to_sync
Definition: blockchain.h:995
size_t get_alternative_blocks_count() const
returns the number of alternative blocks stored
Definition: blockchain.cpp:1719
bool check_tx_outputs(const transaction &tx, tx_verification_context &tvc)
check that a transaction&#39;s outputs conform to current standards
Definition: blockchain.cpp:2315
bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const
Definition: blockchain.cpp:4263
std::atomic< bool > m_cancel
Definition: blockchain.h:1024
virtual uint64_t height() const =0
fetch the current blockchain height
bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
get information about hardfork voting for a version
Definition: blockchain.cpp:4347
uint64_t get_current_blockchain_height() const
get the current height of the blockchain
Definition: blockchain.cpp:331
std::vector< crypto::hash > m_blocks_txs_check
Definition: blockchain.h:983
transactions_container m_transactions
Definition: blockchain.h:971
HardFork * m_hardfork
Definition: blockchain.h:1018
bool m_btc_valid
Definition: blockchain.h:1033
bool deinit()
Uninitializes the blockchain state.
Definition: blockchain.cpp:538
difficulty_type get_difficulty_for_next_block()
returns the difficulty target the next block to be added must meet
Definition: blockchain.cpp:809
Definition: cryptonote_protocol_defs.h:178
uint64_t m_keeper_block_height
Definition: blockchain.h:86
uint8_t get_ideal_hard_fork_version() const
returns the newest hardfork version known to the blockchain
Definition: blockchain.h:746
blockchain_db_sync_mode
Definition: blockchain.h:66
Definition: rctTypes.h:78
void cancel()
Definition: blockchain.cpp:4395
int b
Definition: base.py:1
Definition: cryptonote_basic.h:124
Definition: rctTypes.h:416
Leave syncing up to the backing db (safest, but slowest because of disk I/O)
Definition: blockchain.h:71
bool have_block(const crypto::hash &id) const
checks if a block is known about with a given hash
Definition: blockchain.cpp:2126
std::unordered_map< crypto::hash, transaction_chain_entry > transactions_container
Definition: blockchain.h:951
bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request &arg, NOTIFY_RESPONSE_GET_OBJECTS::request &rsp)
retrieves a set of blocks and their transactions, and possibly other transactions ...
Definition: blockchain.cpp:1659
bool update_next_cumulative_weight_limit()
calculate the block weight limit for the next block to be added
Definition: blockchain.cpp:3558
bool m_enforce_dns_checkpoints
Definition: blockchain.h:1016
bool rollback_blockchain_switching(std::list< block > &original_chain, uint64_t rollback_height)
reverts the blockchain to its previous state following a failed switch
Definition: blockchain.cpp:888
uint64_t get_adjusted_time() const
get the "adjusted time"
Definition: blockchain.cpp:3106
void set_enforce_dns_checkpoints(bool enforce)
configure whether or not to enforce DNS-based checkpoints
Definition: blockchain.cpp:3683
std::map< uint64_t, std::vector< std::pair< crypto::hash, size_t > > > outputs_container
Definition: blockchain.h:961
POD_CLASS public_key
Definition: crypto.h:63
uint8_t get_ideal_hard_fork_version(uint64_t height) const
returns the newest hardfork version voted to be enabled as of a certain height
Definition: blockchain.h:763
bool for_all_outputs(std::function< bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)>) const
perform a check on all outputs in the blockchain
Definition: blockchain.cpp:4517
Definition: cryptonote_core.h:56
std::vector< crypto::hash > m_blocks_hash_of_hashes
Definition: blockchain.h:981
BlockchainDB * m_db
Definition: blockchain.h:964
bool check_block_timestamp(const block &b) const
Definition: blockchain.h:1286
uint8_t version
Definition: blockchain.cpp:87
bool reset_and_set_genesis_block(const block &b)
clears the blockchain and starts a new one
Definition: blockchain.cpp:650
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:93
std::string blobdata
Definition: blobdatatype.h:35
bool store_blockchain()
stores the blockchain
Definition: blockchain.cpp:508
Definition: base.py:1
#define blocks
Definition: sha512-hash.c:11
bool get_transactions_blobs(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs, bool pruned=false) const
gets transactions based on a list of transaction hashes
Definition: blockchain.cpp:1954
Blockchain(tx_memory_pool &tx_pool)
Blockchain constructor.
Definition: blockchain.cpp:166
tx_memory_pool & m_tx_pool
Definition: blockchain.h:966
void on_new_tx_from_block(const cryptonote::transaction &tx)
called when we see a tx originating from a block
Definition: blockchain.cpp:2258
Definition: core_rpc_server_commands_defs.h:696
crypto::hash get_block_id_by_height(uint64_t height) const
gets a block&#39;s hash given a height
Definition: blockchain.cpp:739
network_type
Definition: cryptonote_config.h:204
bool handle_block_to_main_chain(const block &bl, block_verification_context &bvc)
validate and add a new block to the end of the blockchain
Definition: blockchain.cpp:2152
Now-defunct (TODO: remove) struct from in-memory blockchain.
Definition: blockchain.h:83
blobdata m_btc_nonce
Definition: blockchain.h:1029
Definition: cryptonote_basic.h:400
bool complete_timestamps_vector(uint64_t start_height, std::vector< uint64_t > &timestamps)
finish an alternate chain&#39;s timestamp window from the main chain
Definition: blockchain.cpp:1398
void check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image, const std::vector< rct::ctkey > &pubkeys, const std::vector< crypto::signature > &sig, uint64_t &result)
validates a transaction input&#39;s ring signature
Definition: blockchain.cpp:2866
boost::thread_group m_async_pool
Definition: blockchain.h:1005
void set_show_time_stats(bool stats)
set whether or not to show/print time statistics
Definition: blockchain.h:718
blockchain_db_sync_mode m_db_sync_mode
Definition: blockchain.h:985
bool find_blockchain_supplement(const std::list< crypto::hash > &qblock_ids, std::vector< crypto::hash > &hashes, uint64_t &start_height, uint64_t &current_height) const
get recent block hashes for a foreign chain
Definition: blockchain.cpp:2015
std::uint64_t difficulty_type
Definition: difficulty.h:40
size_t m_current_block_cumul_weight_median
Definition: blockchain.h:973
epee::critical_section m_blockchain_lock
Definition: blockchain.h:968
bool check_block_timestamp(const block &b, uint64_t &median_ts) const
checks a block&#39;s timestamp
Definition: blockchain.cpp:3135
uint64_t already_generated_coins
the total coins minted after that block
Definition: blockchain.h:100
POD_CLASS key_image
Definition: crypto.h:89
const BlockchainDB & get_db() const
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:877
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:136
void output_scan_worker(const uint64_t amount, const std::vector< uint64_t > &offsets, std::vector< output_data_t > &outputs, std::unordered_map< crypto::hash, cryptonote::transaction > &txs) const
get a number of outputs of a specific amount
Definition: blockchain.cpp:3776
void safesyncmode(const bool onoff)
Put DB in safe sync mode.
Definition: blockchain.cpp:4297
network_type m_nettype
Definition: blockchain.h:1020
bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>, bool pruned) const
perform a check on all transactions in the blockchain
Definition: blockchain.cpp:4512
uint64_t prevalidate_block_hashes(uint64_t height, const std::vector< crypto::hash > &hashes)
Definition: blockchain.cpp:3792
bool is_within_compiled_block_hash_area() const
Definition: blockchain.h:931
uint8_t get_current_hard_fork_version() const
gets the current hardfork version in use/voted for
Definition: blockchain.h:739
size_t m_current_block_cumul_weight_limit
Definition: blockchain.h:972
bool get_tx_outputs_gindexs(const crypto::hash &tx_id, std::vector< uint64_t > &indexs) const
gets the global indices for outputs from a given transaction
Definition: blockchain.cpp:2235
Definition: cryptonote_protocol_defs.h:228
std::vector< block_extended_info > blocks_container
Definition: blockchain.h:955
size_t get_total_transactions() const
gets the total number of transactions on the main chain
Definition: blockchain.cpp:2159
size_t block_cumulative_weight
the weight of the block
Definition: blockchain.h:98
boost::asio::io_service m_async_service
Definition: blockchain.h:1004
void return_tx_to_pool(std::vector< transaction > &txs)
Definition: blockchain.cpp:3164
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:343
uint64_t block_difficulty(uint64_t i) const
gets the difficulty of the block with a given height
Definition: blockchain.cpp:1895
bool for_blocks_range(const uint64_t &h1, const uint64_t &h2, std::function< bool(uint64_t, const crypto::hash &, const block &)>) const
perform a check on all blocks in the blockchain in the given range
Definition: blockchain.cpp:4507
void get_output_key_mask_unlocked(const uint64_t &amount, const uint64_t &index, crypto::public_key &key, rct::key &mask, bool &unlocked) const
gets an output&#39;s key and unlocked state
Definition: blockchain.cpp:1778
size_t m_blob_size
Definition: blockchain.h:87
crypto::hash get_tail_id() const
get the hash of the most recent block on the blockchain
Definition: blockchain.cpp:674
bool get_blocks(uint64_t start_offset, size_t count, std::vector< std::pair< cryptonote::blobdata, block >> &blocks, std::vector< cryptonote::blobdata > &txs) const
get blocks and transactions from blocks based on start height and count
Definition: blockchain.cpp:1609
POD_CLASS hash
Definition: hash.h:49
std::list< std::pair< block_extended_info, std::vector< crypto::hash > > > get_alternative_chains() const
returns a set of known alternate chains
Definition: blockchain.cpp:4362
bool prepare_handle_incoming_blocks(const std::vector< block_complete_entry > &blocks)
performs some preprocessing on a group of incoming blocks to speed up verification ...
Definition: blockchain.cpp:3895
difficulty_type m_btc_difficulty
Definition: blockchain.h:1030
A container for blockchain checkpoints.
Definition: checkpoints.h:51
uint64_t get_current_cumulative_block_weight_median() const
gets the block weight median based on recent blocks (same window as for the limit) ...
Definition: blockchain.cpp:1205
State
Definition: hardfork.h:41
bool cleanup_handle_incoming_blocks(bool force_sync=false)
incoming blocks post-processing, cleanup, and disk sync
Definition: blockchain.cpp:3708
void add_txpool_tx(transaction &tx, const txpool_tx_meta_t &meta)
Definition: blockchain.cpp:4243
bool have_tx_keyimges_as_spent(const transaction &tx) const
check if any key image in a transaction has already been spent
Definition: blockchain.cpp:2389
bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
Definition: blockchain.cpp:4268
blocks_ext_by_hash m_alternative_chains
Definition: blockchain.h:1009
bool get_transactions(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs) const
Definition: blockchain.cpp:1981
transaction tx
Definition: blockchain.h:85
bool scan_outputkeys_for_indexes(size_t tx_version, const txin_to_key &tx_in_to_key, visitor_t &vis, const crypto::hash &tx_prefix_hash, uint64_t *pmax_related_block_height=NULL) const
collects the keys for all outputs being "spent" as an input
Definition: blockchain.cpp:200
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0)
Initialize the Blockchain state.
Definition: blockchain.cpp:343
bool flush_txes_from_pool(const std::vector< crypto::hash > &txids)
remove transactions from the transaction pool (if present)
Definition: blockchain.cpp:3182
void unlock()
Definition: blockchain.cpp:4497
void get_last_n_blocks_weights(std::vector< size_t > &weights, size_t count) const
gets recent block weights for median calculation
Definition: blockchain.cpp:1178
epee::critical_section m_difficulty_lock
Definition: blockchain.h:1000
uint8_t get_current_version() const
returns the current version
Definition: hardfork.cpp:357
account_public_address m_btc_address
Definition: blockchain.h:1028
Definition: cryptonote_basic.h:182
uint8_t get_ideal_version() const
returns the latest "ideal" version
Definition: hardfork.cpp:363
uint64_t m_timestamps_and_difficulties_height
Definition: blockchain.h:998
bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const
perform a check on all key images in the blockchain
Definition: blockchain.cpp:4502
void set_checkpoints(checkpoints &&chk_pts)
assign a set of blockchain checkpoint hashes
Definition: blockchain.h:149
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
Definition: hardfork.cpp:344
bool check_fee(size_t tx_weight, uint64_t fee) const
validate a transaction&#39;s fee
Definition: blockchain.cpp:2931
bool get_alternative_blocks(std::vector< block > &blocks) const
compiles a list of all blocks stored as alternative chains
Definition: blockchain.cpp:1706
Definition: blockchain.h:77
uint64_t m_btc_pool_cookie
Definition: blockchain.h:1031
uint8_t threshold
Definition: blockchain.cpp:89