Skip to content

Commit 34c8047

Browse files
laanwjknst
authored andcommitted
Merge bitcoin#19877: [test] clarify rpc_net & p2p_disconnect_ban functional tests
47ff509 [test] Clarify setup of node topology. (Amiti Uttarwar) 0672522 [move-only, test]: Match test order with run order (Amiti Uttarwar) Pull request description: small improvements to clarify logic in the functional tests 1. have test logic in `rpc_net.py` match run order of the test 2. remove `connect_nodes` calls that are redundant with the automatic test setup executed by the test framework Noticed when I was trying to debug a test for bitcoin#19725. Small changes but imo very helpful, because they initially confused me. ACKs for top commit: laanwj: ACK 47ff509 Tree-SHA512: 2843da2c0b4f06b2600b3adb97900a62be7bb2228770abd67d86f2a65c58079af22c7c20957474a98c17da85f40a958a6f05cb8198aa0c56a58adc1c31100492
1 parent e424129 commit 34c8047

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

test/functional/p2p_disconnect_ban.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ def set_test_params(self):
1717

1818
def run_test(self):
1919
self.log.info("Connect nodes both way")
20+
# By default, the test framework sets up an addnode connection from
21+
# node 1 --> node0. By connecting node0 --> node 1, we're left with
22+
# the two nodes being connected both ways.
23+
# Topology will look like: node0 <--> node1
2024
self.connect_nodes(0, 1)
21-
self.connect_nodes(1, 0)
2225

2326
self.log.info("Test setban and listbanned RPCs")
2427

test/functional/rpc_net.py

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ def run_test(self):
5454
# Especially the exchange of messages like getheaders and friends causes test failures here
5555
self.nodes[0].ping()
5656
self.wait_until(lambda: all(['pingtime' in n for n in self.nodes[0].getpeerinfo()]))
57-
self.log.info('Connect nodes both way')
57+
# By default, the test framework sets up an addnode connection from
58+
# node 1 --> node0. By connecting node0 --> node 1, we're left with
59+
# the two nodes being connected both ways.
60+
# Topology will look like: node0 <--> node1
5861
self.connect_nodes(0, 1)
59-
self.connect_nodes(1, 0)
6062
self.sync_all()
6163

6264
self.test_connection_count()
@@ -75,6 +77,41 @@ def test_connection_count(self):
7577
# during network setup
7678
assert_equal(self.nodes[0].getconnectioncount(), 3)
7779

80+
def test_getpeerinfo(self):
81+
self.log.info("Test getpeerinfo")
82+
# Create a few getpeerinfo last_block/last_transaction values.
83+
self.wallet.send_self_transfer(from_node=self.nodes[0]) # Make a transaction so we can see it in the getpeerinfo results
84+
self.nodes[1].generate(1)
85+
self.sync_all()
86+
time_now = self.mocktime
87+
peer_info = [x.getpeerinfo() for x in self.nodes]
88+
# Verify last_block and last_transaction keys/values.
89+
for node, peer, field in product(range(self.num_nodes - self.mn_count), range(2), ['last_block', 'last_transaction']):
90+
assert field in peer_info[node][peer].keys()
91+
if peer_info[node][peer][field] != 0:
92+
assert_approx(peer_info[node][peer][field], time_now, vspan=60)
93+
# check both sides of bidirectional connection between nodes
94+
# the address bound to on one side will be the source address for the other node
95+
assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
96+
assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
97+
# check the `servicesnames` field
98+
for info in peer_info:
99+
assert_net_servicesnames(int(info[0]["services"], 0x10), info[0]["servicesnames"])
100+
101+
# Check dynamically generated networks list in getpeerinfo help output.
102+
assert "(ipv4, ipv6, onion, i2p, not_publicly_routable)" in self.nodes[0].help("getpeerinfo")
103+
# This part is slightly different comparing to the Bitcoin implementation. This is expected because we create connections on network setup a bit differently too.
104+
# We also create more connection during the test itself to test mn specific stats
105+
assert_equal(peer_info[0][0]['connection_type'], 'inbound')
106+
assert_equal(peer_info[0][1]['connection_type'], 'inbound')
107+
assert_equal(peer_info[0][2]['connection_type'], 'manual')
108+
109+
assert_equal(peer_info[1][0]['connection_type'], 'manual')
110+
assert_equal(peer_info[1][1]['connection_type'], 'inbound')
111+
112+
assert_equal(peer_info[2][0]['connection_type'], 'manual')
113+
114+
78115
def test_getnettotals(self):
79116
self.log.info("Test getnettotals")
80117
# Test getnettotals and getpeerinfo by doing a ping. The bytes
@@ -113,15 +150,13 @@ def test_getnetworkinfo(self):
113150

