Skip to content

Commit 602e69e

Browse files
committed
[test] P2PBlocksOnly - Test block-relay-only connections.
Ensure we will disconnect if the peer sends us a transaction & we don't announce transactions to the peer.
1 parent 8bb6bea commit 602e69e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

test/functional/p2p_blocksonly.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
# Copyright (c) 2019-2020 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test p2p blocksonly"""
5+
"""Test p2p blocksonly mode & block-relay-only connections."""
6+
7+
import time
68

79
from test_framework.blocktools import create_transaction
810
from test_framework.messages import msg_tx
9-
from test_framework.p2p import P2PInterface
11+
from test_framework.p2p import P2PInterface, P2PTxInvStore
1012
from test_framework.test_framework import BitcoinTestFramework
1113
from test_framework.util import assert_equal
1214

@@ -22,6 +24,7 @@ def skip_test_if_missing_module(self):
2224

2325
def run_test(self):
2426
self.blocksonly_mode_tests()
27+
self.blocks_relay_conn_tests()
2528

2629
def blocksonly_mode_tests(self):
2730
self.log.info("Tests with node running in -blocksonly mode")
@@ -66,10 +69,37 @@ def blocksonly_mode_tests(self):
6669
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
6770
self.log.info("Relay-permission peer's transaction is accepted and relayed")
6871

69-
def check_p2p_tx_violation(self):
70-
self.log.info('Check that txs from P2P are rejected and result in disconnect')
72+
self.nodes[0].disconnect_p2ps()
73+
self.nodes[0].generate(1)
74+
75+
def blocks_relay_conn_tests(self):
76+
self.log.info('Tests with node in normal mode with block-relay-only connections')
77+
self.restart_node(0, ["-noblocksonly"]) # disables blocks only mode
78+
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], True)
79+
80+
# Ensure we disconnect if a block-relay-only connection sends us a transaction
81+
self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="block-relay-only")
82+
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], False)
83+
_, txid, tx_hex = self.check_p2p_tx_violation(index=2)
84+
85+
self.log.info("Check that txs from RPC are not sent to blockrelay connection")
86+
conn = self.nodes[0].add_outbound_p2p_connection(P2PTxInvStore(), p2p_idx=1, connection_type="block-relay-only")
7187

72-
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]['txid']
88+
self.nodes[0].sendrawtransaction(tx_hex)
89+
90+
# Bump time forward to ensure nNextInvSend timer pops
91+
self.nodes[0].setmocktime(int(time.time()) + 60)
92+
93+
# Calling sync_with_ping twice requires that the node calls
94+
# `ProcessMessage` twice, and thus ensures `SendMessages` must have
95+
# been called at least once
96+
conn.sync_with_ping()
97+
conn.sync_with_ping()
98+
assert(int(txid, 16) not in conn.get_invs())
99+
100+
def check_p2p_tx_violation(self, index=1):
101+
self.log.info('Check that txs from P2P are rejected and result in disconnect')
102+
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(index), 2)['tx'][0]['txid']
73103
tx = create_transaction(self.nodes[0], input_txid, self.nodes[0].getnewaddress(), amount=(50 - 0.001))
74104
txid = tx.rehash()
75105
tx_hex = tx.serialize().hex()

0 commit comments

Comments
 (0)