diff options
Diffstat (limited to 'src/blockchain_db/lmdb/db_lmdb.cpp')
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 230 |
1 files changed, 144 insertions, 86 deletions
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index de8066123..b941fe6b1 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -208,6 +208,12 @@ inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi throw0(cryptonote::DB_OPEN_FAILURE(error_string.c_str())); } +const std::string lmdb_error(const std::string& error_string, int mdb_res) +{ + const std::string full_string = error_string + mdb_strerror(mdb_res); + return full_string; +} + } // anonymous namespace namespace cryptonote @@ -943,7 +949,7 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) } else { - if (!boost::filesystem::create_directory(direc)) + if (!boost::filesystem::create_directories(direc)) throw0(DB_OPEN_FAILURE(std::string("Failed to create directory ").append(filename).c_str())); } @@ -994,8 +1000,8 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) // get a read/write MDB_txn, depending on mdb_flags mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, txn_flags, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, txn_flags, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); // open necessary databases, and set properties as needed // uses macros to avoid having to change things too many places @@ -1159,7 +1165,33 @@ void BlockchainLMDB::sync() void BlockchainLMDB::reset() { LOG_PRINT_L3("BlockchainLMDB::" << __func__); - // TODO: this + check_open(); + + mdb_txn_safe txn; + if (mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + mdb_drop(txn, m_blocks, 0); + mdb_drop(txn, m_block_timestamps, 0); + mdb_drop(txn, m_block_heights, 0); + mdb_drop(txn, m_block_hashes, 0); + mdb_drop(txn, m_block_sizes, 0); + mdb_drop(txn, m_block_diffs, 0); + mdb_drop(txn, m_block_coins, 0); + mdb_drop(txn, m_txs, 0); + mdb_drop(txn, m_tx_unlocks, 0); + mdb_drop(txn, m_tx_heights, 0); + mdb_drop(txn, m_tx_outputs, 0); + mdb_drop(txn, m_output_txs, 0); + mdb_drop(txn, m_output_indices, 0); + mdb_drop(txn, m_output_amounts, 0); + mdb_drop(txn, m_output_keys, 0); + mdb_drop(txn, m_spent_keys, 0); + mdb_drop(txn, m_hf_starting_heights, 0); + mdb_drop(txn, m_hf_versions, 0); + mdb_drop(txn, m_properties, 0); + txn.commit(); + m_height = 0; + m_num_outputs = 0; } std::vector<std::string> BlockchainLMDB::get_filenames() const @@ -1206,8 +1238,8 @@ bool BlockchainLMDB::block_exists(const crypto::hash& h) const check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<crypto::hash> key(h); MDB_val result; @@ -1238,9 +1270,10 @@ uint64_t BlockchainLMDB::get_block_height(const crypto::hash& h) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<crypto::hash> key(h); MDB_val result; @@ -1250,8 +1283,9 @@ uint64_t BlockchainLMDB::get_block_height(const crypto::hash& h) const else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a block height from the db")); + ret = *(const uint64_t *)result.mv_data; txn.commit(); - return *(const uint64_t*)result.mv_data; + return ret; } block_header BlockchainLMDB::get_block_header(const crypto::hash& h) const @@ -1269,8 +1303,8 @@ block BlockchainLMDB::get_block_from_height(const uint64_t& height) const check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<uint64_t> key(height); MDB_val result; @@ -1282,8 +1316,6 @@ block BlockchainLMDB::get_block_from_height(const uint64_t& height) const else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a block from the db")); - txn.commit(); - blobdata bd; bd.assign(reinterpret_cast<char*>(result.mv_data), result.mv_size); @@ -1291,6 +1323,8 @@ block BlockchainLMDB::get_block_from_height(const uint64_t& height) const if (!parse_and_validate_block_from_blob(bd, b)) throw0(DB_ERROR("Failed to parse block from blob retrieved from the db")); + txn.commit(); + return b; } @@ -1299,14 +1333,15 @@ uint64_t BlockchainLMDB::get_block_timestamp(const uint64_t& height) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<uint64_t> key(height); MDB_val result; @@ -1318,9 +1353,10 @@ uint64_t BlockchainLMDB::get_block_timestamp(const uint64_t& height) const else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a timestamp from the db")); + ret = *(const uint64_t *)result.mv_data; if (! m_batch_active) txn.commit(); - return *(const uint64_t*)result.mv_data; + return ret; } uint64_t BlockchainLMDB::get_top_block_timestamp() const @@ -1348,10 +1384,11 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } + size_t ret; MDB_val_copy<uint64_t> key(height); MDB_val result; auto get_result = mdb_get(*txn_ptr, m_block_sizes, &key, &result); @@ -1362,9 +1399,10 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a block size from the db")); + ret = *(const size_t *)result.mv_data; if (! m_batch_active) txn.commit(); - return *(const size_t*)result.mv_data; + return ret; } difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t& height) const @@ -1372,14 +1410,15 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t& LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " height: " << height); check_open(); + difficulty_type ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<uint64_t> key(height); MDB_val result; @@ -1391,9 +1430,10 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t& else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a cumulative difficulty from the db")); + ret = *(difficulty_type*)result.mv_data; if (! m_batch_active) txn.commit(); - return *(difficulty_type*)result.mv_data; + return ret; } difficulty_type BlockchainLMDB::get_block_difficulty(const uint64_t& height) const @@ -1418,14 +1458,15 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<uint64_t> key(height); @@ -1438,9 +1479,10 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh else if (get_result) throw0(DB_ERROR("Error attempting to retrieve a total generated coins from the db")); + ret = *(const uint64_t*)result.mv_data; if (! m_batch_active) txn.commit(); - return *(const uint64_t*)result.mv_data; + return ret; } crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height) const @@ -1448,14 +1490,15 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height) LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + crypto::hash ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<uint64_t> key(height); @@ -1469,9 +1512,10 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height) throw0(DB_ERROR(std::string("Error attempting to retrieve a block hash from the db: "). append(mdb_strerror(get_result)).c_str())); + ret = *(crypto::hash*)result.mv_data; if (! m_batch_active) txn.commit(); - return *(crypto::hash*)result.mv_data; + return ret; } std::vector<block> BlockchainLMDB::get_blocks_range(const uint64_t& h1, const uint64_t& h2) const @@ -1547,8 +1591,8 @@ bool BlockchainLMDB::tx_exists(const crypto::hash& h) const txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<crypto::hash> key(h); @@ -1558,10 +1602,10 @@ bool BlockchainLMDB::tx_exists(const crypto::hash& h) const auto get_result = mdb_get(*txn_ptr, m_txs, &key, &result); TIME_MEASURE_FINISH(time1); time_tx_exists += time1; + if (! m_batch_active) + txn.commit(); if (get_result == MDB_NOTFOUND) { - if (! m_batch_active) - txn.commit(); LOG_PRINT_L1("transaction with hash " << epee::string_tools::pod_to_hex(h) << " not found in db"); return false; } @@ -1576,9 +1620,10 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<crypto::hash> key(h); MDB_val result; @@ -1588,7 +1633,9 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const else if (get_result) throw0(DB_ERROR("DB error attempting to fetch tx unlock time from hash")); - return *(const uint64_t*)result.mv_data; + ret = *(const uint64_t*)result.mv_data; + txn.commit(); + return ret; } transaction BlockchainLMDB::get_tx(const crypto::hash& h) const @@ -1602,8 +1649,8 @@ transaction BlockchainLMDB::get_tx(const crypto::hash& h) const txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<crypto::hash> key(h); @@ -1632,8 +1679,8 @@ uint64_t BlockchainLMDB::get_tx_count() const check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_stat db_stats; if (mdb_stat(txn, m_txs, &db_stats)) @@ -1663,14 +1710,15 @@ uint64_t BlockchainLMDB::get_tx_block_height(const crypto::hash& h) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<crypto::hash> key(h); @@ -1683,10 +1731,11 @@ uint64_t BlockchainLMDB::get_tx_block_height(const crypto::hash& h) const else if (get_result) throw0(DB_ERROR("DB error attempting to fetch tx height from hash")); + ret = *(const uint64_t*)result.mv_data; if (! m_batch_active) txn.commit(); - return *(const uint64_t*)result.mv_data; + return ret; } uint64_t BlockchainLMDB::get_num_outputs(const uint64_t& amount) const @@ -1695,8 +1744,8 @@ uint64_t BlockchainLMDB::get_num_outputs(const uint64_t& amount) const check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); lmdb_cur cur(txn, m_output_amounts); @@ -1723,9 +1772,10 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t &global_index) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + output_data_t ret; mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<uint64_t> k(global_index); MDB_val v; @@ -1734,8 +1784,9 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t &global_index) const throw1(OUTPUT_DNE("Attempting to get output pubkey by global index, but key does not exist")); else if (get_result) throw0(DB_ERROR("Error attempting to retrieve an output pubkey from the db")); + ret = *(output_data_t *) v.mv_data; txn.commit(); - return *(output_data_t *) v.mv_data; + return ret; } output_data_t BlockchainLMDB::get_output_key(const uint64_t& amount, const uint64_t& index) @@ -1752,14 +1803,15 @@ tx_out_index BlockchainLMDB::get_output_tx_and_index_from_global(const uint64_t& LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + tx_out_index ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } MDB_val_copy<uint64_t> k(index); MDB_val v; @@ -1777,10 +1829,12 @@ tx_out_index BlockchainLMDB::get_output_tx_and_index_from_global(const uint64_t& throw1(OUTPUT_DNE("output with given index not in db")); else if (get_result) throw0(DB_ERROR("DB error attempting to fetch output tx index")); + + ret = tx_out_index(tx_hash, *(const uint64_t *)v.mv_data); if (! m_batch_active) txn.commit(); - return tx_out_index(tx_hash, *(const uint64_t *)v.mv_data); + return ret; } tx_out_index BlockchainLMDB::get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) @@ -1803,8 +1857,8 @@ std::vector<uint64_t> BlockchainLMDB::get_tx_output_indices(const crypto::hash& std::vector<uint64_t> index_vec; mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); lmdb_cur cur(txn, m_tx_outputs); @@ -1848,8 +1902,8 @@ std::vector<uint64_t> BlockchainLMDB::get_tx_amount_output_indices(const crypto: transaction tx = get_tx(h); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); uint64_t i = 0; uint64_t global_index; @@ -1919,8 +1973,8 @@ bool BlockchainLMDB::has_key_image(const crypto::key_image& img) const check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val_copy<crypto::key_image> val_key(img); MDB_val unused; @@ -1940,8 +1994,8 @@ bool BlockchainLMDB::for_all_key_images(std::function<bool(const crypto::key_ima check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val k; MDB_val v; @@ -1976,8 +2030,8 @@ bool BlockchainLMDB::for_all_blocks(std::function<bool(uint64_t, const crypto::h check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val k; MDB_val v; @@ -2020,8 +2074,8 @@ bool BlockchainLMDB::for_all_transactions(std::function<bool(const crypto::hash& check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val k; MDB_val v; @@ -2061,8 +2115,8 @@ bool BlockchainLMDB::for_all_outputs(std::function<bool(uint64_t amount, const c check_open(); mdb_txn_safe txn; - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); MDB_val k; MDB_val v; @@ -2112,8 +2166,8 @@ void BlockchainLMDB::batch_start(uint64_t batch_num_blocks) m_write_batch_txn = new mdb_txn_safe(); // NOTE: need to make sure it's destroyed properly when done - if (mdb_txn_begin(m_env, NULL, 0, *m_write_batch_txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, *m_write_batch_txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); // indicates this transaction is for batch transactions, but not whether it's // active m_write_batch_txn->m_batch_txn = true; @@ -2210,8 +2264,8 @@ uint64_t BlockchainLMDB::add_block(const block& blk, const size_t& block_size, c mdb_txn_safe txn; if (! m_batch_active) { - if (mdb_txn_begin(m_env, NULL, 0, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); m_write_txn = &txn; } @@ -2248,8 +2302,8 @@ void BlockchainLMDB::pop_block(block& blk, std::vector<transaction>& txs) mdb_txn_safe txn; if (! m_batch_active) { - if (mdb_txn_begin(m_env, NULL, 0, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); m_write_txn = &txn; } @@ -2287,8 +2341,8 @@ void BlockchainLMDB::get_output_tx_and_index_from_global(const std::vector<uint6 txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } for (const uint64_t &index : global_indices) @@ -2339,8 +2393,8 @@ void BlockchainLMDB::get_output_global_indices(const uint64_t& amount, const std txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } lmdb_cur cur(*txn_ptr, m_output_amounts); @@ -2445,8 +2499,8 @@ void BlockchainLMDB::get_output_key(const uint64_t &amount, const std::vector<ui txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } for (const uint64_t &index : global_indices) { @@ -2500,8 +2554,8 @@ void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t hei txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, 0, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); txn_ptr = &txn; } @@ -2519,14 +2573,15 @@ uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint64_t ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); txn_ptr = &txn; } @@ -2538,9 +2593,10 @@ uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const if (result) throw0(DB_ERROR("Error attempting to retrieve a hard fork starting height from the db")); + ret = *(const uint64_t*)val_ret.mv_data; if (!m_batch_active) txn.commit(); - return *(const uint64_t*)val_ret.mv_data; + return ret; } void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) @@ -2555,8 +2611,8 @@ void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, 0, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); txn_ptr = &txn; } @@ -2574,14 +2630,15 @@ uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); + uint8_t ret; mdb_txn_safe txn; mdb_txn_safe* txn_ptr = &txn; if (m_batch_active) txn_ptr = m_write_txn; else { - if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) - throw0(DB_ERROR("Failed to create a transaction for the db")); + if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); txn_ptr = &txn; } @@ -2591,9 +2648,10 @@ uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const if (result == MDB_NOTFOUND || result) throw0(DB_ERROR("Error attempting to retrieve a hard fork version from the db")); + ret = *(const uint8_t*)val_ret.mv_data; if (!m_batch_active) txn.commit(); - return *(const uint8_t*)val_ret.mv_data; + return ret; } void BlockchainLMDB::fixup() |