Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/llmq/instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/llmq/instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ class CInstantSendDb
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
Expand Down Expand Up @@ -209,7 +209,6 @@ class CInstantSendManager : public CRecoveredSigsListener
const std::unique_ptr<PeerManager>& m_peerman;

const bool m_is_masternode;
std::atomic<bool> fUpgradedDB{false};

std::thread workThread;
CThreadInterrupt workInterrupt;
Expand Down
Loading