Skip to content

Commit

Permalink
update recent_rejects filters on ActiveTipChange
Browse files Browse the repository at this point in the history
Resetting m_recent_rejects once per block is more efficient than
comparing hashRecentRejectsChainTip with the chain tip every time we
call AlreadyHaveTx. We keep hashRecentRejectsChainTip for now to assert
that updates happen correctly; it is removed in the next commit.
  • Loading branch information
glozow committed Jul 16, 2024
1 parent 36f170d commit 18a4355
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ class PeerManagerImpl final : public PeerManager
CTxMemPool& pool, node::Warnings& warnings, Options opts);

/** Overridden from CValidationInterface. */
void ActiveTipChange(const CBlockIndex* new_tip, bool) override
EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex, !m_tx_download_mutex);
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex, !m_tx_download_mutex);
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
Expand Down Expand Up @@ -2074,6 +2076,22 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
}

void PeerManagerImpl::ActiveTipChange(const CBlockIndex* new_tip, bool is_ibd)
{
AssertLockNotHeld(m_mempool.cs);
AssertLockNotHeld(m_tx_download_mutex);

if (!is_ibd) {
LOCK(m_tx_download_mutex);
// If the chain tip has changed, previously rejected transactions might now be valid, e.g. due
// to a timelock. Reset the rejection filters to give those transactions another chance if we
// see them again.
m_recent_rejects.reset();
m_recent_rejects_reconsiderable.reset();
hashRecentRejectsChainTip = new_tip->GetBlockHash();
}
}

/**
* Evict orphan txn pool entries based on a newly connected
* block, remember the recently confirmed transactions, and delete tracked
Expand Down Expand Up @@ -2277,7 +2295,11 @@ bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconside
AssertLockHeld(::cs_main);
AssertLockHeld(m_tx_download_mutex);

if (m_chainman.ActiveChain().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
// Since recent_rejects is updated whenever the tip changes, and hashRecentRejectsChainTip is
// not set until the first time it is called outside of IBD, hashRecentRejectsChainTip should
// always be up to date with the current chain tip.
if (!Assume(hashRecentRejectsChainTip == uint256::ZERO ||
hashRecentRejectsChainTip == m_chainman.ActiveChain().Tip()->GetBlockHash())) {
// If the chain tip has changed previously rejected transactions
// might be now valid, e.g. due to a nLockTime'd tx becoming valid,
// or a double-spend. Reset the rejects filter and give those
Expand Down

0 comments on commit 18a4355

Please sign in to comment.