Merge #6394: feat: update IS database instantly, no more dependency on DIP0020

89528bc4dd feat: overwrite outdated IS db with a new empty one (UdjinM6)
3b7df9aea0 feat: update IS database instantly, no more dependency on DIP0020 (Konstantin Akimov)

Pull request description:

  ## Issue being fixed or feature implemented

  Upgrade of IS database depends on activation of DIP0020. It's no more need because it is activated long time ago.

  ## What was done?
  Removed dependency of IS database upgrade on DIP0020. Also removed LOCK for `::mempool.cs` which is not actually has been used inside GetTransaction during upgrade: no mempool -- no lock.

  ## How Has This Been Tested?
  Run unit and functional tests.

  ## Breaking Changes
  N/A

  ## Checklist:
  - [x] I have performed a self-review of my own code
  - [x] I have commented my code, particularly in hard-to-understand areas
  - [x] I have added or updated relevant unit/integration/functional/e2e tests
  - [x] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone

ACKs for top commit:
  UdjinM6:
    utACK 89528bc4dd
  PastaPastaPasta:
    utACK 89528bc4dd

Tree-SHA512: 50feb2a8d0bb88b6443e4fc019c04187a5e1901f50bd08085b1e81379b297dd929b33fc2990c1a78e0b8607ba590069eca7927f3e1df5564b0709caafa2b4326
This commit is contained in:
pasta 2024-11-16 19:52:56 -06:00
commit d159abc422
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38
2 changed files with 12 additions and 35 deletions

View File

@ -57,42 +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(const CTxMemPool& mempool)
void CInstantSendDb::Upgrade(bool unitTests)
{
LOCK2(cs_main, mempool.cs);
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();
}
}
@ -1103,7 +1086,7 @@ void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx)
void CInstantSendManager::TransactionRemovedFromMempool(const CTransactionRef& tx)
{
if (tx->vin.empty() || !fUpgradedDB) {
if (tx->vin.empty()) {
return;
}
@ -1252,11 +1235,6 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
{
if (!fUpgradedDB && pindexNew->nHeight + 1 >= Params().GetConsensus().DIP0020Height) {
db.Upgrade(mempool);
fUpgradedDB = true;
}
bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev->nHeight >= Params().GetConsensus().DIP0008Height;
if (AreChainLocksEnabled(spork_manager) && fDIP0008Active) {

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(const CTxMemPool& mempool) 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
@ -209,7 +209,6 @@ private:
const std::unique_ptr<PeerManager>& m_peerman;
const bool m_is_masternode;
std::atomic<bool> fUpgradedDB{false};
std::thread workThread;
CThreadInterrupt workInterrupt;