Skip to content

Commit 08529b6

Browse files
committed
Hint GetBeaconBlock message type with TypedDict
1 parent 0284944 commit 08529b6

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

trinity/protocol/bcc/commands.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
from typing import (
2+
Union,
3+
)
4+
15
from rlp import sedes
26

7+
from mypy_extensions import (
8+
TypedDict,
9+
)
10+
11+
from eth_typing import (
12+
Hash32,
13+
)
14+
315
from p2p.protocol import (
416
Command,
517
)
618

7-
from trinity.rlp.sedes import HashOrNumber
19+
from trinity.rlp.sedes import (
20+
HashOrNumber,
21+
)
22+
823
from eth.beacon.types.blocks import BaseBeaconBlock
924
from eth.beacon.types.attestation_records import AttestationRecord
1025

@@ -19,6 +34,12 @@ class Status(Command):
1934
]
2035

2136

37+
GetBeaconBlocksMessage = TypedDict("GetBeaconBlocksMessage", {
38+
"block_slot_or_hash": Union[int, Hash32],
39+
"max_blocks": int,
40+
})
41+
42+
2243
class GetBeaconBlocks(Command):
2344
_cmd_id = 1
2445
structure = [

trinity/protocol/bcc/servers.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import itertools
22
from typing import (
33
cast,
4-
Any,
5-
Dict,
64
Iterable,
75
Set,
86
Type,
@@ -33,7 +31,10 @@
3331
from eth.beacon.types.blocks import BaseBeaconBlock
3432

3533
from trinity.protocol.common.servers import BaseRequestServer
36-
from trinity.protocol.bcc import commands
34+
from trinity.protocol.bcc.commands import (
35+
GetBeaconBlocks,
36+
GetBeaconBlocksMessage,
37+
)
3738
from trinity.protocol.bcc.peer import (
3839
BCCPeer,
3940
BCCPeerPool,
@@ -42,7 +43,7 @@
4243

4344
class BCCRequestServer(BaseRequestServer):
4445
subscription_msg_types: Set[Type[Command]] = {
45-
commands.GetBeaconBlocks,
46+
GetBeaconBlocks,
4647
}
4748

4849
def __init__(self,
@@ -56,16 +57,16 @@ async def _handle_msg(self, base_peer: BasePeer, cmd: Command,
5657
msg: protocol._DecodedMsgType) -> None:
5758
peer = cast(BCCPeer, base_peer)
5859

59-
if isinstance(cmd, commands.GetBeaconBlocks):
60-
await self._handle_get_beacon_blocks(peer, cast(Dict[str, Any], msg))
60+
if isinstance(cmd, GetBeaconBlocks):
61+
await self._handle_get_beacon_blocks(peer, cast(GetBeaconBlocksMessage, msg))
6162
else:
6263
raise Exception("Invariant: Only subscribed to GetBeaconBlocks")
6364

64-
async def _handle_get_beacon_blocks(self, peer: BCCPeer, msg: Dict[str, Any]) -> None:
65+
async def _handle_get_beacon_blocks(self, peer: BCCPeer, msg: GetBeaconBlocksMessage) -> None:
6566
if not peer.is_operational:
6667
return
6768

68-
max_blocks = cast(int, msg["max_blocks"])
69+
max_blocks = msg["max_blocks"]
6970
block_slot_or_hash = msg["block_slot_or_hash"]
7071

7172
try:

0 commit comments

Comments
 (0)