Skip to content

Commit 581791c

Browse files
author
bruno
committed
test: add functional test for anchors.dat
1 parent b9f41df commit 581791c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

test/functional/feature_anchors.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test Anchors functionality"""
6+
7+
import os
8+
9+
from test_framework.p2p import P2PInterface
10+
from test_framework.test_framework import BitcoinTestFramework
11+
from test_framework.util import assert_equal
12+
13+
14+
def check_node_connections(*, node, num_in, num_out):
15+
info = node.getnetworkinfo()
16+
assert_equal(info["connections_in"], num_in)
17+
assert_equal(info["connections_out"], num_out)
18+
19+
20+
class AnchorsTest(BitcoinTestFramework):
21+
def set_test_params(self):
22+
self.num_nodes = 1
23+
24+
def setup_network(self):
25+
self.setup_nodes()
26+
27+
def run_test(self):
28+
self.log.info("Add 2 block-relay-only connections to node 0")
29+
for i in range(2):
30+
self.log.debug(f"block-relay-only: {i}")
31+
self.nodes[0].add_outbound_p2p_connection(
32+
P2PInterface(), p2p_idx=i, connection_type="block-relay-only"
33+
)
34+
35+
self.log.info("Add 5 inbound connections to node 0")
36+
for i in range(5):
37+
self.log.debug(f"inbound: {i}")
38+
self.nodes[0].add_p2p_connection(P2PInterface())
39+
40+
self.log.info("Check node 0 connections")
41+
check_node_connections(node=self.nodes[0], num_in=5, num_out=2)
42+
43+
# 127.0.0.1
44+
ip = "7f000001"
45+
46+
# Since the ip is always 127.0.0.1 for this case,
47+
# we store only the port to identify the peers
48+
block_relay_nodes_port = []
49+
inbound_nodes_port = []
50+
for p in self.nodes[0].getpeerinfo():
51+
addr_split = p["addr"].split(":")
52+
if p["connection_type"] == "block-relay-only":
53+
block_relay_nodes_port.append(hex(int(addr_split[1]))[2:])
54+
else:
55+
inbound_nodes_port.append(hex(int(addr_split[1]))[2:])
56+
57+
self.log.info("Stop node 0")
58+
self.stop_node(0)
59+
60+
node0_anchors_path = os.path.join(
61+
self.nodes[0].datadir, "regtest", "anchors.dat"
62+
)
63+
64+
# It should contain only the block-relay-only addresses
65+
self.log.info("Check the addresses in anchors.dat")
66+
67+
with open(node0_anchors_path, "rb") as file_handler:
68+
anchors = file_handler.read().hex()
69+
70+
for port in block_relay_nodes_port:
71+
ip_port = ip + port
72+
assert ip_port in anchors
73+
for port in inbound_nodes_port:
74+
ip_port = ip + port
75+
assert ip_port not in anchors
76+
77+
self.log.info("Start node 0")
78+
self.start_node(0)
79+
80+
self.log.info("When node starts, check if anchors.dat doesn't exist anymore")
81+
assert not os.path.exists(node0_anchors_path)
82+
83+
84+
if __name__ == "__main__":
85+
AnchorsTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
'p2p_ping.py',
280280
'rpc_scantxoutset.py',
281281
'feature_logging.py',
282+
'feature_anchors.py',
282283
'p2p_node_network_limited.py',
283284
'p2p_permissions.py',
284285
'feature_blocksdir.py',

0 commit comments

Comments
 (0)