diff options
author | jeffro256 <jeffro256@tutanota.com> | 2023-06-30 14:52:43 -0500 |
---|---|---|
committer | jeffro256 <jeffro256@tutanota.com> | 2023-07-01 07:12:53 -0500 |
commit | ffbf9f4766945f33b80366c414a5721ce90e5a2e (patch) | |
tree | ad1a816d0dde18062afaad3357e88ef0d062375a /src | |
parent | Avoid nullptr dereference when constructing Blockchain and tx_memory_pool (diff) | |
download | monero-ffbf9f4766945f33b80366c414a5721ce90e5a2e.tar.xz |
blockchain_and_pool: move to crytonote_core and enforce its usage
Diffstat (limited to 'src')
-rw-r--r-- | src/blockchain_utilities/blockchain_ancestry.cpp | 4 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_depth.cpp | 4 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_export.cpp | 2 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_prune.cpp | 3 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_prune_known_spent_data.cpp | 4 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_stats.cpp | 3 | ||||
-rw-r--r-- | src/blockchain_utilities/blockchain_usage.cpp | 4 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.h | 16 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain_and_pool.h (renamed from src/blockchain_utilities/blockchain_and_pool.h) | 28 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_core.cpp | 5 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_core.h | 8 | ||||
-rw-r--r-- | src/cryptonote_core/tx_pool.h | 17 |
12 files changed, 46 insertions, 52 deletions
diff --git a/src/blockchain_utilities/blockchain_ancestry.cpp b/src/blockchain_utilities/blockchain_ancestry.cpp index c226da230..36c17357a 100644 --- a/src/blockchain_utilities/blockchain_ancestry.cpp +++ b/src/blockchain_utilities/blockchain_ancestry.cpp @@ -37,11 +37,7 @@ #include "common/command_line.h" #include "common/varint.h" #include "cryptonote_basic/cryptonote_boost_serialization.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" -#include "blockchain_db/blockchain_db.h" -#include "blockchain_and_pool.h" #include "version.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/blockchain_utilities/blockchain_depth.cpp b/src/blockchain_utilities/blockchain_depth.cpp index 4ebc90fb9..f49211233 100644 --- a/src/blockchain_utilities/blockchain_depth.cpp +++ b/src/blockchain_utilities/blockchain_depth.cpp @@ -31,11 +31,7 @@ #include <boost/algorithm/string.hpp> #include "common/command_line.h" #include "common/varint.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" -#include "blockchain_db/blockchain_db.h" -#include "blockchain_and_pool.h" #include "version.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp index 890edba09..1f8370034 100644 --- a/src/blockchain_utilities/blockchain_export.cpp +++ b/src/blockchain_utilities/blockchain_export.cpp @@ -28,9 +28,7 @@ #include "bootstrap_file.h" #include "blocksdat_file.h" -#include "blockchain_and_pool.h" #include "common/command_line.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" #include "blockchain_db/blockchain_db.h" #include "version.h" diff --git a/src/blockchain_utilities/blockchain_prune.cpp b/src/blockchain_utilities/blockchain_prune.cpp index 28e8d2cbe..1a9618617 100644 --- a/src/blockchain_utilities/blockchain_prune.cpp +++ b/src/blockchain_utilities/blockchain_prune.cpp @@ -33,11 +33,8 @@ #include <boost/system/error_code.hpp> #include <boost/filesystem.hpp> #include "common/command_line.h" -#include "blockchain_and_pool.h" #include "common/pruning.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" -#include "blockchain_db/blockchain_db.h" #include "blockchain_db/lmdb/db_lmdb.h" #include "version.h" diff --git a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp index e0a853fbf..4a459dc66 100644 --- a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp +++ b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp @@ -30,11 +30,7 @@ #include <boost/filesystem.hpp> #include "common/command_line.h" #include "serialization/crypto.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" -#include "blockchain_db/blockchain_db.h" -#include "blockchain_and_pool.h" #include "version.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/blockchain_utilities/blockchain_stats.cpp b/src/blockchain_utilities/blockchain_stats.cpp index c2fae4039..f65054fc5 100644 --- a/src/blockchain_utilities/blockchain_stats.cpp +++ b/src/blockchain_utilities/blockchain_stats.cpp @@ -31,10 +31,7 @@ #include "common/command_line.h" #include "common/varint.h" #include "cryptonote_basic/cryptonote_boost_serialization.h" -#include "blockchain_and_pool.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" #include "blockchain_db/blockchain_db.h" #include "version.h" diff --git a/src/blockchain_utilities/blockchain_usage.cpp b/src/blockchain_utilities/blockchain_usage.cpp index d2ecf2abf..0b9686765 100644 --- a/src/blockchain_utilities/blockchain_usage.cpp +++ b/src/blockchain_utilities/blockchain_usage.cpp @@ -31,11 +31,7 @@ #include <boost/filesystem/path.hpp> #include "common/command_line.h" #include "common/varint.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/blockchain.h" -#include "blockchain_db/blockchain_db.h" -#include "blockchain_and_pool.h" #include "version.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index a45d3ec60..3ad051fc3 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -113,13 +113,6 @@ namespace cryptonote }; /** - * @brief Blockchain constructor - * - * @param tx_pool a reference to the transaction pool to be kept by the Blockchain - */ - Blockchain(tx_memory_pool& tx_pool); - - /** * @brief Blockchain destructor */ ~Blockchain(); @@ -1236,6 +1229,13 @@ namespace cryptonote mutable rct_ver_cache_t m_rct_ver_cache; /** + * @brief Blockchain constructor + * + * @param tx_pool a reference to the transaction pool to be kept by the Blockchain + */ + Blockchain(tx_memory_pool& tx_pool); + + /** * @brief collects the keys for all outputs being "spent" as an input * * This function makes sure that each "input" in an input (mixins) exists @@ -1608,5 +1608,7 @@ namespace cryptonote * @param already_generated_coins total coins mined by the network so far */ void send_miner_notifications(uint64_t height, const crypto::hash &seed_hash, const crypto::hash &prev_id, uint64_t already_generated_coins); + + friend class BlockchainAndPool; }; } // namespace cryptonote diff --git a/src/blockchain_utilities/blockchain_and_pool.h b/src/cryptonote_core/blockchain_and_pool.h index 8d26c7a61..c0f607f64 100644 --- a/src/blockchain_utilities/blockchain_and_pool.h +++ b/src/cryptonote_core/blockchain_and_pool.h @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2020, The Monero Project +// Copyright (c) 2023, The Monero Project // // All rights reserved. // @@ -33,12 +33,26 @@ #pragma once -#include "cryptonote_core/blockchain.h" -#include "cryptonote_core/tx_pool.h" +#include <memory> -struct BlockchainAndPool { - cryptonote::Blockchain blockchain; - cryptonote::tx_memory_pool tx_pool; +#include "blockchain.h" +#include "tx_pool.h" - BlockchainAndPool() : blockchain(tx_pool), tx_pool(blockchain) {} +namespace cryptonote +{ +/** + * @brief Container for safely constructing Blockchain and tx_memory_pool classes + * + * The reason for this class existing is that the constructors for both Blockchain and + * tx_memory_pool take a reference for tx_memory_pool and Blockchain, respectively. Because of this + * circular reference, it is annoying/unsafe to construct these normally. This class guarantees that + * we don't make any silly mistakes with pointers / dangling references. + */ +struct BlockchainAndPool +{ + Blockchain blockchain; + tx_memory_pool tx_pool; + + BlockchainAndPool(): blockchain(tx_pool), tx_pool(blockchain) {} }; +} diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 7b0c9e495..a7745ff58 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -221,8 +221,9 @@ namespace cryptonote //----------------------------------------------------------------------------------------------- core::core(i_cryptonote_protocol* pprotocol): - m_mempool(m_blockchain_storage), - m_blockchain_storage(m_mempool), + m_bap(), + m_mempool(m_bap.tx_pool), + m_blockchain_storage(m_bap.blockchain), m_miner(this, [this](const cryptonote::block &b, uint64_t height, const crypto::hash *seed_hash, unsigned int threads, crypto::hash &hash) { return cryptonote::get_block_longhash(&m_blockchain_storage, b, hash, height, seed_hash, threads); }), diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index e0655dfa2..8108dfae0 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -42,8 +42,7 @@ #include "cryptonote_protocol/enums.h" #include "common/download.h" #include "common/command_line.h" -#include "tx_pool.h" -#include "blockchain.h" +#include "blockchain_and_pool.h" #include "cryptonote_basic/miner.h" #include "cryptonote_basic/connection_context.h" #include "warnings.h" @@ -1098,8 +1097,9 @@ namespace cryptonote uint64_t m_test_drop_download_height = 0; //!< height under which to drop incoming blocks, if doing so - tx_memory_pool m_mempool; //!< transaction pool instance - Blockchain m_blockchain_storage; //!< Blockchain instance + BlockchainAndPool m_bap; //! Contains owned instances of Blockchain and tx_memory_pool + tx_memory_pool& m_mempool; //!< ref to transaction pool instance in m_bap + Blockchain& m_blockchain_storage; //!< ref to Blockchain instance in m_bap i_cryptonote_protocol* m_pprotocol; //!< cryptonote protocol instance diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h index 6fe2eea59..47268efb6 100644 --- a/src/cryptonote_core/tx_pool.h +++ b/src/cryptonote_core/tx_pool.h @@ -98,14 +98,6 @@ namespace cryptonote { public: /** - * @brief Constructor - * - * @param bchs a Blockchain class instance, for getting chain info - */ - tx_memory_pool(Blockchain& bchs); - - - /** * @copydoc add_tx(transaction&, tx_verification_context&, bool, bool, uint8_t) * * @param id the transaction's hash @@ -489,6 +481,13 @@ namespace cryptonote private: /** + * @brief Constructor + * + * @param bchs a Blockchain class instance, for getting chain info + */ + tx_memory_pool(Blockchain& bchs); + + /** * @brief insert key images into m_spent_key_images * * @return true on success, false on error @@ -676,6 +675,8 @@ private: //! Next timestamp that a DB check for relayable txes is allowed std::atomic<time_t> m_next_check; + + friend class BlockchainAndPool; }; } |