Remove hidden transaction copy in add_block

This commit is contained in:
Lee *!* Clagett 2025-03-17 19:19:20 -04:00
parent f90a267fa3
commit 834438acb8
5 changed files with 16 additions and 17 deletions

View file

@ -179,10 +179,8 @@ void BlockchainDB::pop_block()
pop_block(blk, txs); pop_block(blk, txs);
} }
void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& txp, const crypto::hash* tx_hash_ptr, const crypto::hash* tx_prunable_hash_ptr) void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transaction& tx, const epee::span<const std::uint8_t> blob, const crypto::hash* tx_hash_ptr, const crypto::hash* tx_prunable_hash_ptr)
{ {
const transaction &tx = txp.first;
bool miner_tx = false; bool miner_tx = false;
crypto::hash tx_hash, tx_prunable_hash; crypto::hash tx_hash, tx_prunable_hash;
if (!tx_hash_ptr) if (!tx_hash_ptr)
@ -197,8 +195,9 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const std::pair
} }
if (tx.version >= 2) if (tx.version >= 2)
{ {
const boost::string_ref ref{reinterpret_cast<const char*>(blob.data()), blob.size()};
if (!tx_prunable_hash_ptr) if (!tx_prunable_hash_ptr)
tx_prunable_hash = get_transaction_prunable_hash(tx, &txp.second); tx_prunable_hash = get_transaction_prunable_hash(tx, &ref);
else else
tx_prunable_hash = *tx_prunable_hash_ptr; tx_prunable_hash = *tx_prunable_hash_ptr;
} }
@ -221,7 +220,7 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const std::pair
} }
} }
uint64_t tx_id = add_transaction_data(blk_hash, txp, tx_hash, tx_prunable_hash); uint64_t tx_id = add_transaction_data(blk_hash, tx, blob, tx_hash, tx_prunable_hash);
std::vector<uint64_t> amount_output_indices(tx.vout.size()); std::vector<uint64_t> amount_output_indices(tx.vout.size());
@ -275,7 +274,7 @@ uint64_t BlockchainDB::add_block( const std::pair<block, blobdata>& blck
uint64_t num_rct_outs = 0; uint64_t num_rct_outs = 0;
blobdata miner_bd = tx_to_blob(blk.miner_tx); blobdata miner_bd = tx_to_blob(blk.miner_tx);
add_transaction(blk_hash, std::make_pair(blk.miner_tx, blobdata_ref(miner_bd))); add_transaction(blk_hash, blk.miner_tx, epee::strspan<std::uint8_t>(miner_bd));
if (blk.miner_tx.version == 2) if (blk.miner_tx.version == 2)
num_rct_outs += blk.miner_tx.vout.size(); num_rct_outs += blk.miner_tx.vout.size();
int tx_i = 0; int tx_i = 0;
@ -283,7 +282,7 @@ uint64_t BlockchainDB::add_block( const std::pair<block, blobdata>& blck
for (const std::pair<transaction, blobdata>& tx : txs) for (const std::pair<transaction, blobdata>& tx : txs)
{ {
tx_hash = blk.tx_hashes[tx_i]; tx_hash = blk.tx_hashes[tx_i];
add_transaction(blk_hash, tx, &tx_hash); add_transaction(blk_hash, tx.first, epee::strspan<std::uint8_t>(tx.second), &tx_hash);
for (const auto &vout: tx.first.vout) for (const auto &vout: tx.first.vout)
{ {
if (vout.amount == 0) if (vout.amount == 0)

View file

@ -436,11 +436,12 @@ private:
* *
* @param blk_hash the hash of the block containing the transaction * @param blk_hash the hash of the block containing the transaction
* @param tx the transaction to be added * @param tx the transaction to be added
* @param blob for `tx`
* @param tx_hash the hash of the transaction * @param tx_hash the hash of the transaction
* @param tx_prunable_hash the hash of the prunable part of the transaction * @param tx_prunable_hash the hash of the prunable part of the transaction
* @return the transaction ID * @return the transaction ID
*/ */
virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) = 0; virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, epee::span<const std::uint8_t> blob, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) = 0;
/** /**
* @brief remove data about a transaction * @brief remove data about a transaction
@ -565,10 +566,11 @@ protected:
* *
* @param blk_hash hash of the block which has the transaction * @param blk_hash hash of the block which has the transaction
* @param tx the transaction to add * @param tx the transaction to add
* @param blob for `tx`
* @param tx_hash_ptr the hash of the transaction, if already calculated * @param tx_hash_ptr the hash of the transaction, if already calculated
* @param tx_prunable_hash_ptr the hash of the prunable part of the transaction, if already calculated * @param tx_prunable_hash_ptr the hash of the prunable part of the transaction, if already calculated
*/ */
void add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash* tx_hash_ptr = NULL, const crypto::hash* tx_prunable_hash_ptr = NULL); void add_transaction(const crypto::hash& blk_hash, const transaction& tx, epee::span<const std::uint8_t> blob, const crypto::hash* tx_hash_ptr = NULL, const crypto::hash* tx_prunable_hash_ptr = NULL);
mutable uint64_t time_tx_exists = 0; //!< a performance metric mutable uint64_t time_tx_exists = 0; //!< a performance metric
uint64_t time_commit1 = 0; //!< a performance metric uint64_t time_commit1 = 0; //!< a performance metric

View file

@ -887,7 +887,7 @@ void BlockchainLMDB::remove_block()
throw1(DB_ERROR(lmdb_error("Failed to add removal of block info to db transaction: ", result).c_str())); throw1(DB_ERROR(lmdb_error("Failed to add removal of block info to db transaction: ", result).c_str()));
} }
uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& txp, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const epee::span<const std::uint8_t> blob, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash)
{ {
LOG_PRINT_L3("BlockchainLMDB::" << __func__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open(); check_open();
@ -913,7 +913,6 @@ uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, cons
throw1(DB_ERROR(lmdb_error(std::string("Error checking if tx index exists for tx hash ") + epee::string_tools::pod_to_hex(tx_hash) + ": ", result).c_str())); throw1(DB_ERROR(lmdb_error(std::string("Error checking if tx index exists for tx hash ") + epee::string_tools::pod_to_hex(tx_hash) + ": ", result).c_str()));
} }
const cryptonote::transaction &tx = txp.first;
txindex ti; txindex ti;
ti.key = tx_hash; ti.key = tx_hash;
ti.data.tx_id = tx_id; ti.data.tx_id = tx_id;
@ -927,8 +926,6 @@ uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, cons
if (result) if (result)
throw0(DB_ERROR(lmdb_error("Failed to add tx data to db transaction: ", result).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add tx data to db transaction: ", result).c_str()));
const cryptonote::blobdata_ref &blob = txp.second;
unsigned int unprunable_size = tx.unprunable_size; unsigned int unprunable_size = tx.unprunable_size;
if (unprunable_size == 0) if (unprunable_size == 0)
{ {
@ -5133,7 +5130,8 @@ void BlockchainLMDB::migrate_0_1()
if (!parse_and_validate_block_from_blob(bd, b)) if (!parse_and_validate_block_from_blob(bd, b))
throw0(DB_ERROR("Failed to parse block from blob retrieved from the db")); throw0(DB_ERROR("Failed to parse block from blob retrieved from the db"));
add_transaction(null_hash, std::make_pair(b.miner_tx, tx_to_blob(b.miner_tx))); const auto miner_blob = tx_to_blob(b.miner_tx);
add_transaction(null_hash, b.miner_tx, epee::strspan<std::uint8_t>(miner_blob));
for (unsigned int j = 0; j<b.tx_hashes.size(); j++) { for (unsigned int j = 0; j<b.tx_hashes.size(); j++) {
transaction tx; transaction tx;
hk.mv_data = &b.tx_hashes[j]; hk.mv_data = &b.tx_hashes[j];
@ -5143,7 +5141,7 @@ void BlockchainLMDB::migrate_0_1()
bd = {reinterpret_cast<char*>(v.mv_data), v.mv_size}; bd = {reinterpret_cast<char*>(v.mv_data), v.mv_size};
if (!parse_and_validate_tx_from_blob(bd, tx)) if (!parse_and_validate_tx_from_blob(bd, tx))
throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db")); throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db"));
add_transaction(null_hash, std::make_pair(std::move(tx), bd), &b.tx_hashes[j]); add_transaction(null_hash, std::move(tx), epee::strspan<std::uint8_t>(bd), &b.tx_hashes[j]);
result = mdb_cursor_del(c_txs, 0); result = mdb_cursor_del(c_txs, 0);
if (result) if (result)
throw0(DB_ERROR(lmdb_error("Failed to get record from txs: ", result).c_str())); throw0(DB_ERROR(lmdb_error("Failed to get record from txs: ", result).c_str()));

View file

@ -374,7 +374,7 @@ private:
virtual void remove_block(); virtual void remove_block();
virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash); virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, epee::span<const std::uint8_t> blob, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash);
virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx); virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);

View file

@ -110,7 +110,7 @@ public:
virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_index, size_t n_txes) const override { return std::vector<std::vector<uint64_t>>(); } virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_index, size_t n_txes) const override { return std::vector<std::vector<uint64_t>>(); }
virtual bool has_key_image(const crypto::key_image& img) const override { return false; } virtual bool has_key_image(const crypto::key_image& img) const override { return false; }
virtual void remove_block() override { } virtual void remove_block() override { }
virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<cryptonote::transaction, cryptonote::blobdata_ref>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) override {return 0;} virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const cryptonote::transaction& tx, epee::span<const std::uint8_t> blob, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) override {return 0;}
virtual void remove_transaction_data(const crypto::hash& tx_hash, const cryptonote::transaction& tx) override {} virtual void remove_transaction_data(const crypto::hash& tx_hash, const cryptonote::transaction& tx) override {}
virtual uint64_t add_output(const crypto::hash& tx_hash, const cryptonote::tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time, const rct::key *commitment) override {return 0;} virtual uint64_t add_output(const crypto::hash& tx_hash, const cryptonote::tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time, const rct::key *commitment) override {return 0;}
virtual void add_tx_amount_output_indices(const uint64_t tx_index, const std::vector<uint64_t>& amount_output_indices) override {} virtual void add_tx_amount_output_indices(const uint64_t tx_index, const std::vector<uint64_t>& amount_output_indices) override {}