|
| 1 | +""" |
| 2 | +Request/Response Domain Message Types. |
| 3 | +
|
| 4 | +This module defines the data structures for the peer-to-peer request/response |
| 5 | +domain. All messages are SSZ-encoded and then compressed with Snappy frames. |
| 6 | +""" |
| 7 | + |
| 8 | +from pydantic import Field |
| 9 | +from typing_extensions import Annotated |
| 10 | + |
| 11 | +from lean_spec.subspecs.containers import Checkpoint, SignedBlock |
| 12 | +from lean_spec.types import Bytes32, StrictBaseModel |
| 13 | + |
| 14 | +from .config import MAX_REQUEST_BLOCKS |
| 15 | +from .types import ProtocolId |
| 16 | + |
| 17 | +# --- Status v1 --- |
| 18 | + |
| 19 | +STATUS_PROTOCOL_V1: ProtocolId = "/leanconsensus/req/status/1/" |
| 20 | +"""The protocol ID for the Status v1 request/response message.""" |
| 21 | + |
| 22 | + |
| 23 | +class Status(StrictBaseModel): |
| 24 | + """ |
| 25 | + The Status message, used by clients to share their chain state. |
| 26 | +
|
| 27 | + This is the first message sent upon a new connection and is essential for |
| 28 | + the peer-to-peer handshake. It allows nodes to verify compatibility and |
| 29 | + determine if they are on the same chain. |
| 30 | + """ |
| 31 | + |
| 32 | + finalized: Checkpoint |
| 33 | + """The client's latest finalized checkpoint.""" |
| 34 | + |
| 35 | + head: Checkpoint |
| 36 | + """The client's current head checkpoint.""" |
| 37 | + |
| 38 | + |
| 39 | +# --- BlocksByRoot v1 --- |
| 40 | + |
| 41 | +BLOCKS_BY_ROOT_PROTOCOL_V1: ProtocolId = "/leanconsensus/req/blocks_by_root/1/" |
| 42 | +"""The protocol ID for the BlocksByRoot v1 request/response message.""" |
| 43 | + |
| 44 | +BlocksByRootRequest = Annotated[ |
| 45 | + list[Bytes32], |
| 46 | + Field(max_length=MAX_REQUEST_BLOCKS), |
| 47 | +] |
| 48 | +""" |
| 49 | +A request for one or more blocks by their root hashes. |
| 50 | +
|
| 51 | +This is primarily used to recover recent or missing blocks from a peer. |
| 52 | +""" |
| 53 | + |
| 54 | +BlocksByRootResponse = Annotated[ |
| 55 | + list[SignedBlock], |
| 56 | + Field(max_length=MAX_REQUEST_BLOCKS), |
| 57 | +] |
| 58 | +""" |
| 59 | +A response containing the requested `SignedBlock` objects. |
| 60 | +
|
| 61 | +The length of the list may be less than the number of requested blocks if |
| 62 | +the responding peer does not have all of them. Each block is sent in a |
| 63 | +separate `response_chunk`. |
| 64 | +""" |
0 commit comments