Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fd46f65
Replace async service with aniyo service
parth-soni07 Oct 2, 2025
5318b10
Modularized the code to match the async service pattern
parth-soni07 Oct 3, 2025
12b2239
Minor changes to fix CI/CD
parth-soni07 Oct 3, 2025
30f3b01
Minor changes to conf.py
parth-soni07 Oct 3, 2025
353f006
revert conf.py to prev
parth-soni07 Oct 3, 2025
7f814db
Removed async service
parth-soni07 Oct 4, 2025
12be84c
Fixed the docs based CI/CD errors
parth-soni07 Oct 4, 2025
0ba27c8
Fixed the docs based CI/CD errors
parth-soni07 Oct 4, 2025
218fed8
Merge branch 'main' into replace-async-with-anyio
seetadev Oct 6, 2025
e2b76c9
Merge branch 'main' into replace-async-with-anyio
seetadev Oct 10, 2025
ffee747
Fixed the imports to now use anyio instead of trio
parth-soni07 Oct 20, 2025
bd06a69
minor version changes
parth-soni07 Oct 20, 2025
08f0777
Skip obsolete dependency
parth-soni07 Oct 21, 2025
c2d0442
Updated the p2pclient version
parth-soni07 Oct 22, 2025
fa8aa7e
Added constraints file for anyio version issue
parth-soni07 Oct 22, 2025
44bd3bb
Updated the tox.in for tox tests
parth-soni07 Oct 22, 2025
e68f5fd
Updated readthedocs
parth-soni07 Oct 22, 2025
353ceb9
Updated readthedocs
parth-soni07 Oct 22, 2025
2202c27
Reverted to prev
parth-soni07 Oct 22, 2025
5fec215
Merge branch 'main' into replace-async-with-anyio
yashksaini-coder Nov 15, 2025
0530e69
fix pyprojec.toml
yashksaini-coder Nov 15, 2025
ea38e1e
Update to latest main branch commit
yashksaini-coder Nov 15, 2025
6945ea5
lint error fix
yashksaini-coder Nov 15, 2025
6cb27bd
chore: Replace async to anyio
yashksaini-coder Nov 15, 2025
ecb24ed
refactor: Update hashing implementation across DHT and peer ID modules
yashksaini-coder Nov 16, 2025
479efca
Merge pull request #1 from parth-soni07/fix/replace-async-with-anyio
parth-soni07 Nov 16, 2025
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
1 change: 1 addition & 0 deletions constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
anyio>=4.11.0
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
"tests.factories",
# Mocked ONLY for Sphinx/autodoc: this module does not exist in the codebase
# but some doc tools may try to import it. No real code references this import.
"libp2p.relay.circuit_v2.lib"
"libp2p.relay.circuit_v2.lib",
]

# Documents to append as an appendix to all manuals.
Expand Down
69 changes: 69 additions & 0 deletions docs/libp2p.tools.anyio_service.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
libp2p.tools.anyio\_service package
====================================

Submodules
----------

libp2p.tools.anyio\_service.api module
---------------------------------------

.. automodule:: libp2p.tools.anyio_service.api
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.context module
-------------------------------------------

.. automodule:: libp2p.tools.anyio_service.context
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.exceptions module
----------------------------------------------

.. automodule:: libp2p.tools.anyio_service.exceptions
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.manager module
-------------------------------------------

.. automodule:: libp2p.tools.anyio_service.manager
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.stats module
-----------------------------------------

.. automodule:: libp2p.tools.anyio_service.stats
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.tasks module
-----------------------------------------

.. automodule:: libp2p.tools.anyio_service.tasks
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio\_service.utils module
-----------------------------------------

.. automodule:: libp2p.tools.anyio_service.utils
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: libp2p.tools.anyio_service
:members:
:undoc-members:
:show-inheritance:
61 changes: 0 additions & 61 deletions docs/libp2p.tools.async_service.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/libp2p.tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Subpackages
.. toctree::
:maxdepth: 4

