Skip to content

Commit e57bb2b

Browse files
committed
networking: simplify peer abstraction
1 parent 8b7636b commit e57bb2b

File tree

13 files changed

+48
-57
lines changed

13 files changed

+48
-57
lines changed

src/lean_spec/subspecs/networking/gossipsub/rpc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def decode(cls, data: bytes) -> ControlGraft:
351351

352352

353353
@dataclass(slots=True)
354-
class PeerInfo:
354+
class PrunePeerInfo:
355355
"""Peer information for PRUNE peer exchange."""
356356

357357
peer_id: bytes = b""
@@ -370,7 +370,7 @@ def encode(self) -> bytes:
370370
return bytes(result)
371371

372372
@classmethod
373-
def decode(cls, data: bytes) -> PeerInfo:
373+
def decode(cls, data: bytes) -> PrunePeerInfo:
374374
"""Decode from protobuf."""
375375
peer_id = b""
376376
signed_peer_record = b""
@@ -401,7 +401,7 @@ class ControlPrune:
401401
topic_id: str = ""
402402
"""Topic being pruned from."""
403403

404-
peers: list[PeerInfo] = field(default_factory=list)
404+
peers: list[PrunePeerInfo] = field(default_factory=list)
405405
"""Peer exchange - alternative peers for the topic (v1.1)."""
406406

407407
backoff: int = 0
@@ -422,7 +422,7 @@ def encode(self) -> bytes:
422422
def decode(cls, data: bytes) -> ControlPrune:
423423
"""Decode from protobuf."""
424424
topic_id = ""
425-
peers: list[PeerInfo] = []
425+
peers: list[PrunePeerInfo] = []
426426
backoff = 0
427427
pos = 0
428428

@@ -435,7 +435,7 @@ def decode(cls, data: bytes) -> ControlPrune:
435435
pos += length
436436
elif field_num == 2 and wire_type == WIRE_TYPE_LENGTH_DELIMITED:
437437
length, pos = _decode_length_at(data, pos)
438-
peers.append(PeerInfo.decode(data[pos : pos + length]))
438+
peers.append(PrunePeerInfo.decode(data[pos : pos + length]))
439439
pos += length
440440
elif field_num == 3 and wire_type == WIRE_TYPE_VARINT:
441441
backoff, pos = _decode_varint_at(data, pos)

src/lean_spec/subspecs/networking/peer/info.py renamed to src/lean_spec/subspecs/networking/peer.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,15 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass, field
6-
from enum import IntEnum, auto
76
from time import time
87
from typing import TYPE_CHECKING
98

10-
from ..transport import PeerId
11-
from ..types import ConnectionState, Multiaddr
9+
from .transport import PeerId
10+
from .types import ConnectionState, Direction, Multiaddr
1211

1312
if TYPE_CHECKING:
14-
from ..enr import ENR
15-
from ..reqresp import Status
16-
17-
18-
class Direction(IntEnum):
19-
"""
20-
Direction of a peer connection.
21-
22-
Indicates whether:
23-
- we initiated the connection (outbound) or
24-
- the peer connected to us (inbound).
25-
"""
26-
27-
INBOUND = auto()
28-
"""Peer initiated the connection to us."""
29-
30-
OUTBOUND = auto()
31-
"""We initiated the connection to the peer."""
13+
from .enr import ENR
14+
from .reqresp import Status
3215

3316

3417
@dataclass

src/lean_spec/subspecs/networking/peer/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/lean_spec/subspecs/networking/service/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from lean_spec.subspecs.containers.attestation import SignedAggregatedAttestation, SignedAttestation
3232
from lean_spec.subspecs.networking.client.event_source import LiveNetworkEventSource
3333
from lean_spec.subspecs.networking.gossipsub.topic import GossipTopic
34-
from lean_spec.subspecs.networking.peer.info import PeerInfo
34+
from lean_spec.subspecs.networking.peer import PeerInfo
3535
from lean_spec.subspecs.networking.types import ConnectionState
3636

