Skip to content

Commit

Permalink
Merge pull request #334 from pipermerriam/piper/start-content-process…
Browse files Browse the repository at this point in the history
…ing-from-random-key

Content processing starts at random key
  • Loading branch information
pipermerriam committed Dec 19, 2020
2 parents 64fba8f + c0b80a3 commit cc8b4e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 17 additions & 4 deletions ddht/v5_1/alexandria/content_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import secrets
from typing import Optional

from async_service import Service
from async_service import external_trio_api as external_api
from eth_typing import Hash32
from eth_typing import Hash32, NodeID
from eth_utils import humanize_seconds
from eth_utils.toolz import first, take
import ssz
Expand Down Expand Up @@ -135,13 +136,20 @@ async def _periodically_advertise_content(self) -> None:
if not total_keys:
continue

first_key = first(
self.content_storage.iter_closest(NodeID(secrets.token_bytes(32)))
)

self.logger.info(
"content-processing-starting: total=%d", total_keys,
"content-processing-starting: total=%d start=%s",
total_keys,
first_key.hex(),
)

processed_keys = 0

last_key: Optional[ContentKey] = None
last_key = first_key
has_wrapped_around = False

while self.manager.is_running:
elapsed = trio.current_time() - start_at
Expand All @@ -160,12 +168,17 @@ async def _periodically_advertise_content(self) -> None:
content_keys = content_keys[1:]

if not content_keys:
break
last_key = None
has_wrapped_around = True
continue

for content_key in content_keys:
await send_channel.send(content_key)

last_key = content_keys[-1]
if has_wrapped_around and last_key >= first_key:
break

processed_keys += len(content_keys)
progress = processed_keys * 100 / total_keys

Expand Down
3 changes: 2 additions & 1 deletion tests/core/v5_1/alexandria/test_message_encoding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from eth_enr.tools.factories import ENRFactory
from eth_keys import keys
from hypothesis import given
from hypothesis import given, settings
from hypothesis import strategies as st
import rlp

Expand Down Expand Up @@ -82,6 +82,7 @@ def test_found_nodes_message_encoding_round_trip(num_enr_records):
).map(lambda key_and_root: Advertisement.create(*key_and_root, PRIVATE_KEY))


@settings(deadline=500)
@given(advertisements=st.lists(advertisement_st, min_size=1, max_size=5).map(tuple),)
def test_advertisement_message_encoding_round_trip(advertisements):
message = AdvertiseMessage(advertisements)
Expand Down

0 comments on commit cc8b4e5

Please sign in to comment.