diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 9be21b03..b941fe6b 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 @@ -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 @@ -1232,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 key(h); MDB_val result; @@ -1266,8 +1272,8 @@ uint64_t BlockchainLMDB::get_block_height(const crypto::hash& h) const 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 key(h); MDB_val result; @@ -1297,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 key(height); MDB_val result; @@ -1334,8 +1340,8 @@ uint64_t BlockchainLMDB::get_block_timestamp(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())); } MDB_val_copy key(height); MDB_val result; @@ -1378,8 +1384,8 @@ 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; @@ -1411,8 +1417,8 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t& 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 key(height); MDB_val result; @@ -1459,8 +1465,8 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh 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 key(height); @@ -1491,8 +1497,8 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height) 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 key(height); @@ -1585,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 key(h); @@ -1616,8 +1622,8 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const 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 key(h); MDB_val result; @@ -1643,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 key(h); @@ -1673,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)) @@ -1711,8 +1717,8 @@ uint64_t BlockchainLMDB::get_tx_block_height(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 key(h); @@ -1738,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); @@ -1768,8 +1774,8 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t &global_index) const 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 k(global_index); MDB_val v; @@ -1804,8 +1810,8 @@ tx_out_index BlockchainLMDB::get_output_tx_and_index_from_global(const uint64_t& 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 k(index); MDB_val v; @@ -1851,8 +1857,8 @@ std::vector BlockchainLMDB::get_tx_output_indices(const crypto::hash& std::vector 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); @@ -1896,8 +1902,8 @@ std::vector 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; @@ -1967,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 val_key(img); MDB_val unused; @@ -1988,8 +1994,8 @@ bool BlockchainLMDB::for_all_key_images(std::functionm_batch_txn = true; @@ -2258,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; } @@ -2296,8 +2302,8 @@ void BlockchainLMDB::pop_block(block& blk, std::vector& 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; } @@ -2335,8 +2341,8 @@ void BlockchainLMDB::get_output_tx_and_index_from_global(const std::vector