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;
59  };
60 
67 
68  /************************************************************************/
69  /* */
70  /************************************************************************/
71 
79  class core: public i_miner_handler
80  {
81  public:
82 
90  core(i_cryptonote_protocol* pprotocol);
91 
99 
107  bool on_idle();
108 
123  bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
124 
139  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);
140 
155  bool handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate = true);
156 
162  bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks);
163 
169  bool cleanup_handle_incoming_blocks(bool force_sync = false);
170 
178  bool check_incoming_block_size(const blobdata& block_blob) const;
179 
185  i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
186 
187  //-------------------- i_miner_handler -----------------------
188 
200  virtual bool handle_block_found( block& b);
201 
207  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);
208 
212  virtual void on_transaction_relayed(const cryptonote::blobdata& tx);
213 
214 
220  miner& get_miner(){return m_miner;}
221 
227  const miner& get_miner()const{return m_miner;}
228 
237  static void init_options(boost::program_options::options_description& desc);
238 
251  bool init(const boost::program_options::variables_map& vm, const char *config_subdir = NULL, const test_options *test_options = NULL);
252 
258  bool set_genesis_block(const block& b);
259 
267  bool deinit();
268 
272  void test_drop_download();
273 
279  void test_drop_download_height(uint64_t height);
280 
286  bool get_test_drop_download() const;
287 
296  bool get_test_drop_download_height() const;
297 
303  uint64_t get_current_blockchain_height() const;
304 
311  void get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
312 
318  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
319 
325  bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
326 
332  bool get_blocks(uint64_t start_offset, size_t count, std::vector<block>& blocks) const;
333 
339  template<class t_ids_container, class t_blocks_container, class t_missed_container>
340  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
341  {
342  return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
343  }
344 
350  crypto::hash get_block_id_by_height(uint64_t height) const;
351 
357  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs) const;
358 
364  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<transaction>& txs, std::vector<crypto::hash>& missed_txs) const;
365 
371  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
372 
378  bool get_alternative_blocks(std::vector<block>& blocks) const;
379 
385  size_t get_alternative_blocks_count() const;
386 
392  void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
393 
399  void set_checkpoints(checkpoints&& chk_pts);
400 
406  void set_checkpoints_file_path(const std::string& path);
407 
413  void set_enforce_dns_checkpoints(bool enforce_dns);
414 
420  void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
421 
427  bool pool_has_tx(const crypto::hash &txid) const;
428 
435  bool get_pool_transactions(std::vector<transaction>& txs, bool include_unrelayed_txes = true) const;
436 
442  bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
443 
450  bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const;
451 
458  bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const;
459 
465  bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx) const;
466 
473  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;
474 
480  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;
481 
487  size_t get_pool_transactions_count() const;
488 
494  size_t get_blockchain_total_transactions() const;
495 
501  bool have_block(const crypto::hash& id) const;
502 
508  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
509 
515  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
516 
522  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;
523 
531  bool get_stat_info(core_stat_info& st_inf) const;
532 
538  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
539 
545  crypto::hash get_tail_id() const;
546 
552  difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
553 
560 
566  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;
567 
573  void pause_mine();
574 
580  void resume_mine();
581 
587  Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
588 
594  const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
595 
601  std::string print_pool(bool short_format) const;
602 
608  void on_synchronized();
609 
615  void safesyncmode(const bool onoff);
616 
622  void set_target_blockchain_height(uint64_t target_blockchain_height);
623 
629  uint64_t get_target_blockchain_height() const;
630 
636  uint8_t get_ideal_hard_fork_version() const;
637 
643  uint8_t get_ideal_hard_fork_version(uint64_t height) const;
644 
650  uint8_t get_hard_fork_version(uint64_t height) const;
651 
657  uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
658 
663  std::time_t get_start_time() const;
664 
675  bool update_checkpoints();
676 
683  void graceful_exit();
684 
690  void stop();
691 
697  bool is_key_image_spent(const crypto::key_image& key_im) const;
698 
709  bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
710 
719  bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
720 
726  size_t get_block_sync_size(uint64_t height) const;
727 
733  std::pair<uint64_t, uint64_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
734 
740  network_type get_nettype() const { return m_nettype; };
741 
750  bool is_update_available() const { return m_update_available; }
751 
757  bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
758 
764  uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes);
765 
771  uint64_t get_free_space() const;
772 
778  bool offline() const { return m_offline; }
779 
780  private:
781 
792  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);
793 
809  bool add_new_tx(transaction& tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
810 
816  bool add_new_block(const block& b, block_verification_context& bvc);
817 
825  bool load_state_data();
826 
832  bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash, const blobdata& blob) const;
833 
844  bool check_tx_syntax(const transaction& tx) const;
845 
862  bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
863  void set_semantics_failed(const crypto::hash &tx_hash);
864 
865  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);
866  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);
868  bool handle_incoming_tx_accumulated_batch(std::vector<tx_verification_batch_info> &tx_info, bool keeped_by_block);
869 
877  bool update_miner_block_template();
878 
886  bool handle_command_line(const boost::program_options::variables_map& vm);
887 
895  bool check_tx_inputs_keyimages_diff(const transaction& tx) const;
896 
904  bool check_tx_inputs_ring_members_diff(const transaction& tx) const;
905 
914  bool check_tx_inputs_keyimages_domain(const transaction& tx) const;
915 
926  bool check_fork_time();
927 
933  bool relay_txpool_transactions();
934 
940  bool check_updates();
941 
947  bool check_disk_space();
948 
954  bool check_block_rate();
955 
956  bool m_test_drop_download = true;
957 
958  uint64_t m_test_drop_download_height = 0;
959 
962 
964 
965  epee::critical_section m_incoming_tx_lock;
966 
967  //m_miner and m_miner_addres are probably temporary here
970 
971  std::string m_config_folder;
972 
974 
975  epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
976  epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner;
977  epee::math_helper::once_a_time_seconds<60*2, false> m_txpool_auto_relayer;
978  epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval;
979  epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval;
980  epee::math_helper::once_a_time_seconds<90, false> m_block_rate_interval;
981 
982  std::atomic<bool> m_starter_message_showed;
983 
985 
987 
988  std::atomic<bool> m_update_available;
989 
990  std::string m_checkpoints_path;
993 
994  std::atomic_flag m_checkpoints_updating;
996 
998 
999  time_t start_time;
1000 
1001  std::unordered_set<crypto::hash> bad_semantics_txes[2];
1003 
1004  enum {
1009  } check_updates_level;
1010 
1013  boost::mutex m_update_mutex;
1014 
1017 
1018  std::shared_ptr<tools::Notify> m_block_rate_notify;
1019  };
1020 }
1021 
1022 POP_WARNINGS
Definition: cryptonote_protocol_defs.h:162
Blockchain m_blockchain_storage
Blockchain instance.
Definition: cryptonote_core.h:961
std::string m_config_folder
folder to look in for configs and other files
Definition: cryptonote_core.h:971
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:978
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:994
crypto::hash tx_hash
Definition: cryptonote_core.h:867
Definition: cryptonote_protocol_handler_common.h:51
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:223
Definition: cryptonote_core.h:1008
Definition: cryptonote_basic.h:366
time_t m_last_json_checkpoints_update
time when json checkpoints were last updated
Definition: cryptonote_core.h:992
uint64_t height
Definition: blockchain.cpp:89
time_t start_time
Definition: cryptonote_core.h:999
account_public_address m_miner_address
address to mine to (for miner instance)
Definition: cryptonote_core.h:969
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:991
const command_line::arg_descriptor< bool, false > arg_stagenet_on
Definition: cryptonote_core.cpp:75
void disable_dns_checkpoints(bool disable=true)
set whether or not to enable or disable DNS checkpoints
Definition: cryptonote_core.h:420
Definition: connection_context.h:40
network_type get_nettype() const
get the network type we're on
Definition: cryptonote_core.h:740
Definition: cryptonote_core.h:1006
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:1016
std::atomic< bool > m_update_available
Definition: cryptonote_core.h:988
bool m_fluffy_blocks_enabled
Definition: cryptonote_core.h:1015
std::atomic< bool > m_starter_message_showed
has the "daemon will sync now" message been shown?
Definition: cryptonote_core.h:982
network_type m_nettype
which network are we on?
Definition: cryptonote_core.h:986
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:85
epee::math_helper::once_a_time_seconds< 60 *60 *2, true > m_fork_moaner
interval for checking HardFork status
Definition: cryptonote_core.h:976
tx_memory_pool m_mempool
transaction pool instance
Definition: cryptonote_core.h:960
const command_line::arg_descriptor< bool, false > arg_testnet_on
Definition: cryptonote_core.cpp:70
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:975
size_t m_last_update_length
Definition: cryptonote_core.h:1012
bool m_disable_dns_checkpoints
Definition: cryptonote_core.h:995
Definition: cryptonote_core.h:1005
i_cryptonote_protocol * get_protocol()
get the cryptonote protocol instance
Definition: cryptonote_core.h:185
Definition: miner.h:62
Definition: cryptonote_protocol_defs.h:178
handles core cryptonote functionality
Definition: cryptonote_core.h:79
Definition: cryptonote_core.h:1007
bool fluffy_blocks_enabled() const
get whether fluffy blocks are enabled
Definition: cryptonote_core.h:757
int b
Definition: base.py:1
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
Definition: cryptonote_core.cpp:90
bool is_update_available() const
check whether an update is known to be available or not
Definition: cryptonote_core.h:750
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:340
boost::mutex bad_semantics_txes_lock
Definition: cryptonote_core.h:1002
std::string m_checkpoints_path
path to json checkpoints file
Definition: cryptonote_core.h:990
epee::critical_section m_incoming_tx_lock
incoming transaction lock
Definition: cryptonote_core.h:965
boost::mutex m_update_mutex
Definition: cryptonote_core.h:1013
cryptonote_protocol_stub m_protocol_stub
cryptonote protocol stub instance
Definition: cryptonote_core.h:973
Definition: cryptonote_core.h:56
uint64_t m_target_blockchain_height
blockchain height target
Definition: cryptonote_core.h:984
const Blockchain & get_blockchain_storage() const
gets the Blockchain instance (const)
Definition: cryptonote_core.h:594
epee::math_helper::once_a_time_seconds< 90, false > m_block_rate_interval
interval for checking block rate
Definition: cryptonote_core.h:980
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:979
const command_line::arg_descriptor< bool > arg_offline
Definition: cryptonote_core.cpp:103
uint8_t version
Definition: blockchain.cpp:88
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:778
Definition: core_rpc_server_commands_defs.h:1542
Definition: core_rpc_server_commands_defs.h:696
network_type
Definition: cryptonote_config.h:208
miner & get_miner()
gets the miner instance
Definition: cryptonote_core.h:220
Definition: core_rpc_server_commands_defs.h:1384
Definition: cryptonote_basic.h:400
Blockchain & get_blockchain_storage()
gets the Blockchain instance
Definition: cryptonote_core.h:587
std::uint64_t difficulty_type
Definition: difficulty.h:40
size_t block_sync_size
Definition: cryptonote_core.h:997
POD_CLASS key_image
Definition: crypto.h:89
const miner & get_miner() const
gets the miner instance (const)
Definition: cryptonote_core.h:227
Definition: cryptonote_protocol_defs.h:228
miner m_miner
miner instance
Definition: cryptonote_core.h:968
const command_line::arg_descriptor< bool > arg_regtest_on
Definition: cryptonote_core.cpp:80
tools::download_async_handle m_update_download
Definition: cryptonote_core.h:1011
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:963
A container for blockchain checkpoints.
Definition: checkpoints.h:51
std::shared_ptr< download_thread_control > download_async_handle
Definition: download.h:35
std::shared_ptr< tools::Notify > m_block_rate_notify
Definition: cryptonote_core.h:1018
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:977
Definition: cryptonote_basic.h:182
const size_t long_term_block_weight_window
Definition: cryptonote_core.h:58
Definition: miner.h:51
Definition: cryptonote_protocol_handler_common.h:41
Definition: blockchain.h:80
Definition: cryptonote_core.h:867