diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp index b1b233b58..8cde4f138 100644 --- a/src/blockchain_db/blockchain_db.cpp +++ b/src/blockchain_db/blockchain_db.cpp @@ -82,7 +82,7 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti } } - add_transaction_data(blk_hash, tx, tx_hash); + uint64_t tx_index = add_transaction_data(blk_hash, tx, tx_hash); std::vector amount_output_indices; std::vector global_output_indices; @@ -96,7 +96,7 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti amount_output_indices.push_back(amount_output_index); global_output_indices.push_back(global_output_index); } - add_amount_and_global_output_indices(tx_hash, amount_output_indices, global_output_indices); + add_amount_and_global_output_indices(tx_index, amount_output_indices, global_output_indices); } uint64_t BlockchainDB::add_block( const block& blk diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 933cc34f5..b71bb4416 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -291,7 +291,7 @@ private: virtual void remove_block() = 0; // tells the subclass to store the transaction and its metadata - virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) = 0; + virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) = 0; // tells the subclass to remove data about a transaction virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0; @@ -306,7 +306,7 @@ private: ) = 0; // tells the subclass to store indices for a tx's outputs, both amount output indices and global output indices - virtual void add_amount_and_global_output_indices(const crypto::hash& tx_hash, + virtual void add_amount_and_global_output_indices(const uint64_t tx_index, const std::vector& amount_output_indices, const std::vector& global_output_indices ) = 0; diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index a07b257a9..a1ca2e716 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -616,20 +616,21 @@ void BlockchainLMDB::remove_block() throw1(DB_ERROR("Failed to add removal of block info to db transaction")); } -void BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) +uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); mdb_txn_cursors *m_cursors = &m_wcursors; int result = 0; + uint64_t tx_index = m_num_txs; CURSOR(txs) CURSOR(tx_indices) CURSOR(tx_heights) CURSOR(tx_unlocks) - MDB_val_copy val_tx_index(m_num_txs); + MDB_val_copy val_tx_index(tx_index); MDB_val_copy val_h(tx_hash); MDB_val unused; result = mdb_cursor_get(m_cur_tx_indices, &val_h, &unused, MDB_SET); @@ -658,6 +659,7 @@ void BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const tr throw0(DB_ERROR(lmdb_error("Failed to add tx unlock time to db transaction: ", result).c_str())); m_num_txs++; + return tx_index; } // TODO: compare pros and cons of looking up the tx hash's tx index once and @@ -761,7 +763,7 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, m_num_outputs++; } -void BlockchainLMDB::add_amount_and_global_output_indices(const crypto::hash& tx_hash, +void BlockchainLMDB::add_amount_and_global_output_indices(const uint64_t tx_index, const std::vector& amount_output_indices, const std::vector& global_output_indices) { @@ -772,8 +774,6 @@ void BlockchainLMDB::add_amount_and_global_output_indices(const crypto::hash& tx int result = 0; - MDB_val_copy k(tx_hash); - int num_outputs = amount_output_indices.size(); std::unique_ptr paired_indices(new uint64_t[2*num_outputs]); for (int i = 0; i < num_outputs; ++i) @@ -782,17 +782,13 @@ void BlockchainLMDB::add_amount_and_global_output_indices(const crypto::hash& tx paired_indices[2*i+1] = global_output_indices[i]; } + MDB_val_copy k_tx_index(tx_index); MDB_val v; v.mv_data = (void*)paired_indices.get(); v.mv_size = sizeof(uint64_t) * 2 * num_outputs; // LOG_PRINT_L1("tx_outputs[tx_hash] size: " << v.mv_size); - MDB_val val_tx_index; - result = mdb_cursor_get(m_cur_tx_indices, &k, &val_tx_index, MDB_SET); - if (result) - throw0(DB_ERROR(lmdb_error(std::string("Failed to get tx index for tx hash ") + epee::string_tools::pod_to_hex(tx_hash), result).c_str())); - - result = mdb_cursor_put(m_cur_tx_outputs, &val_tx_index, &v, 0); + result = mdb_cursor_put(m_cur_tx_outputs, &k_tx_index, &v, 0); if (result) throw0(DB_ERROR(std::string("Failed to add to db transaction: ").append(mdb_strerror(result)).c_str())); } @@ -1724,7 +1720,6 @@ bool BlockchainLMDB::tx_exists(const crypto::hash& h) const throw0(DB_ERROR(std::string("transaction with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found at index").c_str())); else if (get_result) throw0(DB_ERROR(lmdb_error(std::string("DB error attempting to fetch transaction ") + epee::string_tools::pod_to_hex(h) + " at index: ", get_result).c_str())); - return true; } @@ -2009,10 +2004,10 @@ void BlockchainLMDB::get_amount_and_global_output_indices(const uint64_t tx_inde RCURSOR(tx_outputs); int result = 0; - MDB_val_copy val_tx_index(tx_index); + MDB_val_copy k_tx_index(tx_index); MDB_val v; - result = mdb_cursor_get(m_cur_tx_outputs, &val_tx_index, &v, MDB_SET); + result = mdb_cursor_get(m_cur_tx_outputs, &k_tx_index, &v, MDB_SET); if (result == MDB_NOTFOUND) LOG_PRINT_L0("WARNING: Unexpected: tx has no amount and global indices stored in " "tx_outputs, but it should have an empty entry even if it's a tx without " diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index ef47ea5f7..e60103712 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -289,7 +289,7 @@ private: virtual void remove_block(); - virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash); + virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash); virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx); @@ -301,7 +301,7 @@ private: uint64_t& global_output_index ); - virtual void add_amount_and_global_output_indices(const crypto::hash& tx_hash, + virtual void add_amount_and_global_output_indices(const uint64_t tx_index, const std::vector& amount_output_indices, const std::vector& global_output_indices );