diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2015-09-20 18:41:38 +0100 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2015-09-20 18:42:52 +0100 |
commit | 5b11a89a76cf44c0fb98d98b05eb1c9e4ddce0c4 (patch) | |
tree | 8f6b2d879f7e8b5c224a4592373bd37afc2a9957 /src/blockchain_db | |
parent | hardfork: remove the "parts are copyright cryptonote" notices (diff) | |
download | monero-5b11a89a76cf44c0fb98d98b05eb1c9e4ddce0c4.tar.xz |
hardfork: most state now saved to the DB
There will be a delay on first load of an existing blockchain
as it gets reparsed for this state data.
Diffstat (limited to 'src/blockchain_db')
-rw-r--r-- | src/blockchain_db/berkeleydb/db_bdb.cpp | 75 | ||||
-rw-r--r-- | src/blockchain_db/berkeleydb/db_bdb.h | 10 | ||||
-rw-r--r-- | src/blockchain_db/blockchain_db.h | 6 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 124 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.h | 9 |
5 files changed, 224 insertions, 0 deletions
diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp index a990d7aaf..2a9e9a7f8 100644 --- a/src/blockchain_db/berkeleydb/db_bdb.cpp +++ b/src/blockchain_db/berkeleydb/db_bdb.cpp @@ -118,6 +118,9 @@ const char* const BDB_OUTPUT_KEYS = "output_keys"; const char* const BDB_SPENT_KEYS = "spent_keys"; +const char* const BDB_HF_STARTING_HEIGHTS = "hf_starting_heights"; +const char* const BDB_HF_VERSIONS = "hf_versions"; + const unsigned int MB = 1024 * 1024; // ND: FIXME: db keeps running out of locks when doing full syncs. Possible bug??? Set it to 5K for now. const unsigned int DB_MAX_LOCKS = 5000; @@ -667,6 +670,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_spent_keys = new Db(m_env, 0); + m_hf_starting_heights = new Db(m_env, 0); + m_hf_versions = new Db(m_env, 0); + // Tell DB about Dbs that need duplicate support // Note: no need to tell about sorting, // as the default is insertion order, which we want @@ -684,6 +690,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_output_indices->set_re_len(sizeof(uint64_t)); m_output_keys->set_re_len(sizeof(output_data_t)); + m_hf_starting_heights->set_re_len(sizeof(uint64_t)); + m_hf_versions->set_re_len(sizeof(uint8_t)); + //TODO: Find out if we need to do Db::set_flags(DB_RENUMBER) // for the RECNO databases. We shouldn't as we're only // inserting/removing from the end, but we'll see. @@ -713,6 +722,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_spent_keys->open(txn, BDB_SPENT_KEYS, NULL, DB_HASH, DB_CREATE, 0); + m_hf_starting_heights->open(txn, BDB_HF_STARTING_HEIGHTS, NULL, DB_RECNO, DB_CREATE, 0); + m_hf_versions->open(txn, BDB_HF_VERSIONS, NULL, DB_RECNO, DB_CREATE, 0); + txn.commit(); DB_BTREE_STAT* stats; @@ -785,6 +797,9 @@ void BlockchainBDB::sync() m_output_keys->sync(0); m_spent_keys->sync(0); + + m_hf_starting_heights->sync(0); + m_hf_versions->sync(0); } catch (const std::exception& e) { @@ -859,6 +874,12 @@ std::vector<std::string> BlockchainBDB::get_filenames() const m_spent_keys->get_dbname(pfname, pdbname); filenames.push_back(fname); + m_hf_starting_heights->get_dbname(pfname, pdbname); + filenames.push_back(fname); + + m_hf_versions->get_dbname(pfname, pdbname); + filenames.push_back(fname); + std::vector<std::string> full_paths; for (auto& filename : filenames) @@ -1839,6 +1860,60 @@ void BlockchainBDB::get_output_tx_and_index(const uint64_t& amount, const std::v LOG_PRINT_L3("db3: " << db3); } +void BlockchainBDB::set_hard_fork_starting_height(uint8_t version, uint64_t height) +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint8_t> val_key(version); + Dbt_copy<uint64_t> val(height); + if (m_hf_starting_heights->put(DB_DEFAULT_TX, &val_key, &val, 0)) + throw1(DB_ERROR("Error adding hard fork starting height to db transaction.")); +} + +uint64_t BlockchainBDB::get_hard_fork_starting_height(uint8_t version) const +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint8_t> key(version); + Dbt_copy<uint64_t> result; + + auto get_result = m_hf_starting_heights->get(DB_DEFAULT_TX, &key, &result, 0); + if (get_result == DB_NOTFOUND) + return std::numeric_limits<uint64_t>::max(); + else if (get_result) + throw0(DB_ERROR("Error attempting to retrieve hard fork starting height from the db")); + + return result; +} + +void BlockchainBDB::set_hard_fork_version(uint64_t height, uint8_t version) +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint64_t> val_key(height); + Dbt_copy<uint8_t> val(version); + if (m_hf_versions->put(DB_DEFAULT_TX, &val_key, &val, 0)) + throw1(DB_ERROR("Error adding hard fork version to db transaction.")); +} + +uint8_t BlockchainBDB::get_hard_fork_version(uint64_t height) const +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint64_t> key(height); + Dbt_copy<uint8_t> result; + + auto get_result = m_hf_versions->get(DB_DEFAULT_TX, &key, &result, 0); + if (get_result == DB_NOTFOUND || get_result) + throw0(DB_ERROR("Error attempting to retrieve hard fork version from the db")); + + return result; +} + void BlockchainBDB::checkpoint_worker() const { LOG_PRINT_L0("Entering BDB checkpoint thread.") diff --git a/src/blockchain_db/berkeleydb/db_bdb.h b/src/blockchain_db/berkeleydb/db_bdb.h index f92bbef68..54edcf0ad 100644 --- a/src/blockchain_db/berkeleydb/db_bdb.h +++ b/src/blockchain_db/berkeleydb/db_bdb.h @@ -374,6 +374,13 @@ private: virtual void remove_spent_key(const crypto::key_image& k_image); void get_output_global_indices(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<uint64_t> &global_indices); + + // Hard fork related storage + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height); + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const; + virtual void set_hard_fork_version(uint64_t height, uint8_t version); + virtual uint8_t get_hard_fork_version(uint64_t height) const; + /** * @brief convert a tx output to a blob for storage * @@ -430,6 +437,9 @@ private: Db* m_spent_keys; + Db* m_hf_starting_heights; + Db* m_hf_versions; + uint64_t m_height; uint64_t m_num_outputs; std::string m_folder; diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 25a34fc09..24bf4024d 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -493,6 +493,12 @@ public: // returns true if key image <img> is present in spent key images storage virtual bool has_key_image(const crypto::key_image& img) const = 0; + // Hard fork related storage + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) = 0; + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const = 0; + virtual void set_hard_fork_version(uint64_t height, uint8_t version) = 0; + virtual uint8_t get_hard_fork_version(uint64_t height) const = 0; + void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; } bool m_open; diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 1583a0c06..2695d83f8 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -131,6 +131,15 @@ auto compare_uint64 = [](const MDB_val *a, const MDB_val *b) else return 1; }; +auto compare_uint8 = [](const MDB_val *a, const MDB_val *b) +{ + const uint8_t va = *(const uint8_t*)a->mv_data; + const uint8_t vb = *(const uint8_t*)b->mv_data; + if (va < vb) return -1; + else if (va == vb) return 0; + else return 1; +}; + int compare_hash32(const MDB_val *a, const MDB_val *b) { uint32_t *va = (uint32_t*) a->mv_data; @@ -166,6 +175,9 @@ const char* const LMDB_OUTPUTS = "outputs"; const char* const LMDB_OUTPUT_GINDICES = "output_gindices"; const char* const LMDB_SPENT_KEYS = "spent_keys"; +const char* const LMDB_HF_STARTING_HEIGHTS = "hf_starting_heights"; +const char* const LMDB_HF_VERSIONS = "hf_versions"; + inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi, const std::string& error_string) { if (mdb_dbi_open(txn, name, flags, &dbi)) @@ -1022,6 +1034,9 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) lmdb_db_open(txn, LMDB_SPENT_KEYS, MDB_CREATE, m_spent_keys, "Failed to open db handle for m_spent_keys"); + lmdb_db_open(txn, LMDB_HF_STARTING_HEIGHTS, MDB_CREATE, m_hf_starting_heights, "Failed to open db handle for m_hf_starting_heights"); + lmdb_db_open(txn, LMDB_HF_VERSIONS, MDB_CREATE, m_hf_versions, "Failed to open db handle for m_hf_versions"); + mdb_set_dupsort(txn, m_output_amounts, compare_uint64); mdb_set_dupsort(txn, m_tx_outputs, compare_uint64); mdb_set_compare(txn, m_spent_keys, compare_hash32); @@ -1029,6 +1044,8 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) mdb_set_compare(txn, m_txs, compare_hash32); mdb_set_compare(txn, m_tx_unlocks, compare_hash32); mdb_set_compare(txn, m_tx_heights, compare_hash32); + mdb_set_compare(txn, m_hf_starting_heights, compare_uint8); + mdb_set_compare(txn, m_hf_versions, compare_uint64); // get and keep current height MDB_stat db_stats; @@ -2347,4 +2364,111 @@ void BlockchainLMDB::get_output_tx_and_index(const uint64_t& amount, const std:: LOG_PRINT_L3("db3: " << db3); } +void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t height) +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint8_t> val_key(version); + MDB_val_copy<uint64_t> val_value(height); + if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, 0)) + throw1(DB_ERROR(std::string("Error adding hard fork starting height to db transaction: ").append(mdb_strerror(result)).c_str())); + + if (!m_batch_active) + txn.commit(); +} + +uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + 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")); + txn_ptr = &txn; + } + + MDB_val_copy<uint8_t> val_key(version); + MDB_val val_ret; + auto result = mdb_get(*txn_ptr, m_hf_starting_heights, &val_key, &val_ret); + if (result == MDB_NOTFOUND) + return std::numeric_limits<uint64_t>::max(); + if (result) + throw0(DB_ERROR("Error attempting to retrieve a hard fork starting height from the db")); + + if (!m_batch_active) + txn.commit(); + return *(const uint64_t*)val_ret.mv_data; +} + +void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + +//LOG_PRINT_L1("BlockchainLMDB::set_hard_fork_version: batch " << m_batch_active << ", height " << height << ", version " << (int)version); + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint64_t> val_key(height); + MDB_val_copy<uint8_t> val_value(version); + if (auto result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0)) + throw1(DB_ERROR(std::string("Error adding hard fork version to db transaction: ").append(mdb_strerror(result)).c_str())); + + if (!m_batch_active) + txn.commit(); +} + +uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + 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")); + txn_ptr = &txn; + } + + MDB_val_copy<uint64_t> val_key(height); + MDB_val val_ret; + auto result = mdb_get(*txn_ptr, m_hf_versions, &val_key, &val_ret); + if (result == MDB_NOTFOUND || result) + throw0(DB_ERROR("Error attempting to retrieve a hard fork version from the db")); + + if (!m_batch_active) + txn.commit(); + return *(const uint8_t*)val_ret.mv_data; +} + } // namespace cryptonote diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index b4c197803..380954295 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -238,6 +238,12 @@ private: virtual void remove_spent_key(const crypto::key_image& k_image); + // Hard fork + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height); + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const; + virtual void set_hard_fork_version(uint64_t height, uint8_t version); + virtual uint8_t get_hard_fork_version(uint64_t height) const; + /** * @brief convert a tx output to a blob for storage * @@ -292,6 +298,9 @@ private: MDB_dbi m_spent_keys; + MDB_dbi m_hf_starting_heights; + MDB_dbi m_hf_versions; + uint64_t m_height; uint64_t m_num_outputs; std::string m_folder; |