Skip to content
Merged
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
16 changes: 2 additions & 14 deletions src/lean_spec/subspecs/networking/reqresp/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ class RequestHandler:
block_lookup: BlockLookup | None = None
"""Callback to look up blocks by root."""

async def handle_status(self, request: Status, response: StreamResponseAdapter) -> None:
async def handle_status(self, response: StreamResponseAdapter) -> None:
"""
Handle incoming Status request.

Responds with our current chain status.

Args:
request: Peer's status (logged but not used for response).
response: Stream for sending our status.
"""
# Guard: Ensure we have a status configured.
Expand All @@ -166,17 +165,6 @@ async def handle_status(self, request: Status, response: StreamResponseAdapter)
await response.send_error(ResponseCode.SERVER_ERROR, "Status not available")
return

# Respond with OUR status, not the peer's.
#
# The Status exchange is symmetric: each side sends its own chain state.
# The peer's status (in `request`) is useful for:
#
# - Logging for debugging
# - Peer scoring (handled elsewhere)
# - Fork detection (handled by sync layer)
#
# But it does NOT affect what we respond with.
# We always send our current head and finalized checkpoint.
await response.send_success(self.our_status.encode_bytes())

async def handle_blocks_by_root(
Expand Down Expand Up @@ -440,7 +428,7 @@ async def _dispatch(
logger.debug("Status decode error: %s", e)
await response.send_error(ResponseCode.INVALID_REQUEST, "Invalid Status message")
return
await self.handler.handle_status(request, response)
await self.handler.handle_status(response)

elif protocol_id == BLOCKS_BY_ROOT_PROTOCOL_V1:
# BlocksByRoot request: Peer wants specific blocks by hash.
Expand Down
23 changes: 5 additions & 18 deletions tests/lean_spec/subspecs/networking/reqresp/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,7 @@ async def test_handle_status_returns_our_status(self) -> None:
handler = RequestHandler(our_status=our_status)
response = MockResponseStream()

peer_status = Status(
finalized=Checkpoint(root=Bytes32(b"\xaa" * 32), slot=Slot(50)),
head=Checkpoint(root=Bytes32(b"\xbb" * 32), slot=Slot(150)),
)

await handler.handle_status(peer_status, response) # type: ignore[arg-type]
await handler.handle_status(response) # type: ignore[arg-type]

assert len(response.errors) == 0
assert len(response.successes) == 1
Expand All @@ -219,8 +214,7 @@ async def test_handle_status_no_status_returns_error(self) -> None:
handler = RequestHandler() # No our_status set
response = MockResponseStream()

peer_status = make_test_status()
await handler.handle_status(peer_status, response) # type: ignore[arg-type]
await handler.handle_status(response) # type: ignore[arg-type]

assert len(response.successes) == 0
assert len(response.errors) == 1
Expand All @@ -233,15 +227,8 @@ async def test_handle_status_ignores_peer_status(self) -> None:
handler = RequestHandler(our_status=our_status)
response = MockResponseStream()

# Peer claims different chain state
peer_status = Status(
finalized=Checkpoint(root=Bytes32(b"\xff" * 32), slot=Slot(9999)),
head=Checkpoint(root=Bytes32(b"\xee" * 32), slot=Slot(10000)),
)

await handler.handle_status(peer_status, response) # type: ignore[arg-type]
await handler.handle_status(response) # type: ignore[arg-type]

# Our response is independent of peer's status
returned_status = Status.decode_bytes(response.successes[0])
assert returned_status.head.slot == Slot(200)
assert returned_status.finalized.slot == Slot(100)
Expand Down Expand Up @@ -953,13 +940,13 @@ async def test_status_update_after_initialization(self) -> None:
response1 = MockResponseStream()

# First request with no status
await handler.handle_status(make_test_status(), response1) # type: ignore[arg-type]
await handler.handle_status(response1) # type: ignore[arg-type]

# Update status
handler.our_status = make_test_status()

response2 = MockResponseStream()
await handler.handle_status(make_test_status(), response2) # type: ignore[arg-type]
await handler.handle_status(response2) # type: ignore[arg-type]

# First request should fail
assert len(response1.successes) == 0
Expand Down
Loading