Skip to content

Commit 1278a59

Browse files
committed
net_processing: process unique tx INVs only
There is no reason we should process conflicting advertisements for transactions, as they cannot be both accepted into our mempool. Avoid processing these and doing spurious work.
1 parent 975a314 commit 1278a59

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎src/net_processing.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <txmempool.h>
5858
#include <uint256.h>
5959
#include <util/check.h>
60+
#include <util/hasher.h>
6061
#include <util/strencodings.h>
6162
#include <util/time.h>
6263
#include <util/tokenbucket.h>
@@ -85,6 +86,7 @@
8586
#include <set>
8687
#include <span>
8788
#include <typeinfo>
89+
#include <unordered_set>
8890
#include <utility>
8991

9092
using kernel::ChainstateRole;
@@ -842,6 +844,8 @@ class PeerManagerImpl final : public PeerManager
842844

843845
FastRandomContext m_rng GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
844846

847+
/** Copied into short-lived tx INV deduplication sets to avoid generating salts per message. */
848+
const SaltedUint256Hasher m_txhash_hasher;
845849
FeeFilterRounder m_fee_filter_rounder GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
846850

847851
const CChainParams& m_chainparams;
@@ -4337,6 +4341,8 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
43374341
}
43384342

43394343
const bool reject_tx_invs{RejectIncomingTxs(pfrom)};
4344+
std::unordered_set<uint256, SaltedUint256Hasher> seen_txids{0, m_txhash_hasher};
4345+
std::unordered_set<uint256, SaltedUint256Hasher> seen_wtxids{0, m_txhash_hasher};
43404346

43414347
LOCK2(cs_main, m_tx_download_mutex);
43424348

@@ -4375,6 +4381,9 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
43754381
pfrom.fDisconnect = true;
43764382
return;
43774383
}
4384+
// MSG_WITNESS_TX is treated as a txid, despite only being specified for getdata.
4385+
auto& seen_hashes{inv.IsMsgWtx() ? seen_wtxids : seen_txids};
4386+
if (!seen_hashes.insert(inv.hash).second) continue;
43784387
const GenTxid gtxid = ToGenTxid(inv);
43794388
AddKnownTx(peer, inv.hash);
43804389

‎test/functional/p2p_tx_download.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
CInv,
1717
MSG_TX,
1818
MSG_TYPE_MASK,
19+
MSG_WITNESS_TX,
1920
MSG_WTX,
2021
msg_inv,
2122
msg_notfound,
@@ -324,6 +325,72 @@ def test_large_inv_batch(self):
324325
peer.wait_until(lambda: peer.tx_getdata_count == MAX_PEER_TX_ANNOUNCEMENTS)
325326
peer.sync_with_ping()
326327

328+
def test_duplicate_tx_inv(self):
329+
self.log.info('Check that duplicate transaction identifiers in one inv message are processed once')
330+
node = self.nodes[0]
331+
node.logging(include=['net'])
332+
333+
def send_invs_and_read_log(peer, invs):
334+
log_start = node.debug_log_size(encoding='utf-8')
335+
peer.send_and_ping(msg_inv(invs))
336+
with open(node.debug_log_path, encoding='utf-8', errors='replace') as debug_log:
337+
debug_log.seek(log_start)
338+
return debug_log.read()
339+
340+
for wtxidrelay, inv_type, inv_name, mismatched_type, mismatched_name, hash_a, hash_b in [
341+
(False, MSG_TX, 'tx', MSG_WTX, 'wtx', 0xaabbcc, 0xddeeff),
342+
(True, MSG_WTX, 'wtx', MSG_TX, 'tx', 0x112233, 0x445566),
343+
]:
344+
peer = node.add_p2p_connection(TestP2PConn(wtxidrelay=wtxidrelay))
345+
inv_a_log = f"got inv: {inv_name} {hash_a:064x}"
346+
inv_b_log = f"got inv: {inv_name} {hash_b:064x}"
347+
mismatched_inv_log = f"got inv: {mismatched_name} {hash_a:064x}"
348+
349+
log = send_invs_and_read_log(peer, [
350+
CInv(t=mismatched_type, h=hash_a),
351+
CInv(t=inv_type, h=hash_a),
352+
CInv(t=inv_type, h=hash_b),
353+
CInv(t=inv_type, h=hash_a),
354+
CInv(t=inv_type, h=hash_b),
355+
])
356+
assert_equal(log.count(inv_a_log), 1)
357+
assert_equal(log.count(inv_b_log), 1)
358+
assert_equal(log.count(mismatched_inv_log), 0)
359+
360+
# The duplicate filter is scoped to a single INV message.
361+
log = send_invs_and_read_log(peer, [
362+
CInv(t=inv_type, h=hash_a),
363+
CInv(t=inv_type, h=hash_a),
364+
])
365+
assert_equal(log.count(inv_a_log), 1)
366+
assert_equal(log.count(inv_b_log), 0)
367+
368+
self.log.info('Check that MSG_TX and MSG_WITNESS_TX are deduplicated as txids')
369+
peer = node.add_p2p_connection(TestP2PConn(wtxidrelay=False))
370+
for first_type, first_name, second_type, second_name, hash_a in [
371+
(MSG_TX, 'tx', MSG_WITNESS_TX, 'witness-tx', 0x667788),
372+
(MSG_WITNESS_TX, 'witness-tx', MSG_TX, 'tx', 0x778899),
373+
]:
374+
log = send_invs_and_read_log(peer, [
375+
CInv(t=first_type, h=hash_a),
376+
CInv(t=second_type, h=hash_a),
377+
])
378+
assert_equal(log.count(f"got inv: {first_name} {hash_a:064x}"), 1)
379+
assert_equal(log.count(f"got inv: {second_name} {hash_a:064x}"), 0)
380+
381+
self.log.info('Check that txids and wtxids are deduplicated separately')
382+
peer = node.add_p2p_connection(TestP2PConn(wtxidrelay=True))
383+
for first_type, second_type, hash_a in [
384+
(MSG_WITNESS_TX, MSG_WTX, 0x8899aa),
385+
(MSG_WTX, MSG_WITNESS_TX, 0x99aabb),
386+
]:
387+
log = send_invs_and_read_log(peer, [
388+
CInv(t=first_type, h=hash_a),
389+
CInv(t=second_type, h=hash_a),
390+
])
391+
assert_equal(log.count(f"got inv: witness-tx {hash_a:064x}"), 1)
392+
assert_equal(log.count(f"got inv: wtx {hash_a:064x}"), 1)
393+
327394
def test_spurious_notfound(self):
328395
self.log.info('Check that spurious notfound is ignored')
329396
self.nodes[0].p2ps[0].send_without_ping(msg_notfound(vec=[CInv(MSG_TX, 1)]))
@@ -395,6 +462,7 @@ def run_test(self):
395462
self.test_txid_inv_delay()
396463
self.test_txid_inv_delay(True)
397464
self.test_large_inv_batch()
465+
self.test_duplicate_tx_inv()
398466
self.test_spurious_notfound()
399467

400468
# Run each test against new bitcoind instances, as setting mocktimes has long-term effects on when

0 commit comments

Comments
 (0)