Merge pull request #532

79ff6a5 Add LMDB error string to database transaction exception message (warptangent)
This commit is contained in:
Riccardo Spagni 2015-12-13 22:16:02 +02:00
commit 0eb3b6645c
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
1 changed files with 74 additions and 68 deletions

View File

@ -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())); 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 } // anonymous namespace
namespace cryptonote 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 // get a read/write MDB_txn, depending on mdb_flags
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, txn_flags, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, txn_flags, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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 // open necessary databases, and set properties as needed
// uses macros to avoid having to change things too many places // 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(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
MDB_val result; MDB_val result;
@ -1266,8 +1272,8 @@ uint64_t BlockchainLMDB::get_block_height(const crypto::hash& h) const
uint64_t ret; uint64_t ret;
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
MDB_val result; MDB_val result;
@ -1297,8 +1303,8 @@ block BlockchainLMDB::get_block_from_height(const uint64_t& height) const
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> key(height);
MDB_val result; MDB_val result;
@ -1334,8 +1340,8 @@ uint64_t BlockchainLMDB::get_block_timestamp(const uint64_t& height) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> key(height);
MDB_val result; MDB_val result;
@ -1378,8 +1384,8 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
} }
size_t ret; size_t ret;
@ -1411,8 +1417,8 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t&
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> key(height);
MDB_val result; MDB_val result;
@ -1459,8 +1465,8 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> key(height);
@ -1491,8 +1497,8 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height)
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> key(height);
@ -1585,8 +1591,8 @@ bool BlockchainLMDB::tx_exists(const crypto::hash& h) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
@ -1616,8 +1622,8 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const
uint64_t ret; uint64_t ret;
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
MDB_val result; MDB_val result;
@ -1643,8 +1649,8 @@ transaction BlockchainLMDB::get_tx(const crypto::hash& h) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
@ -1673,8 +1679,8 @@ uint64_t BlockchainLMDB::get_tx_count() const
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
MDB_stat db_stats; MDB_stat db_stats;
if (mdb_stat(txn, m_txs, &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; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::hash> key(h);
@ -1738,8 +1744,8 @@ uint64_t BlockchainLMDB::get_num_outputs(const uint64_t& amount) const
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
lmdb_cur cur(txn, m_output_amounts); 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; output_data_t ret;
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> k(global_index);
MDB_val v; 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; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<uint64_t> k(index);
MDB_val v; MDB_val v;
@ -1851,8 +1857,8 @@ std::vector<uint64_t> BlockchainLMDB::get_tx_output_indices(const crypto::hash&
std::vector<uint64_t> index_vec; std::vector<uint64_t> index_vec;
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
lmdb_cur cur(txn, m_tx_outputs); lmdb_cur cur(txn, m_tx_outputs);
@ -1896,8 +1902,8 @@ std::vector<uint64_t> BlockchainLMDB::get_tx_amount_output_indices(const crypto:
transaction tx = get_tx(h); transaction tx = get_tx(h);
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
uint64_t i = 0; uint64_t i = 0;
uint64_t global_index; uint64_t global_index;
@ -1967,8 +1973,8 @@ bool BlockchainLMDB::has_key_image(const crypto::key_image& img) const
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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_copy<crypto::key_image> val_key(img);
MDB_val unused; MDB_val unused;
@ -1988,8 +1994,8 @@ bool BlockchainLMDB::for_all_key_images(std::function<bool(const crypto::key_ima
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
MDB_val k; MDB_val k;
MDB_val v; MDB_val v;
@ -2024,8 +2030,8 @@ bool BlockchainLMDB::for_all_blocks(std::function<bool(uint64_t, const crypto::h
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
MDB_val k; MDB_val k;
MDB_val v; MDB_val v;
@ -2068,8 +2074,8 @@ bool BlockchainLMDB::for_all_transactions(std::function<bool(const crypto::hash&
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
MDB_val k; MDB_val k;
MDB_val v; MDB_val v;
@ -2109,8 +2115,8 @@ bool BlockchainLMDB::for_all_outputs(std::function<bool(uint64_t amount, const c
check_open(); check_open();
mdb_txn_safe txn; mdb_txn_safe txn;
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
MDB_val k; MDB_val k;
MDB_val v; MDB_val v;
@ -2160,8 +2166,8 @@ void BlockchainLMDB::batch_start(uint64_t batch_num_blocks)
m_write_batch_txn = new mdb_txn_safe(); m_write_batch_txn = new mdb_txn_safe();
// NOTE: need to make sure it's destroyed properly when done // NOTE: need to make sure it's destroyed properly when done
if (mdb_txn_begin(m_env, NULL, 0, *m_write_batch_txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, *m_write_batch_txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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 // indicates this transaction is for batch transactions, but not whether it's
// active // active
m_write_batch_txn->m_batch_txn = true; m_write_batch_txn->m_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; mdb_txn_safe txn;
if (! m_batch_active) if (! m_batch_active)
{ {
if (mdb_txn_begin(m_env, NULL, 0, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
m_write_txn = &txn; m_write_txn = &txn;
} }
@ -2296,8 +2302,8 @@ void BlockchainLMDB::pop_block(block& blk, std::vector<transaction>& txs)
mdb_txn_safe txn; mdb_txn_safe txn;
if (! m_batch_active) if (! m_batch_active)
{ {
if (mdb_txn_begin(m_env, NULL, 0, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
m_write_txn = &txn; m_write_txn = &txn;
} }
@ -2335,8 +2341,8 @@ void BlockchainLMDB::get_output_tx_and_index_from_global(const std::vector<uint6
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
} }
for (const uint64_t &index : global_indices) for (const uint64_t &index : global_indices)
@ -2387,8 +2393,8 @@ void BlockchainLMDB::get_output_global_indices(const uint64_t& amount, const std
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); 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); lmdb_cur cur(*txn_ptr, m_output_amounts);
@ -2493,8 +2499,8 @@ void BlockchainLMDB::get_output_key(const uint64_t &amount, const std::vector<ui
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
} }
for (const uint64_t &index : global_indices) for (const uint64_t &index : global_indices)
{ {
@ -2548,8 +2554,8 @@ void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t hei
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, 0, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
txn_ptr = &txn; txn_ptr = &txn;
} }
@ -2574,8 +2580,8 @@ uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
txn_ptr = &txn; txn_ptr = &txn;
} }
@ -2605,8 +2611,8 @@ void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version)
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, 0, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, 0, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
txn_ptr = &txn; txn_ptr = &txn;
} }
@ -2631,8 +2637,8 @@ uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const
txn_ptr = m_write_txn; txn_ptr = m_write_txn;
else else
{ {
if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) if (auto mdb_res = mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
throw0(DB_ERROR("Failed to create a transaction for the db")); throw0(DB_ERROR(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str()));
txn_ptr = &txn; txn_ptr = &txn;
} }