Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 14 additions & 11 deletions libp2p/relay/circuit_v2/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
)
from .pb.circuit_pb2 import (
HopMessage,
Peer,
)
from .pb_framing import (
read_circuit_v2_pb,
write_circuit_v2_pb,
)
from .protocol import (
PROTOCOL_ID,
Expand Down Expand Up @@ -398,17 +403,19 @@ async def make_reservation(self, peer_id: ID) -> bool:
# Prepare signed envelope
envelope_bytes, _ = env_to_send_in_RPC(self.host)
# Create and send reservation request
rpeer = Peer()
rpeer.id = self.host.get_id().to_bytes()
request = HopMessage(
type=HopMessage.RESERVE,
peer=self.host.get_id().to_bytes(),
type=HopMessage.Type.RESERVE,
senderRecord=envelope_bytes,
)
request.peer.CopyFrom(rpeer)

with trio.fail_after(self.stream_timeout):
await stream.write(request.SerializeToString())
await write_circuit_v2_pb(stream, request.SerializeToString())

# Wait for response
response_bytes = await stream.read(1024)
response_bytes = await read_circuit_v2_pb(stream)
if not response_bytes:
logger.error("No response received from relay %s", peer_id)
return False
Expand All @@ -428,12 +435,10 @@ async def make_reservation(self, peer_id: ID) -> bool:
await stream.close()
return False

# Check if reservation was successful
if response.type == HopMessage.STATUS and response.HasField(
if response.type == HopMessage.Type.STATUS and response.HasField(
"status"
):
# Access status code directly from protobuf object
status_code = getattr(response.status, "code", StatusCode.OK)
status_code = StatusCode(response.status)

if status_code == StatusCode.OK:
# Update relay info with reservation details
Expand All @@ -453,11 +458,9 @@ async def make_reservation(self, peer_id: ID) -> bool:
)
return True

# Reservation failed
error_message = "Unknown error"
if response.HasField("status"):
# Access message directly from protobuf object
error_message = getattr(response.status, "message", "")
error_message = StatusCode(response.status).name

logger.warning(
"Reservation request rejected by relay %s: %s",
Expand Down
11 changes: 10 additions & 1 deletion libp2p/relay/circuit_v2/pb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
from .circuit_pb2 import (
HopMessage,
Limit,
Peer,
Reservation,
Status,
StopMessage,
)

__all__ = ["HopMessage", "Limit", "Reservation", "Status", "StopMessage", "HolePunch"]
__all__ = [
"HopMessage",
"Limit",
"Peer",
"Reservation",
"Status",
"StopMessage",
"HolePunch",
]
65 changes: 36 additions & 29 deletions libp2p/relay/circuit_v2/pb/circuit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ syntax = "proto3";

package circuit.pb.v2;

// Circuit v2 message types
// Aligned with libp2p relay/circuit-v2 and rust-libp2p 0.52
// (protocols/relay/src/generated/message.proto), proto3 + optional for presence.

message HopMessage {
enum Type {
RESERVE = 0;
CONNECT = 1;
STATUS = 2;
}

Type type = 1;
bytes peer = 2;
Reservation reservation = 3;
Limit limit = 4;
Status status = 5;
optional bytes senderRecord = 6; // Envelope(PeerRecord)
optional Type type = 1;
optional Peer peer = 2;
optional Reservation reservation = 3;
optional Limit limit = 4;
optional Status status = 5;
optional bytes senderRecord = 6;
}

message StopMessage {
Expand All @@ -24,34 +26,39 @@ message StopMessage {
STATUS = 1;
}

Type type = 1;
bytes peer = 2;
Status status = 3;
optional bytes senderRecord = 4; // Envelope(PeerRecord) encoded
optional Type type = 1;
optional Peer peer = 2;
optional Limit limit = 3;
optional Status status = 4;
optional bytes senderRecord = 5;
}

message Peer {
optional bytes id = 1;
repeated bytes addrs = 2;
}

message Reservation {
bytes voucher = 1;
bytes signature = 2;
int64 expire = 3;
optional uint64 expire = 1;
repeated bytes addrs = 2;
optional bytes voucher = 3;
// py-libp2p relay↔relay signing extension; ignored by rust-libp2p
optional bytes signature = 4;
}

message Limit {
int64 duration = 1;
int64 data = 2;
optional uint32 duration = 1;
optional uint64 data = 2;
}

message Status {
enum Code {
OK = 0;
RESERVATION_REFUSED = 100;
RESOURCE_LIMIT_EXCEEDED = 101;
PERMISSION_DENIED = 102;
CONNECTION_FAILED = 200;
DIAL_REFUSED = 201;
STOP_FAILED = 300;
MALFORMED_MESSAGE = 400;
}
Code code = 1;
string message = 2;
enum Status {
UNUSED = 0;
OK = 100;
RESERVATION_REFUSED = 200;
RESOURCE_LIMIT_EXCEEDED = 201;
PERMISSION_DENIED = 202;
CONNECTION_FAILED = 203;
NO_RESERVATION = 204;
MALFORMED_MESSAGE = 400;
UNEXPECTED_MESSAGE = 401;
}
38 changes: 19 additions & 19 deletions libp2p/relay/circuit_v2/pb/circuit_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading