Skip to content

Commit

Permalink
new_leaf_tuples -> new_outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
j-berman committed Aug 13, 2024
1 parent 67f5546 commit 918befb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/blockchain_db/blockchain_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void get_outs_by_unlock_block(const cryptonote::transaction &tx,
CHECK_AND_ASSERT_THROW_MES(tx.rct_signatures.outPk.size() > i, "unexpected size of outPk");

rct::key commitment = (miner_tx || tx.version != 2)
? rct::zeroCommit(out.amount) // Needs ringct
? rct::zeroCommit(out.amount)
: tx.rct_signatures.outPk[i].mask;

auto output_pair = fcmp_pp::curve_trees::OutputPair{
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain_db/blockchain_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ class BlockchainDB
virtual bool for_all_alt_blocks(std::function<bool(const crypto::hash &blkid, const alt_block_data_t &data, const cryptonote::blobdata_ref *blob)> f, bool include_blob = false) const = 0;

// TODO: description and make private
virtual void grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_leaves) = 0;
virtual void grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs) = 0;

virtual void trim_tree(const uint64_t trim_n_leaf_tuples) = 0;

Expand Down
6 changes: 3 additions & 3 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,10 +1345,10 @@ void BlockchainLMDB::remove_spent_key(const crypto::key_image& k_image)
}
}

void BlockchainLMDB::grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_leaves)
void BlockchainLMDB::grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
if (new_leaves.empty())
if (new_outputs.empty())
return;

check_open();
Expand All @@ -1366,7 +1366,7 @@ void BlockchainLMDB::grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext>

// Use the number of leaf tuples and the existing last hashes to get a struct we can use to extend the tree
CHECK_AND_ASSERT_THROW_MES(m_curve_trees != nullptr, "curve trees must be set");
const auto tree_extension = m_curve_trees->get_tree_extension(old_n_leaf_tuples, last_hashes, std::move(new_leaves));
const auto tree_extension = m_curve_trees->get_tree_extension(old_n_leaf_tuples, last_hashes, std::move(new_outputs));

// Insert the leaves
// TODO: grow_leaves
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain_db/lmdb/db_lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class BlockchainLMDB : public BlockchainDB
static int compare_string(const MDB_val *a, const MDB_val *b);

// make private
virtual void grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_leaves);
virtual void grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs);

virtual void trim_tree(const uint64_t trim_n_leaf_tuples);

Expand Down
2 changes: 1 addition & 1 deletion src/blockchain_db/testdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class BaseTestDB: public cryptonote::BlockchainDB {
virtual void add_tx_amount_output_indices(const uint64_t tx_index, const std::vector<uint64_t>& amount_output_indices) override {}
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(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_leaves) override {};
virtual void grow_tree(std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs) override {};
virtual void trim_tree(const uint64_t trim_n_leaf_tuples) override {};
virtual bool audit_tree(const uint64_t expected_n_leaf_tuples) const override { return false; };
virtual std::array<uint8_t, 32UL> get_tree_root() const override { return {}; };
Expand Down
16 changes: 8 additions & 8 deletions src/fcmp_pp/curve_trees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,25 +706,25 @@ 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,
std::vector<OutputContext> &&new_leaf_tuples) const
std::vector<OutputContext> &&new_outputs) const
{
TreeExtension tree_extension;
tree_extension.leaves.start_leaf_tuple_idx = old_n_leaf_tuples;

if (new_leaf_tuples.empty())
if (new_outputs.empty())
return tree_extension;

// Sort the leaves by order they appear in the chain
// Sort the outputs by order they appear in the chain
const auto sort_fn = [](const OutputContext &a, const OutputContext &b) { return a.output_id < b.output_id; };
std::sort(new_leaf_tuples.begin(), new_leaf_tuples.end(), sort_fn);
std::sort(new_outputs.begin(), new_outputs.end(), sort_fn);

// Convert sorted outputs into leaf tuples, place each element of each leaf tuple in a flat vector to be hashed,
// and place the outputs in a tree extension struct for insertion into the db. We ignore invalid outputs, since
// they cannot be inserted to the tree.
std::vector<typename C2::Scalar> flattened_leaves;
flattened_leaves.reserve(new_leaf_tuples.size() * LEAF_TUPLE_SIZE);
tree_extension.leaves.tuples.reserve(new_leaf_tuples.size());
for (auto &o : new_leaf_tuples)
flattened_leaves.reserve(new_outputs.size() * LEAF_TUPLE_SIZE);
tree_extension.leaves.tuples.reserve(new_outputs.size());
for (auto &o : new_outputs)
{
// TODO: this loop can be parallelized
LeafTuple leaf;
Expand Down Expand Up @@ -806,7 +806,7 @@ typename CurveTrees<C1, C2>::TreeExtension CurveTrees<C1, C2>::get_tree_extensio
template CurveTrees<Helios, Selene>::TreeExtension CurveTrees<Helios, Selene>::get_tree_extension(
const uint64_t old_n_leaf_tuples,
const LastHashes &existing_last_hashes,
std::vector<OutputContext> &&new_leaf_tuples) const;
std::vector<OutputContext> &&new_outputs) const;
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
std::vector<TrimLayerInstructions> CurveTrees<C1, C2>::get_trim_instructions(
Expand Down
2 changes: 1 addition & 1 deletion src/fcmp_pp/curve_trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class CurveTrees
std::vector<typename C2::Scalar> flatten_leaves(std::vector<LeafTuple> &&leaves) const;

// Take in the existing number of leaf tuples and the existing last hash in each layer in the tree, as well as new
// leaves to add to the tree, and return a tree extension struct that can be used to extend a tree
// outputs 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,
std::vector<OutputContext> &&new_leaf_tuples) const;
Expand Down
16 changes: 8 additions & 8 deletions tests/unit_tests/curve_trees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,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);
auto new_outputs = 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,
std::move(new_leaf_tuples));
std::move(new_outputs));

global_tree.log_tree_extension(tree_extension);

Expand Down Expand Up @@ -884,18 +884,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");

auto init_leaf_tuples = generate_random_leaves(*curve_trees, 0, init_leaves);
auto init_outputs = generate_random_leaves(*curve_trees, 0, init_leaves);

test_db.m_db->grow_tree(std::move(init_leaf_tuples));
test_db.m_db->grow_tree(std::move(init_outputs));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(init_leaves), false,
"failed to add initial leaves to db");

MDEBUG("Successfully added initial " << init_leaves << " leaves to db, extending by "
<< ext_leaves << " leaves");

auto ext_leaf_tuples = generate_random_leaves(*curve_trees, init_leaves, ext_leaves);
auto ext_outputs = generate_random_leaves(*curve_trees, init_leaves, ext_leaves);

test_db.m_db->grow_tree(std::move(ext_leaf_tuples));
test_db.m_db->grow_tree(std::move(ext_outputs));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(init_leaves + ext_leaves), false,
"failed to extend tree in db");

Expand All @@ -917,9 +917,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");

auto init_leaf_tuples = generate_random_leaves(*curve_trees, 0, init_leaves);
auto init_outputs = generate_random_leaves(*curve_trees, 0, init_leaves);

test_db.m_db->grow_tree(std::move(init_leaf_tuples));
test_db.m_db->grow_tree(std::move(init_outputs));
CHECK_AND_ASSERT_MES(test_db.m_db->audit_tree(init_leaves), false,
"failed to add initial leaves to db");

Expand Down

0 comments on commit 918befb

Please sign in to comment.