From 61c3b9c76202963dfe17e35f3f3888f8b45af42d Mon Sep 17 00:00:00 2001 From: j-berman Date: Mon, 20 Jan 2025 15:13:34 -0800 Subject: [PATCH] Rename TreeSyncMemory -> TreeCache --- src/fcmp_pp/CMakeLists.txt | 2 +- .../{tree_sync_memory.cpp => tree_cache.cpp} | 56 +++---- .../{tree_sync_memory.h => tree_cache.h} | 22 +-- src/wallet/wallet2.cpp | 54 +++--- src/wallet/wallet2.h | 12 +- tests/unit_tests/CMakeLists.txt | 2 +- .../{tree_sync_memory.cpp => tree_cache.cpp} | 158 +++++++++--------- 7 files changed, 153 insertions(+), 153 deletions(-) rename src/fcmp_pp/{tree_sync_memory.cpp => tree_cache.cpp} (97%) rename src/fcmp_pp/{tree_sync_memory.h => tree_cache.h} (93%) rename tests/unit_tests/{tree_sync_memory.cpp => tree_cache.cpp} (83%) diff --git a/src/fcmp_pp/CMakeLists.txt b/src/fcmp_pp/CMakeLists.txt index 82323dd1082..1da2be64334 100644 --- a/src/fcmp_pp/CMakeLists.txt +++ b/src/fcmp_pp/CMakeLists.txt @@ -30,7 +30,7 @@ set(fcmp_pp_sources curve_trees.cpp fcmp_pp_crypto.cpp tower_cycle.cpp - tree_sync_memory.cpp) + tree_cache.cpp) monero_find_all_headers(fcmp_pp_headers "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/fcmp_pp/tree_sync_memory.cpp b/src/fcmp_pp/tree_cache.cpp similarity index 97% rename from src/fcmp_pp/tree_sync_memory.cpp rename to src/fcmp_pp/tree_cache.cpp index a2d33f52b07..aa8157f2ab9 100644 --- a/src/fcmp_pp/tree_sync_memory.cpp +++ b/src/fcmp_pp/tree_cache.cpp @@ -26,7 +26,7 @@ // 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. -#include "tree_sync_memory.h" +#include "tree_cache.h" #include "common/merge_sorted_vectors.h" #include "misc_log_ex.h" @@ -791,7 +791,7 @@ static std::vector get_layer_last_chunk_children_to_r //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- template -bool TreeSyncMemory::register_output(const OutputPair &output, const uint64_t unlock_block_idx) +bool TreeCache::register_output(const OutputPair &output, const uint64_t unlock_block_idx) { if (!m_cached_blocks.empty()) { @@ -815,10 +815,10 @@ bool TreeSyncMemory::register_output(const OutputPair &output, const uin } // Explicit instantiation -template bool TreeSyncMemory::register_output(const OutputPair &output, const uint64_t unlock_block_idx); +template bool TreeCache::register_output(const OutputPair &output, const uint64_t unlock_block_idx); //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::sync_block(const uint64_t block_idx, +void TreeCache::sync_block(const uint64_t block_idx, const crypto::hash &block_hash, const crypto::hash &prev_block_hash, const fcmp_pp::curve_trees::OutputsByUnlockBlock &outs_by_unlock_block) @@ -840,13 +840,13 @@ void TreeSyncMemory::sync_block(const uint64_t block_idx, } // Explicit instantiation -template void TreeSyncMemory::sync_block(const uint64_t block_idx, +template void TreeCache::sync_block(const uint64_t block_idx, const crypto::hash &block_hash, const crypto::hash &prev_block_hash, const fcmp_pp::curve_trees::OutputsByUnlockBlock &outs_by_unlock_block); //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::sync_blocks(const uint64_t start_block_idx, +void TreeCache::sync_blocks(const uint64_t start_block_idx, const crypto::hash &prev_block_hash, const std::vector &new_block_hashes, const std::vector &outs_by_unlock_blocks, @@ -964,7 +964,7 @@ void TreeSyncMemory::sync_blocks(const uint64_t start_block_idx, } // Explicit instantiation -template void TreeSyncMemory::sync_blocks(const uint64_t start_block_idx, +template void TreeCache::sync_blocks(const uint64_t start_block_idx, const crypto::hash &prev_block_hash, const std::vector &new_block_hashes, const std::vector &outs_by_unlock_blocks, @@ -972,7 +972,7 @@ template void TreeSyncMemory::sync_blocks(const uint64_t start_b std::vector &n_new_leaf_tuples_per_block_out); //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::process_synced_blocks(const uint64_t start_block_idx, +void TreeCache::process_synced_blocks(const uint64_t start_block_idx, const std::vector &new_block_hashes, const typename fcmp_pp::curve_trees::CurveTrees::TreeExtension &tree_extension, const std::vector &n_new_leaf_tuples_per_block) @@ -1102,13 +1102,13 @@ void TreeSyncMemory::process_synced_blocks(const uint64_t start_block_id LOG_ERROR("Cached blocks exceeded max reorg depth"); } -template void TreeSyncMemory::process_synced_blocks(const uint64_t start_block_idx, +template void TreeCache::process_synced_blocks(const uint64_t start_block_idx, const std::vector &new_block_hashes, const typename fcmp_pp::curve_trees::CurveTrees::TreeExtension &tree_extension, const std::vector &n_new_leaf_tuples_per_block); //---------------------------------------------------------------------------------------------------------------------- template -bool TreeSyncMemory::pop_block() +bool TreeCache::pop_block() { if (m_cached_blocks.empty()) return false; @@ -1208,10 +1208,10 @@ bool TreeSyncMemory::pop_block() } // Explicit instantiation -template bool TreeSyncMemory::pop_block(); +template bool TreeCache::pop_block(); //---------------------------------------------------------------------------------------------------------------------- template -bool TreeSyncMemory::get_output_path(const OutputPair &output, +bool TreeCache::get_output_path(const OutputPair &output, typename CurveTrees::Path &path_out) const { path_out.clear(); @@ -1296,12 +1296,12 @@ bool TreeSyncMemory::get_output_path(const OutputPair &output, } // Explicit instantiation -template bool TreeSyncMemory::get_output_path(const OutputPair &output, +template bool TreeCache::get_output_path(const OutputPair &output, CurveTrees::Path &path_out) const; //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::init(const uint64_t start_block_idx, +void TreeCache::init(const uint64_t start_block_idx, const crypto::hash &start_block_hash, const uint64_t n_leaf_tuples, const fcmp_pp::curve_trees::PathBytes &last_path, @@ -1399,14 +1399,14 @@ void TreeSyncMemory::init(const uint64_t start_block_idx, } // Explicit instantiation -template void TreeSyncMemory::init(const uint64_t start_block_idx, +template void TreeCache::init(const uint64_t start_block_idx, const crypto::hash &start_block_hash, const uint64_t n_leaf_tuples, const fcmp_pp::curve_trees::PathBytes &last_hashes, const OutputsByUnlockBlock &timelocked_outputs); //---------------------------------------------------------------------------------------------------------------------- template -crypto::ec_point TreeSyncMemory::get_tree_root() const +crypto::ec_point TreeCache::get_tree_root() const { CHECK_AND_ASSERT_THROW_MES(!m_cached_blocks.empty(), "empty cache"); @@ -1430,20 +1430,20 @@ crypto::ec_point TreeSyncMemory::get_tree_root() const } // Explicit instantiation -template crypto::ec_point TreeSyncMemory::get_tree_root() const; +template crypto::ec_point TreeCache::get_tree_root() const; //---------------------------------------------------------------------------------------------------------------------- template -uint64_t TreeSyncMemory::get_n_leaf_tuples() const +uint64_t TreeCache::get_n_leaf_tuples() const { CHECK_AND_ASSERT_THROW_MES(!m_cached_blocks.empty(), "empty cache"); return m_cached_blocks.back().n_leaf_tuples; } // Explicit instantiation -template uint64_t TreeSyncMemory::get_n_leaf_tuples() const; +template uint64_t TreeCache::get_n_leaf_tuples() const; //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::clear() +void TreeCache::clear() { m_locked_outputs.clear(); m_locked_output_refs.clear(); @@ -1453,11 +1453,11 @@ void TreeSyncMemory::clear() m_tree_elem_cache.clear(); m_cached_blocks.clear(); } -template void TreeSyncMemory::clear(); +template void TreeCache::clear(); //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- template -typename CurveTrees::LastHashes TreeSyncMemory::get_last_hashes(const uint64_t n_leaf_tuples) const +typename CurveTrees::LastHashes TreeCache::get_last_hashes(const uint64_t n_leaf_tuples) const { MDEBUG("Getting last hashes on tree with " << n_leaf_tuples << " leaf tuples"); @@ -1507,11 +1507,11 @@ typename CurveTrees::LastHashes TreeSyncMemory::get_last_hashes( } // Explicit instantiation -template CurveTrees::LastHashes TreeSyncMemory::get_last_hashes( +template CurveTrees::LastHashes TreeCache::get_last_hashes( const uint64_t n_leaf_tuples) const; //---------------------------------------------------------------------------------------------------------------------- template -typename CurveTrees::LastChunkChildrenForTrim TreeSyncMemory::get_last_chunk_children_to_regrow( +typename CurveTrees::LastChunkChildrenForTrim TreeCache::get_last_chunk_children_to_regrow( const std::vector &trim_instructions) const { typename CurveTrees::LastChunkChildrenForTrim all_children_to_regrow; @@ -1601,11 +1601,11 @@ typename CurveTrees::LastChunkChildrenForTrim TreeSyncMemory::ge } template -CurveTrees::LastChunkChildrenForTrim TreeSyncMemory::get_last_chunk_children_to_regrow( +CurveTrees::LastChunkChildrenForTrim TreeCache::get_last_chunk_children_to_regrow( const std::vector &trim_instructions) const; //---------------------------------------------------------------------------------------------------------------------- template -typename CurveTrees::LastHashes TreeSyncMemory::get_last_hashes_for_trim( +typename CurveTrees::LastHashes TreeCache::get_last_hashes_for_trim( const std::vector &trim_instructions) const { typename CurveTrees::LastHashes last_hashes; @@ -1671,11 +1671,11 @@ typename CurveTrees::LastHashes TreeSyncMemory::get_last_hashes_ } // Explicit instantiation -template CurveTrees::LastHashes TreeSyncMemory::get_last_hashes_for_trim( +template CurveTrees::LastHashes TreeCache::get_last_hashes_for_trim( const std::vector &trim_instructions) const; //---------------------------------------------------------------------------------------------------------------------- template -void TreeSyncMemory::deque_block(const uint64_t n_leaf_tuples_at_block) +void TreeCache::deque_block(const uint64_t n_leaf_tuples_at_block) { if (n_leaf_tuples_at_block == 0) return; diff --git a/src/fcmp_pp/tree_sync_memory.h b/src/fcmp_pp/tree_cache.h similarity index 93% rename from src/fcmp_pp/tree_sync_memory.h rename to src/fcmp_pp/tree_cache.h index 7a9e8ffad76..3d50ddec22f 100644 --- a/src/fcmp_pp/tree_sync_memory.h +++ b/src/fcmp_pp/tree_cache.h @@ -158,23 +158,23 @@ using ChildChunkCache = std::unordered_map // TODO: technically this can be a vector. There should *always* be at least 1 entry for every layer using TreeElemCache = std::unordered_map; -static const int TREE_SYNC_MEMORY_VERSION = 0; +static const int TREE_CACHE_VERSION = 0; //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- // Syncs the tree and keeps a user's known received outputs up to date, all saved in memory. // - The object does not store the entire tree locally. The object only stores what it needs in order to update paths // of known received outputs as it syncs. -// - The memory footprint of the TreeSyncMemory object is roughly ALL locked outputs in the chain, all known output -// paths, and the last chunk of tree elems at every layer of the tree the last N blocks. The latter is required to -// handle reorgs up to N blocks deep. +// - The memory footprint of the object is roughly ALL locked outputs in the chain, all known output paths, and the last +// chunk of tree elems at every layer of the tree the last N blocks. The latter is required to handle reorgs up to +// N blocks deep. // - WARNING: the implementation is not thread safe, it expects synchronous calls. // TODO: use a mutex to enforce thread safety. template -class TreeSyncMemory final : public TreeSync +class TreeCache final : public TreeSync { public: - TreeSyncMemory(std::shared_ptr> curve_trees, + TreeCache(std::shared_ptr> curve_trees, const uint64_t max_reorg_depth = ORPHANED_BLOCKS_MAX_COUNT): TreeSync(curve_trees, max_reorg_depth) {}; @@ -250,7 +250,7 @@ class TreeSyncMemory final : public TreeSync // Keep a global output counter so the caller knows how output id's should be set uint64_t m_output_count{0}; - // The outputs that TreeSyncMemory should keep track of while syncing + // The outputs that TreeCache should keep track of while syncing RegisteredOutputs m_registered_outputs; // Cached leaves and tree elems @@ -281,7 +281,7 @@ class TreeSyncMemory final : public TreeSync } BEGIN_SERIALIZE_OBJECT() - VERSION_FIELD(TREE_SYNC_MEMORY_VERSION) + VERSION_FIELD(TREE_CACHE_VERSION) FIELD(m_locked_outputs) FIELD(m_locked_output_refs) FIELD(m_output_count) @@ -304,14 +304,14 @@ namespace boost namespace serialization { template -struct version> +struct version> { - typedef mpl::int_ type; + typedef mpl::int_ type; typedef mpl::integral_c_tag tag; BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value); BOOST_MPL_ASSERT(( boost::mpl::less< - boost::mpl::int_, + boost::mpl::int_, boost::mpl::int_<256> > )); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index e3e3b262a2c..035f6c8bb2d 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1258,7 +1258,7 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended, std m_pool_info_query_time(0), m_has_ever_refreshed_from_node(false), m_allow_mismatched_daemon_version(false), - m_tree_sync(fcmp_pp::curve_trees::TreeSyncMemory( + m_tree_cache(fcmp_pp::curve_trees::TreeCache( fcmp_pp::curve_trees::curve_trees_v1(), m_max_reorg_depth)) { @@ -2556,7 +2556,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (!ignore_callbacks && 0 != m_callback) m_callback->on_money_received(height, txid, tx, td.m_amount, 0, td.m_subaddr_index, spends_one_of_ours(tx), td.m_tx.unlock_time); - m_tree_sync.register_output(output_pair, cryptonote::get_unlock_block_index(tx.unlock_time, height)); + m_tree_cache.register_output(output_pair, cryptonote::get_unlock_block_index(tx.unlock_time, height)); } total_received_1 += amount; notify = true; @@ -2635,7 +2635,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (!ignore_callbacks && 0 != m_callback) m_callback->on_money_received(height, txid, tx, td.m_amount, burnt, td.m_subaddr_index, spends_one_of_ours(tx), td.m_tx.unlock_time); - m_tree_sync.register_output(output_pair, cryptonote::get_unlock_block_index(tx.unlock_time, height)); + m_tree_cache.register_output(output_pair, cryptonote::get_unlock_block_index(tx.unlock_time, height)); } total_received_1 += extra_amount; notify = true; @@ -3146,7 +3146,7 @@ void wallet2::pull_blocks(bool first, bool try_incremental, uint64_t start_heigh req.prune = true; req.start_height = start_height; req.no_miner_tx = false; // always need the miner tx so we can grow the tree correctly - req.init_tree_sync = m_tree_sync.n_synced_blocks() == 0; + req.init_tree_sync = m_tree_cache.n_synced_blocks() == 0; MDEBUG("Pulling blocks: start_height " << start_height << " , init_tree_sync: " << req.init_tree_sync); @@ -3218,7 +3218,7 @@ struct TreeSyncStartParams crypto::hash prev_block_hash; }; -static void top_block_match(const hashchain &blockchain, const tools::wallet2::TreeSyncV1 &tree_sync) +static void top_block_match(const hashchain &blockchain, const tools::wallet2::TreeCacheV1 &tree_sync) { if (blockchain.empty()) { @@ -3236,11 +3236,11 @@ static void top_block_match(const hashchain &blockchain, const tools::wallet2::T "empty tree sync cache"); THROW_WALLET_EXCEPTION_IF(top_synced_block.blk_idx != m_blockchain_top_block_idx, - tools::error::wallet_internal_error, "m_blockchain <> m_tree_sync top block idx mismatch (m_blockchain_top_block_idx: " + tools::error::wallet_internal_error, "m_blockchain <> m_tree_cache top block idx mismatch (m_blockchain_top_block_idx: " + std::to_string(m_blockchain_top_block_idx) + " vs tree top idx: " + std::to_string(top_synced_block.blk_idx) + ")"); THROW_WALLET_EXCEPTION_IF(top_synced_block.blk_hash != blockchain[m_blockchain_top_block_idx], tools::error::wallet_internal_error, - "m_blockchain <> m_tree_sync top block hash mismatch (m_blockchain: " + string_tools::pod_to_hex(blockchain[m_blockchain_top_block_idx]) + + "m_blockchain <> m_tree_cache top block hash mismatch (m_blockchain: " + string_tools::pod_to_hex(blockchain[m_blockchain_top_block_idx]) + " vs tree top hash: " + string_tools::pod_to_hex(top_synced_block.blk_hash) + ")"); } @@ -3266,7 +3266,7 @@ static void reorg_depth_check(const uint64_t reorg_blk_idx, const uint64_t start // first on the tree sync cache enables building the tree immediately (we extend // the tree from the cached tree, so the cached tree needs to be in the correct // state to extend the tree correctly). -static TreeSyncStartParams tree_sync_reorg_check(const uint64_t parsed_blocks_start_idx, const std::vector &parsed_blocks, const uint64_t n_blocks_already_synced, const hashchain &blockchain, const uint64_t max_reorg_depth, tools::wallet2::TreeSyncV1 &tree_sync_inout) +static TreeSyncStartParams tree_sync_reorg_check(const uint64_t parsed_blocks_start_idx, const std::vector &parsed_blocks, const uint64_t n_blocks_already_synced, const hashchain &blockchain, const uint64_t max_reorg_depth, tools::wallet2::TreeCacheV1 &tree_cache_inout) { const uint64_t n_downloaded_blocks = (uint64_t) parsed_blocks.size(); if (n_downloaded_blocks == 0) @@ -3277,10 +3277,10 @@ static TreeSyncStartParams tree_sync_reorg_check(const uint64_t parsed_blocks_st << ", n_downloaded_blocks: " << n_downloaded_blocks); // Make sure the tree sync cache matches wallet2's blockchain cache - top_block_match(blockchain, tree_sync_inout); + top_block_match(blockchain, tree_cache_inout); fcmp_pp::curve_trees::BlockMeta top_synced_block; - THROW_WALLET_EXCEPTION_IF(!tree_sync_inout.get_top_block(top_synced_block), tools::error::wallet_internal_error, + THROW_WALLET_EXCEPTION_IF(!tree_cache_inout.get_top_block(top_synced_block), tools::error::wallet_internal_error, "empty tree sync cache"); // Check if the new downloaded blocks are contiguous to the tree sync cache. @@ -3319,13 +3319,13 @@ static TreeSyncStartParams tree_sync_reorg_check(const uint64_t parsed_blocks_st THROW_WALLET_EXCEPTION_IF(sync_start_block_idx == 0, error::wallet_internal_error, "sync_start_block_idx must be > 0"); THROW_WALLET_EXCEPTION_IF(start_parsed_block_i == 0, error::wallet_internal_error, "start_parsed_block_i must be > 0"); - THROW_WALLET_EXCEPTION_IF(!tree_sync_inout.pop_block(), error::wallet_internal_error, "Failed to pop block from tree"); + THROW_WALLET_EXCEPTION_IF(!tree_cache_inout.pop_block(), error::wallet_internal_error, "Failed to pop block from tree"); --sync_start_block_idx; --start_parsed_block_i; prev_block_hash = parsed_blocks[start_parsed_block_i].block.prev_id; - THROW_WALLET_EXCEPTION_IF(!tree_sync_inout.get_top_block(top_synced_block), error::wallet_internal_error, + THROW_WALLET_EXCEPTION_IF(!tree_cache_inout.get_top_block(top_synced_block), error::wallet_internal_error, "empty tree sync cache"); } @@ -3336,7 +3336,7 @@ static TreeSyncStartParams tree_sync_reorg_check(const uint64_t parsed_blocks_st return TreeSyncStartParams { sync_start_block_idx, start_parsed_block_i, prev_block_hash }; } -static void tree_sync_blocks_async(const TreeSyncStartParams &tree_sync_start_params, const std::vector &parsed_blocks, tools::wallet2::TreeSyncV1 &tree_sync_inout, uint64_t &outs_by_unlock_time_ms_inout, uint64_t &sync_blocks_time_ms_inout, std::vector &new_block_hashes_out, fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension_out, std::vector &new_leaf_tuples_per_block_out) +static void tree_sync_blocks_async(const TreeSyncStartParams &tree_sync_start_params, const std::vector &parsed_blocks, tools::wallet2::TreeCacheV1 &tree_cache_inout, uint64_t &outs_by_unlock_time_ms_inout, uint64_t &sync_blocks_time_ms_inout, std::vector &new_block_hashes_out, fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension_out, std::vector &new_leaf_tuples_per_block_out) { TIME_MEASURE_START(sync_blocks_time); @@ -3361,7 +3361,7 @@ static void tree_sync_blocks_async(const TreeSyncStartParams &tree_sync_start_pa new_block_hashes_out.reserve(n_new_blocks); outs_by_unlock_blocks.reserve(n_new_blocks); - uint64_t first_output_id = tree_sync_inout.get_output_count(); + uint64_t first_output_id = tree_cache_inout.get_output_count(); for (size_t i = start_parsed_block_i; i < parsed_blocks.size(); ++i) { const uint64_t created_block_idx = sync_start_block_idx + (i - start_parsed_block_i); @@ -3383,7 +3383,7 @@ static void tree_sync_blocks_async(const TreeSyncStartParams &tree_sync_start_pa TIME_MEASURE_FINISH(collecting_outs_by_unlock_block); // Get a tree extension with the outputs that will unlock in this chunk of blocks - tree_sync_inout.sync_blocks(sync_start_block_idx, + tree_cache_inout.sync_blocks(sync_start_block_idx, prev_block_hash, new_block_hashes_out, outs_by_unlock_blocks, @@ -3428,7 +3428,7 @@ void wallet2::process_parsed_blocks(const uint64_t start_height, const std::vect // Check for reorg and handle it on the tree sync cache if necessary. The main // wallet handles reorgs separately after identifying receives. - const auto tree_sync_start_params = tree_sync_reorg_check(start_height, parsed_blocks, n_blocks_already_synced, m_blockchain, m_max_reorg_depth, m_tree_sync); + const auto tree_sync_start_params = tree_sync_reorg_check(start_height, parsed_blocks, n_blocks_already_synced, m_blockchain, m_max_reorg_depth, m_tree_cache); std::vector new_block_hashes; fcmp_pp::curve_trees::CurveTreesV1::TreeExtension tree_extension; @@ -3439,7 +3439,7 @@ void wallet2::process_parsed_blocks(const uint64_t start_height, const std::vect // the tree extension, saving any path elements we need for received outputs, // and throwing away excess tree elems we won't need to continue syncing. tpool.submit(&tree_sync_blocks_waiter, [this, &parsed_blocks, &new_block_hashes, &tree_extension, &new_leaf_tuples_per_block, tree_sync_start_params]() { - tree_sync_blocks_async(tree_sync_start_params, parsed_blocks, m_tree_sync, m_outs_by_unlock_time_ms, m_sync_blocks_time_ms, new_block_hashes, tree_extension, new_leaf_tuples_per_block); + tree_sync_blocks_async(tree_sync_start_params, parsed_blocks, m_tree_cache, m_outs_by_unlock_time_ms, m_sync_blocks_time_ms, new_block_hashes, tree_extension, new_leaf_tuples_per_block); }); for (size_t i = 0; i < blocks.size(); ++i) @@ -3626,10 +3626,10 @@ void wallet2::process_parsed_blocks(const uint64_t start_height, const std::vect LOG_PRINT_L3("Building the tree..."); THROW_WALLET_EXCEPTION_IF(!tree_sync_blocks_waiter.wait(), error::wallet_internal_error, "Exception in thread pool"); LOG_PRINT_L3("Done waiting on tree build"); - m_tree_sync.process_synced_blocks(tree_sync_start_params.start_block_idx, new_block_hashes, tree_extension, new_leaf_tuples_per_block); + m_tree_cache.process_synced_blocks(tree_sync_start_params.start_block_idx, new_block_hashes, tree_extension, new_leaf_tuples_per_block); - // Check for matching top block in m_blockchain <> m_tree_sync after sync - top_block_match(m_blockchain, m_tree_sync); + // Check for matching top block in m_blockchain <> m_tree_cache after sync + top_block_match(m_blockchain, m_tree_cache); } //---------------------------------------------------------------------------------------------------- void wallet2::refresh(bool trusted_daemon) @@ -3760,7 +3760,7 @@ void wallet2::pull_and_parse_next_blocks(bool first, bool try_incremental, uint6 for (auto &lo : init_tree_sync_data->locked_outputs) locked_outputs[lo.unlock_block] = std::move(lo.outputs); - m_tree_sync.init(init_block_idx, init_block_hash, init_tree_sync_data->n_leaf_tuples, init_tree_sync_data->last_path, locked_outputs); + m_tree_cache.init(init_block_idx, init_block_hash, init_tree_sync_data->n_leaf_tuples, init_tree_sync_data->last_path, locked_outputs); } } catch(...) @@ -4361,7 +4361,7 @@ void wallet2::refresh(bool trusted_daemon, uint64_t start_height, uint64_t & blo m_blockchain.clear(); const crypto::hash genesis_hash = get_block_hash(b); m_blockchain.push_back(genesis_hash); - m_tree_sync.clear(); + m_tree_cache.clear(); short_chain_history.clear(); get_short_chain_history(short_chain_history); fast_refresh(stop_height, blocks_start_height, short_chain_history, true); @@ -4681,7 +4681,7 @@ bool wallet2::clear() m_pool_info_query_time = 0; m_skip_to_height = 0; m_background_sync_data = background_sync_data_t{}; - m_tree_sync.clear(); + m_tree_cache.clear(); return true; } //---------------------------------------------------------------------------------------------------- @@ -4707,7 +4707,7 @@ void wallet2::clear_soft(bool keep_key_images) const crypto::hash genesis_hash = get_block_hash(b); m_blockchain.push_back(genesis_hash); m_last_block_reward = cryptonote::get_outs_money_amount(b.miner_tx); - m_tree_sync.clear(); + m_tree_cache.clear(); } //---------------------------------------------------------------------------------------------------- void wallet2::clear_user_data() @@ -5697,7 +5697,7 @@ void wallet2::setup_new_blockchain() const crypto::hash genesis_hash = get_block_hash(b); m_blockchain.push_back(genesis_hash); m_last_block_reward = cryptonote::get_outs_money_amount(b.miner_tx); - m_tree_sync.clear(); + m_tree_cache.clear(); add_subaddress_account(tr("Primary account")); } @@ -6716,7 +6716,7 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass } // We may have loaded a max reorg depth different than the default - m_tree_sync.set_max_reorg_depth(m_max_reorg_depth); + m_tree_cache.set_max_reorg_depth(m_max_reorg_depth); wallet_keys_unlocker unlocker(*this, m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only && !m_is_background_wallet, password); @@ -6742,7 +6742,7 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass generate_genesis(genesis); crypto::hash genesis_hash = get_block_hash(genesis); - if (m_tree_sync.n_synced_blocks() == 0 && !m_transfers.empty() && m_has_ever_refreshed_from_node) + if (m_tree_cache.n_synced_blocks() == 0 && !m_transfers.empty() && m_has_ever_refreshed_from_node) { // clear the wallet so that we re-sync the tree and get received output paths // TODO: may want to warn users that this is happening before doing it, esp. because it'll delete tx dests diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 48476c9f7b0..67a61fcf187 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -58,7 +58,7 @@ #include "crypto/chacha.h" #include "crypto/hash.h" #include "fcmp_pp/curve_trees.h" -#include "fcmp_pp/tree_sync_memory.h" +#include "fcmp_pp/tree_cache.h" #include "multisig/multisig_account.h" #include "ringct/rctTypes.h" #include "ringct/rctOps.h" @@ -899,7 +899,7 @@ namespace tools using Selene = fcmp_pp::curve_trees::Selene; using Helios = fcmp_pp::curve_trees::Helios; - using TreeSyncV1 = fcmp_pp::curve_trees::TreeSyncMemory; + using TreeCacheV1 = fcmp_pp::curve_trees::TreeCache; /*! * \brief Generates a wallet or restores one. Assumes the multisig setup @@ -1078,7 +1078,7 @@ namespace tools void explicit_refresh_from_block_height(bool expl) {m_explicit_refresh_from_block_height = expl;} bool explicit_refresh_from_block_height() const {return m_explicit_refresh_from_block_height;} - void max_reorg_depth(uint64_t depth) {m_max_reorg_depth = depth; m_tree_sync.set_max_reorg_depth(depth);} + void max_reorg_depth(uint64_t depth) {m_max_reorg_depth = depth; m_tree_cache.set_max_reorg_depth(depth);} uint64_t max_reorg_depth() const {return m_max_reorg_depth;} bool deinit(); @@ -1368,7 +1368,7 @@ namespace tools a & m_background_sync_data; if(ver < 32) return; - a & m_tree_sync; + a & m_tree_cache; } BEGIN_SERIALIZE_OBJECT() @@ -1414,7 +1414,7 @@ namespace tools FIELD(m_background_sync_data) if (version < 3) return true; - FIELD(m_tree_sync) + FIELD(m_tree_cache) END_SERIALIZE() /*! @@ -2061,7 +2061,7 @@ namespace tools bool m_processing_background_cache; background_sync_data_t m_background_sync_data; - TreeSyncV1 m_tree_sync; + TreeCacheV1 m_tree_cache; }; } BOOST_CLASS_VERSION(tools::wallet2, 32) diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 40d15c53afc..49ffa70f008 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -87,7 +87,7 @@ set(unit_tests_sources test_peerlist.cpp test_protocol_pack.cpp threadpool.cpp - tree_sync_memory.cpp + tree_cache.cpp tx_proof.cpp hardfork.cpp unbound.cpp diff --git a/tests/unit_tests/tree_sync_memory.cpp b/tests/unit_tests/tree_cache.cpp similarity index 83% rename from tests/unit_tests/tree_sync_memory.cpp rename to tests/unit_tests/tree_cache.cpp index 0a2a3e199c9..b051514ff37 100644 --- a/tests/unit_tests/tree_sync_memory.cpp +++ b/tests/unit_tests/tree_cache.cpp @@ -32,7 +32,7 @@ #include "cryptonote_basic/cryptonote_format_utils.h" #include "curve_trees.h" #include "fcmp_pp/curve_trees.h" -#include "fcmp_pp/tree_sync_memory.h" +#include "fcmp_pp/tree_cache.h" #include "serialization/binary_utils.h" #include "string_tools.h" @@ -52,11 +52,11 @@ static const std::size_t max_outputs_per_block = (2*selene_chunk_width)+1; \ //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, register_output) +TEST(tree_cache, register_output) { // 1. Init auto curve_trees = fcmp_pp::curve_trees::curve_trees_v1(); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); const std::size_t INIT_LEAVES = 10; auto outputs = test::generate_random_outputs(*curve_trees, 0, INIT_LEAVES); @@ -67,10 +67,10 @@ TEST(tree_sync, register_output) const auto output = outputs[0].output_pair; // 2. Register output - valid - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); // 3. Register same output again - already registered - ASSERT_FALSE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_FALSE(tree_cache->register_output(output, unlock_block_idx)); // 4. Register another output with the same output pubkey as existing, different commitment - valid auto output_new_commitment = output; @@ -79,30 +79,30 @@ TEST(tree_sync, register_output) ASSERT_EQ(output_new_commitment.output_pubkey, output.output_pubkey); ASSERT_NE(output_new_commitment.commitment, output.commitment); - ASSERT_TRUE(tree_sync->register_output(output_new_commitment, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output_new_commitment, unlock_block_idx)); // 5. Sync the block of outputs crypto::hash block_hash{0x01}; crypto::hash prev_block_hash{}; - tree_sync->sync_block(0, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(0, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); // 6. Sync 1 more block so the outputs unlock and enter the tree prev_block_hash = block_hash; block_hash = crypto::hash{0x02}; - tree_sync->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); // 7. Register a new output where we already synced the block output unlocks in - invalid const auto &new_output = test::generate_random_outputs(*curve_trees, INIT_LEAVES, 1).front().output_pair; - ASSERT_FALSE(tree_sync->register_output(new_output, unlock_block_idx)); + ASSERT_FALSE(tree_cache->register_output(new_output, unlock_block_idx)); } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, sync_block_simple) +TEST(tree_cache, sync_block_simple) { // 1. Init static const std::size_t INIT_LEAVES = 10; auto curve_trees = fcmp_pp::curve_trees::curve_trees_v1(); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); auto outputs = test::generate_random_outputs(*curve_trees, 0, INIT_LEAVES); CHECK_AND_ASSERT_THROW_MES(outputs.size() == INIT_LEAVES, "unexpected size of outputs"); @@ -112,21 +112,21 @@ TEST(tree_sync, sync_block_simple) const auto output = outputs[0].output_pair; // 2. Register output - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); // 3. Sync the block of outputs crypto::hash block_hash{0x01}; crypto::hash prev_block_hash{}; - tree_sync->sync_block(0, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(0, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); // 4. Sync 1 more block so the outputs unlock and enter the tree prev_block_hash = block_hash; block_hash = crypto::hash{0x02}; - tree_sync->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); // 5. Get the output's path in the tree CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); // If the c1 layer chunk width < INIT_LEAVES, the test won't use expected values below ASSERT_GE(curve_trees->m_c1_width, INIT_LEAVES); @@ -134,7 +134,7 @@ TEST(tree_sync, sync_block_simple) ASSERT_TRUE(curve_trees->audit_path(output_path, output, INIT_LEAVES)); } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, sync_n_chunks_of_blocks) +TEST(tree_cache, sync_n_chunks_of_blocks) { // 1. Init static const std::size_t INIT_LEAVES = 10; @@ -146,7 +146,7 @@ TEST(tree_sync, sync_n_chunks_of_blocks) const std::size_t rand_leaf = crypto::rand_idx(INIT_LEAVES); auto curve_trees = fcmp_pp::curve_trees::curve_trees_v1(); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); const crypto::hash mock_block_hash{}; @@ -181,7 +181,7 @@ TEST(tree_sync, sync_n_chunks_of_blocks) ASSERT_TRUE(unlock_block_idx > 0); // 3. Register output - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); // 4. Sync the chunks of blocks for (std::size_t i = 0; i < N_CHUNKS; ++i) @@ -191,41 +191,41 @@ TEST(tree_sync, sync_n_chunks_of_blocks) fcmp_pp::curve_trees::CurveTrees::TreeExtension tree_extension; std::vector n_new_leaf_tuples_per_block; - tree_sync->sync_blocks(start_block_idx, + tree_cache->sync_blocks(start_block_idx, mock_block_hash, chunks_of_block_hashes[i], chunks_of_outputs[i], tree_extension, n_new_leaf_tuples_per_block); - tree_sync->process_synced_blocks(start_block_idx, + tree_cache->process_synced_blocks(start_block_idx, chunks_of_block_hashes[i], tree_extension, n_new_leaf_tuples_per_block); // Audit the output's path in the tree CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); if (!output_path.empty()) { - ASSERT_TRUE(curve_trees->audit_path(output_path, output, tree_sync->get_n_leaf_tuples())); + ASSERT_TRUE(curve_trees->audit_path(output_path, output, tree_cache->get_n_leaf_tuples())); } } // 5. Make sure the output was actually added to the tree CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); ASSERT_TRUE(!output_path.empty()); } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, sync_n_blocks_register_n_outputs) +TEST(tree_cache, sync_n_blocks_register_n_outputs) { // Init static const std::size_t tree_depth = 6; INIT_SYNC_TEST(tree_depth); // Sync until we've synced all the leaves needed to get to the desired tree depth - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); uint64_t block_idx = 0; uint64_t n_outputs = 0; uint64_t n_unlocked_outputs = 0; @@ -251,12 +251,12 @@ TEST(tree_sync, sync_n_blocks_register_n_outputs) // Register the output const uint64_t unlock_block_idx = block_idx + 1; - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); // Sync the outputs generated above crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(std::size_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); n_unlocked_outputs = n_outputs; n_outputs += sync_n_outputs; @@ -265,7 +265,7 @@ TEST(tree_sync, sync_n_blocks_register_n_outputs) for (const auto &o : registered_outputs) { CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(o, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(o, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, o, n_unlocked_outputs)); } @@ -278,18 +278,18 @@ TEST(tree_sync, sync_n_blocks_register_n_outputs) // Sync 1 more empty block so all outputs unlock crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(std::size_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {}); // Check all registered output paths for (const auto &o : registered_outputs) { CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(o, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(o, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, o, n_unlocked_outputs)); } } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, sync_n_blocks_register_one_output) +TEST(tree_cache, sync_n_blocks_register_one_output) { // Init static const std::size_t tree_depth = 5; @@ -300,7 +300,7 @@ TEST(tree_sync, sync_n_blocks_register_one_output) for (std::size_t i = 0; i < n_leaves_needed; ++i) { LOG_PRINT_L1("Register output " << (i+1) << " / " << n_leaves_needed); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); fcmp_pp::curve_trees::OutputPair registered_output; bool registered = false; @@ -330,7 +330,7 @@ TEST(tree_sync, sync_n_blocks_register_one_output) auto output_to_register = i - n_outputs; const auto output = outputs[output_to_register].output_pair; - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); registered = true; registered_output = output; @@ -339,7 +339,7 @@ TEST(tree_sync, sync_n_blocks_register_one_output) // Sync the outputs generated above crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(uint64_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); n_unlocked_outputs = n_outputs; n_outputs += sync_n_outputs; @@ -348,7 +348,7 @@ TEST(tree_sync, sync_n_blocks_register_one_output) if (registered && !just_registered) { CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_unlocked_outputs)); } @@ -362,15 +362,15 @@ TEST(tree_sync, sync_n_blocks_register_one_output) // Sync 1 more empty block so all outputs unlock crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(std::size_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {}); CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_unlocked_outputs)); } } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, sync_past_max_reorg_depth) +TEST(tree_cache, sync_past_max_reorg_depth) { // This test is useful for making sure TreeSync syncs correctly even after syncing past the reorg depth. Internally // the class drops cached values from beyond the reorg depth. This test makes sure registered output paths still @@ -388,7 +388,7 @@ TEST(tree_sync, sync_past_max_reorg_depth) LOG_PRINT_L1("Register output " << (i+1) << " / " << n_leaves_needed); // Sync until we've synced all the leaves needed to get to the desired tree depth - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees, max_reorg_depth); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees, max_reorg_depth); uint64_t block_idx = 0; uint64_t n_outputs = 0; @@ -417,7 +417,7 @@ TEST(tree_sync, sync_past_max_reorg_depth) auto output_to_register = i - n_outputs; const auto output = outputs[output_to_register].output_pair; - ASSERT_TRUE(tree_sync->register_output(output, block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, block_idx)); registered = true; registered_output = output; @@ -426,7 +426,7 @@ TEST(tree_sync, sync_past_max_reorg_depth) // Sync the outputs generated above crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(uint64_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); n_unlocked_outputs = n_outputs; n_outputs += sync_n_outputs; @@ -435,7 +435,7 @@ TEST(tree_sync, sync_past_max_reorg_depth) if (registered && !just_registered) { CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_unlocked_outputs)); } @@ -449,15 +449,15 @@ TEST(tree_sync, sync_past_max_reorg_depth) // Sync 1 more empty block so all outputs unlock crypto::hash block_hash; crypto::cn_fast_hash(&block_idx, sizeof(std::size_t), block_hash); - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {}); CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_unlocked_outputs)); } } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, reorg_after_register) +TEST(tree_cache, reorg_after_register) { // Init static const std::size_t tree_depth = 5; @@ -468,7 +468,7 @@ TEST(tree_sync, reorg_after_register) for (std::size_t i = 0; i < n_leaves_needed; ++i) { LOG_PRINT_L1("Register output " << (i+1) << " / " << n_leaves_needed); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); fcmp_pp::curve_trees::OutputPair registered_output; bool registered = false; @@ -497,14 +497,14 @@ TEST(tree_sync, reorg_after_register) // Pop blocks until the included block is the top block while (cur_block_idx > (unlocked_block_idx + 1)) { - ASSERT_TRUE(tree_sync->pop_block()); + ASSERT_TRUE(tree_cache->pop_block()); --cur_block_idx; MDEBUG("cur_block_idx: " << cur_block_idx << " , n_outputs_unlocked(cur_block_idx): " << n_outputs_unlocked(cur_block_idx)); CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_outputs_unlocked(cur_block_idx))); } @@ -523,14 +523,14 @@ TEST(tree_sync, reorg_after_register) crypto::hash block_hash; crypto::cn_fast_hash(&cur_block_idx, sizeof(uint64_t), block_hash); const uint64_t unlock_block_idx = cur_block_idx + 1; - tree_sync->sync_block(cur_block_idx, + tree_cache->sync_block(cur_block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); ++cur_block_idx; CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_outputs_unlocked(cur_block_idx))); prev_block_hash = block_hash; @@ -558,7 +558,7 @@ TEST(tree_sync, reorg_after_register) auto output_to_register = i - n_outputs; const auto output = outputs[output_to_register].output_pair; - ASSERT_TRUE(tree_sync->register_output(output, block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, block_idx)); registered = true; registered_output = output; @@ -569,7 +569,7 @@ TEST(tree_sync, reorg_after_register) unlocked_block_hash = block_hash; // Sync the outputs generated above - tree_sync->sync_block(block_idx, + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); @@ -582,7 +582,7 @@ TEST(tree_sync, reorg_after_register) if (registered && !just_registered) { CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(registered_output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(registered_output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, registered_output, n_unlocked_outputs)); } @@ -595,12 +595,12 @@ TEST(tree_sync, reorg_after_register) } } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, register_after_reorg) +TEST(tree_cache, register_after_reorg) { // Init static const std::size_t tree_depth = 5; INIT_SYNC_TEST(tree_depth); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); // Sync until we reach expected tree depth uint64_t block_idx = 0; @@ -623,7 +623,7 @@ TEST(tree_sync, register_after_reorg) // Sync the outputs generated above const uint64_t unlock_block_idx = block_idx + 1; - tree_sync->sync_block(block_idx, + tree_cache->sync_block(block_idx, block_hash, block_hashes.empty() ? crypto::hash{} : block_hashes.back(), {{ unlock_block_idx, outputs }}); @@ -640,7 +640,7 @@ TEST(tree_sync, register_after_reorg) // Reorg 1 block // TODO: test reorg 1 block to block_idx blocks LOG_PRINT_L1("Popping 1 block"); - ASSERT_TRUE(tree_sync->pop_block()); + ASSERT_TRUE(tree_cache->pop_block()); --block_idx; block_hashes.pop_back(); n_outputs_synced_by_block.pop_back(); @@ -653,7 +653,7 @@ TEST(tree_sync, register_after_reorg) CHECK_AND_ASSERT_THROW_MES(outputs.size() == 1, "unexpected size of outputs"); const auto output = outputs[0].output_pair; - ASSERT_TRUE(tree_sync->register_output(output, block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, block_idx)); // Block metadata crypto::hash block_hash; @@ -661,7 +661,7 @@ TEST(tree_sync, register_after_reorg) // Sync the output generated above const uint64_t unlock_block_idx = block_idx + 1; - tree_sync->sync_block(block_idx, + tree_cache->sync_block(block_idx, block_hash, block_hashes.empty() ? crypto::hash{} : block_hashes.back(), {{ unlock_block_idx, outputs }}); @@ -670,62 +670,62 @@ TEST(tree_sync, register_after_reorg) // Output is not expected to enter the tree until next block CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); ASSERT_TRUE(output_path.leaves.empty() && output_path.c1_layers.empty() && output_path.c2_layers.empty()); // Sync 1 more block so the output unlocks and enters the tree crypto::cn_fast_hash(&unlock_block_idx, sizeof(uint64_t), block_hash); - tree_sync->sync_block(unlock_block_idx, block_hash, block_hashes.back(), {}); + tree_cache->sync_block(unlock_block_idx, block_hash, block_hashes.back(), {}); n_unlocked_outputs += outputs.size(); - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, output, n_unlocked_outputs)); } //---------------------------------------------------------------------------------------------------------------------- -TEST(tree_sync, serialization) +TEST(tree_cache, serialization) { // 1. Grow the tree with a registered output static const std::size_t INIT_LEAVES = 10; auto curve_trees = fcmp_pp::curve_trees::curve_trees_v1(); - auto tree_sync = new fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); + auto tree_cache = new fcmp_pp::curve_trees::TreeCache(curve_trees); auto outputs = test::generate_random_outputs(*curve_trees, 0, INIT_LEAVES); CHECK_AND_ASSERT_THROW_MES(outputs.size() == INIT_LEAVES, "unexpected size of outputs"); const uint64_t block_idx = 0; const uint64_t unlock_block_idx = 1; const auto output = outputs[0].output_pair; - ASSERT_TRUE(tree_sync->register_output(output, unlock_block_idx)); + ASSERT_TRUE(tree_cache->register_output(output, unlock_block_idx)); crypto::hash block_hash{0x01}; crypto::hash prev_block_hash{}; - tree_sync->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); + tree_cache->sync_block(block_idx, block_hash, prev_block_hash, {{ unlock_block_idx, outputs }}); - // 2. Serialize the tree_sync object + // 2. Serialize the tree_cache object std::string blob; - ASSERT_TRUE(serialization::dump_binary(*tree_sync, blob)); + ASSERT_TRUE(serialization::dump_binary(*tree_cache, blob)); // 3. Make sure the output is present in the serialized string const std::string blob_hex = epee::string_tools::buff_to_hex_nodelimer(blob); ASSERT_TRUE(blob_hex.find(epee::string_tools::pod_to_hex(output.output_pubkey)) != std::string::npos); ASSERT_TRUE(blob_hex.find(epee::string_tools::pod_to_hex(output.commitment)) != std::string::npos); - // 4. De-serialize the string into a new tree_sync2 object - auto tree_sync2 = fcmp_pp::curve_trees::TreeSyncMemory(curve_trees); - ASSERT_TRUE(serialization::parse_binary(blob, tree_sync2)); + // 4. De-serialize the string into a new tree_cache2 object + auto tree_cache2 = fcmp_pp::curve_trees::TreeCache(curve_trees); + ASSERT_TRUE(serialization::parse_binary(blob, tree_cache2)); - // 5. Sync 1 more block in tree_sync and tree_sync2 so the init outputs unlock and enter the trees + // 5. Sync 1 more block in tree_cache and tree_cache2 so the init outputs unlock and enter the trees prev_block_hash = block_hash; block_hash = crypto::hash{0x02}; - tree_sync->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); - tree_sync2.sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); + tree_cache->sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); + tree_cache2.sync_block(unlock_block_idx, block_hash, prev_block_hash, {}); - // 6. Make sure output's path is the same across both tree_sync and tree_sync2 + // 6. Make sure output's path is the same across both tree_cache and tree_cache2 CurveTreesV1::Path output_path; - ASSERT_TRUE(tree_sync->get_output_path(output, output_path)); + ASSERT_TRUE(tree_cache->get_output_path(output, output_path)); ASSERT_TRUE(curve_trees->audit_path(output_path, output, INIT_LEAVES)); CurveTreesV1::Path output_path2; - ASSERT_TRUE(tree_sync2.get_output_path(output, output_path2)); + ASSERT_TRUE(tree_cache2.get_output_path(output, output_path2)); // Checking the roots match and auditing path suffices for now, TODO: implement constant time eq // ASSERT_EQ(output_path, output_path2); auto get_root_str = [&curve_trees](const CurveTreesV1::Path &path) -> std::string @@ -740,8 +740,8 @@ TEST(tree_sync, serialization) ASSERT_TRUE(curve_trees->audit_path(output_path2, output, INIT_LEAVES)); // 7. Validate output counts - ASSERT_EQ(tree_sync->get_output_count(), INIT_LEAVES); - ASSERT_EQ(tree_sync2.get_output_count(), INIT_LEAVES); + ASSERT_EQ(tree_cache->get_output_count(), INIT_LEAVES); + ASSERT_EQ(tree_cache2.get_output_count(), INIT_LEAVES); } //---------------------------------------------------------------------------------------------------------------------- // TODO: test edge cases: duplicate output when syncing, mismatched prev block hash in sync_block