From db12610d941af67474f3e79e6f840bc6fda26ecb Mon Sep 17 00:00:00 2001 From: j-berman Date: Thu, 25 Jul 2024 16:12:17 -0700 Subject: [PATCH] Remove leaves from locked leaves table upon insertion to tree --- src/blockchain_db/lmdb/db_lmdb.cpp | 37 ++++++++++++++----- src/blockchain_db/lmdb/db_lmdb.h | 2 + .../cryptonote_format_utils.cpp | 2 +- src/fcmp/curve_trees.cpp | 2 +- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index bf04f0740..9290998c6 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -847,10 +847,10 @@ 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); - // TODO: remove locked from the locked outputs table + // 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); int result = 0; @@ -899,7 +899,7 @@ void BlockchainLMDB::add_block(const block& blk, size_t block_weight, uint64_t l CURSOR(locked_leaves) - // Add the locked leaf tuples from this block to the locked outputs table + // Add the locked leaf tuples from this block to the locked leaves table for (const auto &locked_tuple : leaf_tuples_by_unlock_block) { MDB_val_set(k_block_id, locked_tuple.first); @@ -2218,10 +2218,9 @@ std::vector BlockchainLMDB::g MDB_val_set(k_block_id, block_id); MDB_val v_tuple; - // Get all the locked outputs at that height + // Get all the locked outputs at the provided block id std::vector leaf_tuples; - // TODO: double check this gets all leaf tuples when it does multiple iters MDB_cursor_op op = MDB_SET; while (1) { @@ -2234,7 +2233,7 @@ std::vector BlockchainLMDB::g const uint64_t blk_id = *(const uint64_t*)k_block_id.mv_data; if (blk_id != block_id) - throw0(DB_ERROR(("Height " + std::to_string(blk_id) + " not the expected" + std::to_string(block_id)).c_str())); + throw0(DB_ERROR(("Blk id " + std::to_string(blk_id) + " not the expected" + std::to_string(block_id)).c_str())); const auto range_begin = ((const fcmp::curve_trees::CurveTreesV1::LeafTupleContext*)v_tuple.mv_data); const auto range_end = range_begin + v_tuple.mv_size / sizeof(fcmp::curve_trees::CurveTreesV1::LeafTupleContext); @@ -2257,6 +2256,27 @@ std::vector BlockchainLMDB::g return leaf_tuples; } +void BlockchainLMDB::del_locked_leaf_tuples_at_block_id(uint64_t block_id) +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + mdb_txn_cursors *m_cursors = &m_wcursors; + + CURSOR(locked_leaves) + + MDB_val_set(k_block_id, block_id); + + int result = mdb_cursor_get(m_cur_locked_leaves, &k_block_id, NULL, MDB_SET); + if (result == MDB_NOTFOUND) + return; + if (result != MDB_SUCCESS) + throw1(DB_ERROR(lmdb_error("Error finding locked leaf tuples to remove: ", result).c_str())); + + result = mdb_cursor_del(m_cur_locked_leaves, MDB_NODUPDATA); + if (result) + throw1(DB_ERROR(lmdb_error("Error removing locked leaf tuples: ", result).c_str())); +} + BlockchainLMDB::~BlockchainLMDB() { LOG_PRINT_L3("BlockchainLMDB::" << __func__); @@ -6869,7 +6889,8 @@ void BlockchainLMDB::migrate_5_6() const auto leaf_tuples = this->get_locked_leaf_tuples_at_block_id(i); this->grow_tree(fcmp::curve_trees::curve_trees_v1, leaf_tuples); - // TODO: Remove locked outputs from the locked outputs table after adding them to tree + // 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); // Get old block_info and use it to set the new one with new values result = mdb_cursor_get(c_old_block_info, &k_blk, &v_blk, MDB_NEXT); @@ -6889,8 +6910,6 @@ void BlockchainLMDB::migrate_5_6() bi.bi_n_leaf_tuples = this->get_num_leaf_tuples(); bi.bi_tree_root = this->get_tree_root(); - MDEBUG("Height: " << i << " , n_leaf_tuples: " << bi.bi_n_leaf_tuples); - MDB_val_set(nv, bi); result = mdb_cursor_put(c_new_block_info, (MDB_val *)&zerokval, &nv, MDB_APPENDDUP); if (result) diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 3391cd6f8..b363077bc 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -452,6 +452,8 @@ private: std::vector get_locked_leaf_tuples_at_block_id(uint64_t block_id); + void del_locked_leaf_tuples_at_block_id(uint64_t block_id); + uint64_t num_outputs() const; // Hard fork diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index b8fbd12cc..05dcac4e2 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -1661,7 +1661,7 @@ namespace cryptonote { // The unlock_time in this case is supposed to be the chain height at which the output unlocks // The chain height is 1 higher than the highest block index, so we subtract 1 for this delta - unlock_block_index = unlock_time > 1 ? (unlock_time - 1) : 0; + unlock_block_index = unlock_time > 0 ? (unlock_time - 1) : 0; } else { diff --git a/src/fcmp/curve_trees.cpp b/src/fcmp/curve_trees.cpp index 25efaf858..0c48f55d2 100644 --- a/src/fcmp/curve_trees.cpp +++ b/src/fcmp/curve_trees.cpp @@ -710,7 +710,7 @@ void CurveTrees::tx_outs_to_leaf_tuples(const cryptonote::transa CurveTrees::LeafTupleContext tuple_context; tuple_context.output_id = output_ids[i]; - + try { // Convert output to leaf tuple; throws if output is invalid