From 523b35ddb4fc22feaeedd486dfd17d887cc43eec Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Tue, 22 Oct 2019 18:18:13 +0200 Subject: [PATCH 1/3] Separate _verify_single_precompiled_checksum() This commit separates a function that checks one entry of contracts.json. This eases the implementation of #1257. --- raiden_contracts/contract_source_manager.py | 35 ++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/raiden_contracts/contract_source_manager.py b/raiden_contracts/contract_source_manager.py index f680cbae7..c810bc8ee 100644 --- a/raiden_contracts/contract_source_manager.py +++ b/raiden_contracts/contract_source_manager.py @@ -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: @@ -184,3 +182,18 @@ 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}" + ) From 90d23a76ff9239329c694fc38d13cd6398802015 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Wed, 23 Oct 2019 16:41:16 +0200 Subject: [PATCH 2/3] Cover the case contract_name is not found --- raiden_contracts/contract_source_manager.py | 7 +++++++ raiden_contracts/tests/test_contracts_compilation.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/raiden_contracts/contract_source_manager.py b/raiden_contracts/contract_source_manager.py index c810bc8ee..cbaa60902 100644 --- a/raiden_contracts/contract_source_manager.py +++ b/raiden_contracts/contract_source_manager.py @@ -197,3 +197,10 @@ def _verify_single_precompiled_checksum( 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" + ) diff --git a/raiden_contracts/tests/test_contracts_compilation.py b/raiden_contracts/tests/test_contracts_compilation.py index 6d4e3fa2c..aa455b98c 100644 --- a/raiden_contracts/tests/test_contracts_compilation.py +++ b/raiden_contracts/tests/test_contracts_compilation.py @@ -22,6 +22,7 @@ ContractSourceManager, ContractSourceManagerVerificationError, contracts_source_path, + verify_single_precompiled_checksum_on_nonexistent_contract_name, ) @@ -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() From 01fe4511b53e3a29430f62b462912c8a618a1164 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Thu, 31 Oct 2019 12:07:18 +0100 Subject: [PATCH 3/3] Format Pythong using a new black --- raiden_contracts/tests/test_deploy_data.py | 2 +- raiden_contracts/tests/test_deploy_script.py | 4 ++-- raiden_contracts/utils/versions.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/raiden_contracts/tests/test_deploy_data.py b/raiden_contracts/tests/test_deploy_data.py index 582c7cb4d..686420290 100644 --- a/raiden_contracts/tests/test_deploy_data.py +++ b/raiden_contracts/tests/test_deploy_data.py @@ -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 diff --git a/raiden_contracts/tests/test_deploy_script.py b/raiden_contracts/tests/test_deploy_script.py index ca26208c5..e57859c51 100644 --- a/raiden_contracts/tests/test_deploy_script.py +++ b/raiden_contracts/tests/test_deploy_script.py @@ -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 @@ -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 diff --git a/raiden_contracts/utils/versions.py b/raiden_contracts/utils/versions.py index 12996badb..85efee74b 100644 --- a/raiden_contracts/utils/versions.py +++ b/raiden_contracts/utils/versions.py @@ -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