Electroneum
cryptonote_core.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, 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"
53 
56 
57 namespace cryptonote
58 {
59  struct test_options {
60  const std::pair<uint8_t, uint64_t> *hard_forks;
61  };
62 
63  /************************************************************************/
64  /* */
65  /************************************************************************/
66 
74  class core: public i_miner_handler
75  {
76  public:
77 
85  core(i_cryptonote_protocol* pprotocol);
86 
94 
102  bool on_idle();
103 
118  bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
119 
134  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);
135 
150  bool handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate = true);
151 
157  bool prepare_handle_incoming_blocks(const std::list<block_complete_entry> &blocks);
158 
164  bool cleanup_handle_incoming_blocks(bool force_sync = false);
165 
173  bool check_incoming_block_size(const blobdata& block_blob) const;
174 
180  i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
181 
182  //-------------------- i_miner_handler -----------------------
183 
195  virtual bool handle_block_found( block& b);
196 
202  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);
203 
207  virtual void on_transaction_relayed(const cryptonote::blobdata& tx);
208 
209 
215  miner& get_miner(){return m_miner;}
216 
222  const miner& get_miner()const{return m_miner;}
223 
232  static void init_options(boost::program_options::options_description& desc);
233 
245  bool init(const boost::program_options::variables_map& vm, const test_options *test_options = NULL);
246 
252  bool set_genesis_block(const block& b);
253 
261  bool deinit();
262 
266  void test_drop_download();
267 
273  void test_drop_download_height(uint64_t height);
274 
280  bool get_test_drop_download() const;
281 
290  bool get_test_drop_download_height() const;
291 
297  uint64_t get_current_blockchain_height() const;
298 
307  bool get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
308 
314  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks, std::list<cryptonote::blobdata>& txs) const;
315 
321  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks) const;
322 
328  bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks) const;
329 
335  template<class t_ids_container, class t_blocks_container, class t_missed_container>
336  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
337  {
338  return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
339  }
340 
346  crypto::hash get_block_id_by_height(uint64_t height) const;
347 
353  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<cryptonote::blobdata>& txs, std::list<crypto::hash>& missed_txs) const;
354 
360  bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<transaction>& txs, std::list<crypto::hash>& missed_txs) const;
361 
367  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
368 
374  bool get_alternative_blocks(std::list<block>& blocks) const;
375 
381  size_t get_alternative_blocks_count() const;
382 
388  void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
389 
395  void set_checkpoints(checkpoints&& chk_pts);
396 
402  void set_checkpoints_file_path(const std::string& path);
403 
409  void set_enforce_dns_checkpoints(bool enforce_dns);
410 
416  void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
417 
423  bool pool_has_tx(const crypto::hash &txid) const;
424 
430  bool get_pool_transactions(std::list<transaction>& txs) const;
431 
437  bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
438 
444  bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs) const;
445 
451  bool get_pool_transaction_stats(struct txpool_stats& stats) const;
452 
458  bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx) const;
459 
465  bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const;
466 
472  size_t get_pool_transactions_count() const;
473 
479  size_t get_blockchain_total_transactions() const;
480 
486  bool have_block(const crypto::hash& id) const;
487 
493  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
494 
500  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
501 
507  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;
508 
516  bool get_stat_info(core_stat_info& st_inf) const;
517 
523  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
524 
530  crypto::hash get_tail_id() const;
531 
537  void set_block_cumulative_difficulty(uint64_t height, difficulty_type diff);
538 
544  difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
545 
552 
559 
566  bool get_random_rct_outs(const COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::request& req, COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::response& res) const;
567 
568 
574  void pause_mine();
575 
581  void resume_mine();
582 
588  Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
589 
595  const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
596 
602  void print_blockchain(uint64_t start_index, uint64_t end_index) const;
603 
609  void print_blockchain_index() const;
610 
616  std::string print_pool(bool short_format) const;
617 
623  void print_blockchain_outs(const std::string& file);
624 
630  void on_synchronized();
631 
637  void safesyncmode(const bool onoff);
638 
644  void set_target_blockchain_height(uint64_t target_blockchain_height);
645 
651  uint64_t get_target_blockchain_height() const;
652 
658  uint8_t get_ideal_hard_fork_version(uint64_t height) const;
659 
665  uint8_t get_hard_fork_version(uint64_t height) const;
666 
671  std::time_t get_start_time() const;
672 
683  bool update_checkpoints();
684 
690  std::string get_validators_list();
691 
698  electroneum::basic::list_update_outcome set_validators_list(std::string v_list, bool isEmergencyUpdate);
699 
705  bool isValidatorsListValid();
706 
713  void graceful_exit();
714 
720  void stop();
721 
727  bool is_key_image_spent(const crypto::key_image& key_im) const;
728 
739  bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
740 
746  size_t get_block_sync_size(uint64_t height) const;
747 
753  std::pair<uint64_t, uint64_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
754 
760  bool get_testnet() const { return m_testnet; };
761 
767  bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
768 
776  bool set_validator_key(std::string key);
777 
778  std::vector<std::string> generate_ed25519_keypair();
779 
780  std::string sign_message(std::string sk, std::string msg);
781 
782  private:
783 
794  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);
795 
811  bool add_new_tx(transaction& tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
812 
818  bool add_new_block(const block& b, block_verification_context& bvc);
819 
827  bool load_state_data();
828 
834  bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash, const blobdata& blob) const;
835 
846  bool check_tx_syntax(const transaction& tx) const;
847 
864  bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
865 
866  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);
867  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 
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 
941  bool m_test_drop_download = true;
942 
943  uint64_t m_test_drop_download_height = 0;
944 
947 
949 
950  epee::critical_section m_incoming_tx_lock;
951 
952  //m_miner and m_miner_addres are probably temporary here
955 
956  std::string m_config_folder;
957 
959 
960  epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
961  epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner;
962  epee::math_helper::once_a_time_seconds<60*2, false> m_txpool_auto_relayer;
963  epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval;
964  epee::math_helper::once_a_time_seconds<60*2, true> m_check_validators_interval;
965 
966  std::atomic<bool> m_starter_message_showed;
967 
969 
970  bool m_testnet;
971 
972  bool m_fakechain;
973 
974  std::string m_checkpoints_path;
977 
978  std::atomic_flag m_checkpoints_updating;
980 
982 
983  time_t start_time;
984 
985  std::unordered_set<crypto::hash> bad_semantics_txes[2];
987 
989 
990  std::unique_ptr<electroneum::basic::Validators> m_validators;
991 
992  enum {
997  } check_updates_level;
998 
1001  boost::mutex m_update_mutex;
1002 
1004  };
1005 }
1006 
1007 POP_WARNINGS
epee::math_helper::once_a_time_seconds< 60 *2, true > m_check_validators_interval
Definition: cryptonote_core.h:964
Definition: cryptonote_protocol_defs.h:165
Blockchain m_blockchain_storage
Blockchain instance.
Definition: cryptonote_core.h:946
std::string m_config_folder
folder to look in for configs and other files
Definition: cryptonote_core.h:956
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:963
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:978
Definition: cryptonote_protocol_handler_common.h:55
Definition: cryptonote_basic.h:382
time_t m_last_json_checkpoints_update
time when json checkpoints were last updated
Definition: cryptonote_core.h:976
uint64_t height
Definition: blockchain.cpp:87
time_t start_time
Definition: cryptonote_core.h:983
account_public_address m_miner_address
address to mine to (for miner instance)
Definition: cryptonote_core.h:954
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:975
void disable_dns_checkpoints(bool disable=true)
set whether or not to enable or disable DNS checkpoints
Definition: cryptonote_core.h:416
Definition: connection_context.h:41
Definition: verification_context.h:54
Definition: cryptonote_stat_info.h:38
bool get_testnet() const
get whether we&#39;re on testnet or not
Definition: cryptonote_core.h:760
bool m_fluffy_blocks_enabled
Definition: cryptonote_core.h:1003
std::atomic< bool > m_starter_message_showed
has the "daemon will sync now" message been shown?
Definition: cryptonote_core.h:966
Definition: cryptonote_core.h:994
tools::thread_group m_threadpool
Definition: cryptonote_core.h:988
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:961
tx_memory_pool m_mempool
transaction pool instance
Definition: cryptonote_core.h:945
Definition: cryptonote_core.h:995
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:960
Manages zero or more threads for work dispatching.
Definition: thread_group.h:44
size_t m_last_update_length
Definition: cryptonote_core.h:1000
bool m_disable_dns_checkpoints
Definition: cryptonote_core.h:979
i_cryptonote_protocol * get_protocol()
get the cryptonote protocol instance
Definition: cryptonote_core.h:180
Definition: miner.h:63
Definition: cryptonote_core.h:996
Definition: cryptonote_protocol_defs.h:181
handles core cryptonote functionality
Definition: cryptonote_core.h:74
bool fluffy_blocks_enabled() const
get whether fluffy blocks are enabled
Definition: cryptonote_core.h:767
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:336
boost::mutex bad_semantics_txes_lock
Definition: cryptonote_core.h:986
std::string m_checkpoints_path
path to json checkpoints file
Definition: cryptonote_core.h:974
epee::critical_section m_incoming_tx_lock
incoming transaction lock
Definition: cryptonote_core.h:950
bool m_testnet
are we on testnet?
Definition: cryptonote_core.h:970
boost::mutex m_update_mutex
Definition: cryptonote_core.h:1001
std::unique_ptr< electroneum::basic::Validators > m_validators
Definition: cryptonote_core.h:990
cryptonote_protocol_stub m_protocol_stub
cryptonote protocol stub instance
Definition: cryptonote_core.h:958
Definition: cryptonote_core.h:59
uint64_t m_target_blockchain_height
blockchain height target
Definition: cryptonote_core.h:968
const Blockchain & get_blockchain_storage() const
gets the Blockchain instance (const)
Definition: cryptonote_core.h:595
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:1123
Definition: core_rpc_server_commands_defs.h:363
miner & get_miner()
gets the miner instance
Definition: cryptonote_core.h:215
Definition: cryptonote_core.h:993
Definition: cryptonote_basic.h:421
Blockchain & get_blockchain_storage()
gets the Blockchain instance
Definition: cryptonote_core.h:588
internal::NamedArgWithType< char, T > arg(StringRef name, const T &arg)
Definition: format.h:3450
std::uint64_t difficulty_type
Definition: difficulty.h:41
size_t block_sync_size
Definition: cryptonote_core.h:981
POD_CLASS key_image
Definition: crypto.h:93
const miner & get_miner() const
gets the miner instance (const)
Definition: cryptonote_core.h:222
Definition: core_rpc_server_commands_defs.h:461
Definition: cryptonote_protocol_defs.h:231
list_update_outcome
Definition: validators.h:61
miner m_miner
miner instance
Definition: cryptonote_core.h:953
tools::download_async_handle m_update_download
Definition: cryptonote_core.h:999
POD_CLASS hash
Definition: hash.h:46
i_cryptonote_protocol * m_pprotocol
cryptonote protocol instance
Definition: cryptonote_core.h:948
A container for blockchain checkpoints.
Definition: checkpoints.h:52
std::string sign_message(const std::string &message, const std::string &privateKey)
Definition: crypto.h:275
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:972
const std::pair< uint8_t, uint64_t > * hard_forks
Definition: cryptonote_core.h:60
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:962
Definition: cryptonote_basic.h:198
Definition: miner.h:52
Definition: cryptonote_protocol_handler_common.h:42
Definition: core_rpc_server_commands_defs.h:443
Definition: blockchain.h:80
json::wvalue context
Definition: mustache.h:12