aboutsummaryrefslogtreecommitdiff
path: root/src/blockchain_db
diff options
context:
space:
mode:
Diffstat (limited to 'src/blockchain_db')
-rw-r--r--src/blockchain_db/blockchain_db.cpp72
-rw-r--r--src/blockchain_db/blockchain_db.h25
-rw-r--r--src/blockchain_db/db_types.h7
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp52
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.h5
5 files changed, 144 insertions, 17 deletions
diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp
index a6774a25c..d62a250ff 100644
--- a/src/blockchain_db/blockchain_db.cpp
+++ b/src/blockchain_db/blockchain_db.cpp
@@ -33,6 +33,19 @@
#include "profile_tools.h"
#include "ringct/rctOps.h"
+#include "lmdb/db_lmdb.h"
+#ifdef BERKELEY_DB
+#include "berkeleydb/db_bdb.h"
+#endif
+
+static const char *db_types[] = {
+ "lmdb",
+#ifdef BERKELEY_DB
+ "berkeley",
+#endif
+ NULL
+};
+
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "blockchain.db"
@@ -41,6 +54,65 @@ using epee::string_tools::pod_to_hex;
namespace cryptonote
{
+bool blockchain_valid_db_type(const std::string& db_type)
+{
+ int i;
+ for (i=0; db_types[i]; i++)
+ {
+ if (db_types[i] == db_type)
+ return true;
+ }
+ return false;
+}
+
+std::string blockchain_db_types(const std::string& sep)
+{
+ int i;
+ std::string ret = "";
+ for (i=0; db_types[i]; i++)
+ {
+ if (i)
+ ret += sep;
+ ret += db_types[i];
+ }
+ return ret;
+}
+
+std::string arg_db_type_description = "Specify database type, available: " + cryptonote::blockchain_db_types(", ");
+const command_line::arg_descriptor<std::string> arg_db_type = {
+ "db-type"
+, arg_db_type_description.c_str()
+, DEFAULT_DB_TYPE
+};
+const command_line::arg_descriptor<std::string> arg_db_sync_mode = {
+ "db-sync-mode"
+, "Specify sync option, using format [safe|fast|fastest]:[sync|async]:[nblocks_per_sync]."
+, "fast:async:1000"
+};
+const command_line::arg_descriptor<bool> arg_db_salvage = {
+ "db-salvage"
+, "Try to salvage a blockchain database if it seems corrupted"
+, false
+};
+
+BlockchainDB *new_db(const std::string& db_type)
+{
+ if (db_type == "lmdb")
+ return new BlockchainLMDB();
+#if defined(BERKELEY_DB)
+ if (db_type == "berkeley")
+ return new BlockchainBDB();
+#endif
+ return NULL;
+}
+
+void BlockchainDB::init_options(boost::program_options::options_description& desc)
+{
+ command_line::add_arg(desc, arg_db_type);
+ command_line::add_arg(desc, arg_db_sync_mode);
+ command_line::add_arg(desc, arg_db_salvage);
+}
+
void BlockchainDB::pop_block()
{
block blk;
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index 27e63801d..85a494ce7 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -33,6 +33,8 @@
#include <list>
#include <string>
#include <exception>
+#include <boost/program_options.hpp>
+#include "common/command_line.h"
#include "crypto/hash.h"
#include "cryptonote_protocol/blobdatatype.h"
#include "cryptonote_basic/cryptonote_basic.h"
@@ -101,6 +103,10 @@ namespace cryptonote
/** a pair of <transaction hash, output index>, typedef for convenience */
typedef std::pair<crypto::hash, uint64_t> tx_out_index;
+extern const command_line::arg_descriptor<std::string> arg_db_type;
+extern const command_line::arg_descriptor<std::string> arg_db_sync_mode;
+extern const command_line::arg_descriptor<bool, false> arg_db_salvage;
+
#pragma pack(push, 1)
/**
@@ -145,6 +151,12 @@ struct txpool_tx_meta_t
uint8_t padding[77]; // till 192 bytes
};
+#define DBF_SAFE 1
+#define DBF_FAST 2
+#define DBF_FASTEST 4
+#define DBF_RDONLY 8
+#define DBF_SALVAGE 0x10
+
/***********************************
* Exception Definitions
***********************************/
@@ -530,6 +542,11 @@ public:
virtual ~BlockchainDB() { };
/**
+ * @brief init command line options
+ */
+ static void init_options(boost::program_options::options_description& desc);
+
+ /**
* @brief reset profiling stats
*/
void reset_stats();
@@ -600,6 +617,13 @@ public:
virtual void sync() = 0;
/**
+ * @brief toggle safe syncs for the DB
+ *
+ * Used to switch DBF_SAFE on or off after starting up with DBF_FAST.
+ */
+ virtual void safesyncmode(const bool onoff) = 0;
+
+ /**
* @brief Remove everything from the BlockchainDB
*
* This function should completely remove all data from a BlockchainDB.
@@ -1491,6 +1515,7 @@ public:
}; // class BlockchainDB
+BlockchainDB *new_db(const std::string& db_type);
} // namespace cryptonote
diff --git a/src/blockchain_db/db_types.h b/src/blockchain_db/db_types.h
index 6c21b029e..8e2f58a61 100644
--- a/src/blockchain_db/db_types.h
+++ b/src/blockchain_db/db_types.h
@@ -31,9 +31,6 @@
namespace cryptonote
{
-
- const std::unordered_set<std::string> blockchain_db_types =
- { "lmdb"
- };
-
+ bool blockchain_valid_db_type(const std::string& db_type);
+ std::string blockchain_db_types(const std::string& sep);
} // namespace cryptonote
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 4441c7578..c6e24ef98 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -1083,9 +1083,10 @@ BlockchainLMDB::BlockchainLMDB(bool batch_transactions)
m_hardfork = nullptr;
}
-void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
+void BlockchainLMDB::open(const std::string& filename, const int db_flags)
{
int result;
+ int mdb_flags = MDB_NORDAHEAD;
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -1124,6 +1125,15 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
size_t mapsize = DEFAULT_MAPSIZE;
+ if (db_flags & DBF_FAST)
+ mdb_flags |= MDB_NOSYNC;
+ if (db_flags & DBF_FASTEST)
+ mdb_flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC;
+ if (db_flags & DBF_RDONLY)
+ mdb_flags = MDB_RDONLY;
+ if (db_flags & DBF_SALVAGE)
+ mdb_flags |= MDB_PREVSNAPSHOT;
+
if (auto result = mdb_env_open(m_env, filename.c_str(), mdb_flags, 0644))
throw0(DB_ERROR(lmdb_error("Failed to open lmdb environment: ", result).c_str()));
@@ -1308,6 +1318,11 @@ void BlockchainLMDB::sync()
}
}
+void BlockchainLMDB::safesyncmode(const bool onoff)
+{
+ mdb_env_set_flags(m_env, MDB_NOSYNC|MDB_MAPASYNC, !onoff);
+}
+
void BlockchainLMDB::reset()
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -2405,8 +2420,8 @@ bool BlockchainLMDB::for_blocks_range(const uint64_t& h1, const uint64_t& h2, st
MDB_cursor_op op;
if (h1)
{
- MDB_val_set(k, h1);
- op = MDB_SET;
+ k = MDB_val{sizeof(h1), (void*)&h1};
+ op = MDB_SET;
} else
{
op = MDB_FIRST;
@@ -2589,6 +2604,16 @@ void BlockchainLMDB::batch_commit()
memset(&m_wcursors, 0, sizeof(m_wcursors));
}
+void BlockchainLMDB::cleanup_batch()
+{
+ // for destruction of batch transaction
+ m_write_txn = nullptr;
+ delete m_write_batch_txn;
+ m_write_batch_txn = nullptr;
+ m_batch_active = false;
+ memset(&m_wcursors, 0, sizeof(m_wcursors));
+}
+
void BlockchainLMDB::batch_stop()
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -2603,15 +2628,18 @@ void BlockchainLMDB::batch_stop()
check_open();
LOG_PRINT_L3("batch transaction: committing...");
TIME_MEASURE_START(time1);
- m_write_txn->commit();
- TIME_MEASURE_FINISH(time1);
- time_commit1 += time1;
- // for destruction of batch transaction
- m_write_txn = nullptr;
- delete m_write_batch_txn;
- m_write_batch_txn = nullptr;
- m_batch_active = false;
- memset(&m_wcursors, 0, sizeof(m_wcursors));
+ try
+ {
+ m_write_txn->commit();
+ TIME_MEASURE_FINISH(time1);
+ time_commit1 += time1;
+ cleanup_batch();
+ }
+ catch (const std::exception &e)
+ {
+ cleanup_batch();
+ throw;
+ }
LOG_PRINT_L3("batch transaction: end");
}
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index 14e5d34e2..90274b904 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -165,6 +165,8 @@ public:
virtual void sync();
+ virtual void safesyncmode(const bool onoff);
+
virtual void reset();
virtual std::vector<std::string> get_filenames() const;
@@ -366,6 +368,9 @@ private:
// migrate from DB version 0 to 1
void migrate_0_1();
+ void cleanup_batch();
+
+private:
MDB_env* m_env;
MDB_dbi m_blocks;