Skip to content

Commit 34bc370

Browse files
committed
@jeffro256 latest review comment
Introduced Compressed Tree Extension structs to cleanly separate any curve trees-specific crypto logic from the db layer
1 parent d216ee3 commit 34bc370

File tree

17 files changed

+364
-328
lines changed

17 files changed

+364
-328
lines changed

src/blockchain_db/blockchain_db.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ void BlockchainDB::grow_tree(const uint64_t blk_idx, std::vector<fcmp_pp::Unifie
390390
return;
391391
}
392392

393-
const uint64_t new_n_leaf_tuples = tree_extension.leaves.tuples.size() + old_n_leaf_tuples;
394-
const auto tree_edge = this->grow_with_tree_extension(tree_extension);
393+
const auto compressed_tree_extension = m_curve_trees->compress_tree_extension(std::move(tree_extension));
394+
const auto tree_edge = this->grow_with_tree_extension(compressed_tree_extension);
395+
396+
const uint64_t new_n_leaf_tuples = compressed_tree_extension.leaves.tuples.size() + old_n_leaf_tuples;
395397
this->save_tree_meta(blk_idx, new_n_leaf_tuples, tree_edge);
396398
}
397399

@@ -452,7 +454,7 @@ void BlockchainDB::trim_tree(const uint64_t new_n_leaf_tuples, const uint64_t tr
452454
this->trim_layers(new_n_leaf_tuples, n_elems_per_layer, prev_tree_edge, expected_root_idx);
453455
}
454456

455-
std::pair<uint64_t, fcmp_pp::PathBytes> BlockchainDB::get_last_path(const uint64_t block_idx) const
457+
std::pair<uint64_t, fcmp_pp::CompressedPath> BlockchainDB::get_last_path(const uint64_t block_idx) const
456458
{
457459
LOG_PRINT_L3("BlockchainDB::" << __func__);
458460

@@ -475,8 +477,8 @@ std::pair<uint64_t, fcmp_pp::PathBytes> BlockchainDB::get_last_path(const uint64
475477
// Use tree edge at the provided block to set the last hash for each layer (so path state reflects old state)
476478
for (std::size_t i = 0; i < path.layer_chunks.size(); ++i)
477479
{
478-
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks[i].chunk_bytes.size(), "get_last_path: empty path");
479-
path.layer_chunks[i].chunk_bytes.back() = tree_edge[i];
480+
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks[i].elems.size(), "get_last_path: empty path");
481+
path.layer_chunks[i].elems.back() = tree_edge[i];
480482
}
481483

482484
return { block_n_leaf_tuples, path };
@@ -485,7 +487,7 @@ std::pair<uint64_t, fcmp_pp::PathBytes> BlockchainDB::get_last_path(const uint64
485487
uint64_t BlockchainDB::get_path_by_unified_id(const std::vector<uint64_t> &unified_ids,
486488
const uint64_t as_of_n_blocks,
487489
std::vector<uint64_t> &leaf_idxs_out,
488-
std::vector<fcmp_pp::PathBytes> &paths_out) const
490+
std::vector<fcmp_pp::CompressedPath> &paths_out) const
489491
{
490492
LOG_PRINT_L3("BlockchainDB::" << __func__);
491493

@@ -494,7 +496,7 @@ uint64_t BlockchainDB::get_path_by_unified_id(const std::vector<uint64_t> &unifi
494496
// Initialize result vectors with 0 values. If outptut is not in the tree,
495497
// result vectors kept as 0 values
496498
leaf_idxs_out = std::vector<uint64_t>(unified_ids.size(), 0);
497-
paths_out = std::vector<fcmp_pp::PathBytes>(unified_ids.size(), fcmp_pp::PathBytes{});
499+
paths_out = std::vector<fcmp_pp::CompressedPath>(unified_ids.size(), fcmp_pp::CompressedPath{});
498500

499501
if (unified_ids.empty())
500502
return 0;
@@ -599,11 +601,11 @@ uint64_t BlockchainDB::get_path_by_unified_id(const std::vector<uint64_t> &unifi
599601
if (last_path_idxs.layers.at(i).second != path_idxs.layers.at(i).second)
600602
continue;
601603

602-
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks.at(i).chunk_bytes.size(), "get_path_by_unified_id: empty layer in path");
603-
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks.at(i).chunk_bytes.size() == last_path.second.layer_chunks.at(i).chunk_bytes.size(),
604+
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks.at(i).elems.size(), "get_path_by_unified_id: empty layer in path");
605+
CHECK_AND_ASSERT_THROW_MES(path.layer_chunks.at(i).elems.size() == last_path.second.layer_chunks.at(i).elems.size(),
604606
"get_path_by_unified_id: unexpected size of last path");
605607

606-
path.layer_chunks.at(i).chunk_bytes.back() = last_path.second.layer_chunks.at(i).chunk_bytes.back();
608+
path.layer_chunks.at(i).elems.back() = last_path.second.layer_chunks.at(i).elems.back();
607609
}
608610

