Skip to content

Commit

Permalink
Merge pull request #1677 from vsmelov/issue-1676-fix-verification-of-…
Browse files Browse the repository at this point in the history
…same-name-files

Issue 1676 fix verification of same name files
  • Loading branch information
iamdefinitelyahuman authored Jan 29, 2024
2 parents 45bd746 + f4e03c0 commit 4b235ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Force using utf-8 for reading contracts
- Fix verification for same named files ([#1677](https://github.com/eth-brownie/brownie/pull/1677))

## [1.19.3](https://github.com/eth-brownie/brownie/tree/v1.19.3) - 2023-01-29
### Added
Expand Down
27 changes: 18 additions & 9 deletions brownie/project/flattener.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ def __init__(
self.dependencies: DefaultDict[str, Set[str]] = defaultdict(set)
self.compiler_settings = compiler_settings
self.contract_name = contract_name
self.contract_file = Path(primary_source_fp).name
self.contract_file = self.path_to_name(primary_source_fp)
self.remappings = remappings

self.traverse(primary_source_fp)

license_search = LICENSE_PATTERN.search(self.sources[Path(primary_source_fp).name])
license_search = LICENSE_PATTERN.search(self.path_to_name(primary_source_fp))
self.license = license_search.group(1) if license_search else "NONE"

@classmethod
def path_to_name(cls, pth: str) -> str:
"""Turn the full-path of every Solidity file to a unique shorten name.
Note, that sometimes there could be several different files with the same name in a project,
so these files should keep uniq name to correct verification.
"""
return 'contracts/' + pth.split('/contracts/')[1]

def traverse(self, fp: str) -> None:
"""Traverse a contract source files dependencies.
Expand All @@ -42,8 +51,9 @@ def traverse(self, fp: str) -> None:
fp: The contract source file to traverse, if it's already been traversed, return early.
"""
# if already traversed file, return early
name = self.path_to_name(fp)
fp_obj = Path(fp)
if fp_obj.name in self.sources:
if name in self.sources:
return

# read in the source file
Expand All @@ -56,18 +66,17 @@ def traverse(self, fp: str) -> None:
# replacement function for re.sub, we just sanitize the path
repl = ( # noqa: E731
lambda m: f'import{m.group("prefix")}'
+ f'"{Path(sanitize(m.group("path"))).name}"'
+ f'{m.group("suffix")}'
+ f'"{self.path_to_name(sanitize(m.group("path")))}"'
+ f'{m.group("suffix")}'
)

self.sources[fp_obj.name] = IMPORT_PATTERN.sub(repl, source)
self.sources[name] = IMPORT_PATTERN.sub(repl, source)
if fp_obj.name not in self.dependencies:
self.dependencies[fp_obj.name] = set()
self.dependencies[name] = set()

# traverse dependency files - can circular imports happen?
for m in IMPORT_PATTERN.finditer(source):
import_path = sanitize(m.group("path"))
self.dependencies[fp_obj.name].add(Path(import_path).name)
self.dependencies[name].add(self.path_to_name(import_path))
self.traverse(import_path)

@property
Expand Down

0 comments on commit 4b235ad

Please sign in to comment.