Skip to content

Commit 1729022

Browse files
committed
jeffro comments
1 parent 6b0c789 commit 1729022

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

src/blockchain_db/blockchain_db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ class BlockchainDB
18071807
*/
18081808
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;
18091809

1810-
virtual void advance_tree_one_block(const uint64_t block_idx) = 0;
1810+
virtual void advance_tree(const uint64_t block_idx) = 0;
18111811

18121812
// TODO: description and make private
18131813
virtual void grow_tree(const uint64_t block_id, std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs) = 0;

src/blockchain_db/lmdb/db_lmdb.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ void BlockchainLMDB::remove_spent_key(const crypto::key_image& k_image)
13981398
}
13991399
}
14001400

1401-
void BlockchainLMDB::advance_tree_one_block(const uint64_t blk_idx)
1401+
void BlockchainLMDB::advance_tree(const uint64_t blk_idx)
14021402
{
14031403
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
14041404
check_open();
@@ -1409,21 +1409,24 @@ void BlockchainLMDB::advance_tree_one_block(const uint64_t blk_idx)
14091409
// If we're advancing the genesis block, make sure to initialize the tree
14101410
if (blk_idx == 0)
14111411
{
1412+
// TODO: include a pre-check that tree meta is empty
1413+
14121414
// We grow the first blocks with empty outputs, since no outputs in this range should be spendable yet
14131415
for (uint64_t new_blk_idx = blk_idx; new_blk_idx < earliest_last_locked_block; ++new_blk_idx)
14141416
{
14151417
this->grow_tree(new_blk_idx, {});
14161418
}
14171419
}
1420+
// TODO: include a pre-check that earliest_last_locked_block == last block idx + 1 in tree meta (when blk_idx != 0)
14181421

14191422
// Now we can advance the tree 1 block
1420-
auto unlocked_outputs = this->get_outs_at_last_locked_block_id(earliest_last_locked_block);
1423+
auto unlocked_outputs = this->get_outs_at_last_locked_block_idx(earliest_last_locked_block);
14211424

14221425
// Grow the tree with outputs that are spendable once the earliest_last_locked_block is in the chain
14231426
this->grow_tree(earliest_last_locked_block, std::move(unlocked_outputs));
14241427

14251428
// Now that we've used the unlocked leaves to grow the tree, we delete them from the locked outputs table
1426-
this->del_locked_outs_at_block_id(earliest_last_locked_block);
1429+
this->del_locked_outs_at_block_idx(earliest_last_locked_block);
14271430
}
14281431

14291432
void BlockchainLMDB::grow_tree(const uint64_t blk_idx, std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs)
@@ -2391,16 +2394,16 @@ bool BlockchainLMDB::audit_layer(const std::unique_ptr<C_CHILD> &c_child,
23912394
return audit_complete;
23922395
}
23932396

2394-
std::vector<fcmp_pp::curve_trees::OutputContext> BlockchainLMDB::get_outs_at_last_locked_block_id(
2395-
uint64_t block_id)
2397+
std::vector<fcmp_pp::curve_trees::OutputContext> BlockchainLMDB::get_outs_at_last_locked_block_idx(
2398+
uint64_t block_idx)
23962399
{
23972400
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
23982401
check_open();
23992402

24002403
TXN_PREFIX_RDONLY();
24012404
RCURSOR(locked_outputs)
24022405

2403-
MDB_val_set(k_block_id, block_id);
2406+
MDB_val_set(k_block_idx, block_idx);
24042407
MDB_val v_output;
24052408

24062409
// Get all the locked outputs at the provided block id
@@ -2409,16 +2412,16 @@ std::vector<fcmp_pp::curve_trees::OutputContext> BlockchainLMDB::get_outs_at_las
24092412
MDB_cursor_op op = MDB_SET;
24102413
while (1)
24112414
{
2412-
int result = mdb_cursor_get(m_cur_locked_outputs, &k_block_id, &v_output, op);
2415+
int result = mdb_cursor_get(m_cur_locked_outputs, &k_block_idx, &v_output, op);
24132416
if (result == MDB_NOTFOUND)
24142417
break;
24152418
if (result != MDB_SUCCESS)
24162419
throw0(DB_ERROR(lmdb_error("Failed to get next locked outputs: ", result).c_str()));
24172420
op = MDB_NEXT_MULTIPLE;
24182421

2419-
const uint64_t blk_id = *(const uint64_t*)k_block_id.mv_data;
2420-
if (blk_id != block_id)
2421-
throw0(DB_ERROR(("Blk id " + std::to_string(blk_id) + " not the expected" + std::to_string(block_id)).c_str()));
2422+
const uint64_t blk_id = *(const uint64_t*)k_block_idx.mv_data;
2423+
if (blk_id != block_idx)
2424+
throw0(DB_ERROR(("Blk id " + std::to_string(blk_id) + " not the expected" + std::to_string(block_idx)).c_str()));
24222425

24232426
const auto range_begin = ((const fcmp_pp::curve_trees::OutputContext*)v_output.mv_data);
24242427
const auto range_end = range_begin + v_output.mv_size / sizeof(fcmp_pp::curve_trees::OutputContext);
@@ -2441,17 +2444,17 @@ std::vector<fcmp_pp::curve_trees::OutputContext> BlockchainLMDB::get_outs_at_las
24412444
return outs;
24422445
}
24432446

2444-
void BlockchainLMDB::del_locked_outs_at_block_id(uint64_t block_id)
2447+
void BlockchainLMDB::del_locked_outs_at_block_idx(uint64_t block_idx)
24452448
{
24462449
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
24472450
check_open();
24482451
mdb_txn_cursors *m_cursors = &m_wcursors;
24492452

24502453
CURSOR(locked_outputs)
24512454

2452-
MDB_val_set(k_block_id, block_id);
2455+
MDB_val_set(k_block_idx, block_idx);
24532456

2454-
int result = mdb_cursor_get(m_cur_locked_outputs, &k_block_id, NULL, MDB_SET);
2457+
int result = mdb_cursor_get(m_cur_locked_outputs, &k_block_idx, NULL, MDB_SET);
24552458
if (result == MDB_NOTFOUND)
24562459
return;
24572460
if (result != MDB_SUCCESS)
@@ -7587,7 +7590,7 @@ void BlockchainLMDB::migrate_5_6()
75877590
}
75887591
}
75897592

