Remove copy in get_tree_extension and better named funcs

This commit is contained in:
j-berman 2024-07-25 16:30:50 -07:00
parent db12610d94
commit b585a7f408
7 changed files with 28 additions and 22 deletions

View File

@ -1780,7 +1780,7 @@ public:
// TODO: description and make private
virtual void grow_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees,
const std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &new_leaves) = 0;
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &&new_leaves) = 0;
virtual void trim_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees, const uint64_t trim_n_leaf_tuples) = 0;

View File

@ -846,8 +846,8 @@ void BlockchainLMDB::add_block(const block& blk, size_t block_weight, uint64_t l
}
// Grow the tree with outputs that unlock at this block height
const auto unlocked_leaf_tuples = this->get_locked_leaf_tuples_at_block_id(m_height);
this->grow_tree(fcmp::curve_trees::curve_trees_v1, unlocked_leaf_tuples);
auto unlocked_leaf_tuples = this->get_leaf_tuples_at_unlock_block_id(m_height);
this->grow_tree(fcmp::curve_trees::curve_trees_v1, std::move(unlocked_leaf_tuples));
// Now that we've used the unlocked leaves to grow the tree, we can delete them from the locked leaves table
this->del_locked_leaf_tuples_at_block_id(m_height);
@ -1364,7 +1364,7 @@ void BlockchainLMDB::remove_spent_key(const crypto::key_image& k_image)
}
void BlockchainLMDB::grow_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees,
const std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &new_leaves)
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &&new_leaves)
{
if (new_leaves.empty())
return;
@ -1384,7 +1384,7 @@ void BlockchainLMDB::grow_tree(const fcmp::curve_trees::CurveTreesV1 &curve_tree
const auto last_hashes = this->get_tree_last_hashes();
// Use the number of leaf tuples and the existing last hashes to get a struct we can use to extend the tree
const auto tree_extension = curve_trees.get_tree_extension(old_n_leaf_tuples, last_hashes, new_leaves);
const auto tree_extension = curve_trees.get_tree_extension(old_n_leaf_tuples, last_hashes, std::move(new_leaves));
// Insert the leaves
// TODO: grow_leaves
@ -2206,7 +2206,7 @@ bool BlockchainLMDB::audit_layer(const C_CHILD &c_child,
chunk_width);
}
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> BlockchainLMDB::get_locked_leaf_tuples_at_block_id(
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> BlockchainLMDB::get_leaf_tuples_at_unlock_block_id(
uint64_t block_id)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@ -6885,9 +6885,9 @@ void BlockchainLMDB::migrate_5_6()
}
}
// Get all the locked outputs at that height
const auto leaf_tuples = this->get_locked_leaf_tuples_at_block_id(i);
this->grow_tree(fcmp::curve_trees::curve_trees_v1, leaf_tuples);
// Get the leaf tuples that unlock at the given block
auto unlocked_leaf_tuples = this->get_leaf_tuples_at_unlock_block_id(i);
this->grow_tree(fcmp::curve_trees::curve_trees_v1, std::move(unlocked_leaf_tuples));
// Now that we've used the unlocked leaves to grow the tree, we can delete them from the locked leaves table
this->del_locked_leaf_tuples_at_block_id(i);

View File

@ -369,7 +369,7 @@ public:
// make private
virtual void grow_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees,
const std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &new_leaves);
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &&new_leaves);
virtual void trim_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees, const uint64_t trim_n_leaf_tuples);
@ -450,7 +450,7 @@ private:
const uint64_t child_chunk_idx,
const uint64_t chunk_width) const;
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> get_locked_leaf_tuples_at_block_id(uint64_t block_id);
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> get_leaf_tuples_at_unlock_block_id(uint64_t block_id);
void del_locked_leaf_tuples_at_block_id(uint64_t block_id);

View File

@ -117,7 +117,7 @@ public:
virtual void add_spent_key(const crypto::key_image& k_image) override {}
virtual void remove_spent_key(const crypto::key_image& k_image) override {}
virtual void grow_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees,
const std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &new_leaves) override {};
std::vector<fcmp::curve_trees::CurveTreesV1::LeafTupleContext> &&new_leaves) override {};
virtual void trim_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees, const uint64_t trim_n_leaf_tuples) override {};
virtual bool audit_tree(const fcmp::curve_trees::CurveTreesV1 &curve_trees, const uint64_t expected_n_leaf_tuples) const override { return false; };

View File

