blockchain_and_pool: move to crytonote_core and enforce its usage

This commit is contained in:
jeffro256 2023-06-30 14:52:43 -05:00
parent d6f86e58a6
commit ffbf9f4766
No known key found for this signature in database
GPG Key ID: 6F79797A6E392442
17 changed files with 64 additions and 90 deletions

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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) {}
};
}

View File

@ -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);
}),

View File

@ -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

View File

@ -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<time_t> m_next_check;
friend class BlockchainAndPool;
};
}

View File

@ -30,8 +30,6 @@
#include <stdio.h>
#include <math.h>
#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<cryptonote::Blockchain> bc; \
cryptonote::tx_memory_pool txpool(*bc); \
bc.reset(new cryptonote::Blockchain(txpool)); \
struct get_test_options { \
const std::pair<uint8_t, uint64_t> 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) \
{ \

View File

@ -143,9 +143,8 @@ namespace
}
static std::unique_ptr<cryptonote::Blockchain> init_blockchain(const std::vector<test_event_entry> & events, cryptonote::network_type nettype)
static std::unique_ptr<cryptonote::BlockchainAndPool> init_blockchain(const std::vector<test_event_entry> & events, cryptonote::network_type nettype)
{
std::unique_ptr<cryptonote::Blockchain> 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<cryptonote::Blockchain> 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<cryptonote::BlockchainAndPool> 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<cryptonote::Blockchain> 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<block_info>& 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<cryptonote::Blockchain> bc;
std::unique_ptr<cryptonote::BlockchainAndPool> 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;
}
}

View File

@ -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<uint8_t, uint64_t> hard_forks[3]; \
const cryptonote::test_options test_options = { \

View File

@ -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<uint64_t> &distribution, uint64_t &base)
{
std::unique_ptr<cryptonote::Blockchain> bc;
cryptonote::tx_memory_pool txpool(*bc);
bc.reset(new cryptonote::Blockchain(txpool));
struct get_test_options {
const std::pair<uint8_t, uint64_t> 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)

View File

@ -50,9 +50,6 @@ public:
}
#define PREFIX_WINDOW(hf_version,window) \
std::unique_ptr<cryptonote::Blockchain> bc; \
cryptonote::tx_memory_pool txpool(*bc); \
bc.reset(new cryptonote::Blockchain(txpool)); \
struct get_test_options { \
const std::pair<uint8_t, uint64_t> 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)