Skip to content

Commit c36d0f1

Browse files
committed
PR feedback
1 parent b6afa5f commit c36d0f1

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

p2p/chain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ async def _handle_msg(self, peer: HeaderRequestingPeer, cmd: protocol.Command,
305305
elif isinstance(cmd, les.GetBlockHeaders):
306306
msg = cast(Dict[str, Any], msg)
307307
await self._handle_get_block_headers(cast(LESPeer, peer), msg)
308+
elif isinstance(cmd, les.BlockHeaders):
309+
# `BlockHeaders` messages are handled at the peer level.
310+
pass
308311
else:
309312
self.logger.debug("Ignoring %s message from %s", cmd, peer)
310313

@@ -533,6 +536,9 @@ async def _handle_msg(self, peer: HeaderRequestingPeer, cmd: protocol.Command,
533536
await self._handle_new_block(peer, cast(Dict[str, Any], msg))
534537
elif isinstance(cmd, eth.GetBlockHeaders):
535538
await self._handle_get_block_headers(peer, cast(Dict[str, Any], msg))
539+
elif isinstance(cmd, eth.BlockHeaders):
540+
# `BlockHeaders` messages are handled at the peer level.
541+
pass
536542
elif isinstance(cmd, eth.GetBlockBodies):
537543
# Only serve up to eth.MAX_BODIES_FETCH items in every request.
538544
block_hashes = cast(List[Hash32], msg)[:eth.MAX_BODIES_FETCH]

p2p/eth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class GetBlockHeaders(Command):
7474

7575

7676
class HeaderRequest(BaseHeaderRequest):
77-
MAX_HEADERS_FETCH = MAX_HEADERS_FETCH
77+
max_size = MAX_HEADERS_FETCH
7878

7979
def __init__(self,
8080
block_number_or_hash: BlockIdentifier,

p2p/les.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class GetBlockHeadersQuery(rlp.Serializable):
170170
class HeaderRequest(BaseHeaderRequest):
171171
request_id: int
172172

173-
MAX_HEADERS_FETCH = MAX_HEADERS_FETCH
173+
max_size = MAX_HEADERS_FETCH
174174

175175
def __init__(self,
176176
block_number_or_hash: BlockIdentifier,

p2p/peer.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,24 @@ def handle_sub_proto_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgT
381381
else:
382382
self.logger.warn("Peer %s has no subscribers, discarding %s msg", self, cmd)
383383

384+
cmd_type = type(cmd)
385+
386+
if cmd_type in self.pending_requests:
387+
request, future = self.pending_requests[cmd_type]
388+
try:
389+
request.validate_response(msg)
390+
except ValidationError as err:
391+
self.logger.debug(
392+
"Response validation failure for pending %s request from peer %s: %s",
393+
cmd_type.__name__,
394+
self,
395+
err,
396+
)
397+
pass
398+
else:
399+
future.set_result(msg)
400+
self.pending_requests.pop(cmd_type)
401+
384402
def process_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgType) -> None:
385403
if cmd.is_base_protocol:
386404
self.handle_p2p_msg(cmd, msg)
@@ -531,21 +549,11 @@ def max_headers_fetch(self) -> int:
531549
return les.MAX_HEADERS_FETCH
532550

533551
def handle_sub_proto_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgType) -> None:
534-
cmd_type = type(cmd)
535-
536552
if isinstance(cmd, les.Announce):
537553
self.head_info = cmd.as_head_info(msg)
538554
self.head_td = self.head_info.total_difficulty
539555
self.head_hash = self.head_info.block_hash
540-
elif cmd_type in self.pending_requests:
541-
request, future = self.pending_requests[cmd_type]
542-
try:
543-
request.validate_response(msg)
544-
except ValidationError:
545-
pass
546-
else:
547-
future.set_result(msg)
548-
self.pending_requests.pop(cmd_type)
556+
549557
super().handle_sub_proto_msg(cmd, msg)
550558

551559
async def send_sub_proto_handshake(self) -> None:
@@ -603,14 +611,26 @@ def request_block_headers(self,
603611

604612
async def wait_for_block_headers(self, request: les.HeaderRequest) -> Tuple[BlockHeader, ...]:
605613
future: 'asyncio.Future[protocol._DecodedMsgType]' = asyncio.Future()
614+
if les.BlockHeaders in self.pending_requests:
615+
# the `finally` block below should prevent this from happening, but
616+
# were two requests to the same peer to be fired off at the same
617+
# time, this will prevent us from overwriting the first one.
618+
raise ValueError(
619+
"There is already a pending `BlockHeaders` request for peer {0}".format(self)
620+
)
606621
self.pending_requests[les.BlockHeaders] = cast(
607622
Tuple[protocol.BaseRequest, 'asyncio.Future[protocol._DecodedMsgType]'],
608623
(request, future),
609624
)
610-
response = cast(
611-
Dict[str, Any],
612-
await self.wait(future, timeout=self._response_timeout),
613-
)
625+
try:
626+
response = cast(
627+
Dict[str, Any],
628+
await self.wait(future, timeout=self._response_timeout),
629+
)
630+
finally:
631+
# We always want to be sure that this method cleans up the
632+
# `pending_requests` so that we don't end up in a situation.
633+
self.pending_requests.pop(les.BlockHeaders, None)
614634
return cast(Tuple[BlockHeader, ...], response['headers'])
615635

616636
async def get_block_headers(self,
@@ -631,8 +651,6 @@ def max_headers_fetch(self) -> int:
631651
return eth.MAX_HEADERS_FETCH
632652

633653
def handle_sub_proto_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgType) -> None:
634-
cmd_type = type(cmd)
635-
636654
if isinstance(cmd, eth.NewBlock):
637655
msg = cast(Dict[str, Any], msg)
638656
header, _, _ = msg['block']
@@ -642,16 +660,6 @@ def handle_sub_proto_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgT
642660
self.head_hash = actual_head
643661
self.head_td = actual_td
644662

645-
if cmd_type in self.pending_requests:
646-
request, future = self.pending_requests[cmd_type]
647-
try:
648-
request.validate_response(msg)
649-
except ValidationError:
650-
pass
651-
else:
652-
future.set_result(msg)
653-
self.pending_requests.pop(cmd_type)
654-
655663
super().handle_sub_proto_msg(cmd, msg)
656664

657665
async def send_sub_proto_handshake(self) -> None:

p2p/protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class BaseHeaderRequest(BaseRequest):
140140

141141
@property
142142
@abstractmethod
143-
def MAX_HEADERS_FETCH(self) -> int:
143+
def max_size(self) -> int:
144144
pass
145145

146146
def generate_block_numbers(self,
@@ -158,7 +158,7 @@ def generate_block_numbers(self,
158158
elif block_number is None:
159159
block_number = cast(BlockNumber, self.block_number_or_hash)
160160

161-
max_headers = min(self.MAX_HEADERS_FETCH, self.max_headers)
161+
max_headers = min(self.max_size, self.max_headers)
162162

163163
# inline import until this module is moved to `trinity`
164164
from trinity.utils.headers import sequence_builder

tests/p2p/test_header_request_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class HeaderRequest(BaseHeaderRequest):
18-
MAX_HEADERS_FETCH = 192
18+
max_size = 192
1919

2020
def __init__(self,
2121
block_number_or_hash,

0 commit comments

Comments
 (0)