609611
paths_out.at(i) = std::move(path);

src/blockchain_db/blockchain_db.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ class BlockchainDB
586586

587587
virtual void del_tree_meta(const uint64_t block_idx) = 0;
588588

589-
virtual std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension) = 0;
589+
virtual std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::CompressedTreeExtension &tree_extension) = 0;
590590

591-
virtual fcmp_pp::PathBytes get_path(const fcmp_pp::PathIndexes &path_indexes) const = 0;
591+
virtual fcmp_pp::CompressedPath get_path(const fcmp_pp::PathIndexes &path_indexes) const = 0;
592592

593593
virtual uint64_t find_leaf_idx_by_unified_id_bounded_search(uint64_t unified_id, uint64_t leaf_idx_start, uint64_t leaf_idx_end) const = 0;
594594

@@ -1869,12 +1869,12 @@ class BlockchainDB
18691869

18701870
void trim_tree(const uint64_t new_n_leaf_tuples, const uint64_t trim_block_idx);
18711871

1872-
std::pair<uint64_t, fcmp_pp::PathBytes> get_last_path(const uint64_t block_idx) const;
1872+
std::pair<uint64_t, fcmp_pp::CompressedPath> get_last_path(const uint64_t block_idx) const;
18731873

18741874
uint64_t get_path_by_unified_id(const std::vector<uint64_t> &unified_ids,
18751875
const uint64_t as_of_n_blocks,
18761876
std::vector<uint64_t> &leaf_idxs_out,
1877-
std::vector<fcmp_pp::PathBytes> &paths_out) const;
1877+
std::vector<fcmp_pp::CompressedPath> &paths_out) const;
18781878

