Skip to content

Commit 7c9e5a1

Browse files
committed
test: Check that v1 connections to clearnet peers don't work
when `-v2onlyclearnet` is turned on: - v1 connections to clearnet peers don't work - v2 connections to clearnet peers work - v1 conneections to tor/i2p/cjdns peer works a proxy is used because otherwise NET_UNROUTABLE is the default network in the tests.
1 parent e78c090 commit 7c9e5a1

File tree

1 file changed

+97
-75
lines changed

1 file changed

+97
-75
lines changed

test/functional/p2p_v2_encrypted.py

+97-75
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
assert_equal,
1919
assert_greater_than,
2020
check_node_connections,
21+
p2p_port,
2122
)
2223
from test_framework.crypto.chacha20 import REKEY_INTERVAL
24+
from test_framework.socks5 import Socks5Configuration, Socks5Server
2325

2426

2527
class P2PEncrypted(BitcoinTestFramework):
@@ -53,81 +55,101 @@ def create_test_block(self, txs):
5355

5456
def run_test(self):
5557
node0, node1 = self.nodes[0], self.nodes[1]
56-
self.log.info("Check inbound connection to v2 TestNode from v2 P2PConnection is v2")
57-
peer1 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=True)
58-
assert peer1.supports_v2_p2p
59-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
60-
61-
self.log.info("Check inbound connection to v2 TestNode from v1 P2PConnection is v1")
62-
peer2 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=False)
63-
assert not peer2.supports_v2_p2p
64-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
65-
66-
self.log.info("Check outbound connection from v2 TestNode to v1 P2PConnection advertised as v1 is v1")
67-
peer3 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, supports_v2_p2p=False, advertise_v2_p2p=False)
68-
assert not peer3.supports_v2_p2p
69-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
70-
71-
# v2 TestNode performs downgrading here
72-
self.log.info("Check outbound connection from v2 TestNode to v1 P2PConnection advertised as v2 is v1")
73-
peer4 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, supports_v2_p2p=False, advertise_v2_p2p=True)
74-
assert not peer4.supports_v2_p2p
75-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
76-
77-
self.log.info("Check outbound connection from v2 TestNode to v2 P2PConnection advertised as v2 is v2")
78-
peer5 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, supports_v2_p2p=True, advertise_v2_p2p=True)
79-
assert peer5.supports_v2_p2p
80-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
81-
82-
self.log.info("Check if version is sent and verack is received in inbound/outbound connections")
83-
assert_equal(len(node0.getpeerinfo()), 5) # check if above 5 connections are present in node0's getpeerinfo()
84-
for peer in node0.getpeerinfo():
85-
assert_greater_than(peer['bytessent_per_msg']['version'], 0)
86-
assert_greater_than(peer['bytesrecv_per_msg']['verack'], 0)
87-
88-
self.log.info("Testing whether blocks propagate - check if tips sync when number of blocks >= REKEY_INTERVAL")
89-
# tests whether rekeying (which happens every REKEY_INTERVAL packets) works correctly
90-
test_blocks = self.generate_blocks(node0, REKEY_INTERVAL+1)
91-
92-
for i in range(2):
93-
peer6 = node0.add_p2p_connection(P2PDataStore(), supports_v2_p2p=True)
94-
assert peer6.supports_v2_p2p
95-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
96-
97-
# Consider: node0 <-- peer6. node0 and node1 aren't connected here.
98-
# Construct the following topology: node1 <--> node0 <-- peer6
99-
# and test that blocks produced by peer6 will be received by node1 if sent normally
100-
# and won't be received by node1 if sent as decoy messages
101-
102-
# First, check whether blocks produced be peer6 are received by node0 if sent normally
103-
# and not received by node0 if sent as decoy messages.
104-
if i:
105-
# check that node0 receives blocks produced by peer6
106-
self.log.info("Check if blocks produced by node0's p2p connection is received by node0")
107-
peer6.send_blocks_and_test(test_blocks, node0, success=True) # node0's tip advances
108-
else:
109-
# check that node0 doesn't receive blocks produced by peer6 since they are sent as decoy messages
110-
self.log.info("Check if blocks produced by node0's p2p connection sent as decoys aren't received by node0")
111-
peer6.send_blocks_and_test(test_blocks, node0, success=False, is_decoy=True) # node0's tip doesn't advance
112-
113-
# Then, connect node0 and node1 using v2 and check whether the blocks are received by node1
114-
self.connect_nodes(0, 1, peer_advertises_v2=True)
115-
self.log.info("Wait for node1 to receive all the blocks from node0")
116-
self.sync_all()
117-
self.log.info("Make sure node0 and node1 have same block tips")
118-
assert_equal(node0.getbestblockhash(), node1.getbestblockhash())
119-
120-
self.disconnect_nodes(0, 1)
121-
122-
self.log.info("Check the connections opened as expected")
123-
check_node_connections(node=node0, num_in=4, num_out=3)
124-
125-
self.log.info("Check inbound connection to v1 TestNode from v2 P2PConnection is v1")
126-
self.restart_node(0, ["-v2transport=0"])
127-
peer1 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=True)
128-
assert not peer1.supports_v2_p2p
129-
assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
130-
check_node_connections(node=node0, num_in=1, num_out=0)
58+
# self.log.info("Check inbound connection to v2 TestNode from v2 P2PConnection is v2")
59+
# peer1 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=True)
60+
# assert peer1.supports_v2_p2p
61+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
62+
#
63+
# self.log.info("Check inbound connection to v2 TestNode from v1 P2PConnection is v1")
64+
# peer2 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=False)
65+
# assert not peer2.supports_v2_p2p
66+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
67+
#
68+
# self.log.info("Check outbound connection from v2 TestNode to v1 P2PConnection advertised as v1 is v1")
69+
# peer3 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, supports_v2_p2p=False, advertise_v2_p2p=False)
70+
# assert not peer3.supports_v2_p2p
71+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
72+
#
73+
# # v2 TestNode performs downgrading here
74+
# self.log.info("Check outbound connection from v2 TestNode to v1 P2PConnection advertised as v2 is v1")
75+
# peer4 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, supports_v2_p2p=False, advertise_v2_p2p=True)
76+
# assert not peer4.supports_v2_p2p
77+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
78+
#
79+
# self.log.info("Check outbound connection from v2 TestNode to v2 P2PConnection advertised as v2 is v2")
80+
# peer5 = node0.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, supports_v2_p2p=True, advertise_v2_p2p=True)
81+
# assert peer5.supports_v2_p2p
82+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
83+
#
84+
# self.log.info("Check if version is sent and verack is received in inbound/outbound connections")
85+
# assert_equal(len(node0.getpeerinfo()), 5) # check if above 5 connections are present in node0's getpeerinfo()
86+
# for peer in node0.getpeerinfo():
87+
# assert_greater_than(peer['bytessent_per_msg']['version'], 0)
88+
# assert_greater_than(peer['bytesrecv_per_msg']['verack'], 0)
89+
#
90+
# self.log.info("Testing whether blocks propagate - check if tips sync when number of blocks >= REKEY_INTERVAL")
91+
# # tests whether rekeying (which happens every REKEY_INTERVAL packets) works correctly
92+
# test_blocks = self.generate_blocks(node0, REKEY_INTERVAL+1)
93+
#
94+
# for i in range(2):
95+
# peer6 = node0.add_p2p_connection(P2PDataStore(), supports_v2_p2p=True)
96+
# assert peer6.supports_v2_p2p
97+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v2")
98+
#
99+
# # Consider: node0 <-- peer6. node0 and node1 aren't connected here.
100+
# # Construct the following topology: node1 <--> node0 <-- peer6
101+
# # and test that blocks produced by peer6 will be received by node1 if sent normally
102+
# # and won't be received by node1 if sent as decoy messages
103+
#
104+
# # First, check whether blocks produced be peer6 are received by node0 if sent normally
105+
# # and not received by node0 if sent as decoy messages.
106+
# if i:
107+
# # check that node0 receives blocks produced by peer6
108+
# self.log.info("Check if blocks produced by node0's p2p connection is received by node0")
109+
# peer6.send_blocks_and_test(test_blocks, node0, success=True) # node0's tip advances
110+
# else:
111+
# # check that node0 doesn't receive blocks produced by peer6 since they are sent as decoy messages
112+
# self.log.info("Check if blocks produced by node0's p2p connection sent as decoys aren't received by node0")
113+
# peer6.send_blocks_and_test(test_blocks, node0, success=False, is_decoy=True) # node0's tip doesn't advance
114+
#
115+
# # Then, connect node0 and node1 using v2 and check whether the blocks are received by node1
116+
# self.connect_nodes(0, 1, peer_advertises_v2=True)
117+
# self.log.info("Wait for node1 to receive all the blocks from node0")
118+
# self.sync_all()
119+
# self.log.info("Make sure node0 and node1 have same block tips")
120+
# assert_equal(node0.getbestblockhash(), node1.getbestblockhash())
121+
#
122+
# self.disconnect_nodes(0, 1)
123+
#
124+
# self.log.info("Check the connections opened as expected")
125+
# check_node_connections(node=node0, num_in=4, num_out=3)
126+
#
127+
# self.log.info("Check inbound connection to v1 TestNode from v2 P2PConnection is v1")
128+
# self.restart_node(0, ["-v2transport=0"])
129+
# peer1 = node0.add_p2p_connection(P2PInterface(), wait_for_verack=True, supports_v2_p2p=True)
130+
# assert not peer1.supports_v2_p2p
131+
# assert_equal(node0.getpeerinfo()[-1]["transport_protocol_type"], "v1")
132+
# check_node_connections(node=node0, num_in=1, num_out=0)
133+
134+
conf = Socks5Configuration()
135+
conf.auth = True
136+
conf.unauth = True
137+
conf.addr = ('127.0.0.1', p2p_port(self.num_nodes))
138+
conf.keep_alive = True
139+
proxy = Socks5Server(conf)
140+
proxy.start()
141+
args = ['-listen', f'-proxy={conf.addr[0]}:{conf.addr[1]}', '-proxyrandomize=0', '-v2onlyclearnet=1', '-v2transport=1']
142+
self.restart_node(0, extra_args=args)
143+
self.log.info("Check that v2 connection to an ipv4 peer is successful")
144+
node0.addnode("15.61.23.23:1234", "onetry", True)
145+
assert_equal(node0.getpeerinfo()[-1]["addr"], "15.61.23.23:1234")
146+
self.log.info("Check that v1 connection to an ipv4 peer is unsuccessful")
147+
node0.addnode("8.8.8.8:1234", "onetry", False)
148+
assert all(peer["addr"] != "8.8.8.8:1234" for peer in node0.getpeerinfo())
149+
self.log.info("Check that v1 connection to an onion peer is successful")
150+
addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:8333"
151+
node0.addnode(addr, "onetry", False)
152+
assert_equal(node0.getpeerinfo()[-1]["addr"], addr)
131153

132154

133155
if __name__ == '__main__':

0 commit comments

Comments
 (0)