114151
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
115152
self.nodes[0].setnetworkactive(state=True)
116-
# Connect nodes both ways.
117153
self.connect_nodes(0, 1)
118-
self.connect_nodes(1, 0)
119154

120155
info = self.nodes[1].getnetworkinfo()
121156
assert_equal(info['networkactive'], True)
122-
assert_equal(info['connections'], 2)
157+
assert_equal(info['connections'], 1)
123158
assert_equal(info['connections_in'], 1)
124-
assert_equal(info['connections_out'], 1)
159+
assert_equal(info['connections_out'], 0)
125160
assert_equal(info['connections_mn'], 0)
126161
assert_equal(info['connections_mn_in'], 0)
127162
assert_equal(info['connections_mn_out'], 0)
@@ -135,15 +170,17 @@ def test_getnetworkinfo(self):
135170
assert "(ipv4, ipv6, onion, i2p)" in self.nodes[0].help("getnetworkinfo")
136171

137172
self.log.info('Test extended connections info')
138-
self.connect_nodes(1, 2)
173+
# Connect nodes both ways.
174+
self.connect_nodes(0, 1)
175+
self.connect_nodes(1, 0)
139176
self.nodes[1].ping()
140177
self.wait_until(lambda: all(['pingtime' in n for n in self.nodes[1].getpeerinfo()]))
141-
assert_equal(self.nodes[1].getnetworkinfo()['connections'], 3)
178+
assert_equal(self.nodes[1].getnetworkinfo()['connections'], 2)
142179
assert_equal(self.nodes[1].getnetworkinfo()['connections_in'], 1)
143-
assert_equal(self.nodes[1].getnetworkinfo()['connections_out'], 2)
144-
assert_equal(self.nodes[1].getnetworkinfo()['connections_mn'], 1)
180+
assert_equal(self.nodes[1].getnetworkinfo()['connections_out'], 1)
181+
assert_equal(self.nodes[1].getnetworkinfo()['connections_mn'], 0)
145182
assert_equal(self.nodes[1].getnetworkinfo()['connections_mn_in'], 0)
146-
assert_equal(self.nodes[1].getnetworkinfo()['connections_mn_out'], 1)
183+
assert_equal(self.nodes[1].getnetworkinfo()['connections_mn_out'], 0)
147184

148185
def test_getaddednodeinfo(self):
149186
self.log.info("Test getaddednodeinfo")
@@ -165,40 +202,6 @@ def test_getaddednodeinfo(self):
165202
# check that a non-existent node returns an error
166203
assert_raises_rpc_error(-24, "Node has not been added", self.nodes[0].getaddednodeinfo, '1.1.1.1')
167204

168-
def test_getpeerinfo(self):
169-
self.log.info("Test getpeerinfo")
170-
# Create a few getpeerinfo last_block/last_transaction values.
171-
self.wallet.send_self_transfer(from_node=self.nodes[0]) # Make a transaction so we can see it in the getpeerinfo results
172-
self.nodes[1].generate(1)
173-
self.sync_all()
174-
time_now = self.mocktime
175-
peer_info = [x.getpeerinfo() for x in self.nodes]
176-
# Verify last_block and last_transaction keys/values.
177-
for node, peer, field in product(range(self.num_nodes - self.mn_count), range(2), ['last_block', 'last_transaction']):
178-
assert field in peer_info[node][peer].keys()
179-
if peer_info[node][peer][field] != 0:
180-
assert_approx(peer_info[node][peer][field], time_now, vspan=60)
181-
# check both sides of bidirectional connection between nodes
182-
# the address bound to on one side will be the source address for the other node
183-
assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
184-
assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
185-
# check the `servicesnames` field
186-
for info in peer_info:
187-
assert_net_servicesnames(int(info[0]["services"], 0x10), info[0]["servicesnames"])
188-
189-
# Check dynamically generated networks list in getpeerinfo help output.
190-
assert "(ipv4, ipv6, onion, i2p, not_publicly_routable)" in self.nodes[0].help("getpeerinfo")
191-
# This part is slightly different comparing to the Bitcoin implementation. This is expected because we create connections on network setup a bit differently too.
192-
# We also create more connection during the test itself to test mn specific stats
193-
assert_equal(peer_info[0][0]['connection_type'], 'inbound')
194-
assert_equal(peer_info[0][1]['connection_type'], 'inbound')
195-
assert_equal(peer_info[0][2]['connection_type'], 'manual')
196-
197-
assert_equal(peer_info[1][0]['connection_type'], 'manual')
198-
assert_equal(peer_info[1][1]['connection_type'], 'inbound')
199-
200-
assert_equal(peer_info[2][0]['connection_type'], 'manual')
201-
202205
def test_service_flags(self):
203206
self.log.info("Test service flags")
204207
self.nodes[0].add_p2p_connection(P2PInterface(), services=(1 << 4) | (1 << 63))

0 commit comments

Comments
 (0)