Electroneum
cryptonote_core.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 
34 #include <ctime>
35 
36 #include <boost/program_options/options_description.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 
41 #include "common/download.h"
42 #include "common/command_line.h"
43 #include "tx_pool.h"
44 #include "blockchain.h"
45 #include "cryptonote_basic/miner.h"
48 #include "warnings.h"
49 #include "crypto/hash.h"
51 
54 
55 namespace cryptonote
56 {
57  struct test_options {
58  const std::pair<uint8_t, uint64_t> *hard_forks;
60  };
61 
73 
74  /************************************************************************/
75  /* */
76  /************************************************************************/
77 
85  class core: public i_miner_handler
86  {
87  public:
88 
96  core(i_cryptonote_protocol* pprotocol);
97 
105 
113  bool on_idle();
114 
129  bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
130 
145  bool handle_incoming_txs(const std::vector<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
146 
162  bool handle_incoming_block(const blobdata& block_blob, const block *b, block_verification_context& bvc, bool update_miner_blocktemplate = true);
163 
169  bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks_entry, std::vector<block> &blocks);
170 
176  bool cleanup_handle_incoming_blocks(bool force_sync = false);
177 
185  bool check_incoming_block_size(const blobdata& block_blob) const;
186 
192  i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
193 
194  //-------------------- i_miner_handler -----------------------
195 
208  virtual bool handle_block_found(block& b, block_verification_context &bvc);
209 
215  virtual bool get_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
216  virtual bool get_block_template(block& b, const crypto::hash *prev_block, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
217 
221  virtual void on_transaction_relayed(const cryptonote::blobdata& tx);
222 
223 
229  miner& get_miner(){return m_miner;}
230 
236  const miner& get_miner()const{return m_miner;}
237 
246  static void init_options(boost::program_options::options_description& desc);
247 
260  bool init(const boost::program_options::variables_map& vm, const test_options *test_options = NULL, const GetCheckpointsCallback& get_checkpoints = nullptr);
261 
267  bool set_genesis_block(const block& b);
268 
276  bool deinit();
277 
281  void test_drop_download();
282 
288  void test_drop_download_height(uint64_t height);
289 
295  bool get_test_drop_download() const;
296 
305  bool get_test_drop_download_height() const;
306 
312  uint64_t get_current_blockchain_height() const;
313 
320  void get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
321 
327  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
328 
334  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
335 
341  bool get_blocks(uint64_t start_offset, size_t count, std::vector<block>& blocks) const;
342 
348  template<class t_ids_container, class t_blocks_container, class t_missed_container>
349  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
350  {
351  return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
352  }
353 
359  crypto::hash get_block_id_by_height(uint64_t height) const;
360 
366  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs) const;
367 
373  bool get_split_transactions_blobs(const std::vector<crypto::hash>& txs_ids, std::vector<std::tuple<crypto::hash, cryptonote::blobdata, crypto::hash, cryptonote::blobdata>>& txs, std::vector<crypto::hash>& missed_txs) const;
374 
380  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<transaction>& txs, std::vector<crypto::hash>& missed_txs) const;
381 
387  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
388 
394  bool get_alternative_blocks(std::vector<block>& blocks) const;
395 
401  size_t get_alternative_blocks_count() const;
402 
408  void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
409 
415  void set_checkpoints(checkpoints&& chk_pts);
416 
422  void set_checkpoints_file_path(const std::string& path);
423 
429  void set_enforce_dns_checkpoints(bool enforce_dns);
430 
436  void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
437 
444  bool pool_has_utxo_as_spent(const txin_to_key_public& in) const;
445 
452  bool pool_has_tx(const crypto::hash &txid) const;
453 
460  bool get_pool_transactions(std::vector<transaction>& txs, bool include_unrelayed_txes = true) const;
461 
467  bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
468 
475  bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const;
476 
483  bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const;
484 
490  bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx) const;
491 
498  bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_unrelayed_txes = true) const;
499 
505  bool get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const;
506 
512  size_t get_pool_transactions_count() const;
513 
519  size_t get_blockchain_total_transactions() const;
520 
526  bool have_block(const crypto::hash& id) const;
527 
533  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
534 
540  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
541 
547  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;
548 
556  bool get_stat_info(core_stat_info& st_inf) const;
557 
563  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
564  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, size_t n_txes, std::vector<std::vector<uint64_t>>& indexs) const;
565 
571  crypto::hash get_tail_id() const;
572 
578  void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff);
579 
585  difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
586 
593 
599  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;
600 
606  void pause_mine();
607 
613  void resume_mine();
614 
620  Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
621 
627  const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
628 
634  std::string print_pool(bool short_format) const;
635 
641  void on_synchronized();
642 
648  void safesyncmode(const bool onoff);
649 
655  void set_target_blockchain_height(uint64_t target_blockchain_height);
656 
662  uint64_t get_target_blockchain_height() const;
663 
669  uint8_t get_ideal_hard_fork_version() const;
670 
676  uint8_t get_ideal_hard_fork_version(uint64_t height) const;
677 
683  uint8_t get_hard_fork_version(uint64_t height) const;
684 
690  uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
691 
696  std::time_t get_start_time() const;
697 
708  bool update_checkpoints();
709 
715  std::string get_validators_list();
716 
723  electroneum::basic::list_update_outcome set_validators_list(std::string v_list, bool isEmergencyUpdate);
724 
730  bool isValidatorsListValid();
731 
738  void graceful_exit();
739 
745  void stop();
746 
752  bool is_key_image_spent(const crypto::key_image& key_im) const;
753 
764  bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
765 
776  bool utxo_nonexistant(const std::vector<txin_to_key_public>& public_outputs, std::vector<bool> &spent) const;
777 
786  bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
787 
793  size_t get_block_sync_size(uint64_t height) const;
794 
800  std::pair<uint64_t, uint64_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
801 
807  network_type get_nettype() const { return m_nettype; };
808 
817  bool is_update_available() const { return m_update_available; }
818 
824  bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
825 
831  bool pad_transactions() const { return m_pad_transactions; }
832 
838  uint64_t get_free_space() const;
839 
845  bool offline() const { return m_offline; }
846 
852  uint32_t get_blockchain_pruning_seed() const;
853 
861  bool prune_blockchain(uint32_t pruning_seed = 0);
862 
868  bool update_blockchain_pruning();
869 
875  bool check_blockchain_pruning();
876 
884  bool set_validator_key(std::string key);
885 
886  std::vector<std::string> generate_ed25519_keypair();
887 
889 
891 
892  std::vector<address_outputs> get_address_batch_history(const address_parse_info &addr, const uint64_t &start_tx_id = 0, const uint64_t &batch_size = 100, bool desc = false);
893 
894  std::vector<address_txs> get_addr_tx_batch_history(const address_parse_info &addr, const uint64_t &start_tx_id = 0, const uint64_t &batch_size = 100, bool desc = false);
895 
896  private:
897 
908  bool add_new_tx(transaction& tx, const crypto::hash& tx_hash, const cryptonote::blobdata &blob, size_t tx_weight, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
909 
925  bool add_new_tx(transaction& tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
926 
932  bool add_new_block(const block& b, block_verification_context& bvc);
933 
941  bool load_state_data();
942 
948  bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, const blobdata& blob) const;
949 
960  bool check_tx_syntax(const transaction& tx) const;
961 
978  bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
979  void set_semantics_failed(const crypto::hash &tx_hash);
980 
981  bool handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
982  bool handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
983  struct tx_verification_batch_info { const cryptonote::transaction *tx; crypto::hash tx_hash; tx_verification_context &tvc; bool &result; };
984  bool handle_incoming_tx_accumulated_batch(std::vector<tx_verification_batch_info> &tx_info, bool keeped_by_block);
985 
993  bool update_miner_block_template();
994 
1002  bool handle_command_line(const boost::program_options::variables_map& vm);
1003 
1011  bool check_tx_inputs_keyimages_diff(const transaction& tx) const;
1012 
1013  bool check_tx_inputs_utxos_diff(const transaction& tx) const;
1014 
1022  bool check_tx_inputs_ring_members_diff(const transaction& tx) const;
1023 
1032  bool check_tx_inputs_keyimages_domain(const transaction& tx) const;
1033 
1044  bool check_fork_time();
1045 
1051  bool relay_txpool_transactions();
1052 
1058  bool check_updates();
1059 
1065  bool check_disk_space();
1066 
1072  bool check_block_rate();
1073 
1074  bool m_test_drop_download = true;
1075 
1076  uint64_t m_test_drop_download_height = 0;
1077 
1078  tx_memory_pool m_mempool;
1079  Blockchain m_blockchain_storage;
1080 
1081  i_cryptonote_protocol* m_pprotocol;
1082 
1083  epee::critical_section m_incoming_tx_lock;
1084 
1085  //m_miner and m_miner_addres are probably temporary here
1086  miner m_miner;
1087  account_public_address m_miner_address;
1088 
1089  std::string m_config_folder;
1090 
1091  cryptonote_protocol_stub m_protocol_stub;
1092 
1093  epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
1097  epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval;
1099  epee::math_helper::once_a_time_seconds<60*60*5, true> m_blockchain_pruning_interval;
1100  epee::math_helper::once_a_time_seconds<60*2, true> m_check_validators_interval;
1101 
1102  std::atomic<bool> m_starter_message_showed;
1103 
1104  uint64_t m_target_blockchain_height;
1105 
1106  network_type m_nettype;
1107 
1108  std::atomic<bool> m_update_available;
1109 
1110  std::string m_checkpoints_path;
1111  time_t m_last_dns_checkpoints_update;
1112  time_t m_last_json_checkpoints_update;
1113 
1114  std::atomic_flag m_checkpoints_updating;
1115  bool m_disable_dns_checkpoints;
1116 
1117  size_t block_sync_size;
1118 
1119  time_t start_time;
1120 
1121  std::unordered_set<crypto::hash> bad_semantics_txes[2];
1122  boost::mutex bad_semantics_txes_lock;
1123 
1124  //tools::thread_group m_threadpool;
1125 
1126  std::unique_ptr<electroneum::basic::Validators> m_validators;
1127 
1128  enum {
1129  UPDATES_DISABLED,
1130  UPDATES_NOTIFY,
1131  UPDATES_DOWNLOAD,
1132  UPDATES_UPDATE,
1133  } check_updates_level;
1134 
1135  tools::download_async_handle m_update_download;
1136  size_t m_last_update_length;
1137  boost::mutex m_update_mutex;
1138 
1139  bool m_fluffy_blocks_enabled;
1140  bool m_offline;
1141  bool m_pad_transactions;
1142 
1143  std::shared_ptr<tools::Notify> m_block_rate_notify;
1144 
1145  bool m_fallback_to_pow;
1146  std::string m_fallback_to_pow_checkpoint_hash;
1147  uint64_t m_fallback_to_pow_checkpoint_height;
1148  };
1149 }
1150 
const char * res
Definition: hmac_keccak.cpp:41
PUSH_WARNINGS
Definition: hash-ops.h:54
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
::std::string string
Definition: gtest-port.h:1097
const command_line::arg_descriptor< size_t > arg_block_download_max_size
uint64_t height
Definition: blockchain.cpp:91
const char * key
Definition: hmac_keccak.cpp:39
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
const command_line::arg_descriptor< bool, false > arg_stagenet_on
void disable_dns_checkpoints(bool disable=true)
set whether or not to enable or disable DNS checkpoints
network_type get_nettype() const
get the network type we&#39;re on
uint64_t get_balance(const cryptonote::account_base &addr, const std::vector< cryptonote::block > &blockchain, const map_hash2tx_t &mtx)
Definition: chaingen.cpp:940
unsigned char uint8_t
Definition: stdint.h:124
std::unordered_map< crypto::key_image, std::vector< crypto::hash > > key_images_with_tx_hashes
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
const command_line::arg_descriptor< difficulty_type > arg_fixed_difficulty
mdb_size_t count(MDB_cursor *cur)
const command_line::arg_descriptor< bool, false > arg_testnet_on
bool check_updates(const std::string &software, const std::string &buildtag, std::string &version, std::string &hash)
Definition: updates.cpp:41
unsigned int uint32_t
Definition: stdint.h:126
#define POP_WARNINGS
Definition: warnings.h:17
i_cryptonote_protocol * get_protocol()
get the cryptonote protocol instance
handles core cryptonote functionality
bool fluffy_blocks_enabled() const
get whether fluffy blocks are enabled
unsigned __int64 uint64_t
Definition: stdint.h:136
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
bool is_update_available() const
check whether an update is known to be available or not
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
bool get_blocks(const t_ids_container &block_ids, t_blocks_container &blocks, t_missed_container &missed_bs) const
gets blocks based on a list of block hashes
version
Supported socks variants.
Definition: socks.h:57
const Blockchain & get_blockchain_storage() const
gets the Blockchain instance (const)
const command_line::arg_descriptor< bool > arg_offline
std::string blobdata
Definition: blobdatatype.h:39
bool offline() const
get whether the core is running offline
miner & get_miner()
gets the miner instance
Blockchain & get_blockchain_storage()
gets the Blockchain instance
const command_line::arg_descriptor< bool > arg_fallback_to_pow
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
POD_CLASS key_image
Definition: crypto.h:102
const miner & get_miner() const
gets the miner instance (const)
const command_line::arg_descriptor< bool > arg_skip_block_sig_verification
const command_line::arg_descriptor< bool > arg_regtest_on
bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector< uint64_t > &distribution, uint64_t &base)
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
POD_CLASS hash
Definition: hash.h:50
const command_line::arg_descriptor< uint64_t > arg_fallback_to_pow_checkpoint_height
A container for blockchain checkpoints.
Definition: checkpoints.h:51
const command_line::arg_descriptor< std::string > arg_fallback_to_pow_checkpoint_hash
std::string sign_message(const std::string &message, const std::string &privateKey)
Definition: crypto.h:376
std::shared_ptr< download_thread_control > download_async_handle
Definition: download.h:36
bool pad_transactions() const
get whether transaction relay should be padded
const std::pair< uint8_t, uint64_t > * hard_forks
const size_t long_term_block_weight_window