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

Separate _verify_single_precompiled_checksum() #1304

Merged
merged 3 commits into from
Oct 31, 2019
Merged
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
42 changes: 31 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}"
)
_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 Down Expand Up @@ -184,3 +182,25 @@ def check_runtime_codesize(d: Dict) -> None:
runtime_code_len = len(compilation["bin-runtime"]) // 2
if runtime_code_len > 0x6000:
raise RuntimeError(f"{name}'s runtime code is too big ({runtime_code_len} bytes).")


def _verify_single_precompiled_checksum(
checked_checksums: Dict[str, str], contract_name: str, expected_checksum: str
) -> None:
""" 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 verify_single_precompiled_checksum_on_nonexistent_contract_name() -> None:
""" A functiohn for testing the case where the contract name is not found """
_verify_single_precompiled_checksum(
checked_checksums={}, contract_name="a", expected_checksum="abc"
)
6 changes: 6 additions & 0 deletions raiden_contracts/tests/test_contracts_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ContractSourceManager,
ContractSourceManagerVerificationError,
contracts_source_path,
verify_single_precompiled_checksum_on_nonexistent_contract_name,
)


Expand Down Expand Up @@ -256,3 +257,8 @@ def test_contract_source_manager_constructor_with_wrong_type() -> None:
""" ConstructSourceManager's constructor raises TypeError on a wrong kind of argument """
with pytest.raises(TypeError):
ContractSourceManager(None) # type: ignore


def test_verify_single_precompiled_cyhecksum_on_nonexistent_contract_name() -> None:
with pytest.raises(ContractSourceManagerVerificationError, match="No checksum for"):
verify_single_precompiled_checksum_on_nonexistent_contract_name()
2 changes: 1 addition & 1 deletion raiden_contracts/tests/test_deploy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_verify_existent_deployment(token_network_registry_contract: Contract) -


def test_verify_existent_deployment_with_wrong_code(
token_network_registry_contract: Contract
token_network_registry_contract: Contract,
) -> None:
""" Test verify_deployed_contracts_in_filesystem() with an existent deployment data

Expand Down
4 changes: 2 additions & 2 deletions raiden_contracts/tests/test_deploy_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ def test_verify_script(mock_verify: MagicMock) -> None:


def test_verify_monitoring_service_deployment_with_wrong_first_constructor_arg(
token_network_registry_contract: Contract
token_network_registry_contract: Contract,
) -> None:
mock_token = MagicMock()
mock_token.call.return_value = EMPTY_ADDRESS
Expand All @@ -1380,7 +1380,7 @@ def test_verify_monitoring_service_deployment_with_wrong_first_constructor_arg(


def test_verify_monitoring_service_deployment_with_wrong_onchain_token_address(
token_network_registry_contract: Contract
token_network_registry_contract: Contract,
) -> None:
mock_token = MagicMock()
mock_token.call.return_value = EMPTY_ADDRESS
Expand Down
2 changes: 1 addition & 1 deletion raiden_contracts/utils/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def contracts_version_has_initial_service_deposit(version: Optional[str]) -> boo


def contracts_version_monitoring_service_takes_token_network_registry(
version: Optional[str]
version: Optional[str],
) -> bool:
""" Returns true if the contracts_version's MonitoringService contracts

Expand Down