Monero
cryptonote_core.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 
33 #include <ctime>
34 
35 #include <boost/program_options/options_description.hpp>
36 #include <boost/program_options/variables_map.hpp>
37 #include <boost/interprocess/sync/file_lock.hpp>
38 
40 #include "storages/portable_storage_template_helper.h"
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"
50 
53 
54 namespace cryptonote
55 {
56  struct test_options {
57  const std::pair<uint8_t, uint64_t> *hard_forks;
58  };
59 
66 
67  /************************************************************************/
68  /* */
69  /************************************************************************/
70 
78  class core: public i_miner_handler
79  {
80  public:
81 
89  core(i_cryptonote_protocol* pprotocol);
90 
98 
106  bool on_idle();
107 
122  bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
123 
138  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);
139 
154  bool handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate = true);
155 
161  bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks);
162 
168  bool cleanup_handle_incoming_blocks(bool force_sync = false);
169 
177  bool check_incoming_block_size(const blobdata& block_blob) const;
178 
184  i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
185 
186  //-------------------- i_miner_handler -----------------------
187 
199  virtual bool handle_block_found( block& b);
200 
206  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);
207 
211  virtual void on_transaction_relayed(const cryptonote::blobdata& tx);
212 
213 
219  miner& get_miner(){return m_miner;}
220 
226  const miner& get_miner()const{return m_miner;}
227 
236  static void init_options(boost::program_options::options_description& desc);
237 
250  bool init(const boost::program_options::variables_map& vm, const char *config_subdir = NULL, const test_options *test_options = NULL);
251 
257  bool set_genesis_block(const block& b);
258 
266  bool deinit();
267 
271  void test_drop_download();
272 
278  void test_drop_download_height(uint64_t height);
279 
285  bool get_test_drop_download() const;
286 
295  bool get_test_drop_download_height() const;
296 
302  uint64_t get_current_blockchain_height() const;
303 
310  void get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
311 
317  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
318 
324  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
325 
331  bool get_blocks(uint64_t start_offset, size_t count, std::vector<block>& blocks) const;
332 
338  template<class t_ids_container, class t_blocks_container, class t_missed_container>
339  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
340  {
341  return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
342  }
343 
349  crypto::hash get_block_id_by_height(uint64_t height) const;
350 
356  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs) const;
357 
363  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<transaction>& txs, std::vector<crypto::hash>& missed_txs) const;
364 
370  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
371 
377  bool get_alternative_blocks(std::vector<block>& blocks) const;
378 
384  size_t get_alternative_blocks_count() const;
385 
391  void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
392 
398  void set_checkpoints(checkpoints&& chk_pts);
399 
405  void set_checkpoints_file_path(const std::string& path);
406 
412  void set_enforce_dns_checkpoints(bool enforce_dns);
413 
419  void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
420 
426  bool pool_has_tx(const crypto::hash &txid) const;
427 
434  bool get_pool_transactions(std::vector<transaction>& txs, bool include_unrelayed_txes = true) const;
435 
441  bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
442 
449  bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const;
450 
457  bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const;
458 
464  bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx) const;
465 
472  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;
473 
479  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;
480 
486  size_t get_pool_transactions_count() const;
487 
493  size_t get_blockchain_total_transactions() const;
494 
500  bool have_block(const crypto::hash& id) const;
501 
507  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
508 
514  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
515 
521  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;
522 
530  bool get_stat_info(core_stat_info& st_inf) const;
531 
537  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
538 
544  crypto::hash get_tail_id() const;
545 
551  difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
552 
559 
565  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;
566 
572  void pause_mine();
573 
579  void resume_mine();
580 
586  Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
587 
593  const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
594 
600  std::string print_pool(bool short_format) const;
601 
607  void on_synchronized();
608 
614  void safesyncmode(const bool onoff);
615 
621  void set_target_blockchain_height(uint64_t target_blockchain_height);
622 
628  uint64_t get_target_blockchain_height() const;
629 
635  uint8_t get_ideal_hard_fork_version() const;
636 
642  uint8_t get_ideal_hard_fork_version(uint64_t height) const;
643 
649  uint8_t get_hard_fork_version(uint64_t height) const;
650 
656  uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
657 
662  std::time_t get_start_time() const;
663 
674  bool update_checkpoints();
675 
682  void graceful_exit();
683 
689  void stop();
690 
696  bool is_key_image_spent(const crypto::key_image& key_im) const;
697 
708  bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
709 
718  bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
719 
725  size_t get_block_sync_size(uint64_t height) const;
726 
732  std::pair<uint64_t, uint64_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
733 
739  network_type get_nettype() const { return m_nettype; };
740 
749  bool is_update_available() const { return m_update_available; }
750 
756  bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
757 
763  uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes);
764 
770  uint64_t get_free_space() const;
771 
777  bool offline() const { return m_offline; }
778 
779  private:
780 
791  bool add_new_tx(transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t tx_weight, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
792 
808  bool add_new_tx(transaction& tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
809 
815  bool add_new_block(const block& b, block_verification_context& bvc);
816 
824  bool load_state_data();
825 
831  bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash, const blobdata& blob) const;
832 
843  bool check_tx_syntax(const transaction& tx) const;
844 
861  bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
862  void set_semantics_failed(const crypto::hash &tx_hash);
863 
864  bool handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
865  bool handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
867  bool handle_incoming_tx_accumulated_batch(std::vector<tx_verification_batch_info> &tx_info, bool keeped_by_block);
868 
876  bool update_miner_block_template();
877 
885  bool handle_command_line(const boost::program_options::variables_map& vm);
886 
894  bool check_tx_inputs_keyimages_diff(const transaction& tx) const;
895 
903  bool check_tx_inputs_ring_members_diff(const transaction& tx) const;
904 
913  bool check_tx_inputs_keyimages_domain(const transaction& tx) const;
914 
925  bool check_fork_time();
926 
932  bool relay_txpool_transactions();
933 
939  bool check_updates();
940 
946  bool check_disk_space();
947 
948  bool m_test_drop_download = true;
949 
950  uint64_t m_test_drop_download_height = 0;
951 
954 
956 
957  epee::critical_section m_incoming_tx_lock;
958 
959  //m_miner and m_miner_addres are probably temporary here
962 
963  std::string m_config_folder;
964 
966 
967  epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
968  epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner;
969  epee::math_helper::once_a_time_seconds<60*2, false> m_txpool_auto_relayer;
970  epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval;
971  epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval;
972 
973  std::atomic<bool> m_starter_message_showed;
974 
976 
978 
979  std::atomic<bool> m_update_available;
980 
981  std::string m_checkpoints_path;
984 
985  std::atomic_flag m_checkpoints_updating;
987 
989 
990  time_t start_time;
991 
992  std::unordered_set<crypto::hash> bad_semantics_txes[2];
994 
995  enum {
1000  } check_updates_level;
1001 
1004  boost::mutex m_update_mutex;
1005 
1008  };
1009 }
1010 
1011 POP_WARNINGS
Definition: cryptonote_protocol_defs.h:162
Blockchain m_blockchain_storage
Blockchain instance.
Definition: cryptonote_core.h:953
std::string m_config_folder
folder to look in for configs and other files
Definition: cryptonote_core.h:963
PUSH_WARNINGS
Definition: hash-ops.h:53
epee::math_helper::once_a_time_seconds< 60 *60 *12, true > m_check_updates_interval
interval for checking for new versions
Definition: cryptonote_core.h:970
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:56
std::atomic_flag m_checkpoints_updating
set if checkpoints are currently updating to avoid multiple threads attempting to update at once ...
Definition: cryptonote_core.h:985
crypto::hash tx_hash
Definition: cryptonote_core.h:866
Definition: cryptonote_protocol_handler_common.h:51
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:223
Definition: cryptonote_core.h:999
Definition: cryptonote_basic.h:366
time_t m_last_json_checkpoints_update
time when json checkpoints were last updated
Definition: cryptonote_core.h:983
uint64_t height
Definition: blockchain.cpp:88
time_t start_time
Definition: cryptonote_core.h:990
account_public_address m_miner_address
address to mine to (for miner instance)
Definition: cryptonote_core.h:961
Definition: core_rpc_server_commands_defs.h:722
time_t m_last_dns_checkpoints_update
time when dns checkpoints were last updated
Definition: cryptonote_core.h:982
const command_line::arg_descriptor< bool, false > arg_stagenet_on
Definition: cryptonote_core.cpp:74
void disable_dns_checkpoints(bool disable=true)
set whether or not to enable or disable DNS checkpoints
Definition: cryptonote_core.h:419
Definition: connection_context.h:40
network_type get_nettype() const
get the network type we&#39;re on
Definition: cryptonote_core.h:739
Definition: cryptonote_core.h:997
Definition: verification_context.h:53
Definition: cryptonote_stat_info.h:37
std::unordered_map< crypto::key_image, std::vector< crypto::hash > > key_images_with_tx_hashes
Definition: message_data_structs.h:102
bool m_offline
Definition: cryptonote_core.h:1007
std::atomic< bool > m_update_available
Definition: cryptonote_core.h:979
bool m_fluffy_blocks_enabled
Definition: cryptonote_core.h:1006
std::atomic< bool > m_starter_message_showed
has the "daemon will sync now" message been shown?
Definition: cryptonote_core.h:973
network_type m_nettype
which network are we on?
Definition: cryptonote_core.h:977
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
Definition: verification_context.h:37
const command_line::arg_descriptor< difficulty_type > arg_fixed_difficulty
Definition: cryptonote_core.cpp:84
epee::math_helper::once_a_time_seconds< 60 *60 *2, true > m_fork_moaner
interval for checking HardFork status
Definition: cryptonote_core.h:968
tx_memory_pool m_mempool
transaction pool instance
Definition: cryptonote_core.h:952
const command_line::arg_descriptor< bool, false > arg_testnet_on
Definition: cryptonote_core.cpp:69
bool check_updates(const std::string &software, const std::string &buildtag, std::string &version, std::string &hash)
Definition: updates.cpp:40
epee::math_helper::once_a_time_seconds< 60 *60 *12, false > m_store_blockchain_interval
interval for manual storing of Blockchain, if enabled
Definition: cryptonote_core.h:967
size_t m_last_update_length
Definition: cryptonote_core.h:1003
bool m_disable_dns_checkpoints
Definition: cryptonote_core.h:986
Definition: cryptonote_core.h:996
i_cryptonote_protocol * get_protocol()
get the cryptonote protocol instance
Definition: cryptonote_core.h:184
Definition: miner.h:62
Definition: cryptonote_protocol_defs.h:178
handles core cryptonote functionality
Definition: cryptonote_core.h:78
Definition: cryptonote_core.h:998
bool fluffy_blocks_enabled() const
get whether fluffy blocks are enabled
Definition: cryptonote_core.h:756
int b
Definition: base.py:1
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
Definition: cryptonote_core.cpp:89
bool is_update_available() const
check whether an update is known to be available or not
Definition: cryptonote_core.h:749
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
Definition: cryptonote_core.h:339
boost::mutex bad_semantics_txes_lock
Definition: cryptonote_core.h:993
std::string m_checkpoints_path
path to json checkpoints file
Definition: cryptonote_core.h:981
epee::critical_section m_incoming_tx_lock
incoming transaction lock
Definition: cryptonote_core.h:957
boost::mutex m_update_mutex
Definition: cryptonote_core.h:1004
cryptonote_protocol_stub m_protocol_stub
cryptonote protocol stub instance
Definition: cryptonote_core.h:965
Definition: cryptonote_core.h:56
uint64_t m_target_blockchain_height
blockchain height target
Definition: cryptonote_core.h:975
const Blockchain & get_blockchain_storage() const
gets the Blockchain instance (const)
Definition: cryptonote_core.h:593
epee::math_helper::once_a_time_seconds< 60 *10, true > m_check_disk_space_interval
interval for checking for disk space
Definition: cryptonote_core.h:971
const command_line::arg_descriptor< bool > arg_offline
Definition: cryptonote_core.cpp:102
uint8_t version
Definition: blockchain.cpp:87
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:93
std::string blobdata
Definition: blobdatatype.h:35
Definition: base.py:1
#define blocks
Definition: sha512-hash.c:11
bool offline() const
get whether the core is running offline
Definition: cryptonote_core.h:777
Definition: core_rpc_server_commands_defs.h:1540
Definition: core_rpc_server_commands_defs.h:696
network_type
Definition: cryptonote_config.h:204
miner & get_miner()
gets the miner instance
Definition: cryptonote_core.h:219
Definition: core_rpc_server_commands_defs.h:1382
Definition: cryptonote_basic.h:400
Blockchain & get_blockchain_storage()
gets the Blockchain instance
Definition: cryptonote_core.h:586
std::uint64_t difficulty_type
Definition: difficulty.h:40
size_t block_sync_size
Definition: cryptonote_core.h:988
POD_CLASS key_image
Definition: crypto.h:89
const miner & get_miner() const
gets the miner instance (const)
Definition: cryptonote_core.h:226
Definition: cryptonote_protocol_defs.h:228
miner m_miner
miner instance
Definition: cryptonote_core.h:960
const command_line::arg_descriptor< bool > arg_regtest_on
Definition: cryptonote_core.cpp:79
tools::download_async_handle m_update_download
Definition: cryptonote_core.h:1002
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
POD_CLASS hash
Definition: hash.h:49
i_cryptonote_protocol * m_pprotocol
cryptonote protocol instance
Definition: cryptonote_core.h:955
A container for blockchain checkpoints.
Definition: checkpoints.h:51
std::shared_ptr< download_thread_control > download_async_handle
Definition: download.h:35
const std::pair< uint8_t, uint64_t > * hard_forks
Definition: cryptonote_core.h:57
epee::math_helper::once_a_time_seconds< 60 *2, false > m_txpool_auto_relayer
interval for checking re-relaying txpool transactions
Definition: cryptonote_core.h:969
Definition: cryptonote_basic.h:182
Definition: miner.h:51
Definition: cryptonote_protocol_handler_common.h:41
Definition: blockchain.h:77
Definition: cryptonote_core.h:866