2
2
# Copyright (c) 2019-2020 The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# 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
6
8
7
9
from test_framework .blocktools import create_transaction
8
10
from test_framework .messages import msg_tx
9
- from test_framework .p2p import P2PInterface
11
+ from test_framework .p2p import P2PInterface , P2PTxInvStore
10
12
from test_framework .test_framework import BitcoinTestFramework
11
13
from test_framework .util import assert_equal
12
14
@@ -22,6 +24,7 @@ def skip_test_if_missing_module(self):
22
24
23
25
def run_test (self ):
24
26
self .blocksonly_mode_tests ()
27
+ self .blocks_relay_conn_tests ()
25
28
26
29
def blocksonly_mode_tests (self ):
27
30
self .log .info ("Tests with node running in -blocksonly mode" )
@@ -66,10 +69,37 @@ def blocksonly_mode_tests(self):
66
69
assert_equal (self .nodes [0 ].getmempoolinfo ()['size' ], 1 )
67
70
self .log .info ("Relay-permission peer's transaction is accepted and relayed" )
68
71
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" )
71
87
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' ]
73
103
tx = create_transaction (self .nodes [0 ], input_txid , self .nodes [0 ].getnewaddress (), amount = (50 - 0.001 ))
74
104
txid = tx .rehash ()
75
105
tx_hex = tx .serialize ().hex ()
0 commit comments