From d6f86e58a6f0e489651af044ad872f9d0e798887 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 5 Nov 2021 14:35:08 +0100 Subject: [PATCH 1/2] Avoid nullptr dereference when constructing Blockchain and tx_memory_pool --- .../blockchain_ancestry.cpp | 9 ++-- .../blockchain_and_pool.h | 44 +++++++++++++++++++ src/blockchain_utilities/blockchain_depth.cpp | 15 ++----- .../blockchain_export.cpp | 19 +++----- src/blockchain_utilities/blockchain_prune.cpp | 24 ++++------ .../blockchain_prune_known_spent_data.cpp | 10 ++--- src/blockchain_utilities/blockchain_stats.cpp | 10 ++--- src/blockchain_utilities/blockchain_usage.cpp | 12 ++--- 8 files changed, 83 insertions(+), 60 deletions(-) create mode 100644 src/blockchain_utilities/blockchain_and_pool.h diff --git a/src/blockchain_utilities/blockchain_ancestry.cpp b/src/blockchain_utilities/blockchain_ancestry.cpp index 66dd7813b..c226da230 100644 --- a/src/blockchain_utilities/blockchain_ancestry.cpp +++ b/src/blockchain_utilities/blockchain_ancestry.cpp @@ -41,6 +41,7 @@ #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 @@ -449,9 +450,7 @@ int main(int argc, char* argv[]) // because unlike blockchain_storage constructor, which takes a pointer to // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); - std::unique_ptr core_storage; - tx_memory_pool m_mempool(*core_storage); - core_storage.reset(new Blockchain(m_mempool)); + std::unique_ptr core_storage = std::make_unique(); BlockchainDB *db = new_db(); if (db == NULL) { @@ -472,7 +471,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, net_type); + r = core_storage->blockchain.init(db, net_type); CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); @@ -716,7 +715,7 @@ int main(int argc, char* argv[]) } done: - core_storage->deinit(); + core_storage->blockchain.deinit(); if (opt_show_cache_stats) MINFO("cache: txes " << std::to_string(cached_txes*100./total_txes) diff --git a/src/blockchain_utilities/blockchain_and_pool.h b/src/blockchain_utilities/blockchain_and_pool.h new file mode 100644 index 000000000..8d26c7a61 --- /dev/null +++ b/src/blockchain_utilities/blockchain_and_pool.h @@ -0,0 +1,44 @@ +// Copyright (c) 2014-2020, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list +// of conditions and the following disclaimer in the documentation and/or +// other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors +// may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include "cryptonote_core/blockchain.h" +#include "cryptonote_core/tx_pool.h" + +struct BlockchainAndPool { + cryptonote::Blockchain blockchain; + cryptonote::tx_memory_pool tx_pool; + + BlockchainAndPool() : blockchain(tx_pool), tx_pool(blockchain) {} +}; diff --git a/src/blockchain_utilities/blockchain_depth.cpp b/src/blockchain_utilities/blockchain_depth.cpp index 6a06e0a96..4ebc90fb9 100644 --- a/src/blockchain_utilities/blockchain_depth.cpp +++ b/src/blockchain_utilities/blockchain_depth.cpp @@ -35,6 +35,7 @@ #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 @@ -129,16 +130,8 @@ int main(int argc, char* argv[]) // Use Blockchain instead of lower-level BlockchainDB for two reasons: // 1. Blockchain has the init() method for easy setup // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash() - // - // cannot match blockchain_storage setup above with just one line, - // e.g. - // Blockchain* core_storage = new Blockchain(NULL); - // because unlike blockchain_storage constructor, which takes a pointer to - // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); - std::unique_ptr core_storage; - tx_memory_pool m_mempool(*core_storage); - core_storage.reset(new Blockchain(m_mempool)); + std::unique_ptr core_storage = std::make_unique(); BlockchainDB *db = new_db(); if (db == NULL) { @@ -159,7 +152,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, net_type); + r = core_storage->blockchain.init(db, net_type); CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); @@ -327,7 +320,7 @@ done: LOG_PRINT_L0("Average min depth for " << start_txids.size() << " transaction(s): " << cumulative_depth/(float)depths.size()); LOG_PRINT_L0("Median min depth for " << start_txids.size() << " transaction(s): " << epee::misc_utils::median(depths)); - core_storage->deinit(); + core_storage->blockchain.deinit(); return 0; CATCH_ENTRY("Depth query error", 1); diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp index 3d7b3f61a..890edba09 100644 --- a/src/blockchain_utilities/blockchain_export.cpp +++ b/src/blockchain_utilities/blockchain_export.cpp @@ -28,6 +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" @@ -129,16 +130,8 @@ int main(int argc, char* argv[]) // Use Blockchain instead of lower-level BlockchainDB for two reasons: // 1. Blockchain has the init() method for easy setup // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash() - // - // cannot match blockchain_storage setup above with just one line, - // e.g. - // Blockchain* core_storage = new Blockchain(NULL); - // because unlike blockchain_storage constructor, which takes a pointer to - // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); - Blockchain* core_storage = NULL; - tx_memory_pool m_mempool(*core_storage); - core_storage = new Blockchain(m_mempool); + std::unique_ptr core_storage = std::make_unique(); BlockchainDB* db = new_db(); if (db == NULL) @@ -162,9 +155,9 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, opt_testnet ? cryptonote::TESTNET : opt_stagenet ? cryptonote::STAGENET : cryptonote::MAINNET); + r = core_storage->blockchain.init(db, opt_testnet ? cryptonote::TESTNET : opt_stagenet ? cryptonote::STAGENET : cryptonote::MAINNET); - if (core_storage->get_blockchain_pruning_seed() && !opt_blocks_dat) + if (core_storage->blockchain.get_blockchain_pruning_seed() && !opt_blocks_dat) { LOG_PRINT_L0("Blockchain is pruned, cannot export"); return 1; @@ -177,12 +170,12 @@ int main(int argc, char* argv[]) if (opt_blocks_dat) { BlocksdatFile blocksdat; - r = blocksdat.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop); + r = blocksdat.store_blockchain_raw(&core_storage->blockchain, NULL, output_file_path, block_stop); } else { BootstrapFile bootstrap; - r = bootstrap.store_blockchain_raw(core_storage, NULL, output_file_path, block_start, block_stop); + r = bootstrap.store_blockchain_raw(&core_storage->blockchain, NULL, output_file_path, block_start, block_stop); } CHECK_AND_ASSERT_MES(r, 1, "Failed to export blockchain raw data"); LOG_PRINT_L0("Blockchain raw data exported OK"); diff --git a/src/blockchain_utilities/blockchain_prune.cpp b/src/blockchain_utilities/blockchain_prune.cpp index 1e4b48b73..28e8d2cbe 100644 --- a/src/blockchain_utilities/blockchain_prune.cpp +++ b/src/blockchain_utilities/blockchain_prune.cpp @@ -33,6 +33,7 @@ #include #include #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" @@ -562,22 +563,15 @@ int main(int argc, char* argv[]) // Use Blockchain instead of lower-level BlockchainDB for two reasons: // 1. Blockchain has the init() method for easy setup // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash() - // - // cannot match blockchain_storage setup above with just one line, - // e.g. - // Blockchain* core_storage = new Blockchain(NULL); - // because unlike blockchain_storage constructor, which takes a pointer to - // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. MINFO("Initializing source blockchain (BlockchainDB)"); - std::array, 2> core_storage; - Blockchain *blockchain = NULL; - tx_memory_pool m_mempool(*blockchain); + std::array, 2> core_storage{ + std::make_unique(), + std::make_unique()}; + boost::filesystem::path paths[2]; bool already_pruned = false; for (size_t n = 0; n < core_storage.size(); ++n) { - core_storage[n].reset(new Blockchain(m_mempool)); - BlockchainDB* db = new_db(); if (db == NULL) { @@ -622,12 +616,12 @@ int main(int argc, char* argv[]) MERROR("Error opening database: " << e.what()); return 1; } - r = core_storage[n]->init(db, net_type); + r = core_storage[n]->blockchain.init(db, net_type); std::string source_dest = n == 0 ? "source" : "pruned"; CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize " << source_dest << " blockchain storage"); MINFO(source_dest << " blockchain storage initialized OK"); - if (n == 0 && core_storage[0]->get_blockchain_pruning_seed()) + if (n == 0 && core_storage[0]->blockchain.get_blockchain_pruning_seed()) { if (!opt_copy_pruned_database) { @@ -637,9 +631,9 @@ int main(int argc, char* argv[]) already_pruned = true; } } - core_storage[0]->deinit(); + core_storage[0]->blockchain.deinit(); core_storage[0].reset(NULL); - core_storage[1]->deinit(); + core_storage[1]->blockchain.deinit(); core_storage[1].reset(NULL); MINFO("Pruning..."); diff --git a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp index 4da9c15c1..e0a853fbf 100644 --- a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp +++ b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp @@ -34,6 +34,7 @@ #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 @@ -160,9 +161,8 @@ int main(int argc, char* argv[]) const std::string input = command_line::get_arg(vm, arg_input); LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); - std::unique_ptr core_storage; - tx_memory_pool m_mempool(*core_storage); - core_storage.reset(new Blockchain(m_mempool)); + std::unique_ptr core_storage = std::make_unique(); + BlockchainDB *db = new_db(); if (db == NULL) { @@ -182,7 +182,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, net_type); + r = core_storage->blockchain.init(db, net_type); CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); @@ -280,7 +280,7 @@ int main(int argc, char* argv[]) MINFO("Prunable outputs: " << num_prunable_outputs); LOG_PRINT_L0("Blockchain known spent data pruned OK"); - core_storage->deinit(); + core_storage->blockchain.deinit(); return 0; CATCH_ENTRY("Error", 1); diff --git a/src/blockchain_utilities/blockchain_stats.cpp b/src/blockchain_utilities/blockchain_stats.cpp index 5e4245ebd..c2fae4039 100644 --- a/src/blockchain_utilities/blockchain_stats.cpp +++ b/src/blockchain_utilities/blockchain_stats.cpp @@ -31,6 +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" @@ -203,9 +204,8 @@ int main(int argc, char* argv[]) do_diff = command_line::get_arg(vm, arg_diff); LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); - std::unique_ptr core_storage; - tx_memory_pool m_mempool(*core_storage); - core_storage.reset(new Blockchain(m_mempool)); + std::unique_ptr core_storage = std::make_unique(); + BlockchainDB *db = new_db(); if (db == NULL) { @@ -225,7 +225,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, net_type); + r = core_storage->blockchain.init(db, net_type); CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); @@ -381,7 +381,7 @@ plot 'stats.csv' index "DATA" using (timecolumn(1,"%Y-%m-%d")):4 with lines, '' if (currblks) doprint(); - core_storage->deinit(); + core_storage->blockchain.deinit(); return 0; CATCH_ENTRY("Stats reporting error", 1); diff --git a/src/blockchain_utilities/blockchain_usage.cpp b/src/blockchain_utilities/blockchain_usage.cpp index a5228eb92..d2ecf2abf 100644 --- a/src/blockchain_utilities/blockchain_usage.cpp +++ b/src/blockchain_utilities/blockchain_usage.cpp @@ -35,6 +35,7 @@ #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 @@ -151,9 +152,8 @@ int main(int argc, char* argv[]) // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); const std::string input = command_line::get_arg(vm, arg_input); - std::unique_ptr core_storage; - tx_memory_pool m_mempool(*core_storage); - core_storage.reset(new Blockchain(m_mempool)); + std::unique_ptr core_storage = std::make_unique(); + BlockchainDB* db = new_db(); if (db == NULL) { @@ -174,7 +174,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, net_type); + r = core_storage->blockchain.init(db, net_type); CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); @@ -185,10 +185,10 @@ int main(int argc, char* argv[]) std::unordered_map indices; LOG_PRINT_L0("Reading blockchain from " << input); - core_storage->for_all_transactions([&](const crypto::hash &hash, const cryptonote::transaction &tx)->bool + core_storage->blockchain.for_all_transactions([&](const crypto::hash &hash, const cryptonote::transaction &tx)->bool { const bool coinbase = tx.vin.size() == 1 && tx.vin[0].type() == typeid(txin_gen); - const uint64_t height = core_storage->get_db().get_tx_block_height(hash); + const uint64_t height = core_storage->blockchain.get_db().get_tx_block_height(hash); // create new outputs for (const auto &out: tx.vout) From ffbf9f4766945f33b80366c414a5721ce90e5a2e Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Fri, 30 Jun 2023 14:52:43 -0500 Subject: [PATCH 2/2] blockchain_and_pool: move to crytonote_core and enforce its usage --- .../blockchain_ancestry.cpp | 4 --- src/blockchain_utilities/blockchain_depth.cpp | 4 --- .../blockchain_export.cpp | 2 -- src/blockchain_utilities/blockchain_prune.cpp | 3 -- .../blockchain_prune_known_spent_data.cpp | 4 --- src/blockchain_utilities/blockchain_stats.cpp | 3 -- src/blockchain_utilities/blockchain_usage.cpp | 4 --- src/cryptonote_core/blockchain.h | 16 ++++++----- .../blockchain_and_pool.h | 28 ++++++++++++++----- src/cryptonote_core/cryptonote_core.cpp | 5 ++-- src/cryptonote_core/cryptonote_core.h | 8 +++--- src/cryptonote_core/tx_pool.h | 17 +++++------ tests/block_weight/block_weight.cpp | 9 ++---- tests/core_tests/chaingen.cpp | 17 +++++------ tests/unit_tests/long_term_block_weight.cpp | 11 ++------ tests/unit_tests/output_distribution.cpp | 12 ++------ tests/unit_tests/scaling_2021.cpp | 7 ++--- 17 files changed, 64 insertions(+), 90 deletions(-) rename src/{blockchain_utilities => cryptonote_core}/blockchain_and_pool.h (68%) 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 #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 #include #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 #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 #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 @@ -112,13 +112,6 @@ namespace cryptonote uint64_t already_generated_coins; //!< the total coins minted after that block }; - /** - * @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 */ @@ -1235,6 +1228,13 @@ namespace cryptonote // cache for verifying transaction RCT non semantics 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 * @@ -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 similarity index 68% rename from src/blockchain_utilities/blockchain_and_pool.h rename to 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 -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 @@ -97,14 +97,6 @@ namespace cryptonote class tx_memory_pool: boost::noncopyable { 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) * @@ -488,6 +480,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 * @@ -676,6 +675,8 @@ private: //! Next timestamp that a DB check for relayable txes is allowed std::atomic m_next_check; + + friend class BlockchainAndPool; }; } diff --git a/tests/block_weight/block_weight.cpp b/tests/block_weight/block_weight.cpp index 7cd0d572b..4b00fc63f 100644 --- a/tests/block_weight/block_weight.cpp +++ b/tests/block_weight/block_weight.cpp @@ -30,8 +30,6 @@ #include #include -#include "cryptonote_core/blockchain.h" -#include "cryptonote_core/tx_pool.h" #include "cryptonote_core/cryptonote_core.h" #include "blockchain_db/testdb.h" @@ -110,9 +108,6 @@ private: } #define PREFIX_WINDOW(hf_version,window) \ - std::unique_ptr bc; \ - cryptonote::tx_memory_pool txpool(*bc); \ - bc.reset(new cryptonote::Blockchain(txpool)); \ struct get_test_options { \ const std::pair hard_forks[3]; \ const cryptonote::test_options test_options = { \ @@ -121,7 +116,9 @@ private: }; \ get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)LONG_TERM_BLOCK_WEIGHT_WINDOW), std::make_pair((uint8_t)0, (uint64_t)0)} {} \ } opts; \ - cryptonote::Blockchain *blockchain = bc.get(); \ + cryptonote::BlockchainAndPool bap; \ + cryptonote::Blockchain *blockchain = &bap.blockchain; \ + cryptonote::Blockchain *bc = blockchain; \ bool r = blockchain->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \ if (!r) \ { \ diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 9f6fe5387..b66aa7924 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -143,9 +143,8 @@ namespace } -static std::unique_ptr init_blockchain(const std::vector & events, cryptonote::network_type nettype) +static std::unique_ptr init_blockchain(const std::vector & events, cryptonote::network_type nettype) { - std::unique_ptr bc; v_hardforks_t hardforks; cryptonote::test_options test_options_tmp{nullptr, 0}; const cryptonote::test_options * test_options = &test_options_tmp; @@ -159,10 +158,8 @@ static std::unique_ptr init_blockchain(const std::vector test_options_tmp.hard_forks = hardforks.data(); test_options = &test_options_tmp; - cryptonote::tx_memory_pool txpool(*bc); - bc.reset(new cryptonote::Blockchain(txpool)); + std::unique_ptr bap(new BlockchainAndPool()); - cryptonote::Blockchain *blockchain = bc.get(); auto bdb = new TestDB(); BOOST_FOREACH(const test_event_entry &ev, events) @@ -177,9 +174,9 @@ static std::unique_ptr init_blockchain(const std::vector bdb->add_block(*blk, 1, 1, 1, 0, 0, blk_hash); } - bool r = blockchain->init(bdb, nettype, true, test_options, 2, nullptr); + bool r = bap->blockchain.init(bdb, nettype, true, test_options, 2, nullptr); CHECK_AND_ASSERT_THROW_MES(r, "could not init blockchain from events"); - return bc; + return bap; } void test_generator::get_block_chain(std::vector& blockchain, const crypto::hash& head, size_t n) const @@ -393,7 +390,7 @@ bool test_generator::construct_block_manually_tx(cryptonote::block& blk, const c void test_generator::fill_nonce(cryptonote::block& blk, const difficulty_type& diffic, uint64_t height) { const cryptonote::Blockchain *blockchain = nullptr; - std::unique_ptr bc; + std::unique_ptr bap; if (blk.major_version >= RX_BLOCK_VERSION && diffic > 1) { @@ -403,8 +400,8 @@ void test_generator::fill_nonce(cryptonote::block& blk, const difficulty_type& d } else { - bc = init_blockchain(*m_events, m_nettype); - blockchain = bc.get(); + bap = init_blockchain(*m_events, m_nettype); + blockchain = &bap->blockchain; } } diff --git a/tests/unit_tests/long_term_block_weight.cpp b/tests/unit_tests/long_term_block_weight.cpp index b9ce2b247..e52bd1955 100644 --- a/tests/unit_tests/long_term_block_weight.cpp +++ b/tests/unit_tests/long_term_block_weight.cpp @@ -106,16 +106,9 @@ static uint32_t lcg() } -struct BlockchainAndPool -{ - cryptonote::tx_memory_pool txpool; - cryptonote::Blockchain bc; - BlockchainAndPool(): txpool(bc), bc(txpool) {} -}; - #define PREFIX_WINDOW(hf_version,window) \ - BlockchainAndPool bap; \ - cryptonote::Blockchain *bc = &bap.bc; \ + cryptonote::BlockchainAndPool bap; \ + cryptonote::Blockchain *bc = &bap.blockchain; \ struct get_test_options { \ const std::pair hard_forks[3]; \ const cryptonote::test_options test_options = { \ diff --git a/tests/unit_tests/output_distribution.cpp b/tests/unit_tests/output_distribution.cpp index ab474a7d8..038c19874 100644 --- a/tests/unit_tests/output_distribution.cpp +++ b/tests/unit_tests/output_distribution.cpp @@ -30,10 +30,7 @@ #include "gtest/gtest.h" #include "misc_log_ex.h" #include "rpc/rpc_handler.h" -#include "blockchain_db/blockchain_db.h" #include "cryptonote_core/cryptonote_core.h" -#include "cryptonote_core/tx_pool.h" -#include "cryptonote_core/blockchain.h" #include "blockchain_db/testdb.h" static const uint64_t test_distribution[32] = { @@ -77,9 +74,6 @@ public: bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector &distribution, uint64_t &base) { - std::unique_ptr bc; - cryptonote::tx_memory_pool txpool(*bc); - bc.reset(new cryptonote::Blockchain(txpool)); struct get_test_options { const std::pair hard_forks[2]; const cryptonote::test_options test_options = { @@ -87,9 +81,9 @@ bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64 }; get_test_options():hard_forks{std::make_pair((uint8_t)1, (uint64_t)0), std::make_pair((uint8_t)0, (uint64_t)0)}{} } opts; - cryptonote::Blockchain *blockchain = bc.get(); - bool r = blockchain->init(new TestDB(test_distribution_size), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); - return r && bc->get_output_distribution(amount, from, to, start_height, distribution, base); + cryptonote::BlockchainAndPool bap; + bool r = bap.blockchain.init(new TestDB(test_distribution_size), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); + return r && bap.blockchain.get_output_distribution(amount, from, to, start_height, distribution, base); } crypto::hash get_block_hash(uint64_t height) diff --git a/tests/unit_tests/scaling_2021.cpp b/tests/unit_tests/scaling_2021.cpp index 024a4b4fd..d90f0f9e6 100644 --- a/tests/unit_tests/scaling_2021.cpp +++ b/tests/unit_tests/scaling_2021.cpp @@ -50,9 +50,6 @@ public: } #define PREFIX_WINDOW(hf_version,window) \ - std::unique_ptr bc; \ - cryptonote::tx_memory_pool txpool(*bc); \ - bc.reset(new cryptonote::Blockchain(txpool)); \ struct get_test_options { \ const std::pair hard_forks[3]; \ const cryptonote::test_options test_options = { \ @@ -61,7 +58,9 @@ public: }; \ get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)1), std::make_pair((uint8_t)0, (uint64_t)0)} {} \ } opts; \ - cryptonote::Blockchain *blockchain = bc.get(); \ + cryptonote::BlockchainAndPool bap; \ + cryptonote::Blockchain *blockchain = &bap.blockchain; \ + cryptonote::Blockchain *bc = blockchain; \ bool r = blockchain->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \ ASSERT_TRUE(r)