From 008ba966da88f073f226b299533faca905ceabf8 Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Wed, 17 Jan 2024 17:17:16 -0600 Subject: blockchain sync: reduce disk writes from 2 to 1 per tx --- src/cryptonote_core/blockchain.h | 56 +++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'src/cryptonote_core/blockchain.h') diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 42246fca2..675bcdb9b 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -91,6 +91,7 @@ namespace cryptonote */ typedef std::function(cryptonote::network_type network)> GetCheckpointsCallback; + typedef boost::function)> TxpoolNotifyCallback; typedef boost::function /* blocks */)> BlockNotifyCallback; typedef boost::function& /* tx_backlog */)> MinerNotifyCallback; @@ -254,6 +255,16 @@ namespace cryptonote */ bool prepare_handle_incoming_blocks(const std::vector &blocks_entry, std::vector &blocks); + /** + * @brief prepare the blockchain for handling an incoming block, without performing preprocessing + * + * @param block_byte_estimate an estimate of the byte size of the block & its transactions + * + * This function should *always* be followed up by a call to cleanup_handle_incoming_blocks() + * later in the same thread. + */ + void prepare_handle_incoming_block_no_preprocess(const size_t block_byte_estimate); + /** * @brief incoming blocks post-processing, cleanup, and disk sync * @@ -351,11 +362,15 @@ namespace cryptonote * * @param bl_ the block to be added * @param bvc metadata about the block addition's success/failure + * @param extra_block_txs txs belonging to this block that may not be in the mempool * * @return true on successful addition to the blockchain, else false */ bool add_new_block(const block& bl_, block_verification_context& bvc); + bool add_new_block(const block& bl_, block_verification_context& bvc, + pool_supplement& extra_block_txs); + /** * @brief clears the blockchain and starts a new one * @@ -705,10 +720,13 @@ namespace cryptonote * * @param tx the transaction to check the outputs of * @param tvc returned info about tx verification + * @param hf_version hard fork version * * @return false if any outputs do not conform, otherwise true */ - bool check_tx_outputs(const transaction& tx, tx_verification_context &tvc) const; + static bool check_tx_outputs(const transaction& tx, + tx_verification_context &tvc, + std::uint8_t hf_version); /** * @brief gets the block weight limit based on recent blocks @@ -822,6 +840,13 @@ namespace cryptonote void set_user_options(uint64_t maxthreads, bool sync_on_blocks, uint64_t sync_threshold, blockchain_db_sync_mode sync_mode, bool fast_sync); + /** + * @brief sets a txpool notify object to call for every new tx used to add a new block + * + * @param notify the notify object to call at every new tx used to add a new block + */ + void set_txpool_notify(TxpoolNotifyCallback&& notify); + /** * @brief sets a block notify object to call for every new block * @@ -843,6 +868,11 @@ namespace cryptonote */ void set_reorg_notify(const std::shared_ptr ¬ify) { m_reorg_notify = notify; } + /** + * @brief Notify this Blockchain's txpool notifier about a txpool event + */ + void notify_txpool_event(std::vector&& event) const; + /** * @brief Put DB in safe sync mode */ @@ -1076,13 +1106,6 @@ namespace cryptonote void cancel(); - /** - * @brief called when we see a tx originating from a block - * - * Used for handling txes from historical blocks in a fast way - */ - void on_new_tx_from_block(const cryptonote::transaction &tx); - /** * @brief returns the timestamps of the last N blocks */ @@ -1159,7 +1182,6 @@ namespace cryptonote // Keccak hashes for each block and for fast pow checking std::vector> m_blocks_hash_of_hashes; std::vector> m_blocks_hash_check; - std::vector m_blocks_txs_check; blockchain_db_sync_mode m_db_sync_mode; bool m_fast_sync; @@ -1219,6 +1241,9 @@ namespace cryptonote bool m_batch_success; + TxpoolNotifyCallback m_txpool_notifier; + mutable std::mutex m_txpool_notifier_mutex; + /* `boost::function` is used because the implementation never allocates if the callable object has a single `std::shared_ptr` or `std::weap_ptr` internally. Whereas, the libstdc++ `std::function` will allocate. */ @@ -1332,11 +1357,10 @@ namespace cryptonote * * @param bl the block to be added * @param bvc metadata concerning the block's validity - * @param notify if set to true, sends new block notification on success * * @return true if the block was added successfully, otherwise false */ - bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc, bool notify = true); + bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc); /** * @brief validate and add a new block to the end of the blockchain @@ -1348,11 +1372,12 @@ namespace cryptonote * @param bl the block to be added * @param id the hash of the block * @param bvc metadata concerning the block's validity - * @param notify if set to true, sends new block notification on success + * @param extra_block_txs txs belonging to this block that may not be in the mempool * * @return true if the block was added successfully, otherwise false */ - bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc, bool notify = true); + bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, + block_verification_context& bvc, pool_supplement& extra_block_txs); /** * @brief validate and add a new block to an alternate blockchain @@ -1364,10 +1389,12 @@ namespace cryptonote * @param b the block to be added * @param id the hash of the block * @param bvc metadata concerning the block's validity + * @param extra_block_txs txs belonging to this block that may not be in the mempool * * @return true if the block was added successfully, otherwise false */ - bool handle_alternative_block(const block& b, const crypto::hash& id, block_verification_context& bvc); + bool handle_alternative_block(const block& b, const crypto::hash& id, + block_verification_context& bvc, pool_supplement& extra_block_txs); /** * @brief builds a list of blocks connecting a block to the main chain @@ -1552,7 +1579,6 @@ namespace cryptonote * @return true */ bool update_next_cumulative_weight_limit(uint64_t *long_term_effective_median_block_weight = NULL); - void return_tx_to_pool(std::vector> &txs); /** * @brief make sure a transaction isn't attempting a double-spend -- cgit v1.2.3