Skip to content

Commit 1ba628d

Browse files
committed
Avoid unnecessary type casting
1 parent 9b28e82 commit 1ba628d

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

trinity/protocol/bcc/servers.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,33 @@ async def _handle_get_beacon_blocks(self, peer: BCCPeer, msg: Dict[str, Any]) ->
6969
max_blocks = cast(int, msg["max_blocks"])
7070
block_slot_or_hash = msg["block_slot_or_hash"]
7171

72-
if isinstance(block_slot_or_hash, int):
73-
get_start_block = functools.partial(
74-
self.db.get_canonical_block_by_slot,
75-
cast(int, block_slot_or_hash),
76-
)
77-
elif isinstance(block_slot_or_hash, bytes):
78-
get_start_block = functools.partial(
79-
self.db.get_block_by_hash,
80-
cast(Hash32, block_slot_or_hash),
81-
)
82-
else:
83-
actual_type = type(block_slot_or_hash)
84-
raise TypeError(f"Invariant: unexpected type for 'block_slot_or_hash': {actual_type}")
85-
8672
try:
87-
start_block = get_start_block()
73+
if isinstance(block_slot_or_hash, int):
74+
start_block = self.db.get_canonical_block_by_slot(block_slot_or_hash)
75+
elif isinstance(block_slot_or_hash, bytes):
76+
start_block = self.db.get_block_by_hash(Hash32(block_slot_or_hash))
77+
else:
78+
raise TypeError(
79+
f"Invariant: unexpected type for 'block_slot_or_hash': "
80+
f"{type(block_slot_or_hash)}"
81+
)
8882
except BlockNotFound:
89-
self.logger.debug2("%s requested unknown block %s", block_slot_or_hash)
90-
blocks = ()
91-
else:
83+
start_block = None
84+
85+
if start_block is not None:
9286
self.logger.debug2(
9387
"%s requested %d blocks starting with %s",
9488
peer,
9589
max_blocks,
9690
start_block,
9791
)
9892
blocks = self._get_blocks(start_block, max_blocks)
99-
finally:
100-
self.logger.debug2("Replying to %s with %d blocks", peer, len(blocks))
101-
peer.sub_proto.send_blocks(blocks)
93+
else:
94+
self.logger.debug2("%s requested unknown block %s", block_slot_or_hash)
95+
blocks = ()
96+
97+
self.logger.debug2("Replying to %s with %d blocks", peer, len(blocks))
98+
peer.sub_proto.send_blocks(blocks)
10299

103100
@to_tuple
104101
def _get_blocks(self,

0 commit comments

Comments
 (0)