@@ -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. */
@@ -947,7 +947,7 @@ class PeerManagerImpl final : public PeerManager
947
947
std::atomic<std::chrono::seconds> m_last_tip_update{0s};
948
948
949
949
/* * Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
950
- CTransactionRef FindTxForGetData (const Peer::TxRelay& tx_relay, const CInv& inv )
950
+ CTransactionRef FindTxForGetData (const Peer::TxRelay& tx_relay, const GenTxid& gtxid )
951
951
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, NetEventsInterface::g_msgproc_mutex);
952
952
953
953
void ProcessGetData (CNode& pfrom, Peer& peer, const std::atomic<bool >& interruptMsgProc)
@@ -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
}
@@ -2391,15 +2391,14 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
2391
2391
}
2392
2392
}
2393
2393
2394
- CTransactionRef PeerManagerImpl::FindTxForGetData (const Peer::TxRelay& tx_relay, const CInv& inv )
2394
+ CTransactionRef PeerManagerImpl::FindTxForGetData (const Peer::TxRelay& tx_relay, const GenTxid& gtxid )
2395
2395
{
2396
- auto gtxid{ToGenTxid (inv)};
2397
- // If a tx was in the mempool prior to the last INV for this peer, permit the request.
2398
2396
auto txinfo{std::visit (
2399
2397
[&](const auto & id) EXCLUSIVE_LOCKS_REQUIRED (NetEventsInterface::g_msgproc_mutex) {
2400
2398
return m_mempool.info_for_relay (id, tx_relay.m_last_inv_sequence );
2401
2399
},
2402
2400
gtxid)};
2401
+
2403
2402
if (txinfo.tx ) {
2404
2403
return std::move (txinfo.tx );
2405
2404
}
@@ -2442,7 +2441,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
2442
2441
continue ;
2443
2442
}
2444
2443
2445
- if (auto tx{FindTxForGetData (*tx_relay, inv)}) {
2444
+ if (auto tx{FindTxForGetData (*tx_relay, ToGenTxid ( inv) )}) {
2446
2445
// WTX and WITNESS_TX imply we serialize with witness
2447
2446
const auto maybe_with_witness = (inv.IsMsgTx () ? TX_NO_WITNESS : TX_WITH_WITNESS);
2448
2447
MakeAndPushMessage (pfrom, NetMsgType::TX, maybe_with_witness (*tx));
@@ -5440,7 +5439,7 @@ class CompareInvMempoolOrder
5440
5439
public:
5441
5440
explicit CompareInvMempoolOrder (CTxMemPool* mempool) : m_mempool{mempool} {}
5442
5441
5443
- 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)
5444
5443
{
5445
5444
/* As std::make_heap produces a max-heap, we want the entries with the
5446
5445
* fewest ancestors/highest fee to sort later. */
@@ -5756,13 +5755,12 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5756
5755
LOCK (tx_relay->m_bloom_filter_mutex );
5757
5756
5758
5757
for (const auto & txinfo : vtxinfo) {
5759
- CInv inv{
5760
- peer->m_wtxid_relay ? MSG_WTX : MSG_TX,
5761
- peer->m_wtxid_relay ?
5762
- txinfo.tx ->GetWitnessHash ().ToUint256 () :
5763
- txinfo.tx ->GetHash ().ToUint256 (),
5764
- };
5765
- 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);
5766
5764
5767
5765
// Don't send transactions that peers will not put into their mempool
5768
5766
if (txinfo.fee < filterrate.GetFee (txinfo.vsize )) {
@@ -5783,9 +5781,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5783
5781
// Determine transactions to relay
5784
5782
if (fSendTrickle ) {
5785
5783
// Produce a vector with all candidates for sending
5786
- std::vector<std::set<GenTxid >::iterator> vInvTx;
5784
+ std::vector<std::set<Wtxid >::iterator> vInvTx;
5787
5785
vInvTx.reserve (tx_relay->m_tx_inventory_to_send .size ());
5788
- 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++) {
5789
5787
vInvTx.push_back (it);
5790
5788
}
5791
5789
const CFeeRate filterrate{tx_relay->m_fee_filter_received .load ()};
@@ -5802,22 +5800,26 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5802
5800
while (!vInvTx.empty () && nRelayedTransactions < broadcast_max) {
5803
5801
// Fetch the top element from the heap
5804
5802
std::pop_heap (vInvTx.begin (), vInvTx.end (), compareInvMempoolOrder);
5805
- std::set<GenTxid >::iterator it = vInvTx.back ();
5803
+ std::set<Wtxid >::iterator it = vInvTx.back ();
5806
5804
vInvTx.pop_back ();
5807
- GenTxid hash = *it;
5808
- Assume (peer->m_wtxid_relay == hash.IsWtxid ());
5809
- CInv inv (peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash.ToUint256 ());
5805
+ auto wtxid = *it;
5810
5806
// Remove it from the to-be-sent set
5811
5807
tx_relay->m_tx_inventory_to_send .erase (it);
5812
- // Check if not in the filter already
5813
- if (tx_relay->m_tx_inventory_known_filter .contains (hash.ToUint256 ())) {
5814
- continue ;
5815
- }
5816
5808
// Not in the mempool anymore? don't bother sending it.
5817
- auto txinfo{ std::visit ([&]( const auto & id) { return m_mempool.info (id); }, hash)} ;
5809
+ auto txinfo = m_mempool.info (wtxid) ;
5818
5810
if (!txinfo.tx ) {
5819
5811
continue ;
5820
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
+ }
5821
5823
// Peer told you to not send transactions at that feerate? Don't bother sending it.
5822
5824
if (txinfo.fee < filterrate.GetFee (txinfo.vsize )) {
5823
5825
continue ;
@@ -5830,7 +5832,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
5830
5832
MakeAndPushMessage (*pto, NetMsgType::INV, vInv);
5831
5833
vInv.clear ();
5832
5834
}
5833
- tx_relay->m_tx_inventory_known_filter .insert (hash. ToUint256 () );
5835
+ tx_relay->m_tx_inventory_known_filter .insert (inv. hash );
5834
5836
}
5835
5837
5836
5838
// Ensure we'll respond to GETDATA requests for anything we've just announced
0 commit comments