3737
from .events import (

src/lean_spec/subspecs/networking/types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
"""Multiaddress string, e.g. ``/ip4/192.168.1.1/udp/9000/quic-v1``."""
3737

3838

39+
class Direction(IntEnum):
40+
"""
41+
Direction of a peer connection.
42+
43+
Indicates whether:
44+
- we initiated the connection (outbound) or
45+
- the peer connected to us (inbound).
46+
"""
47+
48+
INBOUND = auto()
49+
"""Peer initiated the connection to us."""
50+
51+
OUTBOUND = auto()
52+
"""We initiated the connection to the peer."""
53+
54+
3955
class ConnectionState(IntEnum):
4056
"""
4157
Peer connection state machine.

src/lean_spec/subspecs/sync/peer_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from lean_spec.subspecs.containers.slot import Slot
1313
from lean_spec.subspecs.networking import PeerId
14-
from lean_spec.subspecs.networking.peer.info import PeerInfo
14+
from lean_spec.subspecs.networking.peer import PeerInfo
1515
from lean_spec.subspecs.networking.reqresp.message import Status
1616

1717
from .config import MAX_CONCURRENT_REQUESTS

tests/interop/helpers/node_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from lean_spec.subspecs.forkchoice import Store
2020
from lean_spec.subspecs.networking import PeerId
2121
from lean_spec.subspecs.networking.client import LiveNetworkEventSource
22-
from lean_spec.subspecs.networking.peer.info import PeerInfo
22+
from lean_spec.subspecs.networking.peer import PeerInfo
2323
from lean_spec.subspecs.networking.reqresp.message import Status
2424
from lean_spec.subspecs.networking.types import ConnectionState
2525
from lean_spec.subspecs.node import Node, NodeConfig

tests/lean_spec/helpers/builders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from lean_spec.subspecs.forkchoice import Store
3535
from lean_spec.subspecs.koalabear import Fp
3636
from lean_spec.subspecs.networking import PeerId
37-
from lean_spec.subspecs.networking.peer.info import PeerInfo
37+
from lean_spec.subspecs.networking.peer import PeerInfo
3838
from lean_spec.subspecs.networking.reqresp.message import Status
3939
from lean_spec.subspecs.networking.types import ConnectionState
4040
from lean_spec.subspecs.ssz.hash import hash_tree_root

tests/lean_spec/subspecs/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from lean_spec.subspecs.networking import PeerId
12-
from lean_spec.subspecs.networking.peer.info import PeerInfo
12+
from lean_spec.subspecs.networking.peer import PeerInfo
1313
from lean_spec.subspecs.networking.types import ConnectionState
1414

1515

tests/lean_spec/subspecs/networking/gossipsub/test_rpc_edge_cases.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,33 @@
1616
ControlMessage,
1717
ControlPrune,
1818
Message,
19-
PeerInfo,
2019
ProtobufDecodeError,
20+
PrunePeerInfo,
2121
SubOpts,
2222
_skip_field,
2323
encode_bytes,
2424
encode_tag,
2525
)
2626

2727

28-
class TestPeerInfoRoundtrip:
29-
"""Tests for PeerInfo protobuf encoding/decoding."""
28+
class TestPrunePeerInfoRoundtrip:
29+
"""Tests for PrunePeerInfo protobuf encoding/decoding."""
3030

3131
def test_peer_info_with_both_fields(self) -> None:
32-
"""PeerInfo roundtrips with both peer_id and signed_peer_record."""
33-
info = PeerInfo(peer_id=b"peer123", signed_peer_record=b"record456")
34-
assert PeerInfo.decode(info.encode()) == info
32+
"""PrunePeerInfo roundtrips with both peer_id and signed_peer_record."""
33+
info = PrunePeerInfo(peer_id=b"peer123", signed_peer_record=b"record456")
34+
assert PrunePeerInfo.decode(info.encode()) == info
3535

3636
def test_peer_info_peer_id_only(self) -> None:
37-
"""PeerInfo roundtrips with only peer_id."""
38-
info = PeerInfo(peer_id=b"peerOnly")
39-
assert PeerInfo.decode(info.encode()) == info
37+
"""PrunePeerInfo roundtrips with only peer_id."""
38+
info = PrunePeerInfo(peer_id=b"peerOnly")
39+
assert PrunePeerInfo.decode(info.encode()) == info
4040

4141
def test_peer_info_empty(self) -> None:
42-
"""Empty PeerInfo produces empty encoding."""
43-
info = PeerInfo()
42+
"""Empty PrunePeerInfo produces empty encoding."""
43+
info = PrunePeerInfo()
4444
assert info.encode() == b""
45-
assert PeerInfo.decode(b"") == PeerInfo()
45+
assert PrunePeerInfo.decode(b"") == PrunePeerInfo()
4646

4747

4848
class TestPruneWithPeerExchange:
@@ -53,8 +53,8 @@ def test_prune_with_peers(self) -> None:
5353
prune = ControlPrune(
5454
topic_id="/topic",
5555
peers=[
56-
PeerInfo(peer_id=b"alt1", signed_peer_record=b"rec1"),
57-
PeerInfo(peer_id=b"alt2"),
56+
PrunePeerInfo(peer_id=b"alt1", signed_peer_record=b"rec1"),
57+
PrunePeerInfo(peer_id=b"alt2"),
5858
],
5959
backoff=120,
6060
)

0 commit comments

Comments
 (0)