aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2016-02-20 17:09:49 +0200
committerRiccardo Spagni <ric@spagni.net>2016-02-20 17:09:49 +0200
commita903d9c3f6da8f368c369c5b51af7380d7ac17aa (patch)
tree40d2e089346b12a1f91362489961624a48d9562d /src
parentMerge pull request #676 (diff)
parentBlockchainBDB: Check if hard fork subdbs need reset (diff)
downloadmonero-a903d9c3f6da8f368c369c5b51af7380d7ac17aa.tar.xz
Merge pull request #677
57e75fa BlockchainBDB: Check if hard fork subdbs need reset (warptangent) 47f6cf8 BlockchainBDB: Support blockchain_import --drop-hard-fork command (warptangent)
Diffstat (limited to 'src')
-rw-r--r--src/blockchain_db/berkeleydb/db_bdb.cpp89
1 files changed, 85 insertions, 4 deletions
diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp
index f035bf4c4..d44e14f5b 100644
--- a/src/blockchain_db/berkeleydb/db_bdb.cpp
+++ b/src/blockchain_db/berkeleydb/db_bdb.cpp
@@ -1073,8 +1073,10 @@ void BlockchainBDB::sync()
m_spent_keys->sync(0);
- m_hf_starting_heights->sync(0);
- m_hf_versions->sync(0);
+ if (m_hf_starting_heights != nullptr)
+ m_hf_starting_heights->sync(0);
+ if (m_hf_versions != nullptr)
+ m_hf_versions->sync(0);
m_properties->sync(0);
}
@@ -2208,12 +2210,91 @@ uint64_t BlockchainBDB::get_hard_fork_starting_height(uint8_t version) const
void BlockchainBDB::check_hard_fork_info()
{
- /* FIXME: Some other time */
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ if (m_hf_versions == nullptr)
+ {
+ LOG_PRINT_L0("hf versions DB not open, so not checking");
+ return;
+ }
+
+ DB_BTREE_STAT* db_stat1, * db_stat2;
+
+ // DB_FAST_STAT can apparently cause an incorrect number of records
+ // to be returned. The flag should be set to 0 instead if this proves
+ // to be the case.
+
+ // Set txn to NULL and DB_FAST_STAT to zero (0) for reliability.
+ m_blocks->stat(NULL, &db_stat1, 0);
+ m_hf_versions->stat(NULL, &db_stat2, 0);
+ if (db_stat1->bt_nkeys != db_stat2->bt_nkeys)
+ {
+ LOG_PRINT_L0("num blocks " << db_stat1->bt_nkeys << " != " << "num hf_versions " << db_stat2->bt_nkeys << " - will clear the two hard fork DBs");
+
+ bdb_txn_safe txn;
+ bdb_txn_safe* txn_ptr = &txn;
+ if (m_write_txn)
+ txn_ptr = m_write_txn;
+ else
+ {
+ if (m_env->txn_begin(NULL, txn, 0))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+ }
+
+ try
+ {
+ uint32_t count;
+ m_hf_starting_heights->truncate(*txn_ptr, &count, 0);
+ LOG_PRINT_L0("hf_starting_heights count: " << count);
+ m_hf_versions->truncate(*txn_ptr, &count, 0);
+ LOG_PRINT_L0("hf_versions count: " << count);
+
+ if (!m_write_txn)
+ txn.commit();
+ }
+ catch (const std::exception& e)
+ {
+ throw0(DB_ERROR(std::string("Failed to clear two hard fork DBs: ").append(e.what()).c_str()));
+ }
+ }
+ delete db_stat1;
+ delete db_stat2;
}
void BlockchainBDB::drop_hard_fork_info()
{
- /* TODO */
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ bdb_txn_safe txn;
+ bdb_txn_safe* txn_ptr = &txn;
+ if (m_write_txn)
+ txn_ptr = m_write_txn;
+ else
+ {
+ if (m_env->txn_begin(NULL, txn, 0))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+ }
+
+ try
+ {
+ m_hf_starting_heights->close(0);
+ m_hf_versions->close(0);
+ m_hf_starting_heights = nullptr;
+ m_hf_versions = nullptr;
+ if (m_env->dbremove(*txn_ptr, BDB_HF_STARTING_HEIGHTS, NULL, 0) != 0)
+ LOG_ERROR("Error removing hf_starting_heights");
+ if (m_env->dbremove(*txn_ptr, BDB_HF_VERSIONS, NULL, 0) != 0)
+ LOG_ERROR("Error removing hf_versions");
+
+ if (!m_write_txn)
+ txn.commit();
+ }
+ catch (const std::exception& e)
+ {
+ throw0(DB_ERROR(std::string("Failed to drop hard fork info: ").append(e.what()).c_str()));
+ }
}
void BlockchainBDB::set_hard_fork_version(uint64_t height, uint8_t version)