Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_leak_tx.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017-2018 The Bitcoin Core developers | # Copyright (c) 2017-2018 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # 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""" | """Test that we don't leak txs to inbound peers that we haven't yet announced to""" | ||||
from test_framework.messages import MSG_TX, CInv, msg_getdata | from test_framework.messages import MSG_TX, CInv, msg_getdata | ||||
from test_framework.p2p import P2PDataStore | from test_framework.p2p import P2PDataStore | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal | from test_framework.util import assert_equal | ||||
from test_framework.wallet import MiniWallet | |||||
class P2PNode(P2PDataStore): | class P2PNode(P2PDataStore): | ||||
def on_inv(self, msg): | def on_inv(self, msg): | ||||
pass | pass | ||||
class P2PLeakTxTest(BitcoinTestFramework): | class P2PLeakTxTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
def skip_test_if_missing_module(self): | |||||
self.skip_if_no_wallet() | |||||
def run_test(self): | def run_test(self): | ||||
# The block and tx generating node | # The block and tx generating node | ||||
gen_node = self.nodes[0] | gen_node = self.nodes[0] | ||||
gen_node.generate(1) | miniwallet = MiniWallet(gen_node) | ||||
# Add enough mature utxos to the wallet, so that all txs spend | |||||
# confirmed coins | |||||
miniwallet.generate(1) | |||||
gen_node.generate(100) | |||||
# An "attacking" inbound peer | # An "attacking" inbound peer | ||||
inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) | inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) | ||||
MAX_REPEATS = 100 | MAX_REPEATS = 100 | ||||
self.log.info("Running test up to {} times.".format(MAX_REPEATS)) | self.log.info("Running test up to {} times.".format(MAX_REPEATS)) | ||||
for i in range(MAX_REPEATS): | for i in range(MAX_REPEATS): | ||||
self.log.info('Run repeat {}'.format(i + 1)) | self.log.info('Run repeat {}'.format(i + 1)) | ||||
txid = gen_node.sendtoaddress(gen_node.getnewaddress(), 10000) | txid = miniwallet.send_self_transfer(from_node=gen_node)['txid'] | ||||
want_tx = msg_getdata() | want_tx = msg_getdata() | ||||
want_tx.inv.append(CInv(t=MSG_TX, h=int(txid, 16))) | want_tx.inv.append(CInv(t=MSG_TX, h=int(txid, 16))) | ||||
inbound_peer.last_message.pop('notfound', None) | inbound_peer.last_message.pop('notfound', None) | ||||
inbound_peer.send_and_ping(want_tx) | inbound_peer.send_and_ping(want_tx) | ||||
if inbound_peer.last_message.get('notfound'): | if inbound_peer.last_message.get('notfound'): | ||||
self.log.debug( | self.log.debug( | ||||
Show All 16 Lines |