Merge branch 'libp2p:main' into main

This commit is contained in:
Saksham Chauhan
2025-06-24 18:55:10 +05:30
committed by GitHub
7 changed files with 43 additions and 32 deletions

14
libp2p/kad_dht/common.py Normal file
View File

@ -0,0 +1,14 @@
"""
Shared constants and protocol parameters for the Kademlia DHT.
"""
from libp2p.custom_types import (
TProtocol,
)
# Constants for the Kademlia algorithm
ALPHA = 3 # Concurrency parameter
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
QUERY_TIMEOUT = 10
TTL = DEFAULT_TTL = 24 * 60 * 60 # 24 hours in seconds

View File

@ -5,7 +5,9 @@ This module provides a complete Distributed Hash Table (DHT)
implementation based on the Kademlia algorithm and protocol.
"""
from enum import Enum
from enum import (
Enum,
)
import logging
import time
@ -18,9 +20,6 @@ import varint
from libp2p.abc import (
IHost,
)
from libp2p.custom_types import (
TProtocol,
)
from libp2p.network.stream.net_stream import (
INetStream,
)
@ -34,6 +33,11 @@ from libp2p.tools.async_service import (
Service,
)
from .common import (
ALPHA,
PROTOCOL_ID,
QUERY_TIMEOUT,
)
from .pb.kademlia_pb2 import (
Message,
)
@ -53,11 +57,7 @@ from .value_store import (
logger = logging.getLogger("kademlia-example.kad_dht")
# logger = logging.getLogger("libp2p.kademlia")
# Default parameters
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
ROUTING_TABLE_REFRESH_INTERVAL = 1 * 60 # 1 min in seconds for testing
TTL = 24 * 60 * 60 # 24 hours in seconds
ALPHA = 3
QUERY_TIMEOUT = 10 # seconds
ROUTING_TABLE_REFRESH_INTERVAL = 60 # 1 min in seconds for testing
class DHTMode(Enum):

View File

@ -15,9 +15,6 @@ from libp2p.abc import (
INetStream,
IPeerRouting,
)
from libp2p.custom_types import (
TProtocol,
)
from libp2p.peer.id import (
ID,
)
@ -25,6 +22,10 @@ from libp2p.peer.peerinfo import (
PeerInfo,
)
from .common import (
ALPHA,
PROTOCOL_ID,
)
from .pb.kademlia_pb2 import (
Message,
)
@ -38,10 +39,7 @@ from .utils import (
# logger = logging.getLogger("libp2p.kademlia.peer_routing")
logger = logging.getLogger("kademlia-example.peer_routing")
# Constants for the Kademlia algorithm
ALPHA = 3 # Concurrency parameter
MAX_PEER_LOOKUP_ROUNDS = 20 # Maximum number of rounds in peer lookup
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
class PeerRouting(IPeerRouting):
@ -62,7 +60,6 @@ class PeerRouting(IPeerRouting):
"""
self.host = host
self.routing_table = routing_table
self.protocol_id = PROTOCOL_ID
async def find_peer(self, peer_id: ID) -> PeerInfo | None:
"""
@ -247,7 +244,7 @@ class PeerRouting(IPeerRouting):
# Open a stream to the peer using the Kademlia protocol
logger.debug(f"Opening stream to {peer} for closest peers query")
try:
stream = await self.host.new_stream(peer, [self.protocol_id])
stream = await self.host.new_stream(peer, [PROTOCOL_ID])
logger.debug(f"Stream opened to {peer}")
except Exception as e:
logger.warning(f"Failed to open stream to {peer}: {e}")

View File

@ -29,6 +29,11 @@ from libp2p.peer.peerinfo import (
PeerInfo,
)
from .common import (
ALPHA,
PROTOCOL_ID,
QUERY_TIMEOUT,
)
from .pb.kademlia_pb2 import (
Message,
)
@ -40,9 +45,6 @@ logger = logging.getLogger("kademlia-example.provider_store")
PROVIDER_RECORD_REPUBLISH_INTERVAL = 22 * 60 * 60 # 22 hours in seconds
PROVIDER_RECORD_EXPIRATION_INTERVAL = 48 * 60 * 60 # 48 hours in seconds
PROVIDER_ADDRESS_TTL = 30 * 60 # 30 minutes in seconds
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
ALPHA = 3 # Number of parallel queries/advertisements
QUERY_TIMEOUT = 10 # Timeout for each query in seconds
class ProviderRecord:

View File

@ -13,10 +13,9 @@ import trio
from libp2p.abc import (
IHost,
)
from libp2p.custom_types import (
TProtocol,
from libp2p.kad_dht.utils import (
xor_distance,
)
from libp2p.kad_dht.utils import xor_distance
from libp2p.peer.id import (
ID,
)
@ -24,6 +23,9 @@ from libp2p.peer.peerinfo import (
PeerInfo,
)
from .common import (
PROTOCOL_ID,
)
from .pb.kademlia_pb2 import (
Message,
)
@ -242,12 +244,9 @@ class KBucket:
if not peer_info:
raise ValueError(f"Peer {peer_id} not in bucket")
# Default protocol ID for Kademlia DHT
protocol_id = TProtocol("/ipfs/kad/1.0.0")
try:
# Open a stream to the peer with the DHT protocol
stream = await self.host.new_stream(peer_id, [protocol_id])
stream = await self.host.new_stream(peer_id, [PROTOCOL_ID])
try:
# Create ping protobuf message

View File

@ -19,6 +19,10 @@ from libp2p.peer.id import (
ID,
)
from .common import (
DEFAULT_TTL,
PROTOCOL_ID,
)
from .pb.kademlia_pb2 import (
Message,
)
@ -26,10 +30,6 @@ from .pb.kademlia_pb2 import (
# logger = logging.getLogger("libp2p.kademlia.value_store")
logger = logging.getLogger("kademlia-example.value_store")
# Default time to live for values in seconds (24 hours)
DEFAULT_TTL = 24 * 60 * 60
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
class ValueStore:
"""

View File

@ -89,7 +89,6 @@ class TestPeerRouting:
assert peer_routing.host == mock_host
assert peer_routing.routing_table == mock_routing_table
assert peer_routing.protocol_id == PROTOCOL_ID
@pytest.mark.trio
async def test_find_peer_local_host(self, peer_routing, mock_host):