BlockchainLMDB: Use DB error helper consistently

This commit is contained in:
warptangent 2016-03-03 21:27:13 -08:00
parent c5932eb5ea
commit bdec7cbfb3
No known key found for this signature in database
GPG key ID: 0E490BEBFBE4E92D

View file

@ -228,14 +228,14 @@ inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi
if (!m_cur_ ## name) { \ if (!m_cur_ ## name) { \
int result = mdb_cursor_open(*m_write_txn, m_ ## name, &m_cur_ ## name); \ int result = mdb_cursor_open(*m_write_txn, m_ ## name, &m_cur_ ## name); \
if (result) \ if (result) \
throw0(DB_ERROR(std::string("Failed to open cursor: ").append(mdb_strerror(result)).c_str())); \ throw0(DB_ERROR(lmdb_error("Failed to open cursor: ", result).c_str())); \
} }
#define RCURSOR(name) \ #define RCURSOR(name) \
if (!m_cur_ ## name) { \ if (!m_cur_ ## name) { \
int result = mdb_cursor_open(m_txn, m_ ## name, (MDB_cursor **)&m_cur_ ## name); \ int result = mdb_cursor_open(m_txn, m_ ## name, (MDB_cursor **)&m_cur_ ## name); \
if (result) \ if (result) \
throw0(DB_ERROR(std::string("Failed to open cursor: ").append(mdb_strerror(result)).c_str())); \ throw0(DB_ERROR(lmdb_error("Failed to open cursor: ", result).c_str())); \
if (!m_write_txn) \ if (!m_write_txn) \
m_tinfo->m_ti_rflags.m_rf_ ## name = true; \ m_tinfo->m_ti_rflags.m_rf_ ## name = true; \
} else if (!m_write_txn && !m_tinfo->m_ti_rflags.m_rf_ ## name) { \ } else if (!m_write_txn && !m_tinfo->m_ti_rflags.m_rf_ ## name) { \
@ -300,7 +300,7 @@ void mdb_txn_safe::commit(std::string message)
if (auto result = mdb_txn_commit(m_txn)) if (auto result = mdb_txn_commit(m_txn))
{ {
m_txn = nullptr; m_txn = nullptr;
throw0(DB_ERROR((message + ": ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error(message + ": ", result).c_str()));
} }
m_txn = nullptr; m_txn = nullptr;
} }
@ -592,35 +592,35 @@ void BlockchainLMDB::add_block(const block& blk, const size_t& block_size, const
MDB_val_copy<blobdata> blob(block_to_blob(blk)); MDB_val_copy<blobdata> blob(block_to_blob(blk));
result = mdb_cursor_put(m_cur_blocks, &key, &blob, MDB_APPEND); result = mdb_cursor_put(m_cur_blocks, &key, &blob, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block blob to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block blob to db transaction: ", result).c_str()));
MDB_val_copy<size_t> sz(block_size); MDB_val_copy<size_t> sz(block_size);
result = mdb_cursor_put(m_cur_block_sizes, &key, &sz, MDB_APPEND); result = mdb_cursor_put(m_cur_block_sizes, &key, &sz, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block size to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block size to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> ts(blk.timestamp); MDB_val_copy<uint64_t> ts(blk.timestamp);
result = mdb_cursor_put(m_cur_block_timestamps, &key, &ts, MDB_APPEND); result = mdb_cursor_put(m_cur_block_timestamps, &key, &ts, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block timestamp to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block timestamp to db transaction: ", result).c_str()));
MDB_val_copy<difficulty_type> diff(cumulative_difficulty); MDB_val_copy<difficulty_type> diff(cumulative_difficulty);
result = mdb_cursor_put(m_cur_block_diffs, &key, &diff, MDB_APPEND); result = mdb_cursor_put(m_cur_block_diffs, &key, &diff, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block cumulative difficulty to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block cumulative difficulty to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> coinsgen(coins_generated); MDB_val_copy<uint64_t> coinsgen(coins_generated);
result = mdb_cursor_put(m_cur_block_coins, &key, &coinsgen, MDB_APPEND); result = mdb_cursor_put(m_cur_block_coins, &key, &coinsgen, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block total generated coins to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block total generated coins to db transaction: ", result).c_str()));
result = mdb_cursor_put(m_cur_block_heights, &val_h, &key, 0); result = mdb_cursor_put(m_cur_block_heights, &val_h, &key, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block height by hash to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block height by hash to db transaction: ", result).c_str()));
result = mdb_cursor_put(m_cur_block_hashes, &key, &val_h, MDB_APPEND); result = mdb_cursor_put(m_cur_block_hashes, &key, &val_h, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add block hash to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add block hash to db transaction: ", result).c_str()));
m_cum_size += block_size; m_cum_size += block_size;
m_cum_count++; m_cum_count++;
@ -681,17 +681,17 @@ void BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const tr
MDB_val_copy<blobdata> blob(tx_to_blob(tx)); MDB_val_copy<blobdata> blob(tx_to_blob(tx));
result = mdb_cursor_put(m_cur_txs, &val_h, &blob, 0); result = mdb_cursor_put(m_cur_txs, &val_h, &blob, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add tx blob to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add tx blob to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> height(m_height); MDB_val_copy<uint64_t> height(m_height);
result = mdb_cursor_put(m_cur_tx_heights, &val_h, &height, 0); result = mdb_cursor_put(m_cur_tx_heights, &val_h, &height, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add tx block height to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add tx block height to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> unlock_time(tx.unlock_time); MDB_val_copy<uint64_t> unlock_time(tx.unlock_time);
result = mdb_cursor_put(m_cur_tx_unlocks, &val_h, &unlock_time, 0); result = mdb_cursor_put(m_cur_tx_unlocks, &val_h, &unlock_time, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add tx unlock time to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add tx unlock time to db transaction: ", result).c_str()));
} }
void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx)
@ -717,7 +717,7 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const
if (result == MDB_NOTFOUND) if (result == MDB_NOTFOUND)
LOG_PRINT_L1("tx has no outputs to remove: " << tx_hash); LOG_PRINT_L1("tx has no outputs to remove: " << tx_hash);
else if (result) else if (result)
throw1(DB_ERROR(std::string("Failed to add removal of tx outputs to db transaction: ").append(mdb_strerror(result)).c_str())); throw1(DB_ERROR(lmdb_error("Failed to add removal of tx outputs to db transaction: ", result).c_str()));
} }
void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time) void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time)
@ -739,20 +739,20 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou
result = mdb_cursor_put(m_cur_output_txs, &k, &v, MDB_APPEND); result = mdb_cursor_put(m_cur_output_txs, &k, &v, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add output tx hash to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add output tx hash to db transaction: ", result).c_str()));
result = mdb_cursor_put(m_cur_tx_outputs, &v, &k, 0); result = mdb_cursor_put(m_cur_tx_outputs, &v, &k, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add <tx hash, global output index> to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add <tx hash, global output index> to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> val_local_index(local_index); MDB_val_copy<uint64_t> val_local_index(local_index);
result = mdb_cursor_put(m_cur_output_indices, &k, &val_local_index, MDB_APPEND); result = mdb_cursor_put(m_cur_output_indices, &k, &val_local_index, MDB_APPEND);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add tx output index to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add tx output index to db transaction: ", result).c_str()));
MDB_val_copy<uint64_t> val_amount(tx_output.amount); MDB_val_copy<uint64_t> val_amount(tx_output.amount);
result = mdb_cursor_put(m_cur_output_amounts, &val_amount, &k, 0); result = mdb_cursor_put(m_cur_output_amounts, &val_amount, &k, 0);
if (result) if (result)
throw0(DB_ERROR(std::string("Failed to add output amount to db transaction: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to add output amount to db transaction: ", result).c_str()));
if (tx_output.target.type() == typeid(txout_to_key)) if (tx_output.target.type() == typeid(txout_to_key))
{ {
@ -933,7 +933,7 @@ void BlockchainLMDB::add_spent_key(const crypto::key_image& k_image)
unused.mv_size = sizeof(char); unused.mv_size = sizeof(char);
unused.mv_data = &anything; unused.mv_data = &anything;
if (auto result = mdb_cursor_put(m_cur_spent_keys, &val_key, &unused, 0)) if (auto result = mdb_cursor_put(m_cur_spent_keys, &val_key, &unused, 0))
throw1(DB_ERROR(std::string("Error adding spent key image to db transaction: ").append(mdb_strerror(result)).c_str())); throw1(DB_ERROR(lmdb_error("Error adding spent key image to db transaction: ", result).c_str()));
} }
void BlockchainLMDB::remove_spent_key(const crypto::key_image& k_image) void BlockchainLMDB::remove_spent_key(const crypto::key_image& k_image)
@ -1059,7 +1059,7 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
size_t mapsize = DEFAULT_MAPSIZE; size_t mapsize = DEFAULT_MAPSIZE;
if (auto result = mdb_env_open(m_env, filename.c_str(), mdb_flags, 0644)) if (auto result = mdb_env_open(m_env, filename.c_str(), mdb_flags, 0644))
throw0(DB_ERROR(std::string("Failed to open lmdb environment: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to open lmdb environment: ", result).c_str()));
MDB_envinfo mei; MDB_envinfo mei;
mdb_env_info(m_env, &mei); mdb_env_info(m_env, &mei);
@ -1068,7 +1068,7 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
if (cur_mapsize < mapsize) if (cur_mapsize < mapsize)
{ {
if (auto result = mdb_env_set_mapsize(m_env, mapsize)) if (auto result = mdb_env_set_mapsize(m_env, mapsize))
throw0(DB_ERROR(std::string("Failed to set max memory map size: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to set max memory map size: ", result).c_str()));
mdb_env_info(m_env, &mei); mdb_env_info(m_env, &mei);
cur_mapsize = (double)mei.me_mapsize; cur_mapsize = (double)mei.me_mapsize;
LOG_PRINT_L1("LMDB memory map size: " << cur_mapsize); LOG_PRINT_L1("LMDB memory map size: " << cur_mapsize);
@ -1248,7 +1248,7 @@ void BlockchainLMDB::sync()
// MDB_NOMETASYNC. Force flush to be synchronous. // MDB_NOMETASYNC. Force flush to be synchronous.
if (auto result = mdb_env_sync(m_env, true)) if (auto result = mdb_env_sync(m_env, true))
{ {
throw0(DB_ERROR(std::string("Failed to sync database: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Failed to sync database: ", result).c_str()));
} }
} }
@ -1611,8 +1611,7 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height)
throw0(BLOCK_DNE(std::string("Attempt to get hash from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- hash not in db").c_str())); throw0(BLOCK_DNE(std::string("Attempt to get hash from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- hash not in db").c_str()));
} }
else if (get_result) else if (get_result)
throw0(DB_ERROR(std::string("Error attempting to retrieve a block hash from the db: "). throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a block hash from the db: ", get_result).c_str()));
append(mdb_strerror(get_result)).c_str()));
crypto::hash ret = *(const crypto::hash*)result.mv_data; crypto::hash ret = *(const crypto::hash*)result.mv_data;
TXN_POSTFIX_RDONLY(); TXN_POSTFIX_RDONLY();
@ -2773,7 +2772,7 @@ void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t hei
MDB_val_copy<uint8_t> val_key(version); MDB_val_copy<uint8_t> val_key(version);
MDB_val_copy<uint64_t> val_value(height); MDB_val_copy<uint64_t> val_value(height);
if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, MDB_APPEND)) if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, MDB_APPEND))
throw1(DB_ERROR(std::string("Error adding hard fork starting height to db transaction: ").append(mdb_strerror(result)).c_str())); throw1(DB_ERROR(lmdb_error("Error adding hard fork starting height to db transaction: ", result).c_str()));
TXN_BLOCK_POSTFIX_SUCCESS(); TXN_BLOCK_POSTFIX_SUCCESS();
} }
@ -2817,7 +2816,7 @@ void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version)
if (result == MDB_KEYEXIST) if (result == MDB_KEYEXIST)
result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0); result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0);
if (result) if (result)
throw1(DB_ERROR(std::string("Error adding hard fork version to db transaction: ").append(mdb_strerror(result)).c_str())); throw1(DB_ERROR(lmdb_error("Error adding hard fork version to db transaction: ", result).c_str()));
TXN_BLOCK_POSTFIX_SUCCESS(); TXN_BLOCK_POSTFIX_SUCCESS();
} }
@ -2835,7 +2834,7 @@ uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const
MDB_val val_ret; MDB_val val_ret;
auto result = mdb_cursor_get(m_cur_hf_versions, &val_key, &val_ret, MDB_SET); auto result = mdb_cursor_get(m_cur_hf_versions, &val_key, &val_ret, MDB_SET);
if (result == MDB_NOTFOUND || result) if (result == MDB_NOTFOUND || result)
throw0(DB_ERROR(std::string("Error attempting to retrieve a hard fork version at height ").append(boost::lexical_cast<std::string>(height)).append(" from the db: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a hard fork version at height " + boost::lexical_cast<std::string>(height) + " from the db: ", result).c_str()));
uint8_t ret = *(const uint8_t*)val_ret.mv_data; uint8_t ret = *(const uint8_t*)val_ret.mv_data;
TXN_POSTFIX_RDONLY(); TXN_POSTFIX_RDONLY();
@ -2847,7 +2846,7 @@ bool BlockchainLMDB::is_read_only() const
unsigned int flags; unsigned int flags;
auto result = mdb_env_get_flags(m_env, &flags); auto result = mdb_env_get_flags(m_env, &flags);
if (result) if (result)
throw0(DB_ERROR(std::string("Error getting database environment info: ").append(mdb_strerror(result)).c_str())); throw0(DB_ERROR(lmdb_error("Error getting database environment info: ", result).c_str()));
if (flags & MDB_RDONLY) if (flags & MDB_RDONLY)
return true; return true;