7590-
this->advance_tree_one_block(i);
7593+
this->advance_tree(i);
75917594

75927595
LOGIF(el::Level::Info)
75937596
{

src/blockchain_db/lmdb/db_lmdb.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class BlockchainLMDB : public BlockchainDB
342342
, const std::vector<std::pair<transaction, blobdata>>& txs
343343
);
344344

345-
virtual void advance_tree_one_block(const uint64_t blk_idx);
345+
virtual void advance_tree(const uint64_t blk_idx);
346346

347347
virtual void set_batch_transactions(bool batch_transactions);
348348
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0);
@@ -457,6 +457,11 @@ class BlockchainLMDB : public BlockchainDB
457457

458458
uint64_t get_block_n_leaf_tuples(uint64_t block_idx) const;
459459

460+
// Gets the tree root and n_tree_layers composed of all valid spendable outputs when blk_idx is the tip of the chain.
461+
// If the chain tip is block index n, and `blk_idx == n`, then this will return the tree root and n layers for the
462+
// tree composed of all valid spendable outputs in the chain at that time. Note that the tree stored in the database
463+
// may not match up with the tree root returned here, since the tree stored in the database may have grown past the
464+
// current tip, with outputs that will unlock in future blocks.
460465
virtual uint8_t get_tree_root_at_blk_idx(const uint64_t blk_idx, crypto::ec_point &tree_root_out) const;
461466

462467
std::vector<crypto::ec_point> get_tree_edge(uint64_t block_id) const;
@@ -467,9 +472,9 @@ class BlockchainLMDB : public BlockchainDB
467472
const uint64_t child_layer_idx,
468473
const uint64_t chunk_width) const;
469474

470-
std::vector<fcmp_pp::curve_trees::OutputContext> get_outs_at_last_locked_block_id(uint64_t block_id);
475+
std::vector<fcmp_pp::curve_trees::OutputContext> get_outs_at_last_locked_block_idx(uint64_t block_id);
471476

472-
void del_locked_outs_at_block_id(uint64_t block_id);
477+
void del_locked_outs_at_block_idx(uint64_t block_idx);
473478

474479
uint64_t get_tree_block_idx() const;
475480

src/blockchain_db/testdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class BaseTestDB: public cryptonote::BlockchainDB {
117117
virtual void add_tx_amount_output_indices(const uint64_t tx_index, const std::vector<uint64_t>& amount_output_indices) override {}
118118
virtual void add_spent_key(const crypto::key_image& k_image) override {}
119119
virtual void remove_spent_key(const crypto::key_image& k_image) override {}
120-
virtual void advance_tree_one_block(const uint64_t block_idx) override {}
120+
virtual void advance_tree(const uint64_t block_idx) override {}
121121
virtual void grow_tree(const uint64_t block_idx, std::vector<fcmp_pp::curve_trees::OutputContext> &&new_outputs) override {};
122122
virtual std::pair<uint64_t, fcmp_pp::curve_trees::PathBytes> get_last_path(const uint64_t block_idx) const override { return {0, fcmp_pp::curve_trees::PathBytes{}}; };
123123
virtual void trim_tree(const uint64_t new_n_leaf_tuples, const uint64_t trim_block_id) override {};

src/cryptonote_core/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4699,7 +4699,7 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
46994699
TIME_MEASURE_START(advance_tree);
47004700

47014701
static_assert(CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE > 0, "Expect a non-0 spendable age");
4702-
try { m_db->advance_tree_one_block(new_height-1); }
4702+
try { m_db->advance_tree(new_height-1); }
47034703
catch (const std::exception& e)
47044704
{
47054705
LOG_ERROR("Failed to advance tree at block with hash: " << id << ", what = " << e.what());

src/fcmp_pp/tree_cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class TreeCache final : public TreeSync<C1, C2>
226226

227227
uint64_t get_output_count() const { return m_output_count; }
228228

229+
// Gets the tree root and n_tree_layers for the tree currently cached in the TreeCache. If the TreeCache's tip is
230+
// block index n, then this will return the tree root and n_tree_layers for the tree composed of all valid spendable
231+
// outputs in the chain when the chain tip is block index n.
229232
uint8_t get_tree_root(crypto::ec_point &tree_root_out) const;
230233

231234
// Build the tree extension and all other types needed to grow the cache, returning the state change by ref

0 commit comments

Comments
 (0)