Debian Codegraph @

monero/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, 2> 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 initializes the core as needed
281 *
282 * This function initializes the transaction pool, the Blockchain, and
283 * a miner instance with parameters given on the command line (or defaults)
284 *
285 * @param vm command line parameters
286 * @param test_options configuration options for testing
287 * @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
288 * @param allow_dns whether or not to allow DNS requests
289 *
290 * @return false if one of the init steps fails, otherwise true
291 */
292 bool init(const boost::program_options::variables_map& vm, const test_options *test_options = NULL, const GetCheckpointsCallback& get_checkpoints = nullptr, bool allow_dns = true);
293
294 /**
295 * @copydoc Blockchain::reset_and_set_genesis_block
296 *
297 * @note see Blockchain::reset_and_set_genesis_block
298 */
299 bool set_genesis_block(const block& b);
300
301 /**
302 * @brief performs safe shutdown steps for core and core components
303 *
304 * Uninitializes the miner instance, transaction pool, and Blockchain
305 *
306 * @return true
307 */
308 bool deinit();
309
310 /**
311 * @brief sets to drop blocks downloaded (for testing)
312 */
313 void test_drop_download();
314
315 /**
316 * @brief sets to drop blocks downloaded below a certain height
317 *
318 * @param height height below which to drop blocks
319 */
320 void test_drop_download_height(uint64_t height);
321
322 /**
323 * @brief gets whether or not to drop blocks (for testing)
324 *
325 * @return whether or not to drop blocks
326 */
327 bool get_test_drop_download() const;
328
329 /**
330 * @brief gets whether or not to drop blocks
331 *
332 * If the current blockchain height <= our block drop threshold
333 * and test drop blocks is set, return true
334 *
335 * @return see above
336 */
337 bool get_test_drop_download_height() const;
338
339 /**
340 * @copydoc Blockchain::get_current_blockchain_height
341 *
342 * @note see Blockchain::get_current_blockchain_height()
343 */
344 virtual uint64_t get_current_blockchain_height() const final;
345
346 /**
347 * @brief get the hash and height of the most recent block
348 *
349 * @param height return-by-reference height of the block
350 * @param top_id return-by-reference hash of the block
351 */
352 void get_blockchain_top(uint64_t& height, crypto::hash& top_id) const;
353
354 /**
355 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&, std::vector<transaction>&) const
356 *
357 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&, std::vector<transaction>&) const
358 */
359 bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks, std::vector<cryptonote::blobdata>& txs) const;
360
361 /**
362 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
363 *
364 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
365 */
366 bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata,block>>& blocks) const;
367
368 /**
369 * @copydoc Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
370 *
371 * @note see Blockchain::get_blocks(uint64_t, size_t, std::vector<std::pair<cryptonote::blobdata,block>>&) const
372 */
373 bool get_blocks(uint64_t start_offset, size_t count, std::vector<block>& blocks) const;
374
375 /**
376 * @copydoc Blockchain::get_blocks(const t_ids_container&, t_blocks_container&, t_missed_container&) const
377 *
378 * @note see Blockchain::get_blocks(const t_ids_container&, t_blocks_container&, t_missed_container&) const
379 */
380 template<class t_ids_container, class t_blocks_container, class t_missed_container>
381 bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
382 {
383 return m_blockchain_storage.get_blocks(block_ids, blocks, missed_bs);
384 }
385
386 /**
387 * @copydoc Blockchain::get_block_id_by_height
388 *
389 * @note see Blockchain::get_block_id_by_height
390 */
391 crypto::hash get_block_id_by_height(uint64_t height) const;
392
393 /**
394 * @copydoc Blockchain::get_transactions
395 *
396 * @note see Blockchain::get_transactions
397 */
398 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;
399
400 /**
401 * @copydoc Blockchain::get_transactions
402 *
403 * @note see Blockchain::get_transactions
404 */
405 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;
406
407 /**
408 * @copydoc Blockchain::get_transactions
409 *
410 * @note see Blockchain::get_transactions
411 */
412 bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<transaction>& txs, std::vector<crypto::hash>& missed_txs, bool pruned = false) const;
413
414 /**
415 * @copydoc Blockchain::get_block_by_hash
416 *
417 * @note see Blockchain::get_block_by_hash
418 */
419 bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
420
421 /**
422 * @copydoc Blockchain::get_alternative_blocks
423 *
424 * @note see Blockchain::get_alternative_blocks(std::vector<block>&) const
425 */
426 bool get_alternative_blocks(std::vector<block>& blocks) const;
427
428 /**
429 * @copydoc Blockchain::get_alternative_blocks_count
430 *
431 * @note see Blockchain::get_alternative_blocks_count() const
432 */
433 size_t get_alternative_blocks_count() const;
434
435 /**
436 * @brief set the pointer to the cryptonote protocol object to use
437 *
438 * @param pprotocol the pointer to set ours as
439 */
440 void set_cryptonote_protocol(i_cryptonote_protocol* pprotocol);
441
442 /**
443 * @copydoc Blockchain::get_checkpoints
444 *
445 * @note see Blockchain::get_checkpoints()
446 */
447 const checkpoints& get_checkpoints() const;
448
449 /**
450 * @copydoc Blockchain::set_checkpoints
451 *
452 * @note see Blockchain::set_checkpoints()
453 */
454 void set_checkpoints(checkpoints&& chk_pts);
455
456 /**
457 * @brief set the file path to read from when loading checkpoints
458 *
459 * @param path the path to set ours as
460 */
461 void set_checkpoints_file_path(const std::string& path);
462
463 /**
464 * @brief set whether or not we enforce DNS checkpoints
465 *
466 * @param enforce_dns enforce DNS checkpoints or not
467 */
468 void set_enforce_dns_checkpoints(bool enforce_dns);
469
470 /**
471 * @brief set whether or not to enable or disable DNS checkpoints
472 *
473 * @param disble whether to disable DNS checkpoints
474 */
475 void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
476
477 /**
478 * @copydoc tx_memory_pool::have_tx
479 *
480 * @note see tx_memory_pool::have_tx
481 */
482 bool pool_has_tx(const crypto::hash &txid) const;
483
484 /**
485 * @copydoc tx_memory_pool::get_transactions
486 * @param include_sensitive_txes include private transactions
487 *
488 * @note see tx_memory_pool::get_transactions
489 */
490 bool get_pool_transactions(std::vector<transaction>& txs, bool include_sensitive_txes = false) const;
491
492 /**
493 * @copydoc tx_memory_pool::get_txpool_backlog
494 * @param include_sensitive_txes include private transactions
495 *
496 * @note see tx_memory_pool::get_txpool_backlog
497 */
498 bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog, bool include_sensitive_txes = false) const;
499
500 /**
501 * @copydoc tx_memory_pool::get_transactions
502 * @param include_sensitive_txes include private transactions
503 *
504 * @note see tx_memory_pool::get_transactions
505 */
506 bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive_txes = false) const;
507
508 /**
509 * @copydoc tx_memory_pool::get_pool_transactions_info
510 * @param include_sensitive_txes include private transactions
511 *
512 * @note see tx_memory_pool::get_pool_transactions_info
513 */
514 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;
515
516 /**
517 * @copydoc tx_memory_pool::get_pool_info
518 * @param include_sensitive_txes include private transactions
519 * @param max_tx_count max allowed added_txs in response
520 *
521 * @note see tx_memory_pool::get_pool_info
522 */
523 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;
524
525 /**
526 * @copydoc tx_memory_pool::get_transactions
527 * @param include_sensitive_txes include private transactions
528 *
529 * @note see tx_memory_pool::get_transactions
530 */
531 bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_sensitive_txes = false) const;
532
533 /**
534 * @copydoc tx_memory_pool::get_transaction
535 *
536 * @note see tx_memory_pool::get_transaction
537 */
538 bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx, relay_category tx_category) const;
539
540 /**
541 * @copydoc tx_memory_pool::get_pool_transactions_and_spent_keys_info
542 * @param include_sensitive_txes include private transactions
543 *
544 * @note see tx_memory_pool::get_pool_transactions_and_spent_keys_info
545 */
546 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;
547
548 /**
549 * @copydoc tx_memory_pool::get_pool_for_rpc
550 *
551 * @note see tx_memory_pool::get_pool_for_rpc
552 */
553 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;
554
555 /**
556 * @copydoc tx_memory_pool::get_transactions_count
557 * @param include_sensitive_txes include private transactions
558 *
559 * @note see tx_memory_pool::get_transactions_count
560 */
561 size_t get_pool_transactions_count(bool include_sensitive_txes = false) const;
562
563 /**
564 * @copydoc Blockchain::get_total_transactions
565 *
566 * @note see Blockchain::get_total_transactions
567 */
568 size_t get_blockchain_total_transactions() const;
569
570 /**
571 * @copydoc Blockchain::have_block
572 *
573 * @note see Blockchain::have_block
574 */
575 bool have_block_unlocked(const crypto::hash& id, int *where = NULL) const;
576 bool have_block(const crypto::hash& id, int *where = NULL) const;
577
578 /**
579 * @copydoc Blockchain::get_short_chain_history
580 *
581 * @note see Blockchain::get_short_chain_history
582 */
583 bool get_short_chain_history(std::list<crypto::hash>& ids) const;
584
585 /**
586 * @copydoc Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const
587 *
588 * @note see Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const
589 */
590 bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
591
592 /**
593 * @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
594 *
595 * @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
596 */
597 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;
598
599 /**
600 * @copydoc Blockchain::get_tx_outputs_gindexs
601 *
602 * @note see Blockchain::get_tx_outputs_gindexs
603 */
604 bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
605 bool get_tx_outputs_gindexs(const crypto::hash& tx_id, size_t n_txes, std::vector<std::vector<uint64_t>>& indexs) const;
606
607 /**
608 * @copydoc Blockchain::get_tail_id
609 *
610 * @note see Blockchain::get_tail_id
611 */
612 crypto::hash get_tail_id() const;
613
614 /**
615 * @copydoc Blockchain::get_block_cumulative_difficulty
616 *
617 * @note see Blockchain::get_block_cumulative_difficulty
618 */
619 difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
620
621 /**
622 * @copydoc Blockchain::get_outs
623 *
624 * @note see Blockchain::get_outs
625 */
626 bool get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res) const;
627
628 /**
629 * @copydoc Blockchain::get_output_distribution
630 *
631 * @brief get per block distribution of outputs of a given amount
632 */
633 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;
634
635 /**
636 * @copydoc miner::pause
637 *
638 * @note see miner::pause
639 */
640 void pause_mine();
641
642 /**
643 * @copydoc miner::resume
644 *
645 * @note see miner::resume
646 */
647 void resume_mine();
648
649 /**
650 * @brief gets the Blockchain instance
651 *
652 * @return a reference to the Blockchain instance
653 */
654 Blockchain& get_blockchain_storage(){return m_blockchain_storage;}
655
656 /**
657 * @brief gets the Blockchain instance (const)
658 *
659 * @return a const reference to the Blockchain instance
660 */
661 const Blockchain& get_blockchain_storage()const{return m_blockchain_storage;}
662
663 /**
664 * @copydoc tx_memory_pool::print_pool
665 *
666 * @note see tx_memory_pool::print_pool
667 */
668 std::string print_pool(bool short_format) const;
669
670 /**
671 * @brief gets the core synchronization status
672 *
673 * @return core synchronization status
674 */
675 virtual bool is_synchronized() const final;
676
677 /**
678 * @copydoc miner::on_synchronized
679 *
680 * @note see miner::on_synchronized
681 */
682 void on_synchronized();
683
684 /**
685 * @copydoc Blockchain::safesyncmode
686 *
687 * 2note see Blockchain::safesyncmode
688 */
689 void safesyncmode(const bool onoff);
690
691 /**
692 * @brief sets the target blockchain height
693 *
694 * @param target_blockchain_height the height to set
695 */
696 void set_target_blockchain_height(uint64_t target_blockchain_height);
697
698 /**
699 * @brief gets the target blockchain height
700 *
701 * @param target_blockchain_height the target height
702 */
703 uint64_t get_target_blockchain_height() const;
704
705 /**
706 * @brief returns the newest hardfork version known to the blockchain
707 *
708 * @return the version
709 */
710 uint8_t get_ideal_hard_fork_version() const;
711
712 /**
713 * @brief return the ideal hard fork version for a given block height
714 *
715 * @return what it says above
716 */
717 uint8_t get_ideal_hard_fork_version(uint64_t height) const;
718
719 /**
720 * @brief return the hard fork version for a given block height
721 *
722 * @return what it says above
723 */
724 uint8_t get_hard_fork_version(uint64_t height) const;
725
726 /**
727 * @brief return the earliest block a given version may activate
728 *
729 * @return what it says above
730 */
731 uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
732
733 /**
734 * @brief gets start_time
735 *
736 */
737 std::time_t get_start_time() const;
738
739 /**
740 * @brief tells the Blockchain to update its checkpoints
741 *
742 * This function will check if enough time has passed since the last
743 * time checkpoints were updated and tell the Blockchain to update
744 * its checkpoints if it is time. If updating checkpoints fails,
745 * the daemon is told to shut down.
746 *
747 * @note see Blockchain::update_checkpoints()
748 */
749 bool update_checkpoints(const bool skip_dns = false);
750
751 /**
752 * @brief tells the daemon to wind down operations and stop running
753 *
754 * Currently this function raises SIGTERM, allowing the installed signal
755 * handlers to do the actual stopping.
756 */
757 void graceful_exit();
758
759 /**
760 * @brief stops the daemon running
761 *
762 * @note see graceful_exit()
763 */
764 void stop();
765
766 /**
767 * @copydoc Blockchain::have_tx_keyimg_as_spent
768 *
769 * @note see Blockchain::have_tx_keyimg_as_spent
770 */
771 bool is_key_image_spent(const crypto::key_image& key_im) const;
772
773 /**
774 * @brief check if multiple key images are spent
775 *
776 * plural version of is_key_image_spent()
777 *
778 * @param key_im list of key images to check
779 * @param spent return-by-reference result for each image checked
780 *
781 * @return true
782 */
783 bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
784
785 /**
786 * @brief check if multiple key images are spent in the transaction pool
787 *
788 * @param key_im list of key images to check
789 * @param spent return-by-reference result for each image checked
790 *
791 * @return true
792 */
793 bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
794
795 /**
796 * @brief get the number of blocks to sync in one go
797 *
798 * @return the number of blocks to sync in one go
799 */
800 size_t get_block_sync_size(uint64_t height) const;
801
802 /**
803 * @brief get the sum of coinbase tx amounts between blocks
804 *
805 * @return the number of blocks to sync in one go
806 */
807 std::pair<boost::multiprecision::uint128_t, boost::multiprecision::uint128_t> get_coinbase_tx_sum(const uint64_t start_offset, const size_t count);
808
809 /**
810 * @brief get the network type we're on
811 *
812 * @return which network are we on?
813 */
814 network_type get_nettype() const { return m_nettype; };
815
816 /**
817 * @brief check whether an update is known to be available or not
818 *
819 * This does not actually trigger a check, but returns the result
820 * of the last check
821 *
822 * @return whether an update is known to be available or not
823 */
824 bool is_update_available() const { return m_update_available; }
825
826 /**
827 * @brief check a set of hashes against the precompiled hash set
828 *
829 * @return number of usable blocks
830 */
831 uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights);
832
833 /**
834 * @brief get free disk space on the blockchain partition
835 *
836 * @return free space in bytes
837 */
838 uint64_t get_free_space() const;
839
840 /**
841 * @brief get whether the core is running offline
842 *
843 * @return whether the core is running offline
844 */
845 bool offline() const { return m_offline; }
846
847 /**
848 * @brief get the blockchain pruning seed
849 *
850 * @return the blockchain pruning seed
851 */
852 uint32_t get_blockchain_pruning_seed() const;
853
854 /**
855 * @brief prune the blockchain
856 *
857 * @param pruning_seed the seed to use to prune the chain (0 for default, highly recommended)
858 *
859 * @return true iff success
860 */
861 bool prune_blockchain(uint32_t pruning_seed = 0);
862
863 /**
864 * @brief incrementally prunes blockchain
865 *
866 * @return true on success, false otherwise
867 */
868 bool update_blockchain_pruning();
869
870 /**
871 * @brief checks the blockchain pruning if enabled
872 *
873 * @return true on success, false otherwise
874 */
875 bool check_blockchain_pruning();
876
877 /**
878 * @brief checks whether a given block height is included in the precompiled block hash area
879 *
880 * @param height the height to check for
881 */
882 bool is_within_compiled_block_hash_area(uint64_t height) const;
883
884 /**
885 * @brief checks whether block weights are known for the given range
886 */
887 bool has_block_weights(uint64_t height, uint64_t nblocks) const;
888
889 /**
890 * @brief flushes the invalid block cache
891 */
892 void flush_invalid_blocks();
893
894 /**
895 * @brief returns the set of transactions in the txpool which are not in the argument
896 *
897 * @param hashes hashes of transactions to exclude from the result
898 *
899 * @return true iff success, false otherwise
900 */
901 bool get_txpool_complement(const std::vector<crypto::hash> &hashes, std::vector<cryptonote::blobdata> &txes);
902
903 /**
904 * @brief validates some simple properties of a transaction
905 *
906 * Currently checks: tx has inputs,
907 * tx inputs all of supported type(s),
908 * tx outputs valid (type, key, amount),
909 * input and output total amounts don't overflow,
910 * output amount <= input amount,
911 * tx not too large,
912 * each input has a different key image.
913 *
914 * @param tx the transaction to check
915 * @param tvc tx verification context where extra fail flags are stored
916 * @param hf_version hard fork version
917 *
918 * @return true if all the checks pass, otherwise false
919 */
920 static bool check_tx_semantic(const transaction& tx, tx_verification_context& tvc,
921 uint8_t hf_version);
922
923 /**
924 * @brief verify that each input key image in a transaction is unique
925 *
926 * @param tx the transaction to check
927 *
928 * @return false if any key image is repeated, otherwise true
929 */
930 static bool check_tx_inputs_keyimages_diff(const transaction& tx);
931
932 /**
933 * @brief verify that each ring uses distinct members
934 *
935 * @param tx the transaction to check
936 * @param hf_version the hard fork version rules to use
937 *
938 * @return false if any ring uses duplicate members, true otherwise
939 */
940 static bool check_tx_inputs_ring_members_diff(const transaction& tx, const uint8_t hf_version);
941
942 /**
943 * @brief verify that each input key image in a transaction is in
944 * the valid domain
945 *
946 * @param tx the transaction to check
947 *
948 * @return false if any key image is not in the valid domain, otherwise true
949 */
950 static bool check_tx_inputs_keyimages_domain(const transaction& tx);
951
952 private:
953
954 /**
955 * @copydoc add_new_tx(transaction&, tx_verification_context&, bool)
956 *
957 * @param tx_hash the transaction's hash
958 * @param blob the transaction as a blob
959 * @param tx_weight the weight of the transaction
960 * @param tx_relay how the transaction was received
961 * @param relayed whether or not the transaction was relayed to us
962 *
963 */
964 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);
965
966 /**
967 * @brief add a new transaction to the transaction pool
968 *
969 * Adds a new transaction to the transaction pool.
970 *
971 * @param tx the transaction to add
972 * @param tvc return-by-reference metadata about the transaction's verification process
973 * @param tx_relay how the transaction was received
974 * @param relayed whether or not the transaction was relayed to us
975 *
976 * @return true if the transaction is already in the transaction pool,
977 * is already in a block on the Blockchain, or is successfully added
978 * to the transaction pool
979 */
980 bool add_new_tx(transaction& tx, tx_verification_context& tvc, relay_method tx_relay, bool relayed);
981
982 /**
983 * @copydoc Blockchain::add_new_block
984 *
985 * @note see Blockchain::add_new_block
986 */
987 bool add_new_block(const block& b, block_verification_context& bvc,
988 pool_supplement& extra_block_txs);
989
990 /**
991 * @brief load any core state stored on disk
992 *
993 * currently does nothing, but may have state to load in the future.
994 *
995 * @return true
996 */
997 bool load_state_data();
998
999 /**
1000 * @copydoc miner::on_block_chain_update
1001 *
1002 * @note see miner::on_block_chain_update
1003 *
1004 * @return true
1005 */
1006 bool update_miner_block_template();
1007
1008 /**
1009 * @brief act on a set of command line options given
1010 *
1011 * @param vm the command line options
1012 *
1013 * @return true
1014 */
1015 bool handle_command_line(const boost::program_options::variables_map& vm);
1016
1017 /**
1018 * @brief attempts to relay any transactions in the mempool which need it
1019 *
1020 * @return true
1021 */
1022 bool relay_txpool_transactions();
1023
1024 /**
1025 * @brief sends notification of txpool events to subscribers
1026 *
1027 * @return true on success, false otherwise
1028 */
1029 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;
1030
1031 /**
1032 * @brief checks DNS versions
1033 *
1034 * @return true on success, false otherwise
1035 */
1036 bool check_updates();
1037
1038 /**
1039 * @brief checks free disk space
1040 *
1041 * @return true on success, false otherwise
1042 */
1043 bool check_disk_space();
1044
1045 /**
1046 * @brief checks block rate, and warns if it's too slow
1047 *
1048 * @return true on success, false otherwise
1049 */
1050 bool check_block_rate();
1051
1052 /**
1053 * @brief recalculate difficulties after the last difficulty checklpoint to circumvent the annoying 'difficulty drift' bug
1054 *
1055 * @return true
1056 */
1057 bool recalculate_difficulties();
1058
1059 bool m_test_drop_download = true; //!< whether or not to drop incoming blocks (for testing)
1060
1061 uint64_t m_test_drop_download_height = 0; //!< height under which to drop incoming blocks, if doing so
1062
1063 tx_memory_pool m_mempool; //!< transaction pool instance
1064 Blockchain m_blockchain_storage; //!< Blockchain instance
1065
1066 i_cryptonote_protocol* m_pprotocol; //!< cryptonote protocol instance
1067
1068 epee::critical_section m_incoming_tx_lock; //!< incoming transaction lock
1069
1070 //m_miner and m_miner_addres are probably temporary here
1071 miner m_miner; //!< miner instance
1072
1073 std::string m_config_folder; //!< folder to look in for configs and other files
1074
1075 cryptonote_protocol_stub m_protocol_stub; //!< cryptonote protocol stub instance
1076
1077 epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval; //!< interval for manual storing of Blockchain, if enabled
1078 epee::math_helper::once_a_time_seconds<60*60*2, true> m_fork_moaner; //!< interval for checking HardFork status
1079 epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval; //!< interval for checking for new versions
1080 epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval; //!< interval for checking for disk space
1081 epee::math_helper::once_a_time_seconds<90, false> m_block_rate_interval; //!< interval for checking block rate
1082 epee::math_helper::once_a_time_seconds<60*60*5, true> m_blockchain_pruning_interval; //!< interval for incremental blockchain pruning
1083 epee::math_helper::once_a_time_seconds<60*60*24*7, false> m_diff_recalc_interval; //!< interval for recalculating difficulties
1084
1085 std::atomic<bool> m_starter_message_showed; //!< has the "daemon will sync now" message been shown?
1086
1087 uint64_t m_target_blockchain_height; //!< blockchain height target
1088
1089 network_type m_nettype; //!< which network are we on?
1090
1091 std::atomic<bool> m_update_available;
1092
1093 std::string m_checkpoints_path; //!< path to json checkpoints file
1094 time_t m_last_dns_checkpoints_update; //!< time when dns checkpoints were last updated
1095 time_t m_last_json_checkpoints_update; //!< time when json checkpoints were last updated
1096
1097 std::atomic_flag m_checkpoints_updating; //!< set if checkpoints are currently updating to avoid multiple threads attempting to update at once
1098 bool m_disable_dns_checkpoints;
1099
1100 size_t block_sync_size;
1101
1102 time_t start_time;
1103
1104 enum {
1105 UPDATES_DISABLED,
1106 UPDATES_NOTIFY,
1107 UPDATES_DOWNLOAD,
1108 UPDATES_UPDATE,
1109 } check_updates_level;
1110
1111 tools::download_async_handle m_update_download;
1112 size_t m_last_update_length;
1113 boost::mutex m_update_mutex;
1114
1115 bool m_offline;
1116
1117 std::shared_ptr<tools::Notify> m_block_rate_notify;
1118 };
1119 }
1120
1121 POP_WARNINGS