@@ -298,11 +298,11 @@ struct Peer {
298
298
* us or we have announced to the peer. We use this to avoid announcing
299
299
* the same (w)txid to a peer that already has the transaction. */
300
300
CRollingBloomFilter m_tx_inventory_known_filter GUARDED_BY (m_tx_inventory_mutex){50000 , 0.000001 };
301
- /* * Set of transaction ids we still have to announce (txid for
302
- * non-wtxid-relay peers, wtxid for wtxid-relay peers). We use the
303
- * mempool to sort transactions in dependency order before relay, so
304
- * this does not have to be sorted. */
305
- std::set<GenTxid > m_tx_inventory_to_send GUARDED_BY (m_tx_inventory_mutex);
301
+ /* * Set of wtxids we still have to announce. For non-wtxid-relay peers,
302
+ * we retrieve the txid from the corresponding mempool transaction when
303
+ * constructing the `inv` message. We use the mempool to sort transactions
304
+ * in dependency order before relay, so this does not have to be sorted. */
305
+ std::set<Wtxid > m_tx_inventory_to_send GUARDED_BY (m_tx_inventory_mutex);
306
306
/* * Whether the peer has requested us to send our complete mempool. Only
307
307
* permitted if the peer has NetPermissionFlags::Mempool or we advertise
308
308
* NODE_BLOOM. See BIP35. */
@@ -2166,9 +2166,9 @@ void PeerManagerImpl::RelayTransaction(const Txid& txid, const Wtxid& wtxid)
2166
2166
// in the announcement.
2167
2167
if (tx_relay->m_next_inv_send_time == 0s) continue ;
2168
2168
2169
- const auto gtxid {peer.m_wtxid_relay ? GenTxid{ wtxid} : GenTxid{ txid} };
2170
- if (!tx_relay->m_tx_inventory_known_filter .contains (gtxid. ToUint256 () )) {
2171
- tx_relay->m_tx_inventory_to_send .insert (gtxid );
2169
+ const uint256& hash {peer.m_wtxid_relay ? wtxid. ToUint256 () : txid. ToUint256 () };
2170
+ if (!tx_relay->m_tx_inventory_known_filter .contains (hash )) {
2171
+ tx_relay->m_tx_inventory_to_send .insert (wtxid );
2172
2172
}
2173
2173
}
2174
2174
}
@@ -5439,7 +5439,7 @@ class CompareInvMempoolOrder
5439
5439
public:
5440
5440
explicit CompareInvMempoolOrder (CTxMemPool* mempool) : m_mempool{mempool} {}
5441
5441
5442
- bool operator ()(std::set<GenTxid >::iterator a, std::set<GenTxid >::iterator b)
5442
+ bool operator ()(std::set<Wtxid >::iterator a, std::set<Wtxid >::iterator b)
5443
5443
{
5444
5444
/* As std::make_heap produces a max-heap, we want the entries with the
5445
5445
* fewest ancestors/highest fee to sort later. */
@@ -5755,13 +5755,12 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5755
5755
LOCK (tx_relay->m_bloom_filter_mutex );
5756
5756
5757
5757
for (const auto & txinfo : vtxinfo) {
5758
- CInv inv{
5759
- peer->m_wtxid_relay ? MSG_WTX : MSG_TX,
5760
- peer->m_wtxid_relay ?
5761
- txinfo.tx ->GetWitnessHash ().ToUint256 () :
5762
- txinfo.tx ->GetHash ().ToUint256 (),
5763
- };
5764
- tx_relay->m_tx_inventory_to_send .erase (ToGenTxid (inv));
5758
+ const Txid& txid{txinfo.tx ->GetHash ()};
5759
+ const Wtxid& wtxid{txinfo.tx ->GetWitnessHash ()};
5760
+ const auto inv = peer->m_wtxid_relay ?
5761
+ CInv{MSG_WTX, wtxid.ToUint256 ()} :
5762
+ CInv{MSG_TX, txid.ToUint256 ()};
5763
+ tx_relay->m_tx_inventory_to_send .erase (wtxid);
5765
5764
5766
5765
// Don't send transactions that peers will not put into their mempool
5767
5766
if (txinfo.fee < filterrate.GetFee (txinfo.vsize )) {
@@ -5782,9 +5781,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5782
5781
// Determine transactions to relay
5783
5782
if (fSendTrickle ) {
5784
5783
// Produce a vector with all candidates for sending
5785
- std::vector<std::set<GenTxid >::iterator> vInvTx;
5784
+ std::vector<std::set<Wtxid >::iterator> vInvTx;
5786
5785
vInvTx.reserve (tx_relay->m_tx_inventory_to_send .size ());
5787
- for (std::set<GenTxid >::iterator it = tx_relay->m_tx_inventory_to_send .begin (); it != tx_relay->m_tx_inventory_to_send .end (); it++) {
5786
+ for (std::set<Wtxid >::iterator it = tx_relay->m_tx_inventory_to_send .begin (); it != tx_relay->m_tx_inventory_to_send .end (); it++) {
5788
5787
vInvTx.push_back (it);
5789
5788
}
5790
5789
const CFeeRate filterrate{tx_relay->m_fee_filter_received .load ()};
@@ -5801,22 +5800,26 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5801
5800
while (!vInvTx.empty () && nRelayedTransactions < broadcast_max) {
5802
5801
// Fetch the top element from the heap
5803
5802
std::pop_heap (vInvTx.begin (), vInvTx.end (), compareInvMempoolOrder);
5804
- std::set<GenTxid >::iterator it = vInvTx.back ();
5803
+ std::set<Wtxid >::iterator it = vInvTx.back ();
5805
5804
vInvTx.pop_back ();
5806
- GenTxid hash = *it;
5807
- Assume (peer->m_wtxid_relay == hash.IsWtxid ());
5808
- CInv inv (peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash.ToUint256 ());
5805
+ auto wtxid = *it;
5809
5806
// Remove it from the to-be-sent set
5810
5807
tx_relay->m_tx_inventory_to_send .erase (it);
5811
- // Check if not in the filter already
5812
- if (tx_relay->m_tx_inventory_known_filter .contains (hash.ToUint256 ())) {
5813
- continue ;
5814
- }
5815
5808
// Not in the mempool anymore? don't bother sending it.
5816
- auto txinfo{ std::visit ([&]( const auto & id) { return m_mempool.info (id); }, hash)} ;
5809
+ auto txinfo = m_mempool.info (wtxid) ;
5817
5810
if (!txinfo.tx ) {
5818
5811
continue ;
5819
5812
}
5813
+ // `TxRelay::m_tx_inventory_known_filter` contains either txids or wtxids
5814
+ // depending on whether our peer supports wtxid-relay. Therefore, first
5815
+ // construct the inv and then use its hash for the filter check.
5816
+ const auto inv = peer->m_wtxid_relay ?
5817
+ CInv{MSG_WTX, wtxid.ToUint256 ()} :
5818
+ CInv{MSG_TX, txinfo.tx ->GetHash ().ToUint256 ()};
5819
+ // Check if not in the filter already
5820
+ if (tx_relay->m_tx_inventory_known_filter .contains (inv.hash )) {
5821
+ continue ;
5822
+ }
5820
5823
// Peer told you to not send transactions at that feerate? Don't bother sending it.
5821
5824
if (txinfo.fee < filterrate.GetFee (txinfo.vsize )) {
5822
5825
continue ;
@@ -5829,7 +5832,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5829
5832
MakeAndPushMessage (*pto, NetMsgType::INV, vInv);
5830
5833
vInv.clear ();
5831
5834
}
5832
- tx_relay->m_tx_inventory_known_filter .insert (hash. ToUint256 () );
5835
+ tx_relay->m_tx_inventory_known_filter .insert (inv. hash );
5833
5836
}
5834
5837
5835
5838
// Ensure we'll respond to GETDATA requests for anything we've just announced
0 commit comments