Skip to content

Commit

Permalink
fix integrity sum check and small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Dec 10, 2024
1 parent 42eb3e9 commit 58c3064
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
37 changes: 20 additions & 17 deletions vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,41 @@ def vyper_module(self):
_, ast = self._generate_ast
return ast

def _compute_integrity_sum(self, imports_integrity_sum: str) -> str:
if self.storage_layout_override is not None:
layout_sum = sha256sum(json.dumps(self.storage_layout_override))
return sha256sum(layout_sum + imports_integrity_sum)
return imports_integrity_sum

@cached_property
def _resolve_imports(self):
# deepcopy so as to not interfere with `-f ast` output
vyper_module = copy.deepcopy(self.vyper_module)
with self.input_bundle.search_path(Path(vyper_module.resolved_path).parent):
return vyper_module, resolve_imports(vyper_module, self.input_bundle)
imports = resolve_imports(vyper_module, self.input_bundle)

@cached_property
def resolved_imports(self):
imports = self._resolve_imports[1]
# check integrity sum
integrity_sum = self._compute_integrity_sum(imports._integrity_sum)

expected = self.expected_integrity_sum

if expected is not None and imports.integrity_sum != expected:
if expected is not None and integrity_sum != expected:
# warn for now. strict/relaxed mode was considered but it costs
# interface and testing complexity to add another feature flag.
vyper_warn(
f"Mismatched integrity sum! Expected {expected}"
f" but got {imports.integrity_sum}."
f" but got {integrity_sum}."
" (This likely indicates a corrupted archive)"
)

return imports
return vyper_module, imports, integrity_sum

@cached_property
def integrity_sum(self):
return self._resolve_imports[2]

@cached_property
def resolved_imports(self):
return self._resolve_imports[1]

@cached_property
def _annotate(self) -> tuple[natspec.NatspecOutput, vy_ast.Module]:
Expand Down Expand Up @@ -285,15 +297,6 @@ def blueprint_bytecode(self) -> bytes:

return deploy_bytecode + blueprint_bytecode

@cached_property
def integrity_sum(self) -> str:
if self.storage_layout_override:
return sha256sum(
sha256sum(json.dumps(self.storage_layout_override))
+ self.resolved_imports.integrity_sum
)
return self.resolved_imports.integrity_sum


def generate_ir_nodes(global_ctx: ModuleT, settings: Settings) -> tuple[IRnode, IRnode]:
"""
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/analysis/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def __init__(self, input_bundle: InputBundle, graph: _ImportGraph):

self.seen: set[int] = set()

self.integrity_sum = None
self._integrity_sum = None

def resolve_imports(self, module_ast: vy_ast.Module):
self._resolve_imports_r(module_ast)
self.integrity_sum = self._calculate_integrity_sum_r(module_ast)
self._integrity_sum = self._calculate_integrity_sum_r(module_ast)

def _calculate_integrity_sum_r(self, module_ast: vy_ast.Module):
acc = [sha256sum(module_ast.full_source_code)]
Expand Down

0 comments on commit 58c3064

Please sign in to comment.