[refactor] move AlreadyHaveTx to TxDownload

This is move-only.
Also delete external RecentConfirmedTransactionsFilter() access since it
is no longer necessary.
This commit is contained in:
glozow
2024-04-16 17:06:56 +01:00
parent 84e4ef843d
commit f61d9e4b4b
4 changed files with 50 additions and 60 deletions

View File

@@ -11,6 +11,7 @@
class CBlock;
class CRollingBloomFilter;
class CTxMemPool;
class GenTxid;
class TxOrphanage;
class TxRequestTracker;
namespace node {
@@ -53,12 +54,20 @@ public:
TxRequestTracker& GetTxRequestRef();
CRollingBloomFilter& RecentRejectsFilter();
CRollingBloomFilter& RecentRejectsReconsiderableFilter();
CRollingBloomFilter& RecentConfirmedTransactionsFilter();
// Responses to chain events. TxDownloadManager is not an actual client of ValidationInterface, these are called through PeerManager.
void ActiveTipChange();
void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
void BlockDisconnected();
/** Check whether we already have this gtxid in:
* - mempool
* - orphanage
* - m_recent_rejects
* - m_recent_rejects_reconsiderable (if include_reconsiderable = true)
* - m_recent_confirmed_transactions
* */
bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
};
} // namespace node
#endif // BITCOIN_NODE_TXDOWNLOADMAN_H

View File

@@ -34,10 +34,6 @@ CRollingBloomFilter& TxDownloadManager::RecentRejectsReconsiderableFilter()
{
return m_impl->RecentRejectsReconsiderableFilter();
}
CRollingBloomFilter& TxDownloadManager::RecentConfirmedTransactionsFilter()
{
return m_impl->RecentConfirmedTransactionsFilter();
}
void TxDownloadManager::ActiveTipChange()
{
m_impl->ActiveTipChange();
@@ -50,6 +46,10 @@ void TxDownloadManager::BlockDisconnected()
{
m_impl->BlockDisconnected();
}
bool TxDownloadManager::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable)
{
return m_impl->AlreadyHaveTx(gtxid, include_reconsiderable);
}
// TxDownloadManagerImpl
void TxDownloadManagerImpl::ActiveTipChange()
@@ -84,4 +84,33 @@ void TxDownloadManagerImpl::BlockDisconnected()
// should be just after a new block containing it is found.
RecentConfirmedTransactionsFilter().reset();
}
bool TxDownloadManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable)
{
const uint256& hash = gtxid.GetHash();
if (gtxid.IsWtxid()) {
// Normal query by wtxid.
if (m_orphanage.HaveTx(Wtxid::FromUint256(hash))) return true;
} else {
// Never query by txid: it is possible that the transaction in the orphanage has the same
// txid but a different witness, which would give us a false positive result. If we decided
// not to request the transaction based on this result, an attacker could prevent us from
// downloading a transaction by intentionally creating a malleated version of it. While
// only one (or none!) of these transactions can ultimately be confirmed, we have no way of
// discerning which one that is, so the orphanage can store multiple transactions with the
// same txid.
//
// While we won't query by txid, we can try to "guess" what the wtxid is based on the txid.
// A non-segwit transaction's txid == wtxid. Query this txid "casted" to a wtxid. This will
// help us find non-segwit transactions, saving bandwidth, and should have no false positives.
if (m_orphanage.HaveTx(Wtxid::FromUint256(hash))) return true;
}
if (include_reconsiderable && RecentRejectsReconsiderableFilter().contains(hash)) return true;
if (RecentConfirmedTransactionsFilter().contains(hash)) return true;
return RecentRejectsFilter().contains(hash) || m_opts.m_mempool.exists(gtxid);
}
} // namespace node

View File

@@ -9,6 +9,7 @@
#include <common/bloom.h>
#include <kernel/chain.h>
#include <net.h>
#include <primitives/transaction.h>
#include <txorphanage.h>
#include <txrequest.h>
@@ -130,6 +131,8 @@ public:
void ActiveTipChange();
void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
void BlockDisconnected();
bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
};
} // namespace node
#endif // BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H