libp2p.tools.async_service
libp2p.tools.anyio_service
libp2p.tools.timed_cache

Submodules
Expand Down
2 changes: 1 addition & 1 deletion examples/circuit_relay/relay_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)
from libp2p.relay.circuit_v2.resources import RelayLimits
from libp2p.relay.circuit_v2.transport import CircuitV2Transport
from libp2p.tools.async_service import background_trio_service
from libp2p.tools.anyio_service import background_trio_service
from libp2p.utils.logging import setup_logging as libp2p_setup_logging

# Configure logging (default console for this example)
Expand Down
2 changes: 1 addition & 1 deletion examples/kademlia/kademlia.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from libp2p.kad_dht.utils import (
create_key_from_binary,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
background_trio_service,
)
from libp2p.tools.utils import (
Expand Down
2 changes: 1 addition & 1 deletion examples/pubsub/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
MPLEX_PROTOCOL_ID,
Mplex,
)
from libp2p.tools.async_service.trio_service import (
from libp2p.tools.anyio_service import (
background_trio_service,
)
from libp2p.utils.address_validation import (
Expand Down
2 changes: 1 addition & 1 deletion examples/random_walk/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from libp2p.abc import IHost
from libp2p.crypto.secp256k1 import create_new_key_pair
from libp2p.kad_dht.kad_dht import DHTMode, KadDHT
from libp2p.tools.async_service import background_trio_service
from libp2p.tools.anyio_service import background_trio_service


# Simple logging configuration
Expand Down
4 changes: 1 addition & 3 deletions libp2p/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@
from libp2p.pubsub.pb import (
rpc_pb2,
)
from libp2p.tools.async_service import (
ServiceAPI,
)
from libp2p.tools.anyio_service.api import ServiceAPI

# -------------------------- raw_connection interface.py --------------------------

Expand Down
2 changes: 1 addition & 1 deletion libp2p/host/basic_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
MultiselectCommunicator,
)
from libp2p.rcmgr import ResourceManager
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
background_trio_service,
)

Expand Down
2 changes: 1 addition & 1 deletion libp2p/kad_dht/kad_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from libp2p.peer.peerstore import env_to_send_in_RPC
from libp2p.records.pubkey import PublicKeyValidator
from libp2p.records.validator import NamespacedValidator, Validator
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)

Expand Down
5 changes: 4 additions & 1 deletion libp2p/kad_dht/routing_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import (
OrderedDict,
)
import hashlib
import logging
import time

Expand Down Expand Up @@ -47,7 +48,9 @@ def peer_id_to_key(peer_id: ID) -> bytes:
:param peer_id: The peer ID to convert
:return: 32-byte (256-bit) key for routing table operations
"""
return multihash.digest(peer_id.to_bytes(), "sha2-256").digest
digest = hashlib.sha256(peer_id.to_bytes()).digest()
mh_bytes = multihash.encode(digest, "sha2-256")
return multihash.decode(mh_bytes).digest


def key_to_int(key: bytes) -> int:
Expand Down
12 changes: 10 additions & 2 deletions libp2p/kad_dht/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Utility functions for Kademlia DHT implementation.
"""

import hashlib
import logging

import base58
Expand Down Expand Up @@ -101,7 +102,12 @@ def create_key_from_binary(binary_data: bytes) -> bytes:
bytes: The resulting key.