18791879
/**
18801880
* @brief add outs to locked outputs tables

src/blockchain_db/lmdb/db_lmdb.cpp

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ typedef struct layer_val {
372372
uint64_t child_chunk_idx;
373373
crypto::ec_point child_chunk_hash;
374374
} layer_val;
375+
static_assert(sizeof(layer_val) == (8+32), "layer_val unexpected size");
375376

376377
typedef struct mdb_tree_meta {
377378
uint64_t n_leaf_tuples;
@@ -1427,7 +1428,7 @@ void BlockchainLMDB::del_locked_outs_at_block_idx(uint64_t block_idx)
14271428
throw1(DB_ERROR(lmdb_error("Error removing locked outputs: ", result).c_str()));
14281429
}
14291430

1430-
std::vector<crypto::ec_point> BlockchainLMDB::grow_with_tree_extension(const fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension)
1431+
std::vector<crypto::ec_point> BlockchainLMDB::grow_with_tree_extension(const fcmp_pp::CompressedTreeExtension &tree_extension)
14311432
{
14321433
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
14331434
check_open();
@@ -1452,60 +1453,22 @@ std::vector<crypto::ec_point> BlockchainLMDB::grow_with_tree_extension(const fcm
14521453
}
14531454

14541455
// Grow the layers
1455-
const auto &c1_extensions = tree_extension.c1_layer_extensions;
1456-
const auto &c2_extensions = tree_extension.c2_layer_extensions;
1457-
const std::size_t n_layers = c1_extensions.size() + c2_extensions.size();
1458-
if (n_layers == 0)
1456+
const auto &layer_extensions = tree_extension.layer_extensions;
1457+
if (layer_extensions.empty())
14591458
throw0(DB_ERROR("Unexpected 0 n layers"));
14601459

1461-
bool parent_is_c1 = true;
1462-
uint64_t c1_idx = 0;
1463-
uint64_t c2_idx = 0;
14641460
std::vector<crypto::ec_point> tree_edge;
1465-
tree_edge.reserve(n_layers);
1466-
for (uint64_t i = 0; i < n_layers; ++i)
1461+
tree_edge.reserve(layer_extensions.size());
1462+
for (uint64_t layer_idx = 0; layer_idx < layer_extensions.size(); ++layer_idx)
14671463
{
1468-
const uint64_t layer_idx = c1_idx + c2_idx;
14691464
MTRACE("Growing layer " << layer_idx);
1470-
1471-
if (parent_is_c1)
1472-
{
1473-
if (layer_idx % 2 != 0)
1474-
throw0(DB_ERROR(("Growing odd c1 layer, expected even layer idx for c1: "
1475-
+ std::to_string(layer_idx)).c_str()));
1476-
1477-
tree_edge.emplace_back(this->grow_layer<fcmp_pp::curve_trees::Selene>(
1478-
m_curve_trees->m_c1,
1479-
c1_extensions.at(c1_idx),
1480-
layer_idx
1481-
));
1482-
1483-
++c1_idx;
1484-
}
1485-
else
1486-
{
1487-
if (layer_idx % 2 == 0)
1488-
throw0(DB_ERROR(("Growing even c2 layer, expected odd layer idx for c2: "
1489-
+ std::to_string(layer_idx)).c_str()));
1490-
1491-
tree_edge.emplace_back(this->grow_layer<fcmp_pp::curve_trees::Helios>(
1492-
m_curve_trees->m_c2,
1493-
c2_extensions.at(c2_idx),
1494-
layer_idx
1495-
));
1496-
1497-
++c2_idx;
1498-
}
1499-
1500-
parent_is_c1 = !parent_is_c1;
1465+
tree_edge.emplace_back(this->grow_layer(layer_extensions.at(layer_idx), layer_idx));
15011466
}
15021467

15031468
return tree_edge;
15041469
}
15051470

1506-
template<typename C>
1507-
crypto::ec_point BlockchainLMDB::grow_layer(const std::unique_ptr<C> &curve,
1508-
const fcmp_pp::LayerExtension<C> &layer_extension,
1471+
crypto::ec_point BlockchainLMDB::grow_layer(const fcmp_pp::CompressedLayerExtension &layer_extension,
15091472
const uint64_t layer_idx)
15101473
{
15111474
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -1525,7 +1488,7 @@ crypto::ec_point BlockchainLMDB::grow_layer(const std::unique_ptr<C> &curve,
15251488
// 1. Update the existing last hash if necessary
15261489
if (layer_extension.update_existing_last_hash)
15271490
{
1528-
hashes.emplace_back(curve->to_bytes(layer_extension.hashes.front()));
1491+
hashes.emplace_back(layer_extension.hashes.front());
15291492

15301493
// We updated the last hash, so update it
15311494
layer_val lv;
@@ -1543,7 +1506,7 @@ crypto::ec_point BlockchainLMDB::grow_layer(const std::unique_ptr<C> &curve,
15431506
// 2. Add all the new hashes found in the extension
15441507
for (uint64_t i = layer_extension.update_existing_last_hash ? 1 : 0; i < layer_extension.hashes.size(); ++i)
15451508
{
1546-
hashes.emplace_back(curve->to_bytes(layer_extension.hashes[i]));
1509+
hashes.emplace_back(layer_extension.hashes[i]);
15471510

15481511
layer_val lv;
15491512
lv.child_chunk_idx = i + layer_extension.start_idx;
@@ -1652,7 +1615,7 @@ std::vector<crypto::ec_point> BlockchainLMDB::get_tree_edge(uint64_t block_id) c
16521615
return res;
16531616
}
16541617

1655-
fcmp_pp::PathBytes BlockchainLMDB::get_path(const fcmp_pp::PathIndexes &path_indexes) const
1618+
fcmp_pp::CompressedPath BlockchainLMDB::get_path(const fcmp_pp::PathIndexes &path_indexes) const
16561619
{
16571620
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
16581621
check_open();
@@ -1661,7 +1624,7 @@ fcmp_pp::PathBytes BlockchainLMDB::get_path(const fcmp_pp::PathIndexes &path_ind
16611624
RCURSOR(leaves)
16621625
RCURSOR(layers)
16631626

1664-
fcmp_pp::PathBytes path_bytes;
1627+
fcmp_pp::CompressedPath path_bytes;
16651628

16661629
auto &leaves_out = path_bytes.leaves;
16671630
auto &layer_chunks_out = path_bytes.layer_chunks;
@@ -1705,7 +1668,7 @@ fcmp_pp::PathBytes BlockchainLMDB::get_path(const fcmp_pp::PathIndexes &path_ind
17051668
std::size_t layer_idx = 0;
17061669
for (const auto &layer_idx_range : path_indexes.layers)
17071670
{
1708-
fcmp_pp::ChunkBytes chunk;
1671+
fcmp_pp::CompressedChunk chunk;
17091672

17101673
MDB_val_set(k, layer_idx);
17111674
MDB_val_set(v, layer_idx_range.first);
@@ -1722,7 +1685,7 @@ fcmp_pp::PathBytes BlockchainLMDB::get_path(const fcmp_pp::PathIndexes &path_ind
17221685
throw0(DB_ERROR(lmdb_error("Failed to get layer elem: ", result).c_str()));
17231686

17241687
auto *lv = (layer_val *)v.mv_data;
1725-
chunk.chunk_bytes.emplace_back(std::move(lv->child_chunk_hash));
1688+
chunk.elems.emplace_back(std::move(lv->child_chunk_hash));
17261689
}
17271690

17281691
layer_chunks_out.emplace_back(std::move(chunk));

src/blockchain_db/lmdb/db_lmdb.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,9 @@ class BlockchainLMDB final: public BlockchainDB
436436

437437
void del_locked_outs_at_block_idx(uint64_t block_idx) override;
438438

439-
std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension) override;
439+
std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::CompressedTreeExtension &tree_extension) override;
440440

441-
template<typename C>
442-
crypto::ec_point grow_layer(const std::unique_ptr<C> &curve,
443-
const fcmp_pp::LayerExtension<C> &layer_extension,
444-
const uint64_t layer_idx);
441+
crypto::ec_point grow_layer(const fcmp_pp::CompressedLayerExtension &layer_extension, const uint64_t layer_idx);
445442

446443
uint64_t trim_leaves(const uint64_t new_n_leaf_tuples, const uint64_t trim_block_idx) override;
447444

@@ -464,7 +461,7 @@ class BlockchainLMDB final: public BlockchainDB
464461

465462
std::vector<crypto::ec_point> get_tree_edge(uint64_t block_id) const override;
466463

467-
fcmp_pp::PathBytes get_path(const fcmp_pp::PathIndexes &path_indexes) const override;
464+
fcmp_pp::CompressedPath get_path(const fcmp_pp::PathIndexes &path_indexes) const override;
468465

469466
template<typename C_CHILD, typename C_PARENT>
470467
bool audit_layer(const std::unique_ptr<C_CHILD> &c_child,

src/blockchain_db/testdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class BaseTestDB: public cryptonote::BlockchainDB {
129129
virtual std::vector<crypto::ec_point> get_tree_edge(uint64_t block_id) const override { return {}; };
130130
virtual void save_tree_meta(const uint64_t block_idx, const uint64_t n_leaf_tuples, const std::vector<crypto::ec_point> &tree_edge) override {};
131131
virtual void del_tree_meta(const uint64_t block_idx) override {};
132-
virtual std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::curve_trees::CurveTreesV1::TreeExtension &tree_extension) override { return std::vector<crypto::ec_point>{}; };
133-
virtual fcmp_pp::PathBytes get_path(const fcmp_pp::PathIndexes &path_indexes) const override { return fcmp_pp::PathBytes{}; };
132+
virtual std::vector<crypto::ec_point> grow_with_tree_extension(const fcmp_pp::CompressedTreeExtension &tree_extension) override { return std::vector<crypto::ec_point>{}; };
133+
virtual fcmp_pp::CompressedPath get_path(const fcmp_pp::PathIndexes &path_indexes) const override { return fcmp_pp::CompressedPath{}; };
134134
virtual uint64_t find_leaf_idx_by_unified_id_bounded_search(uint64_t unified_id, uint64_t leaf_idx_start, uint64_t leaf_idx_end) const override { return 0; };
135135
virtual uint64_t trim_leaves(const uint64_t new_n_leaf_tuples, const uint64_t trim_block_idx) override { return 0; };
136136
virtual void trim_layers(const uint64_t new_n_leaf_tuples, const std::vector<uint64_t> &n_elems_per_layer, const std::vector<crypto::ec_point> &prev_tree_edge, const uint64_t expected_root_idx) override {};

src/crypto/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace crypto {
6262

6363
// x or y coordinate
6464
POD_CLASS ec_coord {
65-
char data [32];
65+
char data[32];
6666
};
6767

6868
POD_CLASS public_key: ec_point {

0 commit comments

Comments
 (0)