|
16 | 16 | CInv, |
17 | 17 | MSG_TX, |
18 | 18 | MSG_TYPE_MASK, |
| 19 | + MSG_WITNESS_TX, |
19 | 20 | MSG_WTX, |
20 | 21 | msg_inv, |
21 | 22 | msg_notfound, |
@@ -324,6 +325,72 @@ def test_large_inv_batch(self): |
324 | 325 | peer.wait_until(lambda: peer.tx_getdata_count == MAX_PEER_TX_ANNOUNCEMENTS) |
325 | 326 | peer.sync_with_ping() |
326 | 327 |
|
| 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 | + |
327 | 394 | def test_spurious_notfound(self): |
328 | 395 | self.log.info('Check that spurious notfound is ignored') |
329 | 396 | self.nodes[0].p2ps[0].send_without_ping(msg_notfound(vec=[CInv(MSG_TX, 1)])) |
@@ -395,6 +462,7 @@ def run_test(self): |
395 | 462 | self.test_txid_inv_delay() |
396 | 463 | self.test_txid_inv_delay(True) |
397 | 464 | self.test_large_inv_batch() |
| 465 | + self.test_duplicate_tx_inv() |
398 | 466 | self.test_spurious_notfound() |
399 | 467 |
|
400 | 468 | # Run each test against new bitcoind instances, as setting mocktimes has long-term effects on when |
|
0 commit comments