Skip to content

refactor(p2p): split hathor.p2p.peer_discovery into separate modules #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions hathor/p2p/peer_discovery/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .bootstrap import BootstrapPeerDiscovery
from .dns import DNSPeerDiscovery
from .peer_discovery import PeerDiscovery

__all__ = [
'PeerDiscovery',
'BootstrapPeerDiscovery',
'DNSPeerDiscovery',
]
40 changes: 40 additions & 0 deletions hathor/p2p/peer_discovery/bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable

from structlog import get_logger
from typing_extensions import override

from .peer_discovery import PeerDiscovery

logger = get_logger()


class BootstrapPeerDiscovery(PeerDiscovery):
""" It implements a bootstrap peer discovery, which receives a static list of peers.
"""

def __init__(self, descriptions: list[str]):
"""
:param descriptions: Descriptions of peers to connect to.
"""
super().__init__()
self.log = logger.new()
self.descriptions = descriptions

@override
async def discover_and_connect(self, connect_to: Callable[[str], None]) -> None:
for description in self.descriptions:
connect_to(description)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Hathor Labs
# Copyright 2024 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,6 @@
# limitations under the License.

import socket
from abc import ABC, abstractmethod
from typing import Callable

from structlog import get_logger
Expand All @@ -22,39 +21,9 @@
from twisted.names.dns import Record_A, Record_TXT, RRHeader
from typing_extensions import override

logger = get_logger()


class PeerDiscovery(ABC):
""" Base class to implement peer discovery strategies.
"""

@abstractmethod
async def discover_and_connect(self, connect_to: Callable[[str], None]) -> None:
""" This method must discover the peers and call `connect_to` for each of them.

:param connect_to: Function which will be called for each discovered peer.
:type connect_to: function
"""
raise NotImplementedError

from .peer_discovery import PeerDiscovery

class BootstrapPeerDiscovery(PeerDiscovery):
""" It implements a bootstrap peer discovery, which receives a static list of peers.
"""

def __init__(self, descriptions: list[str]):
"""
:param descriptions: Descriptions of peers to connect to.
"""
super().__init__()
self.log = logger.new()
self.descriptions = descriptions

@override
async def discover_and_connect(self, connect_to: Callable[[str], None]) -> None:
for description in self.descriptions:
connect_to(description)
logger = get_logger()


class DNSPeerDiscovery(PeerDiscovery):
Expand Down
30 changes: 30 additions & 0 deletions hathor/p2p/peer_discovery/peer_discovery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2024 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABC, abstractmethod
from typing import Callable


class PeerDiscovery(ABC):
""" Base class to implement peer discovery strategies.
"""

@abstractmethod
async def discover_and_connect(self, connect_to: Callable[[str], None]) -> None:
""" This method must discover the peers and call `connect_to` for each of them.

:param connect_to: Function which will be called for each discovered peer.
:type connect_to: function
"""
raise NotImplementedError
Loading