Skip to content

Commit e50f9fc

Browse files
authored
Merge branch 'libp2p:main' into main
2 parents 724375e + 4e2be87 commit e50f9fc

File tree

7 files changed

+43
-32
lines changed

7 files changed

+43
-32
lines changed

libp2p/kad_dht/common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Shared constants and protocol parameters for the Kademlia DHT.
3+
"""
4+
5+
from libp2p.custom_types import (
6+
TProtocol,
7+
)
8+
9+
# Constants for the Kademlia algorithm
10+
ALPHA = 3 # Concurrency parameter
11+
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
12+
QUERY_TIMEOUT = 10
13+
14+
TTL = DEFAULT_TTL = 24 * 60 * 60 # 24 hours in seconds

libp2p/kad_dht/kad_dht.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
implementation based on the Kademlia algorithm and protocol.
66
"""
77

8-
from enum import Enum
8+
from enum import (
9+
Enum,
10+
)
911
import logging
1012
import time
1113

@@ -18,9 +20,6 @@
1820
from libp2p.abc import (
1921
IHost,
2022
)
21-
from libp2p.custom_types import (
22-
TProtocol,
23-
)
2423
from libp2p.network.stream.net_stream import (
2524
INetStream,
2625
)
@@ -34,6 +33,11 @@
3433
Service,
3534
)
3635

36+
from .common import (
37+
ALPHA,
38+
PROTOCOL_ID,
39+
QUERY_TIMEOUT,
40+
)
3741
from .pb.kademlia_pb2 import (
3842
Message,
3943
)
@@ -53,11 +57,7 @@
5357
logger = logging.getLogger("kademlia-example.kad_dht")
5458
# logger = logging.getLogger("libp2p.kademlia")
5559
# Default parameters
56-
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
57-
ROUTING_TABLE_REFRESH_INTERVAL = 1 * 60 # 1 min in seconds for testing
58-
TTL = 24 * 60 * 60 # 24 hours in seconds
59-
ALPHA = 3
60-
QUERY_TIMEOUT = 10 # seconds
60+
ROUTING_TABLE_REFRESH_INTERVAL = 60 # 1 min in seconds for testing
6161

6262

6363
class DHTMode(Enum):

libp2p/kad_dht/peer_routing.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
INetStream,
1616
IPeerRouting,
1717
)
18-
from libp2p.custom_types import (
19-
TProtocol,
20-
)
2118
from libp2p.peer.id import (
2219
ID,
2320
)
2421
from libp2p.peer.peerinfo import (
2522
PeerInfo,
2623
)
2724

25+
from .common import (
26+
ALPHA,
27+
PROTOCOL_ID,
28+
)
2829
from .pb.kademlia_pb2 import (
2930
Message,
3031
)
@@ -38,10 +39,7 @@
3839
# logger = logging.getLogger("libp2p.kademlia.peer_routing")
3940
logger = logging.getLogger("kademlia-example.peer_routing")
4041

41-
# Constants for the Kademlia algorithm
42-
ALPHA = 3 # Concurrency parameter
4342
MAX_PEER_LOOKUP_ROUNDS = 20 # Maximum number of rounds in peer lookup
44-
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
4543

4644

4745
class PeerRouting(IPeerRouting):
@@ -62,7 +60,6 @@ def __init__(self, host: IHost, routing_table: RoutingTable):
6260
"""
6361
self.host = host
6462
self.routing_table = routing_table
65-
self.protocol_id = PROTOCOL_ID
6663

6764
async def find_peer(self, peer_id: ID) -> PeerInfo | None:
6865
"""
@@ -247,7 +244,7 @@ async def _query_peer_for_closest(self, peer: ID, target_key: bytes) -> list[ID]
247244
# Open a stream to the peer using the Kademlia protocol
248245
logger.debug(f"Opening stream to {peer} for closest peers query")
249246
try:
250-
stream = await self.host.new_stream(peer, [self.protocol_id])
247+
stream = await self.host.new_stream(peer, [PROTOCOL_ID])
251248
logger.debug(f"Stream opened to {peer}")
252249
except Exception as e:
253250
logger.warning(f"Failed to open stream to {peer}: {e}")

libp2p/kad_dht/provider_store.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
PeerInfo,
3030
)
3131

32+
from .common import (
33+
ALPHA,
34+
PROTOCOL_ID,
35+
QUERY_TIMEOUT,
36+
)
3237
from .pb.kademlia_pb2 import (
3338
Message,
3439
)
@@ -40,9 +45,6 @@
4045
PROVIDER_RECORD_REPUBLISH_INTERVAL = 22 * 60 * 60 # 22 hours in seconds
4146
PROVIDER_RECORD_EXPIRATION_INTERVAL = 48 * 60 * 60 # 48 hours in seconds
4247
PROVIDER_ADDRESS_TTL = 30 * 60 # 30 minutes in seconds
43-
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
44-
ALPHA = 3 # Number of parallel queries/advertisements
45-
QUERY_TIMEOUT = 10 # Timeout for each query in seconds
4648

4749

4850
class ProviderRecord:

libp2p/kad_dht/routing_table.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
from libp2p.abc import (
1414
IHost,
1515
)
16-
from libp2p.custom_types import (
17-
TProtocol,
16+
from libp2p.kad_dht.utils import (
17+
xor_distance,
1818
)
19-
from libp2p.kad_dht.utils import xor_distance
2019
from libp2p.peer.id import (
2120
ID,
2221
)
2322
from libp2p.peer.peerinfo import (
2423
PeerInfo,
2524
)
2625

26+
from .common import (
27+
PROTOCOL_ID,
28+
)
2729
from .pb.kademlia_pb2 import (
2830
Message,
2931
)
@@ -242,12 +244,9 @@ async def _ping_peer(self, peer_id: ID) -> bool:
242244
if not peer_info:
243245
raise ValueError(f"Peer {peer_id} not in bucket")
244246

245-
# Default protocol ID for Kademlia DHT
246-
protocol_id = TProtocol("/ipfs/kad/1.0.0")
247-
248247
try:
249248
# Open a stream to the peer with the DHT protocol
250-
stream = await self.host.new_stream(peer_id, [protocol_id])
249+
stream = await self.host.new_stream(peer_id, [PROTOCOL_ID])
251250

252251
try:
253252
# Create ping protobuf message

libp2p/kad_dht/value_store.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
ID,
2020
)
2121

22+
from .common import (
23+
DEFAULT_TTL,
24+
PROTOCOL_ID,
25+
)
2226
from .pb.kademlia_pb2 import (
2327
Message,
2428
)
2529

2630
# logger = logging.getLogger("libp2p.kademlia.value_store")
2731
logger = logging.getLogger("kademlia-example.value_store")
2832

29-
# Default time to live for values in seconds (24 hours)
30-
DEFAULT_TTL = 24 * 60 * 60
31-
PROTOCOL_ID = TProtocol("/ipfs/kad/1.0.0")
32-
3333

3434
class ValueStore:
3535
"""

tests/core/kad_dht/test_unit_peer_routing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def test_init_peer_routing(self, mock_host, mock_routing_table):
8989

9090
assert peer_routing.host == mock_host
9191
assert peer_routing.routing_table == mock_routing_table
92-
assert peer_routing.protocol_id == PROTOCOL_ID
9392

9493
@pytest.mark.trio
9594
async def test_find_peer_local_host(self, peer_routing, mock_host):

0 commit comments

Comments
 (0)