Electroneum
cryptonote_core.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, 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 #include <boost/interprocess/sync/file_lock.hpp>
39 
40 #include "p2p/net_node_common.h"
42 #include "storages/portable_storage_template_helper.h"
43 #include "common/download.h"
44 #include "common/thread_group.h"
45 #include "tx_pool.h"
46 #include "blockchain.h"
47 #include "cryptonote_basic/miner.h"
50 #include "warnings.h"
51 #include "crypto/hash.h"
52 
55 
56 namespace cryptonote
57 {
58  struct test_options {
59  const std::pair<uint8_t, uint64_t> *hard_forks;
60  };
61 
62  /************************************************************************/
63  /* */
64  /************************************************************************/
65 
73  class core: public i_miner_handler
74  {
75  public:
76 
84  core(i_cryptonote_protocol* pprotocol);
85 
93 
101  bool on_idle();
102 
117  bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
118 
133  bool handle_incoming_txs(const std::list<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
134 
149  bool handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate = true);
150 
156  bool prepare_handle_incoming_blocks(const std::list<block_complete_entry> &blocks);
157 
163  bool cleanup_handle_incoming_blocks(bool force_sync = false);
164 
172  bool check_incoming_block_size(const blobdata& block_blob) const;
173 
179  i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
180 
181  //-------------------- i_miner_handler -----------------------
182 
194  virtual bool handle_block_found( block& b);
195 
201  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);
202 
206  virtual void on_transaction_relayed(const cryptonote::blobdata& tx);
207 
208 
214  miner& get_miner(){return m_miner;}
215 
221  const miner& get_miner()const{return m_miner;}
222 
231  static void init_options(boost::program_options::options_description& desc);
232 
244  bool init(const boost::program_options::variables_map& vm, const test_options *test_options = NULL);
245 
251  bool set_genesis_block(const block& b);
252 
260  bool deinit();
261 
265  void test_drop_download();
266 
272  void test_drop_download_height(uint64_t height);
273 
279  bool get_test_drop_download() const;
280 
289  bool get_test_drop_download_height() const;
290 
296  uint64_t get_current_blockchain_height() const;
297 
306  bool get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
307 
313  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks, std::list<cryptonote::blobdata>& txs) const;
314 
320  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks) const;
321 
327  bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks) const;
328 
334  template<class t_ids_container, class t_blocks_container, class t_missed_container>
335  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
336  {
337  return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
338  }
339 
345  crypto::hash get_block_id_by_height(uint64_t height) const;
346 
352  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<cryptonote::blobdata>& txs, std::list<crypto::hash>& missed_txs) const;
353 
359  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<transaction>& txs, std::list<crypto::hash>& missed_txs) const;
360 
366  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
367 
373  bool get_alternative_blocks(std::list<block>& blocks) const;
374 
380  size_t get_alternative_blocks_count() const;
381 
387  void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
388 
394  void set_checkpoints(checkpoints&& chk_pts);
395 
401  void set_checkpoints_file_path(const std::string& path);
402 
408  void set_enforce_dns_checkpoints(bool enforce_dns);
409 
415  void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
416 
422  bool pool_has_tx(const crypto::hash &txid) const;
423 
429  bool get_pool_transactions(std::list<transaction>& txs) const;
430 
436  bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
437 
443  bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs) const;
444 
450  bool get_pool_transaction_stats(struct txpool_stats& stats) const;
451 
457  bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx) const;
458 
464  bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const;
465 
471  size_t get_pool_transactions_count() const;
472 
478  size_t get_blockchain_total_transactions() const;
479 
485  bool have_block(const crypto::hash& id) const;
486 
492  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
493 
499  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
500 
506  bool find_blockchain_supplement(const uint64_t req_start_block, const std::list<crypto::hash>& qblock_ids, std::list<std::pair<cryptonote::blobdata, std::list<cryptonote::blobdata> > >& blocks, uint64_t& total_height, uint64_t& start_height, size_t max_count) const;
507 
515  bool get_stat_info(core_stat_info& st_inf) const;
516 
522  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
523 
529  crypto::hash get_tail_id() const;
530 
536  difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
537 
544 
551 
558  bool get_random_rct_outs(const COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::request& req, COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::response& res) const;
559 
560 
566  void pause_mine();
567 
573  void resume_mine();
574 
580  Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
581 
587  const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
588 
594  void print_blockchain(uint64_t start_index, uint64_t end_index) const;
595 
601  void print_blockchain_index() const;
602 
608  std::string print_pool(bool short_format) const;
609 
615  void print_blockchain_outs(const std::string& file);
616 
622  void on_synchronized();
623 
629  void safesyncmode(const bool onoff);
630 
636  void set_target_blockchain_height(uint64_t target_blockchain_height);
637 
643  uint64_t get_target_blockchain_height() const;
644 
650  uint8_t get_ideal_hard_fork_version(uint64_t height) const;
651 
657  uint8_t get_hard_fork_version(uint64_t height) 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 
716  size_t get_block_sync_size(uint64_t height) const;
717 
723  std::pair<uint64_t, uint64_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
724 
730  bool get_testnet() const { return m_testnet; };
731 
737  bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
738 
739  private:
740 
751  bool add_new_tx(transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
752 
768  bool add_new_tx(transaction& tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
769 
775  bool add_new_block(const block& b, block_verification_context& bvc);
776 
784  bool load_state_data();
785 
791  bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash, const blobdata& blob) const;
792 
803  bool check_tx_syntax(const transaction& tx) const;
804 
821  bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
822 
823  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);
824  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);
825 
833  bool update_miner_block_template();
834 
842  bool handle_command_line(const boost::program_options::variables_map& vm);
843 
851  bool check_tx_inputs_keyimages_diff(const transaction& tx) const;
852 
860  bool check_tx_inputs_ring_members_diff(const transaction& tx) const;
861 
870  bool check_tx_inputs_keyimages_domain(const transaction& tx) const;
871 
882  bool check_fork_time();
883 
889  bool relay_txpool_transactions();
890 
896  bool check_updates();
897 
898  bool m_test_drop_download = true;
899 
900  uint64_t m_test_drop_download_height = 0;
901 
904 
906 
907  epee::critical_section m_incoming_tx_lock;
908 
909  //m_miner and m_miner_addres are probably temporary here
912 
913  std::string m_config_folder;
914 
916 
917  epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
918  epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner;
919  epee::math_helper::once_a_time_seconds<60*2, false> m_txpool_auto_relayer;
920  epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval;
921 
922  std::atomic<bool> m_starter_message_showed;
923 
925 
926  bool m_testnet;
927 
928  bool m_fakechain;
929 
930  std::string m_checkpoints_path;
933 
934  std::atomic_flag m_checkpoints_updating;
936 
938 
939  time_t start_time;
940 
941  std::unordered_set<crypto::hash> bad_semantics_txes[2];
943 
945 
946  enum {
951  } check_updates_level;
952 
955  boost::mutex m_update_mutex;
956 
958  };
959 }
960 
961 POP_WARNINGS
Definition: cryptonote_protocol_defs.h:165
Blockchain m_blockchain_storage
Blockchain instance.
Definition: cryptonote_core.h:903
std::string m_config_folder
folder to look in for configs and other files
Definition: cryptonote_core.h:913
PUSH_WARNINGS
Definition: hash-ops.h:54
Definition: core_rpc_server_commands_defs.h:339
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:920
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:57
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:934
Definition: cryptonote_protocol_handler_common.h:52
Definition: cryptonote_basic.h:372
time_t m_last_json_checkpoints_update
time when json checkpoints were last updated
Definition: cryptonote_core.h:932
uint64_t height
Definition: blockchain.cpp:87
time_t start_time
Definition: cryptonote_core.h:939
account_public_address m_miner_address
address to mine to (for miner instance)
Definition: cryptonote_core.h:911
Definition: core_rpc_server_commands_defs.h:389
time_t m_last_dns_checkpoints_update
time when dns checkpoints were last updated
Definition: cryptonote_core.h:931
void disable_dns_checkpoints(bool disable=true)
set whether or not to enable or disable DNS checkpoints
Definition: cryptonote_core.h:415
Definition: connection_context.h:41
Definition: verification_context.h:54
Definition: cryptonote_stat_info.h:38
bool get_testnet() const
get whether we're on testnet or not
Definition: cryptonote_core.h:730
bool m_fluffy_blocks_enabled
Definition: cryptonote_core.h:957
std::atomic< bool > m_starter_message_showed
has the "daemon will sync now" message been shown?
Definition: cryptonote_core.h:922
Definition: cryptonote_core.h:948
tools::thread_group m_threadpool
Definition: cryptonote_core.h:944
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
Definition: verification_context.h:38
Definition: core_rpc_server_commands_defs.h:310
epee::math_helper::once_a_time_seconds< 60 *60 *2, true > m_fork_moaner
interval for checking HardFork status
Definition: cryptonote_core.h:918
tx_memory_pool m_mempool
transaction pool instance
Definition: cryptonote_core.h:902
Definition: cryptonote_core.h:949
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:917
Manages zero or more threads for work dispatching.
Definition: thread_group.h:44
size_t m_last_update_length
Definition: cryptonote_core.h:954
bool m_disable_dns_checkpoints
Definition: cryptonote_core.h:935
i_cryptonote_protocol * get_protocol()
get the cryptonote protocol instance
Definition: cryptonote_core.h:179
Definition: miner.h:63
Definition: cryptonote_core.h:950
Definition: cryptonote_protocol_defs.h:181
handles core cryptonote functionality
Definition: cryptonote_core.h:73
bool fluffy_blocks_enabled() const
get whether fluffy blocks are enabled
Definition: cryptonote_core.h:737
DISABLE_VS_WARNINGS(4244 4345 4503) namespace cryptonote
Definition: tx_pool.cpp:53
int b
Definition: base.py:1
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:335
boost::mutex bad_semantics_txes_lock
Definition: cryptonote_core.h:942
std::string m_checkpoints_path
path to json checkpoints file
Definition: cryptonote_core.h:930
epee::critical_section m_incoming_tx_lock
incoming transaction lock
Definition: cryptonote_core.h:907
bool m_testnet
are we on testnet?
Definition: cryptonote_core.h:926
boost::mutex m_update_mutex
Definition: cryptonote_core.h:955
cryptonote_protocol_stub m_protocol_stub
cryptonote protocol stub instance
Definition: cryptonote_core.h:915
Definition: cryptonote_core.h:58
uint64_t m_target_blockchain_height
blockchain height target
Definition: cryptonote_core.h:924
const Blockchain & get_blockchain_storage() const
gets the Blockchain instance (const)
Definition: cryptonote_core.h:587
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:93
std::string blobdata
Definition: blobdatatype.h:36
#define blocks
Definition: sha512-hash.c:11
Definition: core_rpc_server_commands_defs.h:1113
Definition: core_rpc_server_commands_defs.h:363
miner & get_miner()
gets the miner instance
Definition: cryptonote_core.h:214
Definition: cryptonote_core.h:947
Definition: cryptonote_basic.h:406
Blockchain & get_blockchain_storage()
gets the Blockchain instance
Definition: cryptonote_core.h:580
std::uint64_t difficulty_type
Definition: difficulty.h:41
size_t block_sync_size
Definition: cryptonote_core.h:937
POD_CLASS key_image
Definition: crypto.h:86
const miner & get_miner() const
gets the miner instance (const)
Definition: cryptonote_core.h:221
Definition: core_rpc_server_commands_defs.h:461
Definition: cryptonote_protocol_defs.h:231
miner m_miner
miner instance
Definition: cryptonote_core.h:910
tools::download_async_handle m_update_download
Definition: cryptonote_core.h:953
POD_CLASS hash
Definition: hash.h:46
i_cryptonote_protocol * m_pprotocol
cryptonote protocol instance
Definition: cryptonote_core.h:905
A container for blockchain checkpoints.
Definition: checkpoints.h:52
std::shared_ptr< download_thread_control > download_async_handle
Definition: download.h:36
bool m_fakechain
are we using a fake chain (for testing purposes)?
Definition: cryptonote_core.h:928
const std::pair< uint8_t, uint64_t > * hard_forks
Definition: cryptonote_core.h:59
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:919
Definition: cryptonote_basic.h:188
Definition: miner.h:52
Definition: cryptonote_protocol_handler_common.h:42
Definition: core_rpc_server_commands_defs.h:443
Definition: blockchain.h:78