Skip to content

Commit efcc593

Browse files
committed
[refactor] TxOrphanage::HaveTx only by wtxid
1 parent 7e475b9 commit efcc593

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconside
22972297

22982298
if (gtxid.IsWtxid()) {
22992299
// Normal query by wtxid.
2300-
if (m_orphanage.HaveTx(gtxid)) return true;
2300+
if (m_orphanage.HaveTx(Wtxid::FromUint256(hash))) return true;
23012301
} else {
23022302
// Never query by txid: it is possible that the transaction in the orphanage has the same
23032303
// txid but a different witness, which would give us a false positive result. If we decided
@@ -2307,7 +2307,7 @@ bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconside
23072307
// While we won't query by txid, we can try to "guess" what the wtxid is based on the txid.
23082308
// A non-segwit transaction's txid == wtxid. Query this txid "casted" to a wtxid. This will
23092309
// help us find non-segwit transactions, saving bandwidth, and should have no false positives.
2310-
if (m_orphanage.HaveTx(GenTxid::Wtxid(hash))) return true;
2310+
if (m_orphanage.HaveTx(Wtxid::FromUint256(hash))) return true;
23112311
}
23122312

23132313
if (include_reconsiderable && m_recent_rejects_reconsiderable.contains(hash)) return true;

src/test/fuzz/txorphan.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,20 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
112112
{
113113
CTransactionRef ref = orphanage.GetTxToReconsider(peer_id);
114114
if (ref) {
115-
bool have_tx = orphanage.HaveTx(GenTxid::Txid(ref->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(ref->GetWitnessHash()));
116-
Assert(have_tx);
115+
Assert(orphanage.HaveTx(ref->GetWitnessHash()));
117116
}
118117
}
119118
},
120119
[&] {
121-
bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
120+
bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
122121
// AddTx should return false if tx is too big or already have it
123122
// tx weight is unknown, we only check when tx is already in orphanage
124123
{
125124
bool add_tx = orphanage.AddTx(tx, peer_id);
126125
// have_tx == true -> add_tx == false
127126
Assert(!have_tx || !add_tx);
128127
}
129-
have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
128+
have_tx = orphanage.HaveTx(tx->GetWitnessHash());
130129
{
131130
bool add_tx = orphanage.AddTx(tx, peer_id);
132131
// if have_tx is still false, it must be too big
@@ -135,12 +134,12 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
135134
}
136135
},
137136
[&] {
138-
bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
137+
bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
139138
// EraseTx should return 0 if m_orphans doesn't have the tx
140139
{
141140
Assert(have_tx == orphanage.EraseTx(tx->GetHash()));
142141
}
143-
have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
142+
have_tx = orphanage.HaveTx(tx->GetWitnessHash());
144143
// have_tx should be false and EraseTx should fail
145144
{
146145
Assert(!have_tx && !orphanage.EraseTx(tx->GetHash()));

src/txorphanage.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,10 @@ void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx)
169169
}
170170
}
171171

172-
bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
172+
bool TxOrphanage::HaveTx(const Wtxid& wtxid) const
173173
{
174174
LOCK(m_mutex);
175-
if (gtxid.IsWtxid()) {
176-
return m_wtxid_to_orphan_it.count(Wtxid::FromUint256(gtxid.GetHash()));
177-
} else {
178-
return m_orphans.count(Txid::FromUint256(gtxid.GetHash()));
179-
}
175+
return m_wtxid_to_orphan_it.count(wtxid);
180176
}
181177

182178
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)

src/txorphanage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class TxOrphanage {
2323
/** Add a new orphan transaction */
2424
bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
2525

26-
/** Check if we already have an orphan transaction (by txid or wtxid) */
27-
bool HaveTx(const GenTxid& gtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
26+
/** Check if we already have an orphan transaction (by wtxid only) */
27+
bool HaveTx(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
2828

2929
/** Extract a transaction from a peer's work set
3030
* Returns nullptr if there are no transactions to work on.

0 commit comments

Comments
 (0)