@ -732,7 +732,7 @@ template<typename C1, typename C2>
typename CurveTrees<C1, C2>::TreeExtension CurveTrees<C1, C2>::get_tree_extension(
const uint64_t old_n_leaf_tuples,
const LastHashes &existing_last_hashes,
const std::vector<LeafTupleContext> &new_leaf_tuples) const
std::vector<LeafTupleContext> &&new_leaf_tuples) const
{
TreeExtension tree_extension;
@ -748,15 +748,13 @@ typename CurveTrees<C1, C2>::TreeExtension CurveTrees<C1, C2>::get_tree_extensio
tree_extension.leaves.start_leaf_tuple_idx = grow_layer_instructions.old_total_children / LEAF_TUPLE_SIZE;
// Sort the leaves by order they appear in the chain
// TODO: don't copy here
std::vector<LeafTupleContext> sorted_leaf_tuples = new_leaf_tuples;
const auto sort_fn = [](const LeafTupleContext &a, const LeafTupleContext &b) { return a.output_id < b.output_id; };
std::sort(sorted_leaf_tuples.begin(), sorted_leaf_tuples.end(), sort_fn);
std::sort(new_leaf_tuples.begin(), new_leaf_tuples.end(), sort_fn);
// Copy the sorted leaves into the tree extension struct
// TODO: don't copy here
tree_extension.leaves.tuples.reserve(new_leaf_tuples.size());
for (const auto &leaf : sorted_leaf_tuples)
for (const auto &leaf : new_leaf_tuples)
{
tree_extension.leaves.tuples.emplace_back(LeafTuple{
.O_x = leaf.leaf_tuple.O_x,

View File

@ -238,7 +238,7 @@ public:
// leaves to add to the tree, and return a tree extension struct that can be used to extend a tree
TreeExtension get_tree_extension(const uint64_t old_n_leaf_tuples,
const LastHashes &existing_last_hashes,
const std::vector<LeafTupleContext> &new_leaf_tuples) const;
std::vector<LeafTupleContext> &&new_leaf_tuples) const;
// Get instructions useful for trimming all existing layers in the tree
std::vector<TrimLayerInstructions> get_trim_instructions(

View File

@ -775,11 +775,13 @@ static bool grow_tree(CurveTreesV1 &curve_trees,
global_tree.log_last_hashes(last_hashes);
auto new_leaf_tuples = generate_random_leaves(curve_trees, old_n_leaf_tuples, new_n_leaf_tuples);
// Get a tree extension object to the existing tree using randomly generated leaves
// - The tree extension includes all elements we'll need to add to the existing tree when adding the new leaves
const auto tree_extension = curve_trees.get_tree_extension(old_n_leaf_tuples,
last_hashes,
generate_random_leaves(curve_trees, old_n_leaf_tuples, new_n_leaf_tuples));
std::move(new_leaf_tuples));
global_tree.log_tree_extension(tree_extension);
@ -857,14 +859,18 @@ static bool grow_tree_db(const std::size_t init_leaves,
LOG_PRINT_L1("Adding " << init_leaves << " leaves to db, then extending by " << ext_leaves << " leaves");
test_db.m_db->grow_tree(curve_trees, generate_random_leaves(curve_trees, 0, init_leaves));
auto init_leaf_tuples = generate_random_leaves(curve_trees, 0, init_leaves);
test_db.m_db->grow_tree(curve_trees, std::move(init_leaf_tuples));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(curve_trees, init_leaves), false,
"failed to add initial leaves to db");
MDEBUG("Successfully added initial " << init_leaves << " leaves to db, extending by "
<< ext_leaves << " leaves");
test_db.m_db->grow_tree(curve_trees, generate_random_leaves(curve_trees, init_leaves, ext_leaves));
auto ext_leaf_tuples = generate_random_leaves(curve_trees, init_leaves, ext_leaves);
test_db.m_db->grow_tree(curve_trees, std::move(ext_leaf_tuples));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(curve_trees, init_leaves + ext_leaves), false,
"failed to extend tree in db");
@ -886,7 +892,9 @@ static bool trim_tree_db(const std::size_t init_leaves,
LOG_PRINT_L1("Adding " << init_leaves << " leaves to db, then trimming by " << trim_leaves << " leaves");
test_db.m_db->grow_tree(curve_trees, generate_random_leaves(curve_trees, 0, init_leaves));
auto init_leaf_tuples = generate_random_leaves(curve_trees, 0, init_leaves);
test_db.m_db->grow_tree(curve_trees, std::move(init_leaf_tuples));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(curve_trees, init_leaves), false,
"failed to add initial leaves to db");