Skip to content
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

Updated the Mock DHT provider to inherit from its intended superclass #1281

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
14 changes: 9 additions & 5 deletions ipv8/test/messaging/anonymization/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from collections import defaultdict
from typing import TYPE_CHECKING

from ....dht.provider import DHTCommunityProvider

if TYPE_CHECKING:
from ipv8.types import Peer
from ....messaging.anonymization.tunnel import IntroductionPoint
from ....types import Peer

# Map of info_hash -> peer list
global_dht_services = defaultdict(list)


class MockDHTProvider:
class MockDHTProvider(DHTCommunityProvider):
"""
A mocked provider for DHT info.
"""
Expand All @@ -19,23 +22,24 @@ def __init__(self, peer: Peer) -> None:
"""
Our peer to register in the mocked DHT.
"""
super().__init__(None, 0)
self.peer = peer
# DHTDiscoveryCommunity functionality
global_dht_services[peer.mid].append(peer)

async def peer_lookup(self, mid: bytes, peer: Peer = None) -> tuple[bytes, list[Peer]]:
async def peer_lookup(self, mid: bytes, peer: Peer | None = None) -> None:
"""
Look for peers with the corresponding mid.
"""
return await self.lookup(mid)

async def lookup(self, info_hash: bytes) -> tuple[bytes, list[Peer]]:
async def lookup(self, info_hash: bytes) -> tuple[bytes, list[IntroductionPoint]] | None:
"""
Look for peers providing generic SHA-1 resources.
"""
return info_hash, global_dht_services.get(info_hash, [])

async def announce(self, info_hash: bytes, intro_point: Peer) -> None:
async def announce(self, info_hash: bytes, intro_point: IntroductionPoint) -> None:
"""
Announce that a certain peer is serving a given SHA-1 resource.
"""
Expand Down
Loading