feat: overwrite outdated IS db with a new empty one

This commit is contained in:
UdjinM6 2024-11-16 15:44:04 +03:00 committed by Konstantin Akimov
parent 3b7df9aea0
commit 89528bc4dd
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
2 changed files with 12 additions and 29 deletions

View File

@ -57,41 +57,25 @@ uint256 CInstantSendLock::GetRequestId() const
CInstantSendDb::CInstantSendDb(bool unitTests, bool fWipe) :
db(std::make_unique<CDBWrapper>(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests, fWipe))
{
Upgrade(unitTests);
}
CInstantSendDb::~CInstantSendDb() = default;
void CInstantSendDb::Upgrade()
void CInstantSendDb::Upgrade(bool unitTests)
{
LOCK2(cs_main, cs_db);
LOCK(cs_db);
int v{0};
if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) {
// Wipe db
db.reset();
db = std::make_unique<CDBWrapper>(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests,
/*fWipe=*/true);
CDBBatch batch(*db);
CInstantSendLock islock;
auto it = std::unique_ptr<CDBIterator>(db->NewIterator());
auto firstKey = std::make_tuple(std::string{DB_ISLOCK_BY_HASH}, uint256());
it->Seek(firstKey);
decltype(firstKey) curKey;
while (it->Valid()) {
if (!it->GetKey(curKey) || std::get<0>(curKey) != DB_ISLOCK_BY_HASH) {
break;
}
uint256 hashBlock;
CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, islock.txid, Params().GetConsensus(), hashBlock);
if (it->GetValue(islock) && !tx) {
// Drop locks for unknown txes
batch.Erase(std::make_tuple(DB_HASH_BY_TXID, islock.txid));
for (auto& in : islock.inputs) {
batch.Erase(std::make_tuple(DB_HASH_BY_OUTPOINT, in));
}
batch.Erase(curKey);
}
it->Next();
}
batch.Write(DB_VERSION, CInstantSendDb::CURRENT_VERSION);
db->WriteBatch(batch);
// Sync DB changes to disk
db->WriteBatch(batch, /*fSync=*/true);
batch.Clear();
}
}

View File

@ -112,12 +112,12 @@ private:
uint256 GetInstantSendLockHashByTxidInternal(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_db);
void Upgrade(bool unitTests) EXCLUSIVE_LOCKS_REQUIRED(!cs_db);
public:
explicit CInstantSendDb(bool unitTests, bool fWipe);
~CInstantSendDb();
void Upgrade() EXCLUSIVE_LOCKS_REQUIRED(!cs_db);
/**
* This method is called when an InstantSend Lock is processed and adds the lock to the database
* @param hash The hash of the InstantSend Lock
@ -263,7 +263,6 @@ public:
shareman(_shareman), spork_manager(sporkman), mempool(_mempool), m_mn_sync(mn_sync), m_peerman(peerman),
m_is_masternode{is_masternode}
{
db.Upgrade(); // Upgrade DB if need to do it
workInterrupt.reset();
}
~CInstantSendManager() = default;