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

[Breaking API Change] Use list instead of set for filenames #120

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 17 additions & 6 deletions crytic_compile/crytic_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import logging
import re
import subprocess
from typing import Dict, List, Union, Set, Tuple, Optional, Type, TYPE_CHECKING
from typing import Dict, List, Union, Set, Tuple, Optional, Type, TYPE_CHECKING, MutableMapping
from pathlib import Path
import sha3
from collections import OrderedDict # note: this can be removed if we use python >= 3.7


from crytic_compile.platform import solc_standard_json, all_platforms
from crytic_compile.platform.abstract_platform import AbstractPlatform
Expand Down Expand Up @@ -89,9 +91,11 @@ def __init__(self, target: Union[str, AbstractPlatform], **kwargs: str):
self._contracts_name_without_libraries: Optional[Set[str]] = None

# set containing all the filenames
self._filenames: Set[Filename] = set()
# The list keeps the order of insertion, but prevents duplicate
# We need to keep the order for Echidna
self._filenames: List[Filename] = []
# mapping from contract name to filename (naming.Filename)
self._contracts_filenames: Dict[str, Filename] = {}
self._contracts_filenames: OrderedDict[str, Filename] = OrderedDict()

# Libraries used by the contract
# contract_name -> (library, pattern)
Expand Down Expand Up @@ -153,18 +157,20 @@ def natspec(self):
###################################################################################

@property
def filenames(self) -> Set[Filename]:
def filenames(self) -> List[Filename]:
"""
:return: set(naming.Filename)
"""
return self._filenames

@filenames.setter
def filenames(self, all_filenames: Set[Filename]):
def filenames(self, all_filenames: List[Filename]):
self._filenames = all_filenames

# contracts_filenames should return an OrderedDict, but OrderedDict[str, Filename]
# does not work with python 3.6. 3.7.2 introduced typing.OrderedDict
@property
def contracts_filenames(self) -> Dict[str, Filename]:
def contracts_filenames(self) -> MutableMapping[str, Filename]:
"""
Return a dict contract_name -> Filename namedtuple (absolute, used)

Expand All @@ -181,6 +187,11 @@ def contracts_absolute_filenames(self) -> Dict[str, str]:
"""
return {k: f.absolute for (k, f) in self._contracts_filenames.items()}

def add_filename(self, filename: str):
if filename in self._filenames:
return
self._filenames.append((filename))

def filename_of_contract(self, name: str) -> Filename:
"""
:return: utils.namings.Filename
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/brownie.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _iterate_over_files(crytic_compile: "CryticCompile", target: str, filenames:
)

crytic_compile.asts[filename.absolute] = target_loaded["ast"]
crytic_compile.filenames.add(filename)
crytic_compile.add_filename(filename)
contract_name = target_loaded["contractName"]
crytic_compile.contracts_filenames[contract_name] = filename
crytic_compile.contracts_names.add(contract_name)
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/buidler.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
path = convert_filename(
path, relative_to_short, crytic_compile, working_dir=buidler_working_dir
)
crytic_compile.filenames.add(path)
crytic_compile.add_filename(path)
crytic_compile.asts[path.absolute] = info["ast"]

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/dapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
path = convert_filename(
path, _relative_to_short, crytic_compile, working_dir=self._target
)
crytic_compile.filenames.add(path)
crytic_compile.add_filename(path)
crytic_compile.asts[path.absolute] = info["AST"]

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/embark.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
k, _relative_to_short, crytic_compile, working_dir=self._target
)
crytic_compile.asts[filename.absolute] = ast
crytic_compile.filenames.add(filename)
crytic_compile.add_filename(filename)

if not "contracts" in targets_loaded:
LOGGER.error(
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/etherlime.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
filename_txt = target_loaded["ast"]["absolutePath"]
filename = convert_filename(filename_txt, _relative_to_short, crytic_compile)
crytic_compile.asts[filename.absolute] = target_loaded["ast"]
crytic_compile.filenames.add(filename)
crytic_compile.add_filename(filename)
contract_name = target_loaded["contractName"]
crytic_compile.contracts_filenames[contract_name] = filename
crytic_compile.contracts_names.add(contract_name)
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/etherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):

for path, info in targets_json["sources"].items():
path = convert_filename(path, _relative_to_short, crytic_compile)
crytic_compile.filenames.add(path)
crytic_compile.add_filename(path)
crytic_compile.asts[path.absolute] = info["AST"]

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
path = convert_filename(
path, relative_to_short, crytic_compile, working_dir=solc_working_dir
)
crytic_compile.filenames.add(path)
crytic_compile.add_filename(path)
crytic_compile.asts[path.absolute] = info["AST"]

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/solc_standard_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
path = convert_filename(
path, relative_to_short, crytic_compile, working_dir=solc_working_dir
)
crytic_compile.filenames.add(path)
crytic_compile.add_filename(path)
crytic_compile.asts[path.absolute] = info["ast"]

def _guessed_tests(self) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def load_from_compile(crytic_compile: "CryticCompile", loaded_json: Dict) -> Tup
crytic_compile.dependencies.add(filename.used)

# Set our filenames
crytic_compile.filenames = set(crytic_compile.contracts_filenames.values())
crytic_compile.filenames = crytic_compile.contracts_filenames.values()

crytic_compile.working_dir = loaded_json["working_dir"]

Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/truffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
raise InvalidCompilation(txt)

crytic_compile.asts[filename.absolute] = target_loaded["ast"]
crytic_compile.filenames.add(filename)
crytic_compile.add_filename(filename)
contract_name = target_loaded["contractName"]
crytic_compile.natspec[contract_name] = natspec
crytic_compile.contracts_filenames[contract_name] = filename
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/vyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
crytic_compile.srcmaps_init[contract_name] = []
crytic_compile.srcmaps_runtime[contract_name] = []

crytic_compile.filenames.add(contract_filename)
crytic_compile.add_filename(contract_filename)

# Natspec not yet handled for vyper
crytic_compile.natspec[contract_name] = Natspec({}, {})
Expand Down
2 changes: 1 addition & 1 deletion crytic_compile/platform/waffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str):
contract_name = contract[1]

crytic_compile.asts[filename.absolute] = target_all["sources"][contract[0]]["AST"]
crytic_compile.filenames.add(filename)
crytic_compile.add_filename(filename)
crytic_compile.contracts_filenames[contract_name] = filename
crytic_compile.contracts_names.add(contract_name)
crytic_compile.abis[contract_name] = target_loaded["abi"]
Expand Down