"""
return multihash.digest(binary_data, "sha2-256").digest
# Hash the data first, then encode as multihash
digest = hashlib.sha256(binary_data).digest()
mh_bytes = multihash.encode(digest, "sha2-256")
# Decode to get the digest part
mh = multihash.decode(mh_bytes)
return mh.digest


def xor_distance(key1: bytes, key2: bytes) -> int:
Expand Down Expand Up @@ -157,7 +163,9 @@ def sort_peer_ids_by_distance(target_key: bytes, peer_ids: list[ID]) -> list[ID]

def get_distance(peer_id: ID) -> int:
# Hash the peer ID bytes to get a key for distance calculation
peer_hash = multihash.digest(peer_id.to_bytes(), "sha2-256").digest
digest = hashlib.sha256(peer_id.to_bytes()).digest()
mh_bytes = multihash.encode(digest, "sha2-256")
peer_hash = multihash.decode(mh_bytes).digest
return xor_distance(target_key, peer_hash)

return sorted(peer_ids, key=get_distance)
Expand Down
2 changes: 1 addition & 1 deletion libp2p/network/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
PeerStoreError,
)
from libp2p.rcmgr.manager import ResourceManager
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.transport.exceptions import (
Expand Down
19 changes: 10 additions & 9 deletions libp2p/peer/id.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ def update(self, input: bytes) -> None:
def digest(self) -> bytes:
return self._digest

# Register identity hash function if FuncReg is available
if hasattr(multihash, "FuncReg"):
multihash.FuncReg.register(
IDENTITY_MULTIHASH_CODE, "identity", hash_new=lambda: IdentityHash()
)
# Note: FuncReg is not available in pymultihash 0.8.2
# Identity hash is handled manually in from_pubkey method


class ID:
Expand Down Expand Up @@ -84,11 +81,15 @@ def from_base58(cls, b58_encoded_peer_id_str: str) -> "ID":
@classmethod
def from_pubkey(cls, key: PublicKey) -> "ID":
serialized_key = key.serialize()
algo = multihash.Func.sha2_256
# Use identity hash (no hashing) for small keys, otherwise use SHA2-256
if ENABLE_INLINING and len(serialized_key) <= MAX_INLINE_KEY_LENGTH:
algo = IDENTITY_MULTIHASH_CODE
mh_digest = multihash.digest(serialized_key, algo)
return cls(mh_digest.encode())
# Identity multihash: just encode the key directly with code 0x00
mh_bytes = multihash.encode(serialized_key, IDENTITY_MULTIHASH_CODE)
else:
# SHA2-256: hash first, then encode
digest = hashlib.sha256(serialized_key).digest()
mh_bytes = multihash.encode(digest, "sha2-256")
return cls(mh_bytes)


def sha256_digest(data: str | bytes) -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion libp2p/pubsub/gossipsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
floodsub,
)
from libp2p.pubsub.utils import maybe_consume_signed_record
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)

Expand Down
2 changes: 1 addition & 1 deletion libp2p/pubsub/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
)
from libp2p.peer.peerstore import env_to_send_in_RPC
from libp2p.pubsub.utils import maybe_consume_signed_record
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.tools.timed_cache.last_seen_cache import (
Expand Down
2 changes: 1 addition & 1 deletion libp2p/relay/circuit_v2/dcutr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from libp2p.relay.circuit_v2.pb.dcutr_pb2 import (
HolePunch,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)

Expand Down
2 changes: 1 addition & 1 deletion libp2p/relay/circuit_v2/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ID,
)
from libp2p.peer.peerstore import env_to_send_in_RPC
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)

Expand Down
2 changes: 1 addition & 1 deletion libp2p/relay/circuit_v2/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
MplexStreamEOF,
MplexStreamReset,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.tools.constants import (
Expand Down
2 changes: 1 addition & 1 deletion libp2p/relay/circuit_v2/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
PeerInfo,
)
from libp2p.peer.peerstore import env_to_send_in_RPC
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)

Expand Down
5 changes: 3 additions & 2 deletions libp2p/security/secio/transport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import (
dataclass,
)
import hashlib
import itertools

import multihash
Expand Down Expand Up @@ -208,8 +209,8 @@ async def _response_to_msg(read_writer: SecioPacketReadWriter, msg: bytes) -> by


def _mk_multihash_sha256(data: bytes) -> bytes:
mh = multihash.digest(data, "sha2-256")
return mh.encode()
digest = hashlib.sha256(data).digest()
return multihash.encode(digest, "sha2-256")


def _mk_score(public_key: PublicKey, nonce: bytes) -> bytes:
Expand Down
Loading
Loading