Skip to content
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

resolve library deps. recursively #480

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crytic_compile/platform/solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _build_contract_data(compilation_unit: "CompilationUnit") -> Dict:
"bin-runtime": source_unit.bytecode_runtime(contract_name, libraries_to_update),
"userdoc": source_unit.natspec[contract_name].userdoc.export(),
"devdoc": source_unit.natspec[contract_name].devdoc.export(),
"libraries": dict(libraries) if libraries else {},
"libraries": libraries if libraries else [],
}
return contracts

Expand Down
24 changes: 18 additions & 6 deletions crytic_compile/source_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
if TYPE_CHECKING:
from crytic_compile.compilation_unit import CompilationUnit

LIBRARY_PATTERN = re.compile(r"__.{36}__")


def get_library_candidate(filename: Filename, contract_name: str) -> List[str]:
"""
Expand Down Expand Up @@ -348,8 +350,9 @@ def libraries_names(self, name: str) -> List[str]:
return [name for (name, _) in self._libraries[name]]

def libraries_names_and_patterns(self, name: str) -> List[Tuple[str, str]]:
"""Return the names and the patterns of the libraries used by the contract

"""Return the names and the patterns of the libraries used by the contract.
The libraries are recursively resolved and ordered by dependency (post-order traversal).
Thus, they can be deployed first to last and linked as needed.
Args:
name (str): contract name

Expand All @@ -358,10 +361,19 @@ def libraries_names_and_patterns(self, name: str) -> List[Tuple[str, str]]:
"""

if name not in self._libraries:
init = re.findall(r"__.{36}__", self.bytecode_init(name))
runtime = re.findall(r"__.{36}__", self.bytecode_runtime(name))
libraires = [self._library_name_lookup(x, name) for x in set(init + runtime)]
self._libraries[name] = [lib for lib in libraires if lib]
resolved = []

def dep_resolve(name, resolved):
init = re.findall(r"__.{36}__", self.bytecode_init(name))
runtime = re.findall(r"__.{36}__", self.bytecode_runtime(name))
libs = [self._library_name_lookup(x, name) for x in set(init + runtime)]
for (lib, placeholder) in libs:
if lib not in resolved:
dep_resolve(lib, resolved)
resolved.append({"name": lib, "placeholder": placeholder})

dep_resolve(name, resolved)
self._libraries[name] = resolved
return self._libraries[name]

def _update_bytecode_with_libraries(
Expand Down