Skip to content

Commit

Permalink
Separate _verify_single_precompiled_checksum()
Browse files Browse the repository at this point in the history
This commit separates a function that checks one entry of
contracts.json.  This eases the implementation of raiden-network#1257.
  • Loading branch information
pirapira committed Oct 22, 2019
1 parent 54c5ee0 commit 73aaa34
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions raiden_contracts/contract_source_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,21 @@ def compile_contracts(self, target_path: Path) -> ContractManager:
return ContractManager(target_path)

def verify_precompiled_checksums(self, precompiled_path: Path) -> None:
""" Compare source code checksums with those from a precompiled file """
""" Compare source code checksums with those from a precompiled file
If `contract_name` is None, all contracts checksums and the overall checksum are checked.
"""

# We get the precompiled file data
contracts_precompiled = ContractManager(precompiled_path)

# Compare each contract source code checksum with the one from the precompiled file
for contract, checksum in self.contracts_checksums.items():
try:
# Silence mypy
assert contracts_precompiled.contracts_checksums is not None
precompiled_checksum = contracts_precompiled.contracts_checksums[contract]
except KeyError:
raise ContractSourceManagerVerificationError(f"No checksum for {contract}")
if precompiled_checksum != checksum:
raise ContractSourceManagerVerificationError(
f"checksum of {contract} does not match {precompiled_checksum} != {checksum}"
)
self._verify_single_precompiled_checksum(
checked_checksums=contracts_precompiled.contracts_checksums,
contract_name=contract,
expected_checksum=checksum,
)

# Compare the overall source code checksum with the one from the precompiled file
if self.overall_checksum != contracts_precompiled.overall_checksum:
Expand All @@ -127,6 +125,20 @@ def verify_precompiled_checksums(self, precompiled_path: Path) -> None:
f"{self.overall_checksum} != {contracts_precompiled.overall_checksum}"
)

def _verify_single_precompiled_checksum(
self, checked_checksums: Dict[str, str], contract_name: str, expected_checksum: str
):
""" Get `checked_checksums[contract_name]` and compare it against `expected_checksum` """
try:
precompiled_checksum = checked_checksums[contract_name]
except KeyError:
raise ContractSourceManagerVerificationError(f"No checksum for {contract_name}")
if precompiled_checksum != expected_checksum:
raise ContractSourceManagerVerificationError(
f"checksum of {contract_name} does not match. got {precompiled_checksum} != "
"expected {expected_checksum}"
)

def _checksum_contracts(self) -> Tuple[Dict[str, str], str]:
""" Compute the checksum of each source, and the overall checksum
Expand Down

0 comments on commit 73aaa34

Please sign in to comment.