Skip to content

Commit

Permalink
Merge pull request #1319 from mg98/fix/community-bootstrap
Browse files Browse the repository at this point in the history
Fix community bootstrap
  • Loading branch information
qstokkink authored Oct 24, 2024
2 parents f6d55a2 + 4474fad commit ebfa3ba
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ipv8/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,19 @@ def add_message_handler(self, msg_num: int | type[Payload], callback: MessageHan
actual_msg_num: int = 256
if not isinstance(msg_num, int):
if not hasattr(msg_num, "msg_id"):
raise RuntimeError("Attempted to add a handler for Payload %s, which does not specify a msg_id!"
% msg_num)
msg = f"Attempted to add a handler for Payload {msg_num}, which does not specify a msg_id!"
raise RuntimeError(msg)
actual_msg_num = cast(int, msg_num.msg_id) # type: ignore[attr-defined]
else:
if msg_num < 0 or msg_num > 255:
raise RuntimeError("Attempted to add a handler for message number %d, which is not a byte!" % msg_num)
msg = f"Attempted to add a handler for message number {msg_num}, which is not a byte!"
raise RuntimeError(msg)
actual_msg_num = msg_num

if self.decode_map[actual_msg_num]:
raise RuntimeError("Attempted to add a handler for message number %d, already mapped to %s!" %
(actual_msg_num, self.decode_map[actual_msg_num]))
msg = (f"Attempted to add a handler for message number {actual_msg_num}, "
f"already mapped to {self.decode_map[actual_msg_num]}!")
raise RuntimeError(msg)
self.decode_map[actual_msg_num] = callback

def on_deprecated_message(self, source_address: Address, data: bytes) -> None:
Expand All @@ -221,7 +223,7 @@ async def _bootstrap(self, bootstrapper: Bootstrapper) -> None:
task = ensure_future(bootstrapper.initialize(self))

addresses = await bootstrapper.get_addresses(self, 60.0)
for address in (addresses if self.max_peers > 0 else islice(addresses, self.max_peers)):
for address in (islice(addresses, self.max_peers) if self.max_peers > 0 else addresses):
self.walk_to(address)

await task
Expand Down

0 comments on commit ebfa3ba

Please sign in to comment.