aboutsummaryrefslogtreecommitdiff
path: root/src/blockchain_db/lmdb
diff options
context:
space:
mode:
Diffstat (limited to 'src/blockchain_db/lmdb')
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp88
1 files changed, 70 insertions, 18 deletions
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 808058d1e..b941fe6b1 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -949,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()));
}
@@ -1165,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
@@ -1244,6 +1270,7 @@ 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 (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()));
@@ -1256,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
@@ -1288,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);
@@ -1297,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;
}
@@ -1305,6 +1333,7 @@ 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)
@@ -1324,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
@@ -1358,6 +1388,7 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const
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);
@@ -1368,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
@@ -1378,6 +1410,7 @@ 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)
@@ -1397,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
@@ -1424,6 +1458,7 @@ 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)
@@ -1444,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
@@ -1454,6 +1490,7 @@ 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)
@@ -1475,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
@@ -1564,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;
}
@@ -1582,6 +1620,7 @@ 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 (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()));
@@ -1594,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
@@ -1669,6 +1710,7 @@ 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)
@@ -1689,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
@@ -1729,6 +1772,7 @@ 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 (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()));
@@ -1740,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)
@@ -1758,6 +1803,7 @@ 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)
@@ -1783,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)
@@ -2525,6 +2573,7 @@ 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)
@@ -2544,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)
@@ -2580,6 +2630,7 @@ 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)
@@ -2597,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()