Skip to content

Commit dfc0bb4

Browse files
committed
chore(kad_dht): centralize shared values in common.py
1 parent 09b4c84 commit dfc0bb4

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

libp2p/kad_dht/common.py

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

libp2p/kad_dht/kad_dht.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949
from .value_store import (
5050
ValueStore,
5151
)
52+
from .common import (
53+
PROTOCOL_ID,
54+
ALPHA,
55+
QUERY_TIMEOUT
56+
)
5257

5358
logger = logging.getLogger("kademlia-example.kad_dht")
5459
# logger = logging.getLogger("libp2p.kademlia")
5560
# 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
61-
61+
ROUTING_TABLE_REFRESH_INTERVAL = 60 # 1 min in seconds for testing
6262

6363
class DHTMode(Enum):
6464
"""DHT operation modes."""

libp2p/kad_dht/peer_routing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@
2525
PeerInfo,
2626
)
2727

28+
2829
from .pb.kademlia_pb2 import (
2930
Message,
3031
)
3132
from .routing_table import (
3233
RoutingTable,
3334
)
35+
from .common import (
36+
PROTOCOL_ID,
37+
ALPHA
38+
)
3439
from .utils import (
3540
sort_peer_ids_by_distance,
3641
)
3742

3843
# logger = logging.getLogger("libp2p.kademlia.peer_routing")
3944
logger = logging.getLogger("kademlia-example.peer_routing")
4045

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

4648

4749
class PeerRouting(IPeerRouting):

libp2p/kad_dht/provider_store.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@
3333
Message,
3434
)
3535

36+
from .common import (
37+
PROTOCOL_ID,
38+
ALPHA,
39+
QUERY_TIMEOUT
40+
)
41+
3642
# logger = logging.getLogger("libp2p.kademlia.provider_store")
3743
logger = logging.getLogger("kademlia-example.provider_store")
3844

3945
# Constants for provider records (based on IPFS standards)
4046
PROVIDER_RECORD_REPUBLISH_INTERVAL = 22 * 60 * 60 # 22 hours in seconds
4147
PROVIDER_RECORD_EXPIRATION_INTERVAL = 48 * 60 * 60 # 48 hours in seconds
4248
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
4649

4750

4851
class ProviderRecord:

libp2p/kad_dht/value_store.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
Message,
2424
)
2525

26+
from .common import (
27+
PROTOCOL_ID,
28+
DEFAULT_TTL
29+
)
30+
2631
# logger = logging.getLogger("libp2p.kademlia.value_store")
2732
logger = logging.getLogger("kademlia-example.value_store")
2833

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-
33-
3434
class ValueStore:
3535
"""
3636
Store for key-value pairs in a Kademlia DHT.

0 commit comments

Comments
 (0)