Skip to content

Commit

Permalink
Add config option to accept private network addresses from introducers (
Browse files Browse the repository at this point in the history
#15543)

* Add configuration option to accept private network addresses from introducers

* Add tests for the enable_private_networks config option

* Fix lint and such

* Test state of private networks in address manager after init

* Check for None on address_manager
  • Loading branch information
cmmarslender authored Jun 21, 2023
1 parent 3a914ce commit 8fd44c1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chia/server/node_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

class FullNodeDiscovery:
resolver: Optional[dns.asyncresolver.Resolver]
enable_private_networks: bool

def __init__(
self,
Expand All @@ -64,6 +65,9 @@ def __init__(
self.introducer_info: Optional[UnresolvedPeerInfo] = None
if introducer_info is not None:
self.introducer_info = UnresolvedPeerInfo(introducer_info["host"], introducer_info["port"])
self.enable_private_networks = introducer_info.get("enable_private_networks", False)
else:
self.enable_private_networks = False
self.peer_connect_interval = peer_connect_interval
self.log = log
self.relay_queue: Optional[asyncio.Queue[Tuple[TimestampedPeerInfo, int]]] = None
Expand Down Expand Up @@ -112,6 +116,8 @@ async def migrate_address_manager_if_necessary(self) -> None:

async def initialize_address_manager(self) -> None:
self.address_manager = await AddressManagerStore.create_address_manager(self.peers_file_path)
if self.enable_private_networks:
self.address_manager.make_private_subnets_valid()
self.server.set_received_message_callback(self.update_peer_timestamp_on_message)

async def start_tasks(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions chia/util/initial-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ full_node:
introducer_peer:
host: introducer.chia.net # Chia AWS introducer IPv4/IPv6
port: 8444
enable_private_networks: False
wallet_peer:
host: *self_hostname
port: 8449
Expand Down Expand Up @@ -551,6 +552,7 @@ wallet:
introducer_peer:
host: introducer.chia.net # Chia AWS introducer IPv4/IPv6
port: 8444
enable_private_networks: False

ssl:
private_crt: "config/ssl/wallet/private_wallet.crt"
Expand Down
95 changes: 95 additions & 0 deletions tests/core/server/test_node_discovery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from __future__ import annotations

from logging import Logger
from typing import Tuple

import pytest

from chia.full_node.full_node_api import FullNodeAPI
from chia.server.node_discovery import FullNodeDiscovery
from chia.server.peer_store_resolver import PeerStoreResolver
from chia.server.server import ChiaServer
from chia.simulator.block_tools import BlockTools
from chia.util.default_root import SIMULATOR_ROOT_PATH


@pytest.mark.asyncio
async def test_enable_private_networks(
two_nodes: Tuple[FullNodeAPI, FullNodeAPI, ChiaServer, ChiaServer, BlockTools],
) -> None:
chia_server = two_nodes[2]

# Missing `enable_private_networks` config entry in introducer_peer should default to False for back compat
discovery0 = FullNodeDiscovery(
chia_server,
0,
PeerStoreResolver(
SIMULATOR_ROOT_PATH,
chia_server.config,
selected_network=chia_server.config["selected_network"],
peers_file_path_key="peers_file_path",
legacy_peer_db_path_key="db/peer_table_node.sqlite",
default_peers_file_path="db/peers.dat",
),
{"host": "introducer.chia.net", "port": 8444},
[],
0,
chia_server.config["selected_network"],
None,
Logger("node_discovery_tests"),
)
assert discovery0 is not None
assert discovery0.enable_private_networks is False
await discovery0.initialize_address_manager()
assert discovery0.address_manager is not None
assert discovery0.address_manager.allow_private_subnets is False

# Test with enable_private_networks set to False in Config
discovery1 = FullNodeDiscovery(
chia_server,
0,
PeerStoreResolver(
SIMULATOR_ROOT_PATH,
chia_server.config,
selected_network=chia_server.config["selected_network"],
peers_file_path_key="peers_file_path",
legacy_peer_db_path_key="db/peer_table_node.sqlite",
default_peers_file_path="db/peers.dat",
),
{"host": "introducer.chia.net", "port": 8444, "enable_private_networks": False},
[],
0,
chia_server.config["selected_network"],
None,
Logger("node_discovery_tests"),
)
assert discovery1 is not None
assert discovery1.enable_private_networks is False
await discovery1.initialize_address_manager()
assert discovery1.address_manager is not None
assert discovery1.address_manager.allow_private_subnets is False

# Test with enable_private_networks set to True in Config
discovery2 = FullNodeDiscovery(
chia_server,
0,
PeerStoreResolver(
SIMULATOR_ROOT_PATH,
chia_server.config,
selected_network=chia_server.config["selected_network"],
peers_file_path_key="peers_file_path",
legacy_peer_db_path_key="db/peer_table_node.sqlite",
default_peers_file_path="db/peers.dat",
),
{"host": "introducer.chia.net", "port": 8444, "enable_private_networks": True},
[],
0,
chia_server.config["selected_network"],
None,
Logger("node_discovery_tests"),
)
assert discovery2 is not None
assert discovery2.enable_private_networks is True
await discovery2.initialize_address_manager()
assert discovery2.address_manager is not None
assert discovery2.address_manager.allow_private_subnets is True

0 comments on commit 8fd44c1

Please sign in to comment.