Electroneum
blockchain.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 #include <boost/asio/io_service.hpp>
34 #if BOOST_VERSION >= 107400
35  #include <boost/serialization/library_version_type.hpp>
36 #endif
37 #include <boost/serialization/serialization.hpp>
38 #include <boost/serialization/version.hpp>
39 #include <boost/serialization/list.hpp>
40 #include <boost/multi_index_container.hpp>
41 #include <boost/multi_index/global_fun.hpp>
42 #include <boost/multi_index/hashed_index.hpp>
43 #include <boost/multi_index/member.hpp>
44 #include <boost/algorithm/hex.hpp>
45 #include <atomic>
46 #include <functional>
47 #include <unordered_map>
48 #include <unordered_set>
49 
50 #include "span.h"
51 #include "syncobj.h"
52 #include "string_tools.h"
53 #include "rolling_median.h"
55 #include "common/util.h"
59 #include "cryptonote_tx_utils.h"
61 #include "crypto/hash.h"
66 
67 namespace tools { class Notify; }
68 
69 namespace cryptonote
70 {
71  class tx_memory_pool;
72  struct test_options;
73 
78  {
83  };
84 
92  typedef std::function<const epee::span<const unsigned char>(cryptonote::network_type network)> GetCheckpointsCallback;
93 
94  /************************************************************************/
95  /* */
96  /************************************************************************/
97  class Blockchain
98  {
99  public:
104  {
107  size_t m_blob_size;
108  std::vector<uint64_t> m_global_output_indexes;
109  };
110 
115  {
121  };
122 
128  Blockchain(tx_memory_pool& tx_pool);
129 
133  ~Blockchain();
134 
147  bool init(BlockchainDB* db, const network_type nettype = MAINNET, bool offline = false, const cryptonote::test_options *test_options = NULL, difficulty_type fixed_difficulty = 0, const GetCheckpointsCallback& get_checkpoints = nullptr, bool ignore_bsig = false, bool fallback_to_pow = false);
148 
159  bool init(BlockchainDB* db, HardFork*& hf, const network_type nettype = MAINNET, bool offline = false);
160 
168  bool deinit();
169 
175  void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
176 
187  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
188 
198  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
199 
207  bool get_alternative_blocks(std::vector<block>& blocks) const;
208 
214  size_t get_alternative_blocks_count() const;
215 
224 
234  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
235 
244  bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks_entry, std::vector<block> &blocks);
245 
253  bool cleanup_handle_incoming_blocks(bool force_sync = false);
254 
262  bool have_tx(const crypto::hash &id) const;
263 
272  bool key_images_already_spent(const transaction &tx) const;
273 
283 
292  bool utxo_nonexistent(const transaction &tx) const;
293 
306  bool have_tx_keyimg_as_spent(const crypto::key_image &key_im) const;
307 
314 
320  crypto::hash get_tail_id() const;
321 
330 
337 
342 
356  bool add_new_block(const block& bl_, block_verification_context& bvc);
357 
365  bool reset_and_set_genesis_block(const block& b);
366 
380  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);
381  bool create_block_template(block& b, const crypto::hash *from_block, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
382 
393  bool have_block(const crypto::hash& id) const;
394 
400  size_t get_total_transactions() const;
401 
416  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
417 
433  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height, bool clip_pruned) const;
434 
447  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
448 
461  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset) const;
462 
480  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;
481 
495 
504 
513  crypto::public_key get_output_key(uint64_t amount, uint64_t global_index) const;
514 
529 
539  void get_output_key_mask_unlocked(const uint64_t& amount, const uint64_t& index, crypto::public_key& key, rct::key& mask, bool& unlocked) const;
540 
551  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;
552 
565  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
566  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, size_t n_txes, std::vector<std::vector<uint64_t>>& indexs) const;
567 
576  bool store_blockchain();
577 
594  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);
595 
604 
618  static uint64_t get_dynamic_base_fee(uint64_t block_reward, size_t median_block_weight, uint8_t version);
619 
633  uint64_t get_dynamic_base_fee_estimate(uint64_t grace_blocks) const;
634 
647  bool check_fee(size_t tx_weight, uint64_t fee) const;
648 
662 
669 
676 
683 
692 
705  template<class t_ids_container, class t_blocks_container, class t_missed_container>
706  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const;
707 
721  template<class t_ids_container, class t_tx_container, class t_missed_container>
722  bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs, bool pruned = false) const;
723  template<class t_ids_container, class t_tx_container, class t_missed_container>
724  bool get_split_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
725  template<class t_ids_container, class t_tx_container, class t_missed_container>
726  bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
727 
728  //debug functions
729 
741  void check_against_checkpoints(const checkpoints& points, bool enforce);
742 
748  void set_enforce_dns_checkpoints(bool enforce);
749 
758  bool update_checkpoints(const std::string& file_path, bool check_dns);
759 
766  // user options, must be called before calling init()
767 
777  void set_user_options(uint64_t maxthreads, bool sync_on_blocks, uint64_t sync_threshold,
778  blockchain_db_sync_mode sync_mode, bool fast_sync, std::string validator_key);
779 
785  void set_block_notify(const std::shared_ptr<tools::Notify> &notify) { m_block_notify = notify; }
786 
792  void set_reorg_notify(const std::shared_ptr<tools::Notify> &notify) { m_reorg_notify = notify; }
793 
797  void safesyncmode(const bool onoff);
798 
804  void set_show_time_stats(bool stats) { m_show_time_stats = stats; }
805 
812 
818  static const std::vector<HardFork::Params>& get_hard_fork_heights(network_type nettype);
819 
826 
832  uint8_t get_ideal_hard_fork_version() const { return m_hardfork->get_ideal_version(); }
833 
839  uint8_t get_next_hard_fork_version() const { return m_hardfork->get_next_version(); }
840 
850 
858  uint8_t get_hard_fork_version(uint64_t height) const { return m_hardfork->get(height); }
859 
866 
879  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;
880 
887 
895  bool flush_txes_from_pool(const std::vector<crypto::hash> &txids);
896 
907  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;
908 
916  bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
917 
927  bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const block&)>) const;
928 
937  bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const;
938 
946  bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)>) const;
947 
956  bool for_all_outputs(uint64_t amount, std::function<bool(uint64_t height)>) const;
957 
963  const BlockchainDB& get_db() const
964  {
965  return *m_db;
966  }
967 
974  {
975  return *m_db;
976  }
977 
985  void output_scan_worker(const uint64_t amount,const std::vector<uint64_t> &offsets,
986  std::vector<output_data_t> &outputs) const;
987 
996  std::unordered_map<crypto::hash, crypto::hash> &map) const;
997 
1003  std::list<std::pair<block_extended_info,std::vector<crypto::hash>>> get_alternative_chains() const;
1004 
1005  void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &meta);
1006  void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta);
1007  void remove_txpool_tx(const crypto::hash &txid);
1008  uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const;
1009  bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const;
1010  bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
1012  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;
1014 
1018  bool prune_blockchain(uint32_t pruning_seed = 0);
1020  bool check_blockchain_pruning();
1021 
1022  void lock();
1023  void unlock();
1024 
1025  void cancel();
1026 
1033 
1037  std::vector<time_t> get_last_block_timestamps(unsigned int blocks) const;
1038 
1044  void pop_blocks(uint64_t nblocks);
1045 
1053 
1059  bool verify_block_signature(const block& b);
1060 
1064  void set_validator_key(std::string key) { m_validator_key = boost::algorithm::unhex(key); }
1065 
1066  void set_validators_list_instance(std::unique_ptr<electroneum::basic::Validators> &v) { m_validators = v.get(); }
1067 
1069 
1073  network_type get_nettype() const { return m_nettype; };
1074 
1075 
1076 #ifndef IN_UNIT_TESTS
1077  private:
1078 #endif
1079 
1080  // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage
1081  typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
1082 
1083  typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;
1084 
1085  typedef std::unordered_set<crypto::key_image> key_images_container;
1086 
1087  typedef std::vector<block_extended_info> blocks_container;
1088 
1089  typedef std::unordered_map<crypto::hash, block_extended_info> blocks_ext_by_hash;
1090 
1091  typedef std::unordered_map<crypto::hash, block> blocks_by_hash;
1092 
1093  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
1094 
1095 
1096  BlockchainDB* m_db;
1097 
1098  tx_memory_pool& m_tx_pool;
1099 
1100  mutable epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
1101 
1102  // main chain
1103  transactions_container m_transactions;
1104  size_t m_current_block_cumul_weight_limit;
1105  size_t m_current_block_cumul_weight_median;
1106 
1107  // metadata containers
1108  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, std::vector<output_data_t>>> m_scan_table;
1109  std::unordered_map<crypto::hash, crypto::hash> m_blocks_longhash_table;
1110  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, bool>> m_check_txin_table;
1111 
1112  // SHA-3 hashes for each block and for fast pow checking
1113  std::vector<crypto::hash> m_blocks_hash_check;
1114  std::vector<crypto::hash> m_blocks_txs_check;
1115 
1116  blockchain_db_sync_mode m_db_sync_mode;
1117  bool m_fast_sync;
1118  bool m_show_time_stats;
1119  bool m_db_default_sync;
1120  bool m_db_sync_on_blocks;
1121  uint64_t m_db_sync_threshold;
1122  uint64_t m_max_prepare_blocks_threads;
1123  uint64_t m_fake_pow_calc_time;
1124  uint64_t m_fake_scan_time;
1125  uint64_t m_sync_counter;
1126  uint64_t m_bytes_to_sync;
1127  std::vector<uint64_t> m_timestamps;
1128  std::vector<difficulty_type> m_difficulties;
1129  uint64_t m_timestamps_and_difficulties_height;
1130  uint64_t m_long_term_block_weights_window;
1131  uint64_t m_long_term_effective_median_block_weight;
1132  mutable crypto::hash m_long_term_block_weights_cache_tip_hash;
1133  mutable epee::misc_utils::rolling_median_t<uint64_t> m_long_term_block_weights_cache_rolling_median;
1134 
1135  epee::critical_section m_difficulty_lock;
1136  crypto::hash m_difficulty_for_next_block_top_hash;
1137  difficulty_type m_difficulty_for_next_block;
1138 
1139  std::string m_validator_key;
1140  std::vector<std::string> m_validators_public_keys;
1141 
1142  boost::asio::io_service m_async_service;
1143  boost::thread_group m_async_pool;
1144  std::unique_ptr<boost::asio::io_service::work> m_async_work_idle;
1145 
1146  // all alternative chains
1147  blocks_ext_by_hash m_alternative_chains; // crypto::hash -> block_extended_info
1148 
1149  // some invalid blocks
1150  blocks_ext_by_hash m_invalid_blocks; // crypto::hash -> block_extended_info
1151 
1152 
1153  checkpoints m_checkpoints;
1154  bool m_enforce_dns_checkpoints;
1155 
1156  HardFork *m_hardfork;
1157 
1158  network_type m_nettype;
1159  bool m_offline;
1160  difficulty_type m_fixed_difficulty;
1161 
1162  bool m_ignore_bsig;
1163  bool m_fallback_to_pow;
1164 
1165  std::atomic<bool> m_cancel;
1166 
1167  // block template cache
1168  block m_btc;
1169  account_public_address m_btc_address;
1170  blobdata m_btc_nonce;
1171  difficulty_type m_btc_difficulty;
1172  uint64_t m_btc_height;
1173  uint64_t m_btc_pool_cookie;
1174  uint64_t m_btc_expected_reward;
1175  bool m_btc_valid;
1176 
1177 
1178  bool m_batch_success;
1179 
1180  std::shared_ptr<tools::Notify> m_block_notify;
1181  std::shared_ptr<tools::Notify> m_reorg_notify;
1182 
1183  electroneum::basic::Validators* m_validators;
1184 
1204  template<class visitor_t>
1205  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;
1206 
1227  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);
1228 
1249  bool check_tx_inputs(transaction& tx, tx_verification_context &tvc, uint64_t* pmax_used_block_height = NULL);
1250 
1263  bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::const_iterator>& alt_chain, bool discard_disconnected_chain);
1264 
1270  block pop_block_from_blockchain();
1271 
1284  bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc);
1285 
1299  bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc);
1300 
1314  bool handle_alternative_block(const block& b, const crypto::hash& id, block_verification_context& bvc);
1315 
1326  bool build_alt_chain(const crypto::hash &prev_id, std::list<blocks_ext_by_hash::const_iterator>& alt_chain, std::vector<uint64_t> &timestamps, block_verification_context& bvc) const;
1327 
1336  difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::const_iterator>& alt_chain, block_extended_info& bei) const;
1337 
1349  bool prevalidate_miner_transaction(const block& b, uint64_t height);
1350 
1367  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);
1368 
1381  bool rollback_blockchain_switching(std::list<block>& original_chain, uint64_t rollback_height);
1382 
1391  void get_last_n_blocks_weights(std::vector<uint64_t>& weights, size_t count) const;
1392 
1403  uint64_t get_long_term_block_weight_median(uint64_t start_height, size_t count) const;
1404 
1415  bool is_tx_spendtime_unlocked(uint64_t unlock_time) const;
1416 
1428  bool add_block_as_invalid(const block& bl, const crypto::hash& h);
1429 
1441  bool add_block_as_invalid(const block_extended_info& bei, const crypto::hash& h);
1442 
1459  bool check_block_timestamp(const block& b, uint64_t& median_ts) const;
1460  bool check_block_timestamp(const block& b) const { uint64_t median_ts; return check_block_timestamp(b, median_ts); }
1461 
1473  bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b, uint64_t& median_ts) const;
1474  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); }
1475 
1484  uint64_t get_adjusted_time() const;
1485 
1497  bool complete_timestamps_vector(uint64_t start_height, std::vector<uint64_t>& timestamps) const;
1498 
1506  bool update_next_cumulative_weight_limit(uint64_t *long_term_effective_median_block_weight = NULL);
1507  void return_tx_to_pool(std::vector<std::pair<transaction, blobdata>> &txs);
1508 
1517  bool check_for_double_spend(const transaction& tx, key_images_container& keys_this_block) const;
1518 
1528  void check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image,
1529  const std::vector<rct::ctkey> &pubkeys, const std::vector<crypto::signature> &sig, uint64_t &result);
1530 
1540  void load_compiled_in_block_hashes(const GetCheckpointsCallback& get_checkpoints);
1541 
1549  bool expand_transaction_2(transaction &tx, const crypto::hash &tx_prefix_hash, const std::vector<std::vector<rct::ctkey>> &pubkeys);
1550 
1554  void invalidate_block_template_cache();
1555 
1561  void cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t height, uint64_t expected_reward, uint64_t pool_cookie);
1562 
1563  };
1564 } // namespace cryptonote
const char * res
Definition: hmac_keccak.cpp:41
bool verify_block_signature(const block &b)
Verify block&#39;s digital signature.
difficulty_type cumulative_difficulty
the accumulated difficulty after that block
Definition: blockchain.h:119
bool is_within_compiled_block_hash_area() const
Definition: blockchain.h:1016
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
uint64_t get_current_cumulative_block_weight_limit() const
gets the block weight limit based on recent blocks
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
uint64_t height
the height of the block in the blockchain
Definition: blockchain.h:117
bool get_split_transactions_blobs(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs) const
void set_user_options(uint64_t maxthreads, bool sync_on_blocks, uint64_t sync_threshold, blockchain_db_sync_mode sync_mode, bool fast_sync, std::string validator_key)
Update the validators public key by fetching data from electroneum&#39;s endpoint.
void pop_blocks(uint64_t nblocks)
removes blocks from the top of the blockchain
Definition: blockchain.cpp:700
handle syncing calls instead of the backing db, asynchronously
Definition: blockchain.h:81
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
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:849
uint8_t get_current_version() const
returns the current version
Definition: hardfork.cpp:361
uint8_t get_next_version() const
returns the next version
Definition: hardfork.cpp:397
bool get_txpool_tx_meta(const crypto::hash &txid, txpool_tx_meta_t &meta) const
void normalize_v7_difficulties()
Normalize the cumulative difficulty for V7 blocks, fixing the differing difficulty among nodes...
bool utxo_nonexistence_from_output(const txin_to_key_public &public_output) const
check if a single utxo in a transaction has already been spent using the hash and out index (v3 tx on...
bool prepare_handle_incoming_blocks(const std::vector< block_complete_entry > &blocks_entry, std::vector< block > &blocks)
performs some preprocessing on a group of incoming blocks to speed up verification ...
::std::string string
Definition: gtest-port.h:1097
bool utxo_nonexistent(const transaction &tx) const
check if any utxo in a transaction has already been spent using the tx (v3 tx onwards) ...
bool find_blockchain_supplement(const std::list< crypto::hash > &qblock_ids, std::vector< crypto::hash > &hashes, uint64_t &start_height, uint64_t &current_height, bool clip_pruned) const
get recent block hashes for a foreign chain
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
uint64_t height
Definition: blockchain.cpp:91
uint64_t m_keeper_block_height
Definition: blockchain.h:106
user didn&#39;t specify, use db_async
Definition: blockchain.h:79
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
bool store_blockchain()
stores the blockchain
Definition: blockchain.cpp:630
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 ...
void set_validators_list_instance(std::unique_ptr< electroneum::basic::Validators > &v)
Definition: blockchain.h:1066
const char * key
Definition: hmac_keccak.cpp:39
handle syncing calls instead of the backing db, synchronously
Definition: blockchain.h:80
std::function< const epee::span< const unsigned char >cryptonote::network_type network)> GetCheckpointsCallback
Callback routine that returns checkpoints data for specific network type.
Definition: blockchain.h:92
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
void set_block_notify(const std::shared_ptr< tools::Notify > &notify)
sets a block notify object to call for every new block
Definition: blockchain.h:785
void remove_txpool_tx(const crypto::hash &txid)
static const std::vector< HardFork::Params > & get_hard_fork_heights(network_type nettype)
gets the hardfork heights of given network
void set_enforce_dns_checkpoints(bool enforce)
configure whether or not to enforce DNS-based checkpoints
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
uint64_t get_current_blockchain_height() const
get the current height of the blockchain
Definition: blockchain.cpp:322
bool key_images_already_spent(const transaction &tx) const
check if any key image in a transaction has already been spent
void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta)
unsigned char uint8_t
Definition: stdint.h:124
Now-defunct (TODO: remove) struct from in-memory blockchain.
Definition: blockchain.h:103
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
struct hash_func hashes[]
bool add_new_block(const block &bl_, block_verification_context &bvc)
adds a block to the blockchain
void safesyncmode(const bool onoff)
Put DB in safe sync mode.
void set_checkpoints(checkpoints &&chk_pts)
assign a set of blockchain checkpoint hashes
Definition: blockchain.h:175
uint64_t get_current_cumulative_block_weight_median() const
gets the block weight median based on recent blocks (same window as for the limit) ...
void set_reorg_notify(const std::shared_ptr< tools::Notify > &notify)
sets a reorg notify object to call for every reorg
Definition: blockchain.h:792
bool get_short_chain_history(std::list< crypto::hash > &ids) const
gets the hashes for a subset of the blockchain
Definition: blockchain.cpp:859
std::string privateKey
bool update_checkpoints(const std::string &file_path, bool check_dns)
loads new checkpoints from a file and optionally from DNS
std::list< std::pair< block_extended_info, std::vector< crypto::hash > > > get_alternative_chains() const
returns a set of known alternate chains
uint8_t get_current_hard_fork_version() const
gets the current hardfork version in use/voted for
Definition: blockchain.h:825
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
size_t get_total_transactions() const
gets the total number of transactions on the main chain
size_t block_cumulative_weight
the weight of the block
Definition: blockchain.h:118
bool flush_txes_from_pool(const std::vector< crypto::hash > &txids)
remove transactions from the transaction pool (if present)
uint8_t get_next_hard_fork_version() const
returns the next hardfork version
Definition: blockchain.h:839
mdb_size_t count(MDB_cursor *cur)
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
bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const
perform a check on all key images in the blockchain
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
crypto::hash get_tail_id() const
get the hash of the most recent block on the blockchain
Definition: blockchain.cpp:837
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
unsigned int uint32_t
Definition: stdint.h:126
uint64_t get_num_mature_outputs(uint64_t amount) const
get number of outputs of an amount past the minimum spendable age
uint64_t get_txpool_tx_count(bool include_unrelayed_txes=true) const
std::vector< uint64_t > m_global_output_indexes
Definition: blockchain.h:108
Various Tools.
Definition: tools.cpp:31
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
Definition: hardfork.cpp:348
void block_longhash_worker(uint64_t height, const epee::span< const block > &blocks, std::unordered_map< crypto::hash, crypto::hash > &map) const
computes the "short" and "long" hashes for a set of blocks
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
crypto::public_key get_output_key(uint64_t amount, uint64_t global_index) const
get the public key for an output
void sign_block(block &b, std::string privateKey)
Digitally sign the block.
uint64_t already_generated_coins
the total coins minted after that block
Definition: blockchain.h:120
HardFork::State get_hard_fork_state() const
gets the hardfork voting state object
static uint64_t get_fee_quantization_mask()
get fee quantization mask
void set_show_time_stats(bool stats)
set whether or not to show/print time statistics
Definition: blockchain.h:804
blockchain_db_sync_mode
Definition: blockchain.h:77
bool prune_blockchain(uint32_t pruning_seed=0)
unsigned __int64 uint64_t
Definition: stdint.h:136
bool reset_and_set_genesis_block(const block &b)
clears the blockchain and starts a new one
Definition: blockchain.cpp:812
difficulty_type get_difficulty_for_next_block()
returns the difficulty target the next block to be added must meet
Definition: blockchain.cpp:971
size_t m_blob_size
Definition: blockchain.h:107
void check_against_checkpoints(const checkpoints &points, bool enforce)
check the blockchain against a set of checkpoints
virtual uint64_t height() const =0
fetch the current blockchain height
Leave syncing up to the backing db (safest, but slowest because of disk I/O)
Definition: blockchain.h:82
void on_new_tx_from_block(const cryptonote::transaction &tx)
called when we see a tx originating from a block
POD_CLASS public_key
Definition: crypto.h:76
version
Supported socks variants.
Definition: socks.h:57
const BlockchainDB & get_db() const
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:963
bool get_transactions(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs) const
bool check_fee(size_t tx_weight, uint64_t fee) const
validate a transaction&#39;s fee
uint8_t get_ideal_version() const
returns the latest "ideal" version
Definition: hardfork.cpp:367
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:177
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:94
~Blockchain()
Blockchain destructor.
Definition: blockchain.cpp:161
std::string blobdata
Definition: blobdatatype.h:39
crypto::hash get_block_id_by_height(uint64_t height) const
gets a block&#39;s hash given a height
Definition: blockchain.cpp:901
void output_scan_worker(const uint64_t amount, const std::vector< uint64_t > &offsets, std::vector< output_data_t > &outputs) const
get a number of outputs of a specific amount
uint32_t get_mempool_tx_livetime() const
uint32_t get_blockchain_pruning_seed() const
Definition: blockchain.h:1017
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
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
The BlockchainDB backing store interface declaration/contract.
BlockchainDB & get_db()
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:973
transaction tx
Definition: blockchain.h:105
bool have_tx(const crypto::hash &id) const
search the blockchain for a transaction by hash
Definition: blockchain.cpp:167
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
bool have_block(const crypto::hash &id) const
checks if a block is known about with a given hash
POD_CLASS key_image
Definition: crypto.h:102
a struct containing txpool per transaction metadata
bool get_alternative_blocks(std::vector< block > &blocks) const
compiles a list of all blocks stored as alternative chains
uint8_t get_ideal_hard_fork_version() const
returns the newest hardfork version known to the blockchain
Definition: blockchain.h:832
Blockchain(tx_memory_pool &tx_pool)
Blockchain constructor.
Definition: blockchain.cpp:146
uint8_t get_hard_fork_version(uint64_t height) const
returns the actual hardfork version for a given block height
Definition: blockchain.h:858
uint64_t get_next_long_term_block_weight(uint64_t block_weight) const
gets the long term block weight for a new block
uint64_t get_difficulty_target() const
get difficulty target based on chain and hardfork version
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: hardfork.cpp:384
difficulty_type block_difficulty(uint64_t i) const
gets the difficulty of the block with a given height
network_type get_nettype() const
get blockchain nettype
Definition: blockchain.h:1073
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0, const GetCheckpointsCallback &get_checkpoints=nullptr, bool ignore_bsig=false, bool fallback_to_pow=false)
Initialize the Blockchain state.
Definition: blockchain.cpp:334
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
POD_CLASS hash
Definition: hash.h:50
size_t get_alternative_blocks_count() const
returns the number of alternative blocks stored
A container for blockchain checkpoints.
Definition: checkpoints.h:51
const char * address
Definition: multisig.cpp:37
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:928
bool check_tx_outputs(const transaction &tx, tx_verification_context &tvc)
check that a transaction&#39;s outputs conform to current standards
container for passing a block and metadata about it on the blockchain
Definition: blockchain.h:114
electroneum::basic::Validator get_validator_by_height(uint64_t height)
virtual uint32_t get_blockchain_pruning_seed() const =0
get the blockchain pruning seed
void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &meta)
bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
bool deinit()
Uninitializes the blockchain state.
Definition: blockchain.cpp:660
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: blockchain.h:865
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
void set_validator_key(std::string key)
set validator key
Definition: blockchain.h:1064
std::vector< time_t > get_last_block_timestamps(unsigned int blocks) const
returns the timestamps of the last N blocks
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
bool cleanup_handle_incoming_blocks(bool force_sync=false)
incoming blocks post-processing, cleanup, and disk sync
uint8_t threshold
Definition: blockchain.cpp:92