Skip to content

Commit 65a25bb

Browse files
authored
networking: new types and stronger typing (leanEthereum#252)
1 parent 77bde6b commit 65a25bb

3 files changed

Lines changed: 41 additions & 27 deletions

File tree

src/lean_spec/subspecs/networking/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
from typing_extensions import Final
44

5+
from lean_spec.types.byte_arrays import Bytes4
6+
57
from .types import DomainType
68

79
MAX_REQUEST_BLOCKS: Final = 2**10
810
"""Maximum number of blocks in a single request."""
911

10-
MESSAGE_DOMAIN_INVALID_SNAPPY: Final[DomainType] = b"\x00\x00\x00\x00"
12+
MESSAGE_DOMAIN_INVALID_SNAPPY: Final[DomainType] = Bytes4(b"\x00\x00\x00\x00")
1113
"""4-byte domain for gossip message-id isolation of invalid snappy messages."""
1214

13-
MESSAGE_DOMAIN_VALID_SNAPPY: Final[DomainType] = b"\x01\x00\x00\x00"
15+
MESSAGE_DOMAIN_VALID_SNAPPY: Final[DomainType] = Bytes4(b"\x01\x00\x00\x00")
1416
"""4-byte domain for gossip message-id isolation of valid snappy messages."""

src/lean_spec/subspecs/networking/reqresp/message.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
domain. All messages are SSZ-encoded and then compressed with Snappy frames.
66
"""
77

8-
from pydantic import Field
9-
from typing_extensions import Annotated
8+
from typing import ClassVar, Type
109

1110
from lean_spec.subspecs.containers import Checkpoint, SignedBlockWithAttestation
12-
from lean_spec.types import Bytes32, StrictBaseModel
11+
from lean_spec.types import Bytes32, SSZList, SSZType, StrictBaseModel
1312

1413
from ..config import MAX_REQUEST_BLOCKS
1514
from ..types import ProtocolId
@@ -67,24 +66,26 @@ class Status(StrictBaseModel):
6766
BLOCKS_BY_ROOT_PROTOCOL_V1: ProtocolId = "/leanconsensus/req/blocks_by_root/1/"
6867
"""The protocol ID for the BlocksByRoot v1 request/response message."""
6968

70-
BlocksByRootRequest = Annotated[
71-
list[Bytes32],
72-
Field(max_length=MAX_REQUEST_BLOCKS),
73-
]
74-
"""
75-
A request for one or more blocks by their root hashes.
7669

77-
This is primarily used to recover recent or missing blocks from a peer.
78-
"""
70+
class BlocksByRootRequest(SSZList[Bytes32]):
71+
"""
72+
A request for one or more blocks by their root hashes.
7973
80-
BlocksByRootResponse = Annotated[
81-
list[SignedBlockWithAttestation],
82-
Field(max_length=MAX_REQUEST_BLOCKS),
83-
]
84-
"""
85-
A response containing the requested `SignedBlockWithAttestation` objects.
74+
This is primarily used to recover recent or missing blocks from a peer.
75+
"""
8676

87-
The length of the list may be less than the number of requested blocks if
88-
the responding peer does not have all of them. Each block is sent in a
89-
separate `response_chunk`.
90-
"""
77+
ELEMENT_TYPE: ClassVar[Type[SSZType]] = Bytes32
78+
LIMIT: ClassVar[int] = MAX_REQUEST_BLOCKS
79+
80+
81+
class BlocksByRootResponse(SSZList[SignedBlockWithAttestation]):
82+
"""
83+
A response containing the requested `SignedBlockWithAttestation` objects.
84+
85+
The length of the list may be less than the number of requested blocks if
86+
the responding peer does not have all of them. Each block is sent in a
87+
separate `response_chunk`.
88+
"""
89+
90+
ELEMENT_TYPE: ClassVar[Type[SSZType]] = SignedBlockWithAttestation
91+
LIMIT: ClassVar[int] = MAX_REQUEST_BLOCKS
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""Networking-related type definitions for the specification."""
22

3-
from typing import Annotated
3+
from lean_spec.types import Uint64
4+
from lean_spec.types.byte_arrays import Bytes4, Bytes32
45

5-
from pydantic import Field
6-
7-
DomainType = Annotated[bytes, Field(min_length=4, max_length=4)]
6+
DomainType = Bytes4
87
"""A 4-byte value used for domain separation in message-ids."""
98

9+
NodeId = Bytes32
10+
"""32-byte node identifier for Discovery v5, derived from ``keccak256(pubkey)``."""
11+
1012
ProtocolId = str
1113
"""A string representing a libp2p protocol ID."""
14+
15+
SeqNumber = Uint64
16+
"""Sequence number used in ENR records, metadata, and ping messages."""
17+
18+
SubnetId = Uint64
19+
"""Subnet identifier (0-63) for attestation subnet partitioning."""
20+
21+
Multiaddr = str
22+
"""Multiaddress string, e.g. ``/ip4/192.168.1.1/tcp/9000``."""

0 commit comments

Comments
 (0)