Skip to content

[ethPM] Fix bug in package id and release id fetching strategy #1427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions web3/pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
to_text,
to_tuple,
)
from eth_utils.toolz import (
concat,
)

from ethpm import (
ASSETS_DIR,
Expand Down Expand Up @@ -217,6 +214,9 @@ def deploy_new_instance(cls, w3):
pass


BATCH_SIZE = 100


class SimpleRegistry(ERC1319Registry):
"""
This class represents an instance of the `Solidity Reference Registry implementation
Expand Down Expand Up @@ -245,29 +245,34 @@ def _get_package_name(self, package_id: bytes) -> str:
@to_tuple
def _get_all_package_ids(self) -> Iterable[Tuple[bytes]]:
num_packages = self._num_package_ids()
# Logic here b/c Solidity Reference Registry implementation returns ids in reverse order
package_ids = [
self.registry.functions.getAllPackageIds(index, (index + 4)).call()[0]
for index in range(0, num_packages, 4)
]
for package_id in concat([x[::-1] for x in package_ids]):
yield package_id
pointer = 0
while pointer < num_packages:
new_ids, new_pointer = self.registry.functions.getAllPackageIds(
pointer,
(pointer + BATCH_SIZE)
).call()
if not new_pointer > pointer:
break
yield from reversed(new_ids)
pointer = new_pointer

def _get_release_id(self, package_name: str, version: str) -> bytes:
return self.registry.functions.getReleaseId(package_name, version).call()

@to_tuple
def _get_all_release_ids(self, package_name: str) -> Iterable[Tuple[bytes]]:
num_releases = self._num_release_ids(package_name)
# Logic here b/c Solidity Reference Registry implementation returns ids in reverse order
release_ids = [
self.registry.functions.getAllReleaseIds(
package_name, index, (index + 4)
).call()[0]
for index in range(0, num_releases, 4)
]
for release_id in concat([x[::-1] for x in release_ids]):
yield release_id
pointer = 0
while pointer < num_releases:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing. This is probably fine, however, you could save yourself a headache by enforcing that pointer always increases on each loop iteration to ensure you cannot end up in an infinite loop.

new_ids, new_pointer = self.registry.functions.getAllReleaseIds(
package_name,
pointer,
(pointer + BATCH_SIZE)
).call()
if not new_pointer > pointer:
break
yield from reversed(new_ids)
pointer = new_pointer

@to_tuple
def _get_release_data(self, release_id: bytes) -> Iterable[Tuple[str]]:
Expand Down