Skip to content

Commit 80b2848

Browse files
authored
Merge pull request #336 from crytic/dev-filenames-lookup
Refactor filenames/filenames_lookup
2 parents 99c0288 + 4b7e6fa commit 80b2848

File tree

4 files changed

+64
-62
lines changed

4 files changed

+64
-62
lines changed

crytic_compile/compilation_unit.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"""
44
import uuid
55
from collections import defaultdict
6-
from typing import TYPE_CHECKING, Dict, Set
7-
6+
from typing import TYPE_CHECKING, Dict, Set, Optional
87

98
from crytic_compile.compiler.compiler import CompilerVersion
109
from crytic_compile.source_unit import SourceUnit
@@ -35,6 +34,9 @@ def __init__(self, crytic_compile: "CryticCompile", unique_id: str):
3534
# set containing all the filenames of this compilation unit
3635
self._filenames: Set[Filename] = set()
3736

37+
# mapping from absolute/relative/used to filename
38+
self._filenames_lookup: Optional[Dict[str, Filename]] = None
39+
3840
# compiler.compiler
3941
self._compiler_version: CompilerVersion = CompilerVersion(
4042
compiler="N/A", version="N/A", optimized=False
@@ -118,7 +120,6 @@ def create_source_unit(self, filename: Filename) -> SourceUnit:
118120
if not filename in self._source_units:
119121
source_unit = SourceUnit(self, filename) # type: ignore
120122
self.filenames.add(filename)
121-
self.crytic_compile.filenames.add(filename)
122123
self._source_units[filename] = source_unit
123124
return self._source_units[filename]
124125

@@ -194,6 +195,36 @@ def relative_filename_from_absolute_filename(self, absolute_filename: str) -> st
194195
raise ValueError("f{absolute_filename} does not exist in {d}")
195196
return d_file[absolute_filename]
196197

198+
def filename_lookup(self, filename: str) -> Filename:
199+
"""Return a crytic_compile.naming.Filename from a any filename
200+
201+
Args:
202+
filename (str): filename (used/absolute/relative)
203+
204+
Raises:
205+
ValueError: If the filename is not in the project
206+
207+
Returns:
208+
Filename: Associated Filename object
209+
"""
210+
# pylint: disable=import-outside-toplevel
211+
from crytic_compile.platform.truffle import Truffle
212+
213+
if isinstance(self.crytic_compile.platform, Truffle) and filename.startswith("project:/"):
214+
filename = filename[len("project:/") :]
215+
216+
if self._filenames_lookup is None:
217+
self._filenames_lookup = {}
218+
for file in self._filenames:
219+
self._filenames_lookup[file.absolute] = file
220+
self._filenames_lookup[file.relative] = file
221+
self._filenames_lookup[file.used] = file
222+
if filename not in self._filenames_lookup:
223+
raise ValueError(
224+
f"{filename} does not exist in {[f.absolute for f in self._filenames_lookup.values()]}"
225+
)
226+
return self._filenames_lookup[filename]
227+
197228
# endregion
198229
###################################################################################
199230
###################################################################################

crytic_compile/crytic_compile.py

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from crytic_compile.platform.all_export import PLATFORMS_EXPORT
2020
from crytic_compile.platform.solc import Solc
2121
from crytic_compile.platform.standard import export_to_standard
22-
from crytic_compile.platform.truffle import Truffle
2322
from crytic_compile.utils.naming import Filename
2423
from crytic_compile.utils.npm import get_package_name
2524
from crytic_compile.utils.zip import load_from_zip
@@ -78,12 +77,6 @@ def __init__(self, target: Union[str, AbstractPlatform], **kwargs: str) -> None:
7877
# dependencies is needed for platform conversion
7978
self._dependencies: Set = set()
8079

81-
# set containing all the filenames
82-
self._filenames: Set[Filename] = set()
83-
84-
# mapping from absolute/relative/used to filename
85-
self._filenames_lookup: Optional[Dict[str, Filename]] = None
86-
8780
self._src_content: Dict = {}
8881

8982
# Mapping each file to
@@ -152,27 +145,21 @@ def is_in_multiple_compilation_unit(self, contract: str) -> bool:
152145

153146
###################################################################################
154147
###################################################################################
155-
# region Filenames
148+
# region Utils
156149
###################################################################################
157150
###################################################################################
158-
159151
@property
160152
def filenames(self) -> Set[Filename]:
161-
"""All the project filenames
162-
163-
Returns:
164-
Set[Filename]: Project's filenames
165153
"""
166-
return self._filenames
154+
Return the set of all the filenames used
167155
168-
@filenames.setter
169-
def filenames(self, all_filenames: Set[Filename]) -> None:
170-
"""Set the filenames
171-
172-
Args:
173-
all_filenames (Set[Filename]): New filenames
156+
Returns:
157+
Set[Filename]: list of filenames
174158
"""
175-
self._filenames = all_filenames
159+
filenames: Set[Filename] = set()
160+
for compile_unit in self._compilation_units.values():
161+
filenames = filenames.union(compile_unit.filenames)
162+
return filenames
176163

177164
def filename_lookup(self, filename: str) -> Filename:
178165
"""Return a crytic_compile.naming.Filename from a any filename
@@ -186,21 +173,13 @@ def filename_lookup(self, filename: str) -> Filename:
186173
Returns:
187174
Filename: Associated Filename object
188175
"""
176+
for compile_unit in self.compilation_units.values():
177+
try:
178+
return compile_unit.filename_lookup(filename)
179+
except ValueError:
180+
pass
189181

190-
if isinstance(self.platform, Truffle) and filename.startswith("project:/"):
191-
filename = filename[len("project:/") :]
192-
193-
if self._filenames_lookup is None:
194-
self._filenames_lookup = {}
195-
for file in self._filenames:
196-
self._filenames_lookup[file.absolute] = file
197-
self._filenames_lookup[file.relative] = file
198-
self._filenames_lookup[file.used] = file
199-
if filename not in self._filenames_lookup:
200-
raise ValueError(
201-
f"{filename} does not exist in {[f.absolute for f in self._filenames_lookup.values()]}"
202-
)
203-
return self._filenames_lookup[filename]
182+
raise ValueError(f"{filename} does not exist")
204183

205184
@property
206185
def dependencies(self) -> Set[str]:

crytic_compile/platform/standard.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,15 @@ def _load_from_compile_0_0_1(crytic_compile: "CryticCompile", loaded_json: Dict)
416416
crytic_compile.dependencies.add(filename.short)
417417
crytic_compile.dependencies.add(filename.used)
418418

419-
compilation_unit.filenames = {
420-
_convert_dict_to_filename(filename)
421-
for filename in compilation_unit_json["filenames"]
422-
}
419+
compilation_unit.filenames = {
420+
_convert_dict_to_filename(filename) for filename in compilation_unit_json["filenames"]
421+
}
423422

424-
for path, ast in compilation_unit_json["asts"].items():
425-
# The following might create lookup issue?
426-
filename = crytic_compile.filename_lookup(path)
427-
source_unit = compilation_unit.create_source_unit(filename)
428-
source_unit.ast = ast
423+
for path, ast in compilation_unit_json["asts"].items():
424+
# The following might create lookup issue?
425+
filename = crytic_compile.filename_lookup(path)
426+
source_unit = compilation_unit.create_source_unit(filename)
427+
source_unit.ast = ast
429428

430429

431430
def _load_from_compile_current(crytic_compile: "CryticCompile", loaded_json: Dict) -> None:
@@ -466,16 +465,15 @@ def _load_from_compile_current(crytic_compile: "CryticCompile", loaded_json: Dic
466465
crytic_compile.dependencies.add(filename.short)
467466
crytic_compile.dependencies.add(filename.used)
468467

469-
for path, ast in compilation_unit_json["asts"].items:
470-
# The following might create lookup issue?
471-
filename = convert_filename(path, lambda x: x, crytic_compile)
472-
source_unit = compilation_unit.create_source_unit(filename)
473-
source_unit.ast = ast
468+
compilation_unit.filenames = {
469+
_convert_dict_to_filename(filename) for filename in compilation_unit_json["filenames"]
470+
}
474471

475-
compilation_unit.filenames = {
476-
_convert_dict_to_filename(filename)
477-
for filename in compilation_unit_json["filenames"]
478-
}
472+
for path, ast in compilation_unit_json["asts"].items:
473+
# The following might create lookup issue?
474+
filename = convert_filename(path, lambda x: x, crytic_compile)
475+
source_unit = compilation_unit.create_source_unit(filename)
476+
source_unit.ast = ast
479477

480478

481479
def load_from_compile(crytic_compile: "CryticCompile", loaded_json: Dict) -> Tuple[int, List[str]]:
@@ -490,7 +488,6 @@ def load_from_compile(crytic_compile: "CryticCompile", loaded_json: Dict) -> Tup
490488
Tuple[int, List[str]]: (underlying platform types, guessed unit tests)
491489
"""
492490
crytic_compile.package_name = loaded_json.get("package", None)
493-
494491
if "compilation_units" not in loaded_json:
495492
_load_from_compile_legacy1(crytic_compile, loaded_json)
496493

@@ -502,10 +499,6 @@ def load_from_compile(crytic_compile: "CryticCompile", loaded_json: Dict) -> Tup
502499
else:
503500
_load_from_compile_current(crytic_compile, loaded_json)
504501

505-
# Set our filenames
506-
for compilation_unit in crytic_compile.compilation_units.values():
507-
crytic_compile.filenames |= set(compilation_unit.filenames)
508-
509502
crytic_compile.working_dir = loaded_json["working_dir"]
510503

511504
return loaded_json["type"], loaded_json.get("unit_tests", [])

crytic_compile/platform/waffle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
192192
source_unit = compilation_unit.create_source_unit(filename)
193193

194194
source_unit.ast = target_all["sources"][contract[0]]["AST"]
195-
crytic_compile.filenames.add(filename)
196195
compilation_unit.filenames.add(filename)
197196
compilation_unit.filename_to_contracts[filename].add(contract_name)
198197
source_unit.contracts_names.add(contract_name)

0 commit comments

Comments
 (0)