Debian Codegraph @

src/cryptonote_core/cryptonote_core.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, 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/function.hpp>
36 #include <boost/program_options/options_description.hpp>
37 #include <boost/program_options/variables_map.hpp>
38
39 #include "cryptonote_basic/fwd.h"
40 #include "cryptonote_core/i_core_events.h"
41 #include "cryptonote_protocol/cryptonote_protocol_handler_common.h"
42 #include "cryptonote_protocol/enums.h"
43 #include "common/download.h"
44 #include "common/command_line.h"
45 #include "tx_pool.h"
46 #include "blockchain.h"
47 #include "cryptonote_basic/miner.h"
48 #include "cryptonote_basic/connection_context.h"
49 #include "warnings.h"
50 #include "crypto/hash.h"
51 #include "span.h"
52 #include "rpc/fwd.h"
53
54 PUSH_WARNINGS
55 DISABLE_VS_WARNINGS(4355)
56
57 enum { HAVE_BLOCK_MAIN_CHAIN, HAVE_BLOCK_ALT_CHAIN, HAVE_BLOCK_INVALID };
58
59 namespace cryptonote
60 {
61 struct test_options {
62 const std::pair<uint8_t, uint64_t> *hard_forks;
63 const size_t long_term_block_weight_window;
64 };
65
66 extern const command_line::arg_descriptor<std::string, false, true, 3> arg_data_dir;
67 extern const command_line::arg_descriptor<bool, false> arg_testnet_on;
68 extern const command_line::arg_descriptor<bool, false> arg_stagenet_on;
69 extern const command_line::arg_descriptor<bool, false> arg_regtest_on;
70 extern const command_line::arg_descriptor<difficulty_type> arg_fixed_difficulty;
71 extern const command_line::arg_descriptor<bool> arg_offline;
72 extern const command_line::arg_descriptor<size_t> arg_block_download_max_size;
73 extern const command_line::arg_descriptor<bool> arg_sync_pruned_blocks;
74
75 /************************************************************************/
76 /* */
77 /************************************************************************/
78
79 /**
80 * @brief handles core cryptonote functionality
81 *
82 * This class coordinates cryptonote functionality including, but not
83 * limited to, communication among the Blockchain, the transaction pool,
84 * any miners, and the network.
85 */
86 class core final: public i_miner_handler, public i_core_events
87 {
88 public:
89
90 /**
91 * @brief constructor
92 *
93 * sets member variables into a usable state
94 *
95 * @param pprotocol pre-constructed protocol object to store and use
96 */
97 core(i_cryptonote_protocol* pprotocol);
98
99 /**
100 * @copydoc Blockchain::handle_get_objects
101 *
102 * @note see Blockchain::handle_get_objects()
103 * @param context connection context associated with the request
104 */
105 bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NOTIFY_RESPONSE_GET_OBJECTS::request& rsp, cryptonote_connection_context& context);
106
107 /**
108 * @brief calls various idle routines
109 *
110 * @note see miner::on_idle and tx_memory_pool::on_idle
111 *
112 * @return true
113 */
114 bool on_idle();
115
116 /**
117 * @brief handles an incoming transaction
118 *
119 * Parses an incoming transaction and, if nothing is obviously wrong,
120 * passes it along to the transaction pool
121 *
122 * @param tx_blob the tx to handle
123 * @param tvc metadata about the transaction's validity
124 * @param tx_relay how the transaction was received
125 * @param relayed whether or not the transaction was relayed to us
126 *
127 * @return true if the transaction was accepted, false otherwise
128 */
129 bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, relay_method tx_relay, bool relayed);
130
131 /**
132 * @brief handles a single incoming block
133 *
134 * periodic update to checkpoints is triggered here
135 * Attempts to add the block to the Blockchain and, on success,
136 * optionally updates the miner's block template.
137 *
138 * Unlike handle_incoming_block(), a write transaction is created in this method, which means
139 * the caller doesn't have to call prepare_handle_incoming_blocks() nor
140 * cleanup_handle_incoming_blocks() surrounding this call.
141 *
142 * @param block_blob the block to be added
143 * @param block the block to be added, or NULL
144 * @param bvc return-by-reference metadata context about the block's validity
145 * @param extra_block_txs txs belonging to this block that may not be in the mempool
146 * @param update_miner_blocktemplate whether or not to update the miner's block template
147 *
148 * @return false if loading new checkpoints fails, or the block is not
149 * added, otherwise true
150 */
151 bool handle_single_incoming_block(const blobdata& block_blob,
152 const block *b,
153 block_verification_context& bvc,
154 pool_supplement& extra_block_txs,
155 bool update_miner_blocktemplate = true);
156
157 /**
158 * @brief handles an incoming block as part of a batch
159 *
160 * periodic update to checkpoints is triggered here
161 * Attempts to add the block to the Blockchain and, on success,
162 * optionally updates the miner's block template.
163 *
164 * Prerequisite: There must be an active write transaction for the blockchain storage on this
165 * thread. Typically, this is done by calling prepare_handle_incoming_blocks() on
166 * this thread before calls to handle_incoming_block(). Then, after calls to
167 * handle_incoming_block(), a call to cleanup_handle_incoming_blocks() is made
168 * on this thread to either abort or commit the write transaction.
169 *
170 * @param block_blob the block to be added
171 * @param block the block to be added, or NULL
172 * @param bvc return-by-reference metadata context about the block's validity
173 * @param extra_block_txs txs belonging to this block that may not be in the mempool
174 * @param update_miner_blocktemplate whether or not to update the miner's block template
175 *
176 * @return false if loading new checkpoints fails, or the block is not
177 * added, otherwise true
178 */
179 bool handle_incoming_block(const blobdata& block_blob, const block *b,
180 block_verification_context& bvc,
181 bool update_miner_blocktemplate = true);
182
183 bool handle_incoming_block(const blobdata& block_blob, const block *b,
184 block_verification_context& bvc, pool_supplement& extra_block_txs,
185 bool update_miner_blocktemplate = true);
186
187 /**
188 * @copydoc Blockchain::prepare_handle_incoming_blocks
189 *
190 * @note see Blockchain::prepare_handle_incoming_blocks
191 */
192 bool prepare_handle_incoming_blocks(const std::vector<block_complete_entry> &blocks_entry, std::vector<block> &blocks);
193
194 /**
195 * @copydoc Blockchain::cleanup_handle_incoming_blocks
196 *
197 * @note see Blockchain::cleanup_handle_incoming_blocks
198 */
199 bool cleanup_handle_incoming_blocks(bool force_sync = false);
200
201 /**
202 * @brief check the size of a block against the current maximum
203 *
204 * @param block_blob the block to check
205 *
206 * @return whether or not the block is too big
207 */
208 bool check_incoming_block_size(const blobdata& block_blob) const;
209
210 /**
211 * @brief get the cryptonote protocol instance
212 *
213 * @return the instance
214 */
215 i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
216
217 //-------------------- i_miner_handler -----------------------
218
219 /**
220 * @brief stores and relays a block found by a miner
221 *
222 * Updates the miner's target block, attempts to store the found
223 * block in Blockchain, and -- on success -- relays that block to
224 * the network.
225 *
226 * @param b the block found
227 * @param bvc returns the block verification flags
228 *
229 * @return true if the block was added to the main chain, otherwise false
230 */
231 virtual bool handle_block_found(block& b, block_verification_context &bvc) override;
232
233 /**
234 * @copydoc Blockchain::create_block_template
235 *
236 * @note see Blockchain::create_block_template
237 */
238 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, uint64_t &seed_height, crypto::hash &seed_hash) override;
239 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, uint64_t &seed_height, crypto::hash &seed_hash);
240
241 /**
242 * @copydoc Blockchain::get_miner_data
243 *
244 * @note see Blockchain::get_miner_data
245 */
246 bool get_miner_data(uint8_t& major_version, uint64_t& height, crypto::hash& prev_id, crypto::hash& seed_hash, difficulty_type& difficulty, uint64_t& median_weight, uint64_t& already_generated_coins, std::vector<tx_block_template_backlog_entry>& tx_backlog);
247
248 /**
249 * @brief called when a transaction is relayed.
250 * @note Should only be invoked from `levin_notify`.
251 */
252 virtual void on_transactions_relayed(epee::span<const cryptonote::blobdata> tx_blobs, relay_method tx_relay) final;
253
254
255 /**
256 * @brief gets the miner instance
257 *
258 * @return a reference to the miner instance
259 */
260 miner& get_miner(){return m_miner;}
261
262 /**
263 * @brief gets the miner instance (const)
264 *
265 * @return a const reference to the miner instance
266 */
267 const miner& get_miner()const{return m_miner;}
268
269 /**
270 * @brief adds command line options to the given options set
271 *
272 * As of now, there are no command line options specific to core,
273 * so this function simply returns.
274 *
275 * @param desc return-by-reference the command line options set to add to
276 */
277 static void init_options(boost::program_options::options_description& desc);
278
279 /**
280 * @brief resolves the network type based on command line arguments
281 * @param vm variables map
282 * @return network type corresponding to arg_{testnet,stagenet,regtest}_on, defaulting to MAINNET
283 * @throw std::runtime_error if more than 1 of arg_{testnet,stagenet,regtest}_on is present
284 * @throw boost::bad_any_cast if arg_{testnet,stagenet,regtest}_on weren't added to vm
285 */
286 static network_type get_network_type_from_args(const boost::program_options::variables_map& vm);
287
288 /**
289 * @brief initializes the core as needed
290 *
291 * This function initializes the transaction pool, the Blockchain, and
292 * a miner instance with parameters given on the command line (or defaults)
293 *
294 * @param vm command line parameters
295 * @param test_options configuration options for testing
296 * @param get_checkpoints if set, will be called to get checkpoints data, must return checkpoints data pointer and size or nullptr if there ain't any checkpoints for specific network type
297 * @param allow_dns whether or not to allow DNS requests
298 *
299 * @return false if one of the init steps fails, otherwise true
300 */
301 bool init(const boost::program_options::variables_map& vm, const test_options *test_options = NULL, const GetCheckpointsCallback& get_checkpoints = nullptr, bool allow_dns = true);
302
303 /**
304 * @copydoc Blockchain::reset_and_set_genesis_block
305 *
306 * @note see Blockchain::reset_and_set_genesis_block
307 */
308 bool set_genesis_block(const block& b);
309
310 /**
311 * @brief performs safe shutdown steps for core and core components
312 *
313 * Uninitializes the miner instance, transaction pool, and Blockchain
314 *
315 * @return true
316 */
317 bool deinit();
318
319 /**
320 * @brief sets to drop blocks downloaded (for testing)
321 */
322 void test_drop_download();
323
324 /**
325 * @brief sets to drop blocks downloaded below a certain height
326 *
327 * @param height height below which to drop blocks
328 */
329 void test_drop_download_height(uint64_t height);
330
331 /**
332 * @brief gets whether or not to drop blocks (for testing)
333 *
334 * @return whether or not to drop blocks
335 */
336 bool get_test_drop_download() const;
337
338 /**
339 * @brief gets whether or not to drop blocks
340 *
341 * If the current blockchain height <= our block drop threshold
342 * and test drop blocks is set, return true
343 *
344 * @return see above
345 */
346 bool get_test_drop_download_height() const;
347
348 /**
349 * @copydoc Blockchain::get_current_blockchain_height
350 *
351 * @note see Blockchain::get_current_blockchain_height()
352 */
353 virtual uint64_t get_current_blockchain_height() const final;
354
355 /**
356 * @brief get the hash and height of the most recent block
357 *
358 * @param height return-by-reference height of the block
359 * @param top_id return-by-reference hash of the block
360 */
361 void get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
362
363 /**
364 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&, std::vector<transaction>&) const
365 *
366 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&, std::vector<transaction>&) const
367 */
368 bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
369
370 /**
371 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
372 *
373 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
374 */
375 bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
376
377 /**
378 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
379 *
380 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
381 */
382 bool get_blocks(uint64_t start_offset, size_t count, std::vector<block>& blocks) const;
383
384 /**
385 * @copydoc Blockchain::get_blocks(const t_ids_container&, t_blocks_container&, t_missed_container&) const
386 *
387 * @note see Blockchain::get_blocks(const t_ids_container&, t_blocks_container&, t_missed_container&) const
388 */
389 template<class t_ids_container, class t_blocks_container, class t_missed_container>
390 bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
391 {
392 return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
393 }
394
395 /**
396 * @copydoc Blockchain::get_block_id_by_height
397 *
398 * @note see Blockchain::get_block_id_by_height
399 */
400 crypto::hash get_block_id_by_height(uint64_t height) const;
401
402 /**
403 * @copydoc Blockchain::get_transactions
404 *
405 * @note see Blockchain::get_transactions
406 */
407 bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs, bool pruned = false) const;
408
409 /**
410 * @copydoc Blockchain::get_transactions
411 *
412 * @note see Blockchain::get_transactions
413 */
414 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;
415
416 /**
417 * @copydoc Blockchain::get_transactions
418 *
419 * @note see Blockchain::get_transactions
420 */
421 bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<transaction>& txs, std::vector<crypto::hash>& missed_txs, bool pruned = false) const;
422
423 /**
424 * @copydoc Blockchain::get_block_by_hash
425 *
426 * @note see Blockchain::get_block_by_hash
427 */
428 bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
429
430 /**
431 * @copydoc Blockchain::get_alternative_blocks
432 *
433 * @note see Blockchain::get_alternative_blocks(std::vector<block>&) const
434 */
435 bool get_alternative_blocks(std::vector<block>& blocks) const;
436
437 /**
438 * @copydoc Blockchain::get_alternative_blocks_count
439 *
440 * @note see Blockchain::get_alternative_blocks_count() const
441 */
442 size_t get_alternative_blocks_count() const;
443
444 /**
445 * @brief set the pointer to the cryptonote protocol object to use
446 *
447 * @param pprotocol the pointer to set ours as
448 */
449 void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
450
451 /**
452 * @copydoc Blockchain::get_checkpoints
453 *
454 * @note see Blockchain::get_checkpoints()
455 */
456 const checkpoints& get_checkpoints() const;
457
458 /**
459 * @copydoc Blockchain::set_checkpoints
460 *
461 * @note see Blockchain::set_checkpoints()
462 */
463 void set_checkpoints(checkpoints&& chk_pts);
464
465 /**
466 * @brief set the file path to read from when loading checkpoints
467 *
468 * @param path the path to set ours as
469 */
470 void set_checkpoints_file_path(const std::string& path);
471
472 /**
473 * @brief set whether or not we enforce DNS checkpoints
474 *
475 * @param enforce_dns enforce DNS checkpoints or not
476 */
477 void set_enforce_dns_checkpoints(bool enforce_dns);
478
479 /**
480 * @brief set whether or not to enable or disable DNS checkpoints
481 *
482 * @param disble whether to disable DNS checkpoints
483 */
484 void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
485
486 /**
487 * @copydoc tx_memory_pool::have_tx
488 *
489 * @note see tx_memory_pool::have_tx
490 */
491 bool pool_has_tx(const crypto::hash &txid) const;
492
493 /**
494 * @copydoc tx_memory_pool::get_transactions
495 * @param include_sensitive_txes include private transactions
496 *
497 * @note see tx_memory_pool::get_transactions
498 */
499 bool get_pool_transactions(std::vector<transaction>& txs, bool include_sensitive_txes = false) const;
500
501 /**
502 * @copydoc tx_memory_pool::get_txpool_backlog
503 * @param include_sensitive_txes include private transactions
504 *
505 * @note see tx_memory_pool::get_txpool_backlog
506 */
507 bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog, bool include_sensitive_txes = false) const;
508
509 /**
510 * @copydoc tx_memory_pool::get_transactions
511 * @param include_sensitive_txes include private transactions
512 *
513 * @note see tx_memory_pool::get_transactions
514 */
515 bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive_txes = false) const;
516
517 /**
518 * @copydoc tx_memory_pool::get_pool_transactions_info
519 * @param include_sensitive_txes include private transactions
520 *
521 * @note see tx_memory_pool::get_pool_transactions_info
522 */
523 bool get_pool_transactions_info(const std::vector<crypto::hash>& txids, std::vector<std::pair<crypto::hash, tx_memory_pool::tx_details>>& txs, bool include_sensitive_txes = false) const;
524
525 /**
526 * @copydoc tx_memory_pool::get_pool_info
527 * @param include_sensitive_txes include private transactions
528 * @param max_tx_count max allowed added_txs in response
529 *
530 * @note see tx_memory_pool::get_pool_info
531 */
532 bool get_pool_info(time_t start_time, bool include_sensitive_txes, size_t max_tx_count, std::vector<std::pair<crypto::hash, tx_memory_pool::tx_details>>& added_txs, std::vector<crypto::hash>& remaining_added_txids, std::vector<crypto::hash>& removed_txs, bool& incremental) const;
533
534 /**
535 * @copydoc tx_memory_pool::get_transactions
536 * @param include_sensitive_txes include private transactions
537 *
538 * @note see tx_memory_pool::get_transactions
539 */
540 bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_sensitive_txes = false) const;
541
542 /**
543 * @copydoc tx_memory_pool::get_transaction
544 *
545 * @note see tx_memory_pool::get_transaction
546 */
547 bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx, relay_category tx_category) const;
548
549 /**
550 * @copydoc tx_memory_pool::get_pool_transactions_and_spent_keys_info
551 * @param include_sensitive_txes include private transactions
552 *
553 * @note see tx_memory_pool::get_pool_transactions_and_spent_keys_info
554 */
555 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_sensitive_txes = false) const;
556
557 /**
558 * @copydoc tx_memory_pool::get_pool_for_rpc
559 *
560 * @note see tx_memory_pool::get_pool_for_rpc
561 */
562 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;
563
564 /**
565 * @copydoc tx_memory_pool::get_transactions_count
566 * @param include_sensitive_txes include private transactions
567 *
568 * @note see tx_memory_pool::get_transactions_count
569 */
570 size_t get_pool_transactions_count(bool include_sensitive_txes = false) const;
571
572 /**
573 * @copydoc Blockchain::get_total_transactions
574 *
575 * @note see Blockchain::get_total_transactions
576 */
577 size_t get_blockchain_total_transactions() const;
578
579 /**
580 * @copydoc Blockchain::have_block
581 *
582 * @note see Blockchain::have_block
583 */
584 bool have_block_unlocked(const crypto::hash& id, int *where = NULL) const;
585 bool have_block(const crypto::hash& id, int *where = NULL) const;
586
587 /**
588 * @copydoc Blockchain::get_short_chain_history
589 *
590 * @note see Blockchain::get_short_chain_history
591 */
592 bool get_short_chain_history(std::list<crypto::hash>& ids, uint64_t& current_height) const;
593
594 /**
595 * @copydoc Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const
596 *
597 * @note see Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const
598 */
599 bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
600
601 /**
602 * @copydoc Blockchain::find_blockchain_supplement(const uint64_t, const std::list<crypto::hash>&, std::vector<std::pair<cryptonote::blobdata, std::vector<cryptonote::blobdata> > >&, uint64_t&, uint64_t&, size_t) const
603 *
604 * @note see Blockchain::find_blockchain_supplement(const uint64_t, const std::list<crypto::hash>&, std::vector<std::pair<cryptonote::blobdata, std::vector<transaction> > >&, uint64_t&, uint64_t&, size_t) const
605 */
606 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_block_count, size_t max_tx_count) const;
607
608 /**
609 * @copydoc Blockchain::get_tx_outputs_gindexs
610 *
611 * @note see Blockchain::get_tx_outputs_gindexs
612 */
613 bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
614 bool get_tx_outputs_gindexs(const crypto::hash& tx_id, size_t n_txes, std::vector<std::vector<uint64_t>>& indexs) const;
615
616 /**
617 * @copydoc Blockchain::get_tail_id
618 *
619 * @note see Blockchain::get_tail_id
620 */
621 crypto::hash get_tail_id() const;
622
623 /**
624 * @copydoc Blockchain::get_block_cumulative_difficulty
625 *
626 * @note see Blockchain::get_block_cumulative_difficulty
627 */
628 difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
629
630 /**
631 * @copydoc Blockchain::get_outs
632 *
633 * @note see Blockchain::get_outs
634 */
635 bool get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res) const;
636
637 /**
638 * @copydoc Blockchain::get_output_distribution
639 *
640 * @brief get per block distribution of outputs of a given amount
641 */
642 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;
643
644 /**
645 * @copydoc miner::pause
646 *
647 * @note see miner::pause
648 */
649 void pause_mine();
650
651 /**
652 * @copydoc miner::resume
653 *
654 * @note see miner::resume
655 */
656 void resume_mine();
657
658 /**
659 * @brief gets the Blockchain instance
660 *
661 * @return a reference to the Blockchain instance
662 */
663 Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
664
665 /**
666 * @brief gets the Blockchain instance (const)
667 *
668 * @return a const reference to the Blockchain instance
669 */
670 const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
671
672 /**
673 * @copydoc tx_memory_pool::print_pool
674 *
675 * @note see tx_memory_pool::print_pool
676 */
677 std::string print_pool(bool short_format) const;
678
679 /**
680 * @brief gets the core synchronization status
681 *
682 * @return core synchronization status
683 */
684 virtual bool is_synchronized() const final;
685
686 /**
687 * @copydoc miner::on_synchronized
688 *
689 * @note see miner::on_synchronized
690 */
691 void on_synchronized();
692
693 /**
694 * @copydoc Blockchain::safesyncmode
695 *
696 * 2note see Blockchain::safesyncmode
697 */
698 void safesyncmode(const bool onoff);
699
700 /**
701 * @brief sets the target blockchain height
702 *
703 * @param target_blockchain_height the height to set
704 */
705 void set_target_blockchain_height(uint64_t target_blockchain_height);
706
707 /**
708 * @brief gets the target blockchain height
709 *
710 * @param target_blockchain_height the target height
711 */
712 uint64_t get_target_blockchain_height() const;
713
714 /**
715 * @brief returns the newest hardfork version known to the blockchain
716 *
717 * @return the version
718 */
719 uint8_t get_ideal_hard_fork_version() const;
720
721 /**
722 * @brief return the ideal hard fork version for a given block height
723 *
724 * @return what it says above
725 */
726 uint8_t get_ideal_hard_fork_version(uint64_t height) const;
727
728 /**
729 * @brief return the hard fork version for a given block height
730 *
731 * @return what it says above
732 */
733 uint8_t get_hard_fork_version(uint64_t height) const;
734
735 /**
736 * @brief return the earliest block a given version may activate
737 *
738 * @return what it says above
739 */
740 uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
741
742 /**
743 * @brief gets start_time
744 *
745 */
746 std::time_t get_start_time() const;
747
748 /**
749 * @brief tells the Blockchain to update its checkpoints
750 *
751 * This function will check if enough time has passed since the last
752 * time checkpoints were updated and tell the Blockchain to update
753 * its checkpoints if it is time. If updating checkpoints fails,
754 * the daemon is told to shut down.
755 *
756 * @note see Blockchain::update_checkpoints()
757 */
758 bool update_checkpoints(const bool skip_dns = false);
759
760 /**
761 * @brief tells the daemon to wind down operations and stop running
762 *
763 * Currently this function raises SIGTERM, allowing the installed signal
764 * handlers to do the actual stopping.
765 */
766 void graceful_exit();
767
768 /**
769 * @brief stops the daemon running
770 *
771 * @note see graceful_exit()
772 */
773 void stop();
774
775 /**
776 * @copydoc Blockchain::have_tx_keyimg_as_spent
777 *
778 * @note see Blockchain::have_tx_keyimg_as_spent
779 */
780 bool is_key_image_spent(const crypto::key_image& key_im) const;
781
782 /**
783 * @brief check if multiple key images are spent
784 *
785 * plural version of is_key_image_spent()
786 *
787 * @param key_im list of key images to check
788 * @param spent return-by-reference result for each image checked
789 *
790 * @return true
791 */
792 bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
793
794 /**
795 * @brief check if multiple key images are spent in the transaction pool
796 *
797 * @param key_im list of key images to check
798 * @param spent return-by-reference result for each image checked
799 *
800 * @return true
801 */
802 bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
803
804 /**
805 * @brief get the number of blocks to sync in one go
806 *
807 * @return the number of blocks to sync in one go
808 */
809 size_t get_block_sync_size(uint64_t height) const;
810
811 /**
812 * @brief get the sum of coinbase tx amounts between blocks
813 *
814 * @return the number of blocks to sync in one go
815 */
816 std::pair<boost::multiprecision::uint128_t, boost::multiprecision::uint128_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
817
818 /**
819 * @brief get the network type we're on
820 *
821 * @return which network are we on?
822 */
823 network_type get_nettype() const { return m_nettype; };
824
825 /**
826 * @brief check whether an update is known to be available or not
827 *
828 * This does not actually trigger a check, but returns the result
829 * of the last check
830 *
831 * @return whether an update is known to be available or not
832 */
833 bool is_update_available() const { return m_update_available; }
834
835 /**
836 * @brief check a set of hashes against the precompiled hash set
837 *
838 * @return number of usable blocks
839 */
840 uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights);
841
842 /**
843 * @brief get free disk space on the blockchain partition
844 *
845 * @return free space in bytes
846 */
847 uint64_t get_free_space() const;
848
849 /**
850 * @brief get whether the core is running offline
851 *
852 * @return whether the core is running offline
853 */
854 bool offline() const { return m_offline; }
855
856 /**
857 * @brief get the blockchain pruning seed
858 *
859 * @return the blockchain pruning seed
860 */
861 uint32_t get_blockchain_pruning_seed() const;
862
863 /**
864 * @brief prune the blockchain
865 *
866 * @param pruning_seed the seed to use to prune the chain (0 for default, highly recommended)
867 *
868 * @return true iff success
869 */
870 bool prune_blockchain(uint32_t pruning_seed = 0);
871
872 /**
873 * @brief incrementally prunes blockchain
874 *
875 * @return true on success, false otherwise
876 */
877 bool update_blockchain_pruning();
878
879 /**
880 * @brief checks the blockchain pruning if enabled
881 *
882 * @return true on success, false otherwise
883 */
884 bool check_blockchain_pruning();
885
886 /**
887 * @brief checks whether a given block height is included in the precompiled block hash area
888 *
889 * @param height the height to check for
890 */
891 bool is_within_compiled_block_hash_area(uint64_t height) const;
892
893 /**
894 * @brief checks whether block weights are known for the given range
895 */
896 bool has_block_weights(uint64_t height, uint64_t nblocks) const;
897
898 /**
899 * @brief flushes the invalid block cache
900 */
901 void flush_invalid_blocks();
902
903 /**
904 * @brief returns the set of transactions in the txpool which are not in the argument
905 *
906 * @param hashes hashes of transactions to exclude from the result
907 *
908 * @return true iff success, false otherwise
909 */
910 bool get_txpool_complement(std::vector<crypto::hash> hashes, std::vector<cryptonote::blobdata> &txes);
911
912 /**
913 * @brief validates some simple properties of a transaction
914 *
915 * Currently checks: tx has inputs,
916 * tx inputs all of supported type(s),
917 * tx outputs valid (type, key, amount),
918 * input and output total amounts don't overflow,
919 * output amount <= input amount,
920 * tx not too large,
921 * each input has a different key image.
922 *
923 * @param tx the transaction to check
924 * @param tvc tx verification context where extra fail flags are stored
925 * @param hf_version hard fork version
926 *
927 * @return true if all the checks pass, otherwise false
928 */
929 static bool check_tx_semantic(const transaction& tx, tx_verification_context& tvc,
930 uint8_t hf_version);
931
932 /**
933 * @brief verify that each input key image in a transaction is unique
934 *
935 * @param tx the transaction to check
936 *
937 * @return false if any key image is repeated, otherwise true
938 */
939 static bool check_tx_inputs_keyimages_diff(const transaction& tx);
940
941 /**
942 * @brief verify that each ring uses distinct members
943 *
944 * @param tx the transaction to check
945 * @param hf_version the hard fork version rules to use
946 *
947 * @return false if any ring uses duplicate members, true otherwise
948 */
949 static bool check_tx_inputs_ring_members_diff(const transaction& tx, const uint8_t hf_version);
950
951 /**
952 * @brief verify that each input key image in a transaction is in
953 * the valid domain
954 *
955 * @param tx the transaction to check
956 *
957 * @return false if any key image is not in the valid domain, otherwise true
958 */
959 static bool check_tx_inputs_keyimages_domain(const transaction& tx);
960
961 private:
962
963 /**
964 * @copydoc add_new_tx(transaction&, tx_verification_context&, bool)
965 *
966 * @param tx_hash the transaction's hash
967 * @param blob the transaction as a blob
968 * @param tx_weight the weight of the transaction
969 * @param tx_relay how the transaction was received
970 * @param relayed whether or not the transaction was relayed to us
971 *
972 */
973 bool add_new_tx(transaction& tx, const crypto::hash& tx_hash, const cryptonote::blobdata &blob, size_t tx_weight, tx_verification_context& tvc, relay_method tx_relay, bool relayed);
974
975 /**
976 * @brief add a new transaction to the transaction pool
977 *
978 * Adds a new transaction to the transaction pool.
979 *
980 * @param tx the transaction to add
981 * @param tvc return-by-reference metadata about the transaction's verification process
982 * @param tx_relay how the transaction was received
983 * @param relayed whether or not the transaction was relayed to us
984 *
985 * @return true if the transaction is already in the transaction pool,
986 * is already in a block on the Blockchain, or is successfully added
987 * to the transaction pool
988 */
989 bool add_new_tx(transaction& tx, tx_verification_context& tvc, relay_method tx_relay, bool relayed);
990
991 /**
992 * @copydoc Blockchain::add_new_block
993 *
994 * @note see Blockchain::add_new_block
995 */
996 bool add_new_block(const block& b, block_verification_context& bvc,
997 pool_supplement& extra_block_txs);
998
999 /**
1000 * @brief load any core state stored on disk
1001 *
1002 * currently does nothing, but may have state to load in the future.
1003 *
1004 * @return true
1005 */
1006 bool load_state_data();
1007
1008 /**
1009 * @copydoc miner::on_block_chain_update
1010 *
1011 * @note see miner::on_block_chain_update
1012 *
1013 * @return true
1014 */
1015 bool update_miner_block_template();
1016
1017 /**
1018 * @brief act on a set of command line options given
1019 *
1020 * @param vm the command line options
1021 *
1022 * @return true
1023 */
1024 bool handle_command_line(const boost::program_options::variables_map& vm);
1025
1026 /**
1027 * @brief attempts to relay any transactions in the mempool which need it
1028 *
1029 * @return true
1030 */
1031 bool relay_txpool_transactions();
1032
1033 /**
1034 * @brief sends notification of txpool events to subscribers
1035 *
1036 * @return true on success, false otherwise
1037 */
1038 bool notify_txpool_event(const epee::span<const cryptonote::blobdata> tx_blobs, epee::span<const crypto::hash> tx_hashes, epee::span<const cryptonote::transaction> txs, const std::vector<bool> &just_broadcasted) const;
1039
1040 /**
1041 * @brief checks DNS versions
1042 *
1043 * @return true on success, false otherwise
1044 */
1045 bool check_updates();
1046
1047 /**
1048 * @brief checks free disk space
1049 *
1050 * @return true on success, false otherwise
1051 */
1052 bool check_disk_space();
1053
1054 /**
1055 * @brief checks block rate, and warns if it's too slow
1056 *
1057 * @return true on success, false otherwise
1058 */
1059 bool check_block_rate();
1060
1061 /**
1062 * @brief recalculate difficulties after the last difficulty checklpoint to circumvent the annoying 'difficulty drift' bug
1063 *
1064 * @return true
1065 */
1066 bool recalculate_difficulties();
1067
1068 bool m_test_drop_download = true; //!< whether or not to drop incoming blocks (for testing)
1069
1070 uint64_t m_test_drop_download_height = 0; //!< height under which to drop incoming blocks, if doing so
1071
1072 tx_memory_pool m_mempool; //!< transaction pool instance
1073 Blockchain m_blockchain_storage; //!< Blockchain instance
1074
1075 i_cryptonote_protocol* m_pprotocol; //!< cryptonote protocol instance
1076
1077 epee::critical_section m_incoming_tx_lock; //!< incoming transaction lock
1078
1079 //m_miner and m_miner_addres are probably temporary here
1080 miner m_miner; //!< miner instance
1081
1082 std::string m_config_folder; //!< folder to look in for configs and other files
1083
1084 cryptonote_protocol_stub m_protocol_stub; //!< cryptonote protocol stub instance
1085
1086 epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval; //!< interval for manual storing of Blockchain, if enabled
1087 epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner; //!< interval for checking HardFork status
1088 epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval; //!< interval for checking for new versions
1089 epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval; //!< interval for checking for disk space
1090 epee::math_helper::once_a_time_seconds<90, false> m_block_rate_interval; //!< interval for checking block rate
1091 epee::math_helper::once_a_time_seconds<60*60*5, true> m_blockchain_pruning_interval; //!< interval for incremental blockchain pruning
1092 epee::math_helper::once_a_time_seconds<60*60*24*7, false> m_diff_recalc_interval; //!< interval for recalculating difficulties
1093
1094 std::atomic<bool> m_starter_message_showed; //!< has the "daemon will sync now" message been shown?
1095
1096 uint64_t m_target_blockchain_height; //!< blockchain height target
1097
1098 network_type m_nettype; //!< which network are we on?
1099
1100 std::atomic<bool> m_update_available;
1101
1102 std::string m_checkpoints_path; //!< path to json checkpoints file
1103 time_t m_last_dns_checkpoints_update; //!< time when dns checkpoints were last updated
1104 time_t m_last_json_checkpoints_update; //!< time when json checkpoints were last updated
1105
1106 std::atomic_flag m_checkpoints_updating; //!< set if checkpoints are currently updating to avoid multiple threads attempting to update at once
1107 bool m_disable_dns_checkpoints;
1108
1109 size_t block_sync_size;
1110
1111 time_t start_time;
1112
1113 enum {
1114 UPDATES_DISABLED,
1115 UPDATES_NOTIFY,
1116 UPDATES_DOWNLOAD,
1117 UPDATES_UPDATE,
1118 } check_updates_level;
1119
1120 tools::download_async_handle m_update_download;
1121 size_t m_last_update_length;
1122 boost::mutex m_update_mutex;
1123
1124 bool m_offline;
1125
1126 std::shared_ptr<tools::Notify> m_block_rate_notify;
1127 };
1128 }
1129
1130 POP_WARNINGS