mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
fix: regenerate protobuf files to match runtime version
This commit is contained in:
committed by
Paul Robinson
parent
1730999e38
commit
86dce72ae0
@ -1,5 +1,8 @@
|
||||
"""AutoNAT module for libp2p."""
|
||||
|
||||
from .autonat import (
|
||||
AutoNATService,
|
||||
AutoNATStatus,
|
||||
)
|
||||
|
||||
__all__ = ["AutoNATService"]
|
||||
__all__ = ["AutoNATService", "AutoNATStatus"]
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import logging
|
||||
from typing import (
|
||||
Union,
|
||||
)
|
||||
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
@ -30,12 +33,12 @@ logger = logging.getLogger("libp2p.host.autonat")
|
||||
|
||||
class AutoNATStatus:
|
||||
"""
|
||||
Status enumeration for the AutoNAT service.
|
||||
AutoNAT Status Enumeration.
|
||||
|
||||
This class defines the possible states of NAT traversal for a node:
|
||||
- UNKNOWN (0): The node's NAT status has not been determined yet
|
||||
- PUBLIC (1): The node is publicly reachable
|
||||
- PRIVATE (2): The node is behind NAT and not directly reachable
|
||||
Defines the possible states of NAT traversal for a libp2p node:
|
||||
- UNKNOWN (0): Initial state, NAT status not yet determined
|
||||
- PUBLIC (1): Node is publicly reachable from the internet
|
||||
- PRIVATE (2): Node is behind NAT, not directly reachable
|
||||
"""
|
||||
|
||||
UNKNOWN = 0
|
||||
@ -45,23 +48,24 @@ class AutoNATStatus:
|
||||
|
||||
class AutoNATService:
|
||||
"""
|
||||
Service for determining if a node is publicly reachable.
|
||||
AutoNAT Service Implementation.
|
||||
|
||||
The AutoNAT service helps nodes determine their NAT status by attempting
|
||||
to establish connections with other peers. It maintains a record of dial
|
||||
attempts and their results to classify the node as public or private.
|
||||
A service that helps libp2p nodes determine their NAT status by
|
||||
attempting to establish connections with other peers. The service
|
||||
maintains a record of dial attempts and their results to classify
|
||||
the node as either public or private.
|
||||
"""
|
||||
|
||||
def __init__(self, host: BasicHost) -> None:
|
||||
"""
|
||||
Initialize the AutoNAT service.
|
||||
|
||||
Args:
|
||||
----
|
||||
host (BasicHost): The libp2p host instance that provides networking
|
||||
capabilities for the AutoNAT service, including peer discovery
|
||||
and connection management.
|
||||
Create a new AutoNAT service instance.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
host : BasicHost
|
||||
The libp2p host instance that provides networking capabilities
|
||||
for the AutoNAT service, including peer discovery and connection
|
||||
management.
|
||||
"""
|
||||
self.host = host
|
||||
self.peerstore: IPeerStore = host.get_peerstore()
|
||||
@ -70,9 +74,12 @@ class AutoNATService:
|
||||
|
||||
async def handle_stream(self, stream: NetStream) -> None:
|
||||
"""
|
||||
Handle an incoming stream.
|
||||
Process an incoming AutoNAT stream.
|
||||
|
||||
:param stream: The stream to handle
|
||||
Parameters
|
||||
----------
|
||||
stream : NetStream
|
||||
The network stream to handle for AutoNAT protocol communication.
|
||||
"""
|
||||
try:
|
||||
request_bytes = await stream.read()
|
||||
@ -85,23 +92,22 @@ class AutoNATService:
|
||||
finally:
|
||||
await stream.close()
|
||||
|
||||
async def _handle_request(self, request: bytes | Message) -> Message:
|
||||
async def _handle_request(self, request: Union[bytes, Message]) -> Message:
|
||||
"""
|
||||
Handle an AutoNAT request.
|
||||
Process an AutoNAT protocol request.
|
||||
|
||||
Parses and processes incoming AutoNAT requests, routing them to
|
||||
appropriate handlers based on the message type.
|
||||
|
||||
Args:
|
||||
----
|
||||
request (bytes | Message): The request bytes that need to be parsed
|
||||
and handled by the AutoNAT service, or a Message object directly.
|
||||
Parameters
|
||||
----------
|
||||
request : Union[bytes, Message]
|
||||
The request data to be processed, either as raw bytes or a
|
||||
pre-parsed Message object.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Message: The response message containing the result of the request.
|
||||
Returns an error response if the request type is not recognized.
|
||||
|
||||
Message
|
||||
The response message containing the result of processing the
|
||||
request. Returns an error response if the request type is not
|
||||
recognized.
|
||||
"""
|
||||
if isinstance(request, bytes):
|
||||
message = Message()
|
||||
@ -123,21 +129,19 @@ class AutoNATService:
|
||||
|
||||
async def _handle_dial(self, message: Message) -> Message:
|
||||
"""
|
||||
Handle a DIAL request.
|
||||
Process an AutoNAT dial request.
|
||||
|
||||
Processes dial requests by attempting to connect to specified peers
|
||||
and recording the results of these connection attempts.
|
||||
|
||||
Args:
|
||||
----
|
||||
message (Message): The request message containing the dial request
|
||||
parameters and peer information.
|
||||
Parameters
|
||||
----------
|
||||
message : Message
|
||||
The dial request message containing peer information to test
|
||||
connectivity.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Message: The response message containing the dial results, including
|
||||
success/failure status for each attempted peer connection.
|
||||
|
||||
Message
|
||||
The response message containing the results of the dial
|
||||
attempts, including success/failure status for each peer.
|
||||
"""
|
||||
response = Message()
|
||||
response.type = Type.Value("DIAL_RESPONSE")
|
||||
@ -166,21 +170,18 @@ class AutoNATService:
|
||||
|
||||
async def _try_dial(self, peer_id: ID) -> bool:
|
||||
"""
|
||||
Try to dial a peer.
|
||||
Attempt to establish a connection with a peer.
|
||||
|
||||
Attempts to establish a connection with a specified peer to test
|
||||
NAT traversal capabilities.
|
||||
|
||||
Args:
|
||||
----
|
||||
peer_id (ID): The identifier of the peer to attempt to dial for
|
||||
NAT traversal testing.
|
||||
Parameters
|
||||
----------
|
||||
peer_id : ID
|
||||
The identifier of the peer to attempt to dial.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool: True if the dial was successful and a connection could be
|
||||
established, False if the connection attempt failed.
|
||||
|
||||
bool
|
||||
True if the connection was successfully established,
|
||||
False if the connection attempt failed.
|
||||
"""
|
||||
try:
|
||||
stream = await self.host.new_stream(peer_id, [AUTONAT_PROTOCOL_ID])
|
||||
@ -191,18 +192,15 @@ class AutoNATService:
|
||||
|
||||
def get_status(self) -> int:
|
||||
"""
|
||||
Get the current AutoNAT status.
|
||||
|
||||
Retrieves the current NAT status of the node based on previous
|
||||
dial attempts and their results.
|
||||
Retrieve the current AutoNAT status.
|
||||
|
||||
Returns
|
||||
-------
|
||||
int: The current status as an integer:
|
||||
int
|
||||
The current NAT status:
|
||||
- AutoNATStatus.UNKNOWN (0): Status not yet determined
|
||||
- AutoNATStatus.PUBLIC (1): Node is publicly reachable
|
||||
- AutoNATStatus.PRIVATE (2): Node is behind NAT
|
||||
|
||||
"""
|
||||
return self.status
|
||||
|
||||
@ -210,7 +208,7 @@ class AutoNATService:
|
||||
"""
|
||||
Update the AutoNAT status based on dial results.
|
||||
|
||||
Analyzes the results of previous dial attempts to determine if the
|
||||
Analyzes the accumulated dial attempt results to determine if the
|
||||
node is publicly reachable. The node is considered public if at
|
||||
least two successful dial attempts have been recorded.
|
||||
"""
|
||||
|
||||
@ -1,22 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# NO CHECKED-IN PROTOBUF GENCODE
|
||||
# source: libp2p/host/autonat/pb/autonat.proto
|
||||
# Protobuf Python Version: 5.29.0
|
||||
"""Generated protocol buffer code."""
|
||||
from google.protobuf.internal import builder as _builder
|
||||
from google.protobuf import descriptor as _descriptor
|
||||
from google.protobuf import descriptor_pool as _descriptor_pool
|
||||
from google.protobuf import runtime_version as _runtime_version
|
||||
from google.protobuf import symbol_database as _symbol_database
|
||||
from google.protobuf.internal import builder as _builder
|
||||
_runtime_version.ValidateProtobufRuntimeVersion(
|
||||
_runtime_version.Domain.PUBLIC,
|
||||
5,
|
||||
29,
|
||||
0,
|
||||
'',
|
||||
'libp2p/host/autonat/pb/autonat.proto'
|
||||
)
|
||||
# @@protoc_insertion_point(imports)
|
||||
|
||||
_sym_db = _symbol_database.Default()
|
||||
@ -26,23 +15,23 @@ _sym_db = _symbol_database.Default()
|
||||
|
||||
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n$libp2p/host/autonat/pb/autonat.proto\x12\nautonat.pb\"\x81\x01\n\x07Message\x12\x1e\n\x04type\x18\x01 \x01(\x0e\x32\x10.autonat.pb.Type\x12%\n\x04\x64ial\x18\x02 \x01(\x0b\x32\x17.autonat.pb.DialRequest\x12/\n\rdial_response\x18\x03 \x01(\x0b\x32\x18.autonat.pb.DialResponse\"2\n\x0b\x44ialRequest\x12#\n\x05peers\x18\x01 \x03(\x0b\x32\x14.autonat.pb.PeerInfo\"W\n\x0c\x44ialResponse\x12\"\n\x06status\x18\x01 \x01(\x0e\x32\x12.autonat.pb.Status\x12#\n\x05peers\x18\x02 \x03(\x0b\x32\x14.autonat.pb.PeerInfo\"6\n\x08PeerInfo\x12\n\n\x02id\x18\x01 \x01(\x0c\x12\r\n\x05\x61\x64\x64rs\x18\x02 \x03(\x0c\x12\x0f\n\x07success\x18\x03 \x01(\x08*0\n\x04Type\x12\x0b\n\x07UNKNOWN\x10\x00\x12\x08\n\x04\x44IAL\x10\x01\x12\x11\n\rDIAL_RESPONSE\x10\x02*_\n\x06Status\x12\x06\n\x02OK\x10\x00\x12\x10\n\x0c\x45_DIAL_ERROR\x10\x01\x12\x12\n\x0e\x45_DIAL_REFUSED\x10\x02\x12\x11\n\rE_DIAL_FAILED\x10\x03\x12\x14\n\x10\x45_INTERNAL_ERROR\x10\x64\x32=\n\x07\x41utoNAT\x12\x32\n\x04\x44ial\x12\x13.autonat.pb.Message\x1a\x13.autonat.pb.Message\"\x00\x62\x06proto3')
|
||||
|
||||
_globals = globals()
|
||||
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
|
||||
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'libp2p.host.autonat.pb.autonat_pb2', _globals)
|
||||
if not _descriptor._USE_C_DESCRIPTORS:
|
||||
DESCRIPTOR._loaded_options = None
|
||||
_globals['_TYPE']._serialized_start=381
|
||||
_globals['_TYPE']._serialized_end=429
|
||||
_globals['_STATUS']._serialized_start=431
|
||||
_globals['_STATUS']._serialized_end=526
|
||||
_globals['_MESSAGE']._serialized_start=53
|
||||
_globals['_MESSAGE']._serialized_end=182
|
||||
_globals['_DIALREQUEST']._serialized_start=184
|
||||
_globals['_DIALREQUEST']._serialized_end=234
|
||||
_globals['_DIALRESPONSE']._serialized_start=236
|
||||
_globals['_DIALRESPONSE']._serialized_end=323
|
||||
_globals['_PEERINFO']._serialized_start=325
|
||||
_globals['_PEERINFO']._serialized_end=379
|
||||
_globals['_AUTONAT']._serialized_start=528
|
||||
_globals['_AUTONAT']._serialized_end=589
|
||||
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals())
|
||||
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'libp2p.host.autonat.pb.autonat_pb2', globals())
|
||||
if _descriptor._USE_C_DESCRIPTORS == False:
|
||||
|
||||
DESCRIPTOR._options = None
|
||||
_TYPE._serialized_start=381
|
||||
_TYPE._serialized_end=429
|
||||
_STATUS._serialized_start=431
|
||||
_STATUS._serialized_end=526
|
||||
_MESSAGE._serialized_start=53
|
||||
_MESSAGE._serialized_end=182
|
||||
_DIALREQUEST._serialized_start=184
|
||||
_DIALREQUEST._serialized_end=234
|
||||
_DIALRESPONSE._serialized_start=236
|
||||
_DIALRESPONSE._serialized_end=323
|
||||
_PEERINFO._serialized_start=325
|
||||
_PEERINFO._serialized_end=379
|
||||
_AUTONAT._serialized_start=528
|
||||
_AUTONAT._serialized_end=589
|
||||
# @@protoc_insertion_point(module_scope)
|
||||
|
||||
@ -1,56 +1,149 @@
|
||||
from typing import Any, List, Optional, Union
|
||||
"""
|
||||
@generated by mypy-protobuf. Do not edit manually!
|
||||
isort:skip_file
|
||||
"""
|
||||
|
||||
class Message:
|
||||
type: int
|
||||
dial: Any
|
||||
dial_response: Any
|
||||
|
||||
def ParseFromString(self, data: bytes) -> None: ...
|
||||
def SerializeToString(self) -> bytes: ...
|
||||
@staticmethod
|
||||
def FromString(data: bytes) -> 'Message': ...
|
||||
import builtins
|
||||
import collections.abc
|
||||
import google.protobuf.descriptor
|
||||
import google.protobuf.internal.containers
|
||||
import google.protobuf.internal.enum_type_wrapper
|
||||
import google.protobuf.message
|
||||
import sys
|
||||
import typing
|
||||
|
||||
class DialRequest:
|
||||
peers: List[Any]
|
||||
|
||||
def ParseFromString(self, data: bytes) -> None: ...
|
||||
def SerializeToString(self) -> bytes: ...
|
||||
@staticmethod
|
||||
def FromString(data: bytes) -> 'DialRequest': ...
|
||||
if sys.version_info >= (3, 10):
|
||||
import typing as typing_extensions
|
||||
else:
|
||||
import typing_extensions
|
||||
|
||||
class DialResponse:
|
||||
status: int
|
||||
peers: List[Any]
|
||||
|
||||
def ParseFromString(self, data: bytes) -> None: ...
|
||||
def SerializeToString(self) -> bytes: ...
|
||||
@staticmethod
|
||||
def FromString(data: bytes) -> 'DialResponse': ...
|
||||
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor
|
||||
|
||||
class PeerInfo:
|
||||
id: bytes
|
||||
addrs: List[bytes]
|
||||
success: bool
|
||||
|
||||
def ParseFromString(self, data: bytes) -> None: ...
|
||||
def SerializeToString(self) -> bytes: ...
|
||||
@staticmethod
|
||||
def FromString(data: bytes) -> 'PeerInfo': ...
|
||||
class _Type:
|
||||
ValueType = typing.NewType("ValueType", builtins.int)
|
||||
V: typing_extensions.TypeAlias = ValueType
|
||||
|
||||
class Type:
|
||||
UNKNOWN: int
|
||||
DIAL: int
|
||||
DIAL_RESPONSE: int
|
||||
|
||||
@staticmethod
|
||||
def Value(name: str) -> int: ...
|
||||
class _TypeEnumTypeWrapper(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[_Type.ValueType], builtins.type):
|
||||
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor
|
||||
UNKNOWN: _Type.ValueType # 0
|
||||
DIAL: _Type.ValueType # 1
|
||||
DIAL_RESPONSE: _Type.ValueType # 2
|
||||
|
||||
class Status:
|
||||
OK: int
|
||||
E_DIAL_ERROR: int
|
||||
E_DIAL_REFUSED: int
|
||||
E_DIAL_FAILED: int
|
||||
E_INTERNAL_ERROR: int
|
||||
|
||||
@staticmethod
|
||||
def Value(name: str) -> int: ...
|
||||
class Type(_Type, metaclass=_TypeEnumTypeWrapper):
|
||||
"""Message types"""
|
||||
|
||||
UNKNOWN: Type.ValueType # 0
|
||||
DIAL: Type.ValueType # 1
|
||||
DIAL_RESPONSE: Type.ValueType # 2
|
||||
global___Type = Type
|
||||
|
||||
class _Status:
|
||||
ValueType = typing.NewType("ValueType", builtins.int)
|
||||
V: typing_extensions.TypeAlias = ValueType
|
||||
|
||||
class _StatusEnumTypeWrapper(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[_Status.ValueType], builtins.type):
|
||||
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor
|
||||
OK: _Status.ValueType # 0
|
||||
E_DIAL_ERROR: _Status.ValueType # 1
|
||||
E_DIAL_REFUSED: _Status.ValueType # 2
|
||||
E_DIAL_FAILED: _Status.ValueType # 3
|
||||
E_INTERNAL_ERROR: _Status.ValueType # 100
|
||||
|
||||
class Status(_Status, metaclass=_StatusEnumTypeWrapper):
|
||||
"""Status codes"""
|
||||
|
||||
OK: Status.ValueType # 0
|
||||
E_DIAL_ERROR: Status.ValueType # 1
|
||||
E_DIAL_REFUSED: Status.ValueType # 2
|
||||
E_DIAL_FAILED: Status.ValueType # 3
|
||||
E_INTERNAL_ERROR: Status.ValueType # 100
|
||||
global___Status = Status
|
||||
|
||||
@typing.final
|
||||
class Message(google.protobuf.message.Message):
|
||||
"""Main message"""
|
||||
|
||||
DESCRIPTOR: google.protobuf.descriptor.Descriptor
|
||||
|
||||
TYPE_FIELD_NUMBER: builtins.int
|
||||
DIAL_FIELD_NUMBER: builtins.int
|
||||
DIAL_RESPONSE_FIELD_NUMBER: builtins.int
|
||||
type: global___Type.ValueType
|
||||
@property
|
||||
def dial(self) -> global___DialRequest: ...
|
||||
@property
|
||||
def dial_response(self) -> global___DialResponse: ...
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
type: global___Type.ValueType = ...,
|
||||
dial: global___DialRequest | None = ...,
|
||||
dial_response: global___DialResponse | None = ...,
|
||||
) -> None: ...
|
||||
def HasField(self, field_name: typing.Literal["dial", b"dial", "dial_response", b"dial_response"]) -> builtins.bool: ...
|
||||
def ClearField(self, field_name: typing.Literal["dial", b"dial", "dial_response", b"dial_response", "type", b"type"]) -> None: ...
|
||||
|
||||
global___Message = Message
|
||||
|
||||
@typing.final
|
||||
class DialRequest(google.protobuf.message.Message):
|
||||
"""Dial request"""
|
||||
|
||||
DESCRIPTOR: google.protobuf.descriptor.Descriptor
|
||||
|
||||
PEERS_FIELD_NUMBER: builtins.int
|
||||
@property
|
||||
def peers(self) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[global___PeerInfo]: ...
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
peers: collections.abc.Iterable[global___PeerInfo] | None = ...,
|
||||
) -> None: ...
|
||||
def ClearField(self, field_name: typing.Literal["peers", b"peers"]) -> None: ...
|
||||
|
||||
global___DialRequest = DialRequest
|
||||
|
||||
@typing.final
|
||||
class DialResponse(google.protobuf.message.Message):
|
||||
"""Dial response"""
|
||||
|
||||
DESCRIPTOR: google.protobuf.descriptor.Descriptor
|
||||
|
||||
STATUS_FIELD_NUMBER: builtins.int
|
||||
PEERS_FIELD_NUMBER: builtins.int
|
||||
status: global___Status.ValueType
|
||||
@property
|
||||
def peers(self) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[global___PeerInfo]: ...
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
status: global___Status.ValueType = ...,
|
||||
peers: collections.abc.Iterable[global___PeerInfo] | None = ...,
|
||||
) -> None: ...
|
||||
def ClearField(self, field_name: typing.Literal["peers", b"peers", "status", b"status"]) -> None: ...
|
||||
|
||||
global___DialResponse = DialResponse
|
||||
|
||||
@typing.final
|
||||
class PeerInfo(google.protobuf.message.Message):
|
||||
"""Peer information"""
|
||||
|
||||
DESCRIPTOR: google.protobuf.descriptor.Descriptor
|
||||
|
||||
ID_FIELD_NUMBER: builtins.int
|
||||
ADDRS_FIELD_NUMBER: builtins.int
|
||||
SUCCESS_FIELD_NUMBER: builtins.int
|
||||
id: builtins.bytes
|
||||
success: builtins.bool
|
||||
@property
|
||||
def addrs(self) -> google.protobuf.internal.containers.RepeatedScalarFieldContainer[builtins.bytes]: ...
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
id: builtins.bytes = ...,
|
||||
addrs: collections.abc.Iterable[builtins.bytes] | None = ...,
|
||||
success: builtins.bool = ...,
|
||||
) -> None: ...
|
||||
def ClearField(self, field_name: typing.Literal["addrs", b"addrs", "id", b"id", "success", b"success"]) -> None: ...
|
||||
|
||||
global___PeerInfo = PeerInfo
|
||||
|
||||
Reference in New Issue
Block a user