Electroneum
blockchain.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 #include <boost/asio/io_service.hpp>
34 #include <boost/serialization/serialization.hpp>
35 #include <boost/serialization/version.hpp>
36 #include <boost/serialization/list.hpp>
37 #include <boost/multi_index_container.hpp>
38 #include <boost/multi_index/global_fun.hpp>
39 #include <boost/multi_index/hashed_index.hpp>
40 #include <boost/multi_index/member.hpp>
41 #include <atomic>
42 #include <unordered_map>
43 #include <unordered_set>
44 
45 #include "syncobj.h"
46 #include "string_tools.h"
48 #include "common/util.h"
52 #include "cryptonote_tx_utils.h"
54 #include "crypto/hash.h"
58 
59 namespace cryptonote
60 {
61  class tx_memory_pool;
62  struct test_options;
63 
68  {
73  };
74 
75  /************************************************************************/
76  /* */
77  /************************************************************************/
78  class Blockchain
79  {
80  public:
85  {
88  size_t m_blob_size;
89  std::vector<uint64_t> m_global_output_indexes;
90  };
91 
96  {
98  uint64_t height;
102  };
103 
109  Blockchain(tx_memory_pool& tx_pool);
110 
120  bool init(BlockchainDB* db, const bool testnet = false, const cryptonote::test_options *test_options = NULL);
121 
131  bool init(BlockchainDB* db, HardFork*& hf, const bool testnet = false);
132 
140  bool deinit();
141 
147  void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
148 
159  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks, std::list<cryptonote::blobdata>& txs) const;
160 
170  bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata,block>>& blocks) const;
171 
179  bool get_alternative_blocks(std::list<block>& blocks) const;
180 
186  size_t get_alternative_blocks_count() const;
187 
196 
206  bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan = NULL) const;
207 
215  void get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid) const;
216 
224  bool prepare_handle_incoming_blocks(const std::list<block_complete_entry> &blocks);
225 
233  bool cleanup_handle_incoming_blocks(bool force_sync = false);
234 
242  bool have_tx(const crypto::hash &id) const;
243 
251  bool have_tx_keyimges_as_spent(const transaction &tx) const;
252 
265  bool have_tx_keyimg_as_spent(const crypto::key_image &key_im) const;
266 
272  uint64_t get_current_blockchain_height() const;
273 
279  crypto::hash get_tail_id() const;
280 
288  crypto::hash get_tail_id(uint64_t& height) const;
289 
296 
310  bool add_new_block(const block& bl_, block_verification_context& bvc);
311 
319  bool reset_and_set_genesis_block(const block& b);
320 
333  bool create_block_template(block& b, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
334 
345  bool have_block(const crypto::hash& id) const;
346 
352  size_t get_total_transactions() const;
353 
368  bool get_short_chain_history(std::list<crypto::hash>& ids) const;
369 
382  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
383 
396  bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset) const;
397 
414  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;
415 
429 
445 
460 
477 
489  bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs) const;
490 
499  bool store_blockchain();
500 
517  bool check_tx_inputs(transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id, tx_verification_context &tvc, bool kept_by_block = false);
518 
531  static uint64_t get_dynamic_per_kb_fee(uint64_t block_reward, size_t median_block_size, uint8_t version);
532 
545  uint64_t get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks) const;
546 
559  bool check_fee(size_t blob_size, uint64_t fee) const;
560 
574 
581 
590  bool is_storing_blockchain()const{return false;}
591 
599  uint64_t block_difficulty(uint64_t i) const;
600 
613  template<class t_ids_container, class t_blocks_container, class t_missed_container>
614  bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const;
615 
628  template<class t_ids_container, class t_tx_container, class t_missed_container>
629  bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
630  template<class t_ids_container, class t_tx_container, class t_missed_container>
631  bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
632 
633  //debug functions
634 
643  void print_blockchain(uint64_t start_index, uint64_t end_index) const;
644 
652  void print_blockchain_index() const;
653 
659  void print_blockchain_outs(const std::string& file) const;
660 
672  void check_against_checkpoints(const checkpoints& points, bool enforce);
673 
679  void set_enforce_dns_checkpoints(bool enforce);
680 
689  bool update_checkpoints(const std::string& file_path, bool check_dns);
690 
691 
692  // user options, must be called before calling init()
693 
702  void set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync,
703  blockchain_db_sync_mode sync_mode, bool fast_sync);
704 
708  void safesyncmode(const bool onoff);
709 
715  void set_show_time_stats(bool stats) { m_show_time_stats = stats; }
716 
723 
730 
737 
744 
754 
762  uint8_t get_hard_fork_version(uint64_t height) const { return m_hardfork->get(height); }
763 
776  bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
777 
785  bool flush_txes_from_pool(const std::list<crypto::hash> &txids);
786 
796  std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> get_output_histogram(const std::vector<uint64_t> &amounts, bool unlocked, uint64_t recent_cutoff) const;
797 
805  bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
806 
816  bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const block&)>) const;
817 
825  bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
826 
834  bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)>) const;
835 
841  const BlockchainDB& get_db() const
842  {
843  return *m_db;
844  }
845 
852  {
853  return *m_db;
854  }
855 
864  void output_scan_worker(const uint64_t amount,const std::vector<uint64_t> &offsets,
865  std::vector<output_data_t> &outputs, std::unordered_map<crypto::hash,
866  cryptonote::transaction> &txs) const;
867 
875  void block_longhash_worker(uint64_t height, const std::vector<block> &blocks,
876  std::unordered_map<crypto::hash, crypto::hash> &map) const;
877 
883  std::list<std::pair<block_extended_info,uint64_t>> get_alternative_chains() const;
884 
885  void add_txpool_tx(transaction &tx, const txpool_tx_meta_t &meta);
886  void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta);
887  void remove_txpool_tx(const crypto::hash &txid);
888  uint64_t get_txpool_tx_count() const;
890  bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const;
892  bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false) const;
893  uint32_t get_mempool_tx_livetime() const;
894  uint32_t get_difficulty_target() const;
895 
896  bool is_within_compiled_block_hash_area(uint64_t height) const;
898 
899  void lock();
900  void unlock();
901 
902  void cancel();
903 
910 
911  private:
912 
913  // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage
914  typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
915 
916  typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;
917 
918  typedef std::unordered_set<crypto::key_image> key_images_container;
919 
920  typedef std::vector<block_extended_info> blocks_container;
921 
922  typedef std::unordered_map<crypto::hash, block_extended_info> blocks_ext_by_hash;
923 
924  typedef std::unordered_map<crypto::hash, block> blocks_by_hash;
925 
926  typedef std::map<uint64_t, std::vector<std::pair<crypto::hash, size_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
927 
928 
930 
932 
933  mutable epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
934 
935  // main chain
938 
939  // metadata containers
940  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, std::vector<output_data_t>>> m_scan_table;
941  std::unordered_map<crypto::hash, crypto::hash> m_blocks_longhash_table;
942  std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, bool>> m_check_txin_table;
943 
944  // SHA-3 hashes for each block and for fast pow checking
945  std::vector<crypto::hash> m_blocks_hash_check;
946  std::vector<crypto::hash> m_blocks_txs_check;
947 
956  uint64_t m_sync_counter;
957  std::vector<uint64_t> m_timestamps;
958  std::vector<difficulty_type> m_difficulties;
960 
961  boost::asio::io_service m_async_service;
962  boost::thread_group m_async_pool;
963  std::unique_ptr<boost::asio::io_service::work> m_async_work_idle;
964 
965  // all alternative chains
966  blocks_ext_by_hash m_alternative_chains; // crypto::hash -> block_extended_info
967 
968  // some invalid blocks
969  blocks_ext_by_hash m_invalid_blocks; // crypto::hash -> block_extended_info
970 
971 
974 
976 
977  bool m_testnet;
978 
979  std::atomic<bool> m_cancel;
980 
1000  template<class visitor_t>
1001  inline bool scan_outputkeys_for_indexes(size_t tx_version, const txin_to_key& tx_in_to_key, visitor_t &vis, const crypto::hash &tx_prefix_hash, uint64_t* pmax_related_block_height = NULL) const;
1002 
1023  bool check_tx_input(size_t tx_version,const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig, const rct::rctSig &rct_signatures, std::vector<rct::ctkey> &output_keys, uint64_t* pmax_related_block_height);
1024 
1045  bool check_tx_inputs(transaction& tx, tx_verification_context &tvc, uint64_t* pmax_used_block_height = NULL);
1046 
1059  bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
1060 
1067 
1081 
1096 
1111 
1120  difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::iterator>& alt_chain, block_extended_info& bei) const;
1121 
1133  bool prevalidate_miner_transaction(const block& b, uint64_t height);
1134 
1151  bool validate_miner_transaction(const block& b, size_t cumulative_block_size, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins, bool &partial_block_reward, uint8_t version);
1152 
1165  bool rollback_blockchain_switching(std::list<block>& original_chain, uint64_t rollback_height);
1166 
1175  void get_last_n_blocks_sizes(std::vector<size_t>& sz, size_t count) const;
1176 
1184  void add_out_to_get_random_outs(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs, uint64_t amount, size_t i) const;
1185 
1193  void add_out_to_get_rct_random_outs(std::list<COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::out_entry>& outs, uint64_t amount, size_t i) const;
1194 
1205  bool is_tx_spendtime_unlocked(uint64_t unlock_time) const;
1206 
1218  bool add_block_as_invalid(const block& bl, const crypto::hash& h);
1219 
1231  bool add_block_as_invalid(const block_extended_info& bei, const crypto::hash& h);
1232 
1249  bool check_block_timestamp(const block& b, uint64_t& median_ts) const;
1250  bool check_block_timestamp(const block& b) const { uint64_t median_ts; return check_block_timestamp(b, median_ts); }
1251 
1263  bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b, uint64_t& median_ts) const;
1264  bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b) const { uint64_t median_ts; return check_block_timestamp(timestamps, b, median_ts); }
1265 
1274  uint64_t get_adjusted_time() const;
1275 
1287  bool complete_timestamps_vector(uint64_t start_height, std::vector<uint64_t>& timestamps);
1288 
1295  void return_tx_to_pool(std::vector<transaction> &txs);
1296 
1305  bool check_for_double_spend(const transaction& tx, key_images_container& keys_this_block) const;
1306 
1316  void check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image,
1317  const std::vector<rct::ctkey> &pubkeys, const std::vector<crypto::signature> &sig, uint64_t &result);
1318 
1327 
1335  bool expand_transaction_2(transaction &tx, const crypto::hash &tx_prefix_hash, const std::vector<std::vector<rct::ctkey>> &pubkeys);
1336  };
1337 } // namespace cryptonote
Definition: cryptonote_protocol_defs.h:165
uint64_t m_max_prepare_blocks_threads
Definition: blockchain.h:953
bool check_block_timestamp(std::vector< uint64_t > &timestamps, const block &b) const
Definition: blockchain.h:1264
bool m_db_default_sync
Definition: blockchain.h:951
std::vector< crypto::hash > m_blocks_hash_check
Definition: blockchain.h:945
void print_blockchain_outs(const std::string &file) const
currently does nothing, candidate for removal
Definition: blockchain.cpp:2067
std::unordered_map< crypto::hash, std::unordered_map< crypto::key_image, std::vector< output_data_t > > > m_scan_table
Definition: blockchain.h:940
block pop_block_from_blockchain()
removes the most recent block from the blockchain
Definition: blockchain.cpp:504
Definition: core_rpc_server_commands_defs.h:339
checkpoints m_checkpoints
Definition: blockchain.h:972
bool add_block_as_invalid(const block &bl, const crypto::hash &h)
stores an invalid block in a separate container
Definition: blockchain.cpp:2146
bool have_tx_keyimg_as_spent(const crypto::key_image &key_im) const
check if a key image is already spent on the blockchain
Definition: blockchain.cpp:126
uint64_t m_fake_pow_calc_time
Definition: blockchain.h:954
bool is_tx_spendtime_unlocked(uint64_t unlock_time) const
checks if a transaction is unlocked (its outputs spendable)
Definition: blockchain.cpp:2938
block bl
the block
Definition: blockchain.h:97
size_t m_current_block_cumul_sz_limit
Definition: blockchain.h:937
handle syncing calls instead of the backing db, asynchronously
Definition: blockchain.h:71
void block_longhash_worker(uint64_t height, const std::vector< block > &blocks, std::unordered_map< crypto::hash, crypto::hash > &map) const
computes the "short" and "long" hashes for a set of blocks
Definition: blockchain.cpp:3588
std::vector< uint64_t > m_timestamps
Definition: blockchain.h:957
bool check_for_double_spend(const transaction &tx, key_images_container &keys_this_block) const
make sure a transaction isn't attempting a double-spend
Definition: blockchain.cpp:2210
std::unique_ptr< boost::asio::io_service::work > m_async_work_idle
Definition: blockchain.h:963
bool get_transactions_blobs(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs) const
gets transactions based on a list of transaction hashes
Definition: blockchain.cpp:1976
BlockchainDB & get_db()
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:851
size_t block_cumulative_size
the size (in bytes) of the block
Definition: blockchain.h:99
bool expand_transaction_2(transaction &tx, const crypto::hash &tx_prefix_hash, const std::vector< std::vector< rct::ctkey >> &pubkeys)
expands v2 transaction data from blockchain
Definition: blockchain.cpp:2407
std::unordered_set< crypto::key_image > key_images_container
Definition: blockchain.h:918
bool m_fast_sync
Definition: blockchain.h:949
uint64_t get_current_cumulative_blocksize_limit() const
gets the blocksize limit based on recent blocks
Definition: blockchain.cpp:1107
Definition: cryptonote_basic.h:372
bool create_block_template(block &b, const account_public_address &miner_address, difficulty_type &di, uint64_t &height, uint64_t &expected_reward, const blobdata &ex_nonce)
creates a new block to mine against
Definition: blockchain.cpp:1124
bool prevalidate_miner_transaction(const block &b, uint64_t height)
sanity checks a miner transaction before validating an entire block
Definition: blockchain.cpp:1007
blocks_ext_by_hash m_invalid_blocks
Definition: blockchain.h:969
uint64_t height
Definition: blockchain.cpp:87
bool handle_alternative_block(const block &b, const crypto::hash &id, block_verification_context &bvc)
validate and add a new block to an alternate blockchain
Definition: blockchain.cpp:1305
uint64_t m_fake_scan_time
Definition: blockchain.h:955
user didn't specify, use db_async
Definition: blockchain.h:69
std::unordered_map< crypto::hash, block > blocks_by_hash
Definition: blockchain.h:924
std::unordered_map< crypto::hash, std::unordered_map< crypto::key_image, bool > > m_check_txin_table
Definition: blockchain.h:942
bool for_all_transactions(std::function< bool(const crypto::hash &, const cryptonote::transaction &)>) const
perform a check on all transactions in the blockchain
Definition: blockchain.cpp:4276
bool switch_to_alternative_blockchain(std::list< blocks_ext_by_hash::iterator > &alt_chain, bool discard_disconnected_chain)
performs a blockchain reorganization according to the longest chain rule
Definition: blockchain.cpp:839
bool have_tx(const crypto::hash &id) const
search the blockchain for a transaction by hash
Definition: blockchain.cpp:119
difficulty_type get_next_difficulty_for_alternative_chain(const std::list< blocks_ext_by_hash::iterator > &alt_chain, block_extended_info &bei) const
gets the difficulty requirement for a new block on an alternate chain
Definition: blockchain.cpp:934
bool check_tx_inputs(transaction &tx, uint64_t &pmax_used_block_height, crypto::hash &max_used_block_id, tx_verification_context &tvc, bool kept_by_block=false)
validates a transaction's inputs
Definition: blockchain.cpp:2319
Definition: core_rpc_server_commands_defs.h:389
bool get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request &req, COMMAND_RPC_GET_OUTPUTS_BIN::response &res) const
gets specific outputs to mix with
Definition: blockchain.cpp:1847
handle syncing calls instead of the backing db, synchronously
Definition: blockchain.h:70
Definition: hardfork.h:39
uint8_t get_next_hard_fork_version() const
returns the next hardfork version
Definition: blockchain.h:743
int main(int argc, char *argv[])
Definition: blockchain_export.cpp:44
bool get_alternative_blocks(std::list< block > &blocks) const
compiles a list of all blocks stored as alternative chains
Definition: blockchain.cpp:1590
bool flush_txes_from_pool(const std::list< crypto::hash > &txids)
remove transactions from the transaction pool (if present)
Definition: blockchain.cpp:3098
bool add_new_block(const block &bl_, block_verification_context &bvc)
adds a block to the blockchain
Definition: blockchain.cpp:3477
Definition: verification_context.h:54
container for passing a block and metadata about it on the blockchain
Definition: blockchain.h:95
bool m_show_time_stats
Definition: blockchain.h:950
bool get_short_chain_history(std::list< crypto::hash > &ids) const
gets the hashes for a subset of the blockchain
Definition: blockchain.cpp:608
uint64_t height
the height of the block in the blockchain
Definition: blockchain.h:98
static uint64_t get_dynamic_per_kb_fee(uint64_t block_reward, size_t median_block_size, uint8_t version)
get dynamic per kB fee for a given block size
Definition: blockchain.cpp:2838
bool prepare_handle_incoming_blocks(const std::list< block_complete_entry > &blocks)
performs some preprocessing on a group of incoming blocks to speed up verification
Definition: blockchain.cpp:3688
void check_against_checkpoints(const checkpoints &points, bool enforce)
check the blockchain against a set of checkpoints
Definition: blockchain.cpp:3510
void load_compiled_in_block_hashes()
loads block hashes from compiled-in data set
bool check_tx_input(size_t tx_version, const txin_to_key &txin, const crypto::hash &tx_prefix_hash, const std::vector< crypto::signature > &sig, const rct::rctSig &rct_signatures, std::vector< rct::ctkey > &output_keys, uint64_t *pmax_related_block_height)
collect output public keys of a transaction input set
Definition: blockchain.cpp:2965
bool get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan=NULL) const
gets the block with a given hash
Definition: blockchain.cpp:675
std::unordered_map< crypto::hash, crypto::hash > m_blocks_longhash_table
Definition: blockchain.h:941
std::unordered_map< crypto::hash, size_t > blocks_by_id_index
Definition: blockchain.h:914
bool check_fee(size_t blob_size, uint64_t fee) const
validate a transaction's fee
Definition: blockchain.cpp:2865
difficulty_type cumulative_difficulty
the accumulated difficulty after that block
Definition: blockchain.h:100
std::vector< difficulty_type > m_difficulties
Definition: blockchain.h:958
bool update_checkpoints(const std::string &file_path, bool check_dns)
loads new checkpoints from a file and optionally from DNS
Definition: blockchain.cpp:3547
std::unordered_map< crypto::hash, block_extended_info > blocks_ext_by_hash
Definition: blockchain.h:922
void remove_txpool_tx(const crypto::hash &txid)
Definition: blockchain.cpp:4048
void lock()
Definition: blockchain.cpp:4256
HardFork::State get_hard_fork_state() const
gets the hardfork voting state object
Definition: blockchain.cpp:4121
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
uint64_t m_sync_counter
Definition: blockchain.h:956
Definition: verification_context.h:38
uint8_t get_next_version() const
returns the next version
Definition: hardfork.cpp:390
uint8_t get_hard_fork_version(uint64_t height) const
returns the actual hardfork version for a given block height
Definition: blockchain.h:762
void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta)
Definition: blockchain.cpp:4043
Definition: core_rpc_server_commands_defs.h:310
std::vector< uint64_t > m_global_output_indexes
Definition: blockchain.h:89
size_t get_alternative_blocks_count() const
returns the number of alternative blocks stored
Definition: blockchain.cpp:1602
bool check_tx_outputs(const transaction &tx, tx_verification_context &tvc)
check that a transaction's outputs conform to current standards
Definition: blockchain.cpp:2350
std::atomic< bool > m_cancel
Definition: blockchain.h:979
virtual uint64_t height() const =0
fetch the current blockchain height
bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
get information about hardfork voting for a version
Definition: blockchain.cpp:4126
void print_blockchain(uint64_t start_index, uint64_t end_index) const
prints data about a snippet of the blockchain
Definition: blockchain.cpp:2030
void print_blockchain_index() const
prints every block's hash
Definition: blockchain.cpp:2049
uint64_t get_current_blockchain_height() const
get the current height of the blockchain
Definition: blockchain.cpp:257
void add_out_to_get_random_outs(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount &result_outs, uint64_t amount, size_t i) const
adds the given output to the requested set of random outputs
Definition: blockchain.cpp:1611
std::vector< crypto::hash > m_blocks_txs_check
Definition: blockchain.h:946
transactions_container m_transactions
Definition: blockchain.h:936
HardFork * m_hardfork
Definition: blockchain.h:975
void set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync, blockchain_db_sync_mode sync_mode, bool fast_sync)
sets various performance options
Definition: blockchain.cpp:4096
void get_all_known_block_ids(std::list< crypto::hash > &main, std::list< crypto::hash > &alt, std::list< crypto::hash > &invalid) const
get all block hashes (main chain, alt chains, and invalid blocks)
Definition: blockchain.cpp:716
bool deinit()
Uninitializes the blockchain state.
Definition: blockchain.cpp:463
difficulty_type get_difficulty_for_next_block()
returns the difficulty target the next block to be added must meet
Definition: blockchain.cpp:737
txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash &txid) const
Definition: blockchain.cpp:4058
bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request &req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response &res) const
gets random outputs to mix with
Definition: blockchain.cpp:1626
Definition: cryptonote_protocol_defs.h:181
uint64_t m_keeper_block_height
Definition: blockchain.h:87
uint8_t get_ideal_hard_fork_version() const
returns the newest hardfork version known to the blockchain
Definition: blockchain.h:736
blockchain_db_sync_mode
Definition: blockchain.h:67
void cancel()
Definition: blockchain.cpp:4168
int b
Definition: base.py:1
std::map< uint64_t, std::tuple< uint64_t, uint64_t, uint64_t > > get_output_histogram(const std::vector< uint64_t > &amounts, bool unlocked, uint64_t recent_cutoff) const
return a histogram of outputs on the blockchain
Definition: blockchain.cpp:4131
Definition: cryptonote_basic.h:130
std::list< std::pair< block_extended_info, uint64_t > > get_alternative_chains() const
returns a set of known alternate chains
Definition: blockchain.cpp:4136
Definition: rctTypes.h:323
Leave syncing up to the backing db (safest, but slowest because of disk I/O)
Definition: blockchain.h:72
bool have_block(const crypto::hash &id) const
checks if a block is known about with a given hash
Definition: blockchain.cpp:2164
std::unordered_map< crypto::hash, transaction_chain_entry > transactions_container
Definition: blockchain.h:916
bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request &arg, NOTIFY_RESPONSE_GET_OBJECTS::request &rsp)
retrieves a set of blocks and their transactions, and possibly other transactions
Definition: blockchain.cpp:1538
bool m_testnet
Definition: blockchain.h:977
bool m_enforce_dns_checkpoints
Definition: blockchain.h:973
bool rollback_blockchain_switching(std::list< block > &original_chain, uint64_t rollback_height)
reverts the blockchain to its previous state following a failed switch
Definition: blockchain.cpp:797
uint64_t get_adjusted_time() const
get the "adjusted time"
Definition: blockchain.cpp:3023
void set_enforce_dns_checkpoints(bool enforce)
configure whether or not to enforce DNS-based checkpoints
Definition: blockchain.cpp:3582
bool get_blocks(uint64_t start_offset, size_t count, std::list< std::pair< cryptonote::blobdata, block >> &blocks, std::list< cryptonote::blobdata > &txs) const
get blocks and transactions from blocks based on start height and count
Definition: blockchain.cpp:1490
bool get_random_rct_outs(const COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::request &req, COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::response &res) const
gets random ringct outputs to mix with
Definition: blockchain.cpp:1733
std::map< uint64_t, std::vector< std::pair< crypto::hash, size_t > > > outputs_container
Definition: blockchain.h:926
uint8_t get_ideal_hard_fork_version(uint64_t height) const
returns the newest hardfork version voted to be enabled as of a certain height
Definition: blockchain.h:753
Definition: cryptonote_core.h:58
BlockchainDB * m_db
Definition: blockchain.h:929
bool check_block_timestamp(const block &b) const
Definition: blockchain.h:1250
uint8_t version
Definition: blockchain.cpp:86
bool for_all_txpool_txes(std::function< bool(const crypto::hash &, const txpool_tx_meta_t &, const cryptonote::blobdata *)>, bool include_blob=false) const
Definition: blockchain.cpp:4082
bool reset_and_set_genesis_block(const block &b)
clears the blockchain and starts a new one
Definition: blockchain.cpp:566
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:93
std::string blobdata
Definition: blobdatatype.h:36
bool store_blockchain()
stores the blockchain
Definition: blockchain.cpp:433
#define blocks
Definition: sha512-hash.c:11
Blockchain(tx_memory_pool &tx_pool)
Blockchain constructor.
Definition: blockchain.cpp:112
tx_memory_pool & m_tx_pool
Definition: blockchain.h:931
void on_new_tx_from_block(const cryptonote::transaction &tx)
called when we see a tx originating from a block
Definition: blockchain.cpp:2293
Definition: core_rpc_server_commands_defs.h:363
crypto::hash get_block_id_by_height(uint64_t height) const
gets a block's hash given a height
Definition: blockchain.cpp:651
bool handle_block_to_main_chain(const block &bl, block_verification_context &bvc)
validate and add a new block to the end of the blockchain
Definition: blockchain.cpp:2190
Now-defunct (TODO: remove) struct from in-memory blockchain.
Definition: blockchain.h:84
Definition: cryptonote_basic.h:406
bool complete_timestamps_vector(uint64_t start_height, std::vector< uint64_t > &timestamps)
finish an alternate chain's timestamp window from the main chain
Definition: blockchain.cpp:1280
void check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image, const std::vector< rct::ctkey > &pubkeys, const std::vector< crypto::signature > &sig, uint64_t &result)
validates a transaction input's ring signature
Definition: blockchain.cpp:2812
boost::thread_group m_async_pool
Definition: blockchain.h:962
void set_show_time_stats(bool stats)
set whether or not to show/print time statistics
Definition: blockchain.h:715
blockchain_db_sync_mode m_db_sync_mode
Definition: blockchain.h:948
std::uint64_t difficulty_type
Definition: difficulty.h:41
uint64_t get_txpool_tx_count() const
Definition: blockchain.cpp:4053
epee::critical_section m_blockchain_lock
Definition: blockchain.h:933
bool check_block_timestamp(const block &b, uint64_t &median_ts) const
checks a block's timestamp
Definition: blockchain.cpp:3052
uint64_t already_generated_coins
the total coins minted after that block
Definition: blockchain.h:101
POD_CLASS key_image
Definition: crypto.h:86
const BlockchainDB & get_db() const
get a reference to the BlockchainDB in use by Blockchain
Definition: blockchain.h:841
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:131
void output_scan_worker(const uint64_t amount, const std::vector< uint64_t > &offsets, std::vector< output_data_t > &outputs, std::unordered_map< crypto::hash, cryptonote::transaction > &txs) const
get a number of outputs of a specific amount
Definition: blockchain.cpp:3665
void safesyncmode(const bool onoff)
Put DB in safe sync mode.
Definition: blockchain.cpp:4109
Definition: core_rpc_server_commands_defs.h:461
bool is_within_compiled_block_hash_area() const
Definition: blockchain.h:897
uint8_t get_current_hard_fork_version() const
gets the current hardfork version in use/voted for
Definition: blockchain.h:729
bool get_tx_outputs_gindexs(const crypto::hash &tx_id, std::vector< uint64_t > &indexs) const
gets the global indices for outputs from a given transaction
Definition: blockchain.cpp:2270
Definition: cryptonote_protocol_defs.h:231
std::vector< block_extended_info > blocks_container
Definition: blockchain.h:920
size_t get_total_transactions() const
gets the total number of transactions on the main chain
Definition: blockchain.cpp:2197
boost::asio::io_service m_async_service
Definition: blockchain.h:961
uint64_t m_db_blocks_per_sync
Definition: blockchain.h:952
bool for_all_outputs(std::function< bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)>) const
perform a check on all outputs in the blockchain
Definition: blockchain.cpp:4281
void return_tx_to_pool(std::vector< transaction > &txs)
Definition: blockchain.cpp:3080
bool init(BlockchainDB *db, const bool testnet=false, const cryptonote::test_options *test_options=NULL)
Initialize the Blockchain state.
Definition: blockchain.cpp:266
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:336
uint64_t block_difficulty(uint64_t i) const
gets the difficulty of the block with a given height
Definition: blockchain.cpp:1927
bool for_blocks_range(const uint64_t &h1, const uint64_t &h2, std::function< bool(uint64_t, const crypto::hash &, const block &)>) const
perform a check on all blocks in the blockchain in the given range
Definition: blockchain.cpp:4271
size_t m_blob_size
Definition: blockchain.h:88
crypto::hash get_tail_id() const
get the hash of the most recent block on the blockchain
Definition: blockchain.cpp:589
uint32_t get_difficulty_target() const
Definition: blockchain.cpp:4073
POD_CLASS hash
Definition: hash.h:46
A container for blockchain checkpoints.
Definition: checkpoints.h:52
State
Definition: hardfork.h:42
bool is_storing_blockchain() const
checks if the blockchain is currently being stored
Definition: blockchain.h:590
bool cleanup_handle_incoming_blocks(bool force_sync=false)
incoming blocks post-processing, cleanup, and disk sync
Definition: blockchain.cpp:3607
void add_txpool_tx(transaction &tx, const txpool_tx_meta_t &meta)
Definition: blockchain.cpp:4038
bool update_next_cumulative_size_limit()
calculate the block size limit for the next block to be added
Definition: blockchain.cpp:3461
bool validate_miner_transaction(const block &b, size_t cumulative_block_size, uint64_t fee, uint64_t &base_reward, uint64_t already_generated_coins, bool &partial_block_reward, uint8_t version)
validates a miner (coinbase) transaction
Definition: blockchain.cpp:1034
bool have_tx_keyimges_as_spent(const transaction &tx) const
check if any key image in a transaction has already been spent
Definition: blockchain.cpp:2396
bool get_txpool_tx_blob(const crypto::hash &txid, cryptonote::blobdata &bd) const
Definition: blockchain.cpp:4063
blocks_ext_by_hash m_alternative_chains
Definition: blockchain.h:966
bool get_transactions(const t_ids_container &txs_ids, t_tx_container &txs, t_missed_container &missed_txs) const
Definition: blockchain.cpp:2000
transaction tx
Definition: blockchain.h:86
bool scan_outputkeys_for_indexes(size_t tx_version, const txin_to_key &tx_in_to_key, visitor_t &vis, const crypto::hash &tx_prefix_hash, uint64_t *pmax_related_block_height=NULL) const
collects the keys for all outputs being "spent" as an input
Definition: blockchain.cpp:137
void unlock()
Definition: blockchain.cpp:4261
uint8_t get_current_version() const
returns the current version
Definition: hardfork.cpp:358
bool find_blockchain_supplement(const std::list< crypto::hash > &qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request &resp) const
get recent block hashes for a foreign chain
Definition: blockchain.cpp:2076
uint64_t get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks) const
get dynamic per kB fee estimate for the next few blocks
Definition: blockchain.cpp:2898
Definition: cryptonote_basic.h:188
uint8_t get_ideal_version() const
returns the latest "ideal" version
Definition: hardfork.cpp:364
uint64_t m_timestamps_and_difficulties_height
Definition: blockchain.h:959
uint32_t get_mempool_tx_livetime() const
Definition: blockchain.cpp:4087
bool for_all_key_images(std::function< bool(const crypto::key_image &)>) const
perform a check on all key images in the blockchain
Definition: blockchain.cpp:4266
Definition: core_rpc_server_commands_defs.h:443
void set_checkpoints(checkpoints &&chk_pts)
assign a set of blockchain checkpoint hashes
Definition: blockchain.h:147
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
Definition: hardfork.cpp:345
void add_out_to_get_rct_random_outs(std::list< COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS::out_entry > &outs, uint64_t amount, size_t i) const
adds the given output to the requested set of random ringct outputs
Definition: blockchain.cpp:1716
Definition: blockchain.h:78
void get_last_n_blocks_sizes(std::vector< size_t > &sz, size_t count) const
gets recent block sizes for median calculation
Definition: blockchain.cpp:1087
uint8_t threshold
Definition: blockchain.cpp:88