Skip to content

Commit

Permalink
Use db.RemoveConfirmedISLocks() in NotifyChainLock to remove confirme…
Browse files Browse the repository at this point in the history
…d locks

Also move the actual logic into HandleFullyConfirmedBlock and call it
from NotifyChainLock and UpdatedBlockTip.
  • Loading branch information
codablock committed Apr 5, 2019
1 parent 4577438 commit 8e7083c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 50 deletions.
71 changes: 22 additions & 49 deletions src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,48 +820,12 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx, const CBlockIn

void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
{
uint256 lastChainLockBlock;
{
LOCK(cs);
lastChainLockBlock = db.GetLastChainLockBlock();
}

// Let's find all islocks that correspond to TXs which are part of the freshly ChainLocked chain and then delete
// the islocks. We do this because the ChainLocks imply locking and thus it's not needed to further track
// or propagate the islocks
std::unordered_set<uint256> toDelete;
auto pindex = pindexChainLock;
while (pindex && pindex->GetBlockHash() != lastChainLockBlock) {
CBlock block;
{
LOCK(cs_main);
if (!ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
pindex = pindex->pprev;
continue;
}
}

LOCK(cs);
for (const auto& tx : block.vtx) {
auto islock = db.GetInstantSendLockByTxid(tx->GetHash());
if (!islock) {
continue;
}
auto hash = ::SerializeHash(*islock);
LogPrint("instantsend", "CInstantSendManager::%s -- txid=%s, islock=%s: removing islock as it got ChainLocked in block %s\n", __func__,
islock->txid.ToString(), hash.ToString(), pindex->GetBlockHash().ToString());
RemoveFinalISLock(hash, islock);
}

pindex = pindex->pprev;
}

{
LOCK(cs);
db.WriteLastChainLockBlock(pindexChainLock->GetBlockHash());
}

RetryLockTxs(uint256());
HandleFullyConfirmedBlock(pindexChainLock);
}

void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
Expand All @@ -871,26 +835,35 @@ void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
return;
}

int nChainLockMinHeight = pindexNew->nHeight - Params().GetConsensus().nInstantSendKeepLock;
const CBlockIndex* pindex = pindexNew->GetAncestor(nChainLockMinHeight);
int nConfirmedHeight = pindexNew->nHeight - Params().GetConsensus().nInstantSendKeepLock;
const CBlockIndex* pindex = pindexNew->GetAncestor(nConfirmedHeight);

if (pindex) {
// Pretend it was chainlocked at nChainLockMinHeight.
// This effectively drops all islocks below nChainLockMinHeight.
NotifyChainLock(pindex);
HandleFullyConfirmedBlock(pindex);
}
}

void CInstantSendManager::RemoveFinalISLock(const uint256& hash, const CInstantSendLockPtr& islock)
void CInstantSendManager::HandleFullyConfirmedBlock(const CBlockIndex* pindex)
{
AssertLockHeld(cs);
{
LOCK(cs);

db.RemoveInstantSendLock(hash, islock);

for (auto& in : islock->inputs) {
auto inputRequestId = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in));
inputRequestIds.erase(inputRequestId);
auto islocks = db.RemoveConfirmedInstantSendLocks(pindex->nHeight);
for (auto& p : islocks) {
auto& islockHash = p.first;
auto& islock = p.second;
LogPrint("instantsend", "CInstantSendManager::%s -- txid=%s, islock=%s: removed islock as it got fully confirmed\n", __func__,
islock->txid.ToString(), islockHash.ToString());

for (auto& in : islock->inputs) {
auto inputRequestId = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in));
inputRequestIds.erase(inputRequestId);
}
}
}

// Retry all not yet locked mempool TXs and TX which where mined after the fully confirmed block
RetryLockTxs(uint256());
}

void CInstantSendManager::RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock)
Expand Down
3 changes: 2 additions & 1 deletion src/llmq/quorums_instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class CInstantSendManager : public CRecoveredSigsListener
void SyncTransaction(const CTransaction &tx, const CBlockIndex *pindex, int posInBlock);
void NotifyChainLock(const CBlockIndex* pindexChainLock);
void UpdatedBlockTip(const CBlockIndex* pindexNew);
void RemoveFinalISLock(const uint256& hash, const CInstantSendLockPtr& islock);

void HandleFullyConfirmedBlock(const CBlockIndex* pindex);

void RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock);
void RetryLockTxs(const uint256& lockedParentTx);
Expand Down

0 comments on commit 8e7083c

Please sign in to comment.