mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
feat: add agent version to identify protocol and improved tests
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
from importlib.metadata import (
|
||||
version,
|
||||
)
|
||||
import itertools
|
||||
import logging
|
||||
import math
|
||||
|
||||
from libp2p.exceptions import (
|
||||
@ -12,6 +16,8 @@ from .io.utils import (
|
||||
read_exactly,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("libp2p.utils")
|
||||
|
||||
# Unsigned LEB128(varint codec)
|
||||
# Reference: https://github.com/ethereum/py-wasm/blob/master/wasm/parsers/leb128.py
|
||||
|
||||
@ -84,3 +90,19 @@ async def read_delim(reader: Reader) -> bytes:
|
||||
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}'
|
||||
)
|
||||
return msg_bytes[:-1]
|
||||
|
||||
|
||||
def get_agent_version() -> str:
|
||||
"""
|
||||
Return the version of libp2p.
|
||||
|
||||
If the version cannot be determined due to an exception, return "py-libp2p/unknown".
|
||||
|
||||
:return: The version of libp2p.
|
||||
:rtype: str
|
||||
"""
|
||||
try:
|
||||
return f"py-libp2p/{version('libp2p')}"
|
||||
except Exception as e:
|
||||
logger.warning("Could not fetch libp2p version: %s", e)
|
||||
return "py-libp2p/unknown"
|
||||
|
||||
Reference in New Issue
Block a user