2018-09-16 02:01:20 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-07-28 13:57:16 +02:00
|
|
|
# Copyright (c) 2017-2021 The Bitcoin Core developers
|
2018-09-16 02:01:20 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test that we don't leak txs to inbound peers that we haven't yet announced to"""
|
|
|
|
|
2020-04-25 13:44:44 +02:00
|
|
|
from test_framework.messages import msg_getdata, CInv, MSG_TX
|
2020-10-12 00:22:16 +02:00
|
|
|
from test_framework.p2p import p2p_lock, P2PDataStore
|
2018-09-16 02:01:20 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
)
|
2020-10-11 23:57:56 +02:00
|
|
|
from test_framework.wallet import MiniWallet
|
2018-09-16 02:01:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class P2PNode(P2PDataStore):
|
|
|
|
def on_inv(self, msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class P2PLeakTxTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
def run_test(self):
|
2023-05-11 14:13:14 +02:00
|
|
|
self.gen_node = self.nodes[0] # The block and tx generating node
|
|
|
|
self.miniwallet = MiniWallet(self.gen_node)
|
2018-09-16 02:01:20 +02:00
|
|
|
|
2023-05-11 14:13:14 +02:00
|
|
|
self.test_notfound_on_unannounced_tx()
|
|
|
|
|
|
|
|
def test_notfound_on_unannounced_tx(self):
|
2018-09-16 02:01:20 +02:00
|
|
|
inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) # An "attacking" inbound peer
|
|
|
|
|
|
|
|
MAX_REPEATS = 100
|
|
|
|
self.log.info("Running test up to {} times.".format(MAX_REPEATS))
|
|
|
|
for i in range(MAX_REPEATS):
|
|
|
|
self.log.info('Run repeat {}'.format(i + 1))
|
2023-05-11 14:13:14 +02:00
|
|
|
txid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"]
|
2018-09-16 02:01:20 +02:00
|
|
|
|
|
|
|
want_tx = msg_getdata()
|
2020-04-25 13:44:44 +02:00
|
|
|
want_tx.inv.append(CInv(t=MSG_TX, h=int(txid, 16)))
|
2020-10-12 00:22:16 +02:00
|
|
|
with p2p_lock:
|
|
|
|
inbound_peer.last_message.pop('notfound', None)
|
2020-04-01 15:37:20 +02:00
|
|
|
inbound_peer.send_and_ping(want_tx)
|
2018-09-16 02:01:20 +02:00
|
|
|
|
|
|
|
if inbound_peer.last_message.get('notfound'):
|
|
|
|
self.log.debug('tx {} was not yet announced to us.'.format(txid))
|
|
|
|
self.log.debug("node has responded with a notfound message. End test.")
|
|
|
|
assert_equal(inbound_peer.last_message['notfound'].vec[0].hash, int(txid, 16))
|
2020-10-12 00:22:16 +02:00
|
|
|
with p2p_lock:
|
|
|
|
inbound_peer.last_message.pop('notfound')
|
2018-09-16 02:01:20 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.log.debug('tx {} was already announced to us. Try test again.'.format(txid))
|
|
|
|
assert int(txid, 16) in [inv.hash for inv in inbound_peer.last_message['inv'].inv]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PLeakTxTest().main()
|