Skip to content

Commit

Permalink
Rename TreeSyncMemory -> TreeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
j-berman committed Jan 20, 2025
1 parent 57cc635 commit 61c3b9c
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/fcmp_pp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
56 changes: 28 additions & 28 deletions src/fcmp_pp/tree_sync_memory.cpp → src/fcmp_pp/tree_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -791,7 +791,7 @@ static std::vector<typename C_PARENT::Scalar> get_layer_last_chunk_children_to_r
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
bool TreeSyncMemory<C1, C2>::register_output(const OutputPair &output, const uint64_t unlock_block_idx)
bool TreeCache<C1, C2>::register_output(const OutputPair &output, const uint64_t unlock_block_idx)
{
if (!m_cached_blocks.empty())
{
Expand All @@ -815,10 +815,10 @@ bool TreeSyncMemory<C1, C2>::register_output(const OutputPair &output, const uin
}

// Explicit instantiation
template bool TreeSyncMemory<Selene, Helios>::register_output(const OutputPair &output, const uint64_t unlock_block_idx);
template bool TreeCache<Selene, Helios>::register_output(const OutputPair &output, const uint64_t unlock_block_idx);
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::sync_block(const uint64_t block_idx,
void TreeCache<C1, C2>::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)
Expand All @@ -840,13 +840,13 @@ void TreeSyncMemory<C1, C2>::sync_block(const uint64_t block_idx,
}

// Explicit instantiation
template void TreeSyncMemory<Selene, Helios>::sync_block(const uint64_t block_idx,
template void TreeCache<Selene, Helios>::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<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::sync_blocks(const uint64_t start_block_idx,
void TreeCache<C1, C2>::sync_blocks(const uint64_t start_block_idx,
const crypto::hash &prev_block_hash,
const std::vector<crypto::hash> &new_block_hashes,
const std::vector<fcmp_pp::curve_trees::OutputsByUnlockBlock> &outs_by_unlock_blocks,
Expand Down Expand Up @@ -964,15 +964,15 @@ void TreeSyncMemory<C1, C2>::sync_blocks(const uint64_t start_block_idx,
}

// Explicit instantiation
template void TreeSyncMemory<Selene, Helios>::sync_blocks(const uint64_t start_block_idx,
template void TreeCache<Selene, Helios>::sync_blocks(const uint64_t start_block_idx,
const crypto::hash &prev_block_hash,
const std::vector<crypto::hash> &new_block_hashes,
const std::vector<fcmp_pp::curve_trees::OutputsByUnlockBlock> &outs_by_unlock_blocks,
typename fcmp_pp::curve_trees::CurveTrees<Selene, Helios>::TreeExtension &tree_extension_out,
std::vector<uint64_t> &n_new_leaf_tuples_per_block_out);
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::process_synced_blocks(const uint64_t start_block_idx,
void TreeCache<C1, C2>::process_synced_blocks(const uint64_t start_block_idx,
const std::vector<crypto::hash> &new_block_hashes,
const typename fcmp_pp::curve_trees::CurveTrees<C1, C2>::TreeExtension &tree_extension,
const std::vector<uint64_t> &n_new_leaf_tuples_per_block)
Expand Down Expand Up @@ -1102,13 +1102,13 @@ void TreeSyncMemory<C1, C2>::process_synced_blocks(const uint64_t start_block_id
LOG_ERROR("Cached blocks exceeded max reorg depth");
}

template void TreeSyncMemory<Selene, Helios>::process_synced_blocks(const uint64_t start_block_idx,
template void TreeCache<Selene, Helios>::process_synced_blocks(const uint64_t start_block_idx,
const std::vector<crypto::hash> &new_block_hashes,
const typename fcmp_pp::curve_trees::CurveTrees<Selene, Helios>::TreeExtension &tree_extension,
const std::vector<uint64_t> &n_new_leaf_tuples_per_block);
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
bool TreeSyncMemory<C1, C2>::pop_block()
bool TreeCache<C1, C2>::pop_block()
{
if (m_cached_blocks.empty())
return false;
Expand Down Expand Up @@ -1208,10 +1208,10 @@ bool TreeSyncMemory<C1, C2>::pop_block()
}

// Explicit instantiation
template bool TreeSyncMemory<Selene, Helios>::pop_block();
template bool TreeCache<Selene, Helios>::pop_block();
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
bool TreeSyncMemory<C1, C2>::get_output_path(const OutputPair &output,
bool TreeCache<C1, C2>::get_output_path(const OutputPair &output,
typename CurveTrees<C1, C2>::Path &path_out) const
{
path_out.clear();
Expand Down Expand Up @@ -1296,12 +1296,12 @@ bool TreeSyncMemory<C1, C2>::get_output_path(const OutputPair &output,
}

// Explicit instantiation
template bool TreeSyncMemory<Selene, Helios>::get_output_path(const OutputPair &output,
template bool TreeCache<Selene, Helios>::get_output_path(const OutputPair &output,
CurveTrees<Selene, Helios>::Path &path_out) const;
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::init(const uint64_t start_block_idx,
void TreeCache<C1, C2>::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,
Expand Down Expand Up @@ -1399,14 +1399,14 @@ void TreeSyncMemory<C1, C2>::init(const uint64_t start_block_idx,
}

// Explicit instantiation
template void TreeSyncMemory<Selene, Helios>::init(const uint64_t start_block_idx,
template void TreeCache<Selene, Helios>::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<typename C1, typename C2>
crypto::ec_point TreeSyncMemory<C1, C2>::get_tree_root() const
crypto::ec_point TreeCache<C1, C2>::get_tree_root() const
{
CHECK_AND_ASSERT_THROW_MES(!m_cached_blocks.empty(), "empty cache");

Expand All @@ -1430,20 +1430,20 @@ crypto::ec_point TreeSyncMemory<C1, C2>::get_tree_root() const
}

// Explicit instantiation
template crypto::ec_point TreeSyncMemory<Selene, Helios>::get_tree_root() const;
template crypto::ec_point TreeCache<Selene, Helios>::get_tree_root() const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
uint64_t TreeSyncMemory<C1, C2>::get_n_leaf_tuples() const
uint64_t TreeCache<C1, C2>::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<Selene, Helios>::get_n_leaf_tuples() const;
template uint64_t TreeCache<Selene, Helios>::get_n_leaf_tuples() const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::clear()
void TreeCache<C1, C2>::clear()
{
m_locked_outputs.clear();
m_locked_output_refs.clear();
Expand All @@ -1453,11 +1453,11 @@ void TreeSyncMemory<C1, C2>::clear()
m_tree_elem_cache.clear();
m_cached_blocks.clear();
}
template void TreeSyncMemory<Selene, Helios>::clear();
template void TreeCache<Selene, Helios>::clear();
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
typename CurveTrees<C1, C2>::LastHashes TreeSyncMemory<C1, C2>::get_last_hashes(const uint64_t n_leaf_tuples) const
typename CurveTrees<C1, C2>::LastHashes TreeCache<C1, C2>::get_last_hashes(const uint64_t n_leaf_tuples) const
{
MDEBUG("Getting last hashes on tree with " << n_leaf_tuples << " leaf tuples");

Expand Down Expand Up @@ -1507,11 +1507,11 @@ typename CurveTrees<C1, C2>::LastHashes TreeSyncMemory<C1, C2>::get_last_hashes(
}

// Explicit instantiation
template CurveTrees<Selene, Helios>::LastHashes TreeSyncMemory<Selene, Helios>::get_last_hashes(
template CurveTrees<Selene, Helios>::LastHashes TreeCache<Selene, Helios>::get_last_hashes(
const uint64_t n_leaf_tuples) const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
typename CurveTrees<C1, C2>::LastChunkChildrenForTrim TreeSyncMemory<C1, C2>::get_last_chunk_children_to_regrow(
typename CurveTrees<C1, C2>::LastChunkChildrenForTrim TreeCache<C1, C2>::get_last_chunk_children_to_regrow(
const std::vector<TrimLayerInstructions> &trim_instructions) const
{
typename CurveTrees<C1, C2>::LastChunkChildrenForTrim all_children_to_regrow;
Expand Down Expand Up @@ -1601,11 +1601,11 @@ typename CurveTrees<C1, C2>::LastChunkChildrenForTrim TreeSyncMemory<C1, C2>::ge
}

template
CurveTrees<Selene, Helios>::LastChunkChildrenForTrim TreeSyncMemory<Selene, Helios>::get_last_chunk_children_to_regrow(
CurveTrees<Selene, Helios>::LastChunkChildrenForTrim TreeCache<Selene, Helios>::get_last_chunk_children_to_regrow(
const std::vector<TrimLayerInstructions> &trim_instructions) const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
typename CurveTrees<C1, C2>::LastHashes TreeSyncMemory<C1, C2>::get_last_hashes_for_trim(
typename CurveTrees<C1, C2>::LastHashes TreeCache<C1, C2>::get_last_hashes_for_trim(
const std::vector<TrimLayerInstructions> &trim_instructions) const
{
typename CurveTrees<C1, C2>::LastHashes last_hashes;
Expand Down Expand Up @@ -1671,11 +1671,11 @@ typename CurveTrees<C1, C2>::LastHashes TreeSyncMemory<C1, C2>::get_last_hashes_
}

// Explicit instantiation
template CurveTrees<Selene, Helios>::LastHashes TreeSyncMemory<Selene, Helios>::get_last_hashes_for_trim(
template CurveTrees<Selene, Helios>::LastHashes TreeCache<Selene, Helios>::get_last_hashes_for_trim(
const std::vector<TrimLayerInstructions> &trim_instructions) const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
void TreeSyncMemory<C1, C2>::deque_block(const uint64_t n_leaf_tuples_at_block)
void TreeCache<C1, C2>::deque_block(const uint64_t n_leaf_tuples_at_block)
{
if (n_leaf_tuples_at_block == 0)
return;
Expand Down
22 changes: 11 additions & 11 deletions src/fcmp_pp/tree_sync_memory.h → src/fcmp_pp/tree_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@ using ChildChunkCache = std::unordered_map<ChildChunkIdx, CachedTreeElemChunk>
// TODO: technically this can be a vector. There should *always* be at least 1 entry for every layer
using TreeElemCache = std::unordered_map<LayerIdx, ChildChunkCache>;

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<typename C1, typename C2>
class TreeSyncMemory final : public TreeSync<C1, C2>
class TreeCache final : public TreeSync<C1, C2>
{
public:
TreeSyncMemory(std::shared_ptr<CurveTrees<C1, C2>> curve_trees,
TreeCache(std::shared_ptr<CurveTrees<C1, C2>> curve_trees,
const uint64_t max_reorg_depth = ORPHANED_BLOCKS_MAX_COUNT):
TreeSync<C1, C2>(curve_trees, max_reorg_depth)
{};
Expand Down Expand Up @@ -250,7 +250,7 @@ class TreeSyncMemory final : public TreeSync<C1, C2>
// 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
Expand Down Expand Up @@ -281,7 +281,7 @@ class TreeSyncMemory final : public TreeSync<C1, C2>
}

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)
Expand All @@ -304,14 +304,14 @@ namespace boost
namespace serialization
{
template<typename C1, typename C2>
struct version<fcmp_pp::curve_trees::TreeSyncMemory<C1, C2>>
struct version<fcmp_pp::curve_trees::TreeCache<C1, C2>>
{
typedef mpl::int_<fcmp_pp::curve_trees::TREE_SYNC_MEMORY_VERSION> type;
typedef mpl::int_<fcmp_pp::curve_trees::TREE_CACHE_VERSION> 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_<fcmp_pp::curve_trees::TREE_SYNC_MEMORY_VERSION>,
boost::mpl::int_<fcmp_pp::curve_trees::TREE_CACHE_VERSION>,
boost::mpl::int_<256>
>
));
Expand Down
Loading

0 comments on commit 61c3b9c

Please sign in to comment.