Skip to content

Commit

Permalink
Merge pull request #896 from pirapira/split_store_and_verify
Browse files Browse the repository at this point in the history
Split store_and_verify_deployment_info_services()
  • Loading branch information
pirapira authored May 2, 2019
2 parents b71560b + 493b436 commit 7240b2c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 63 deletions.
66 changes: 27 additions & 39 deletions raiden_contracts/deploy/contract_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def verify_deployed_contracts_in_filesystem(self):
if deployment_data is None:
raise RuntimeError(f'Deployment data cannot be found at {deployment_file_path}')

if self._verify_deployment_data(deployment_data):
if self.verify_deployment_data(deployment_data):
print(
f'Deployment info from {deployment_file_path} has been verified'
'and it is CORRECT.',
Expand All @@ -76,10 +76,10 @@ def verify_deployed_service_contracts_in_filesystem(
if deployment_data is None:
raise RuntimeError(f'Deployment data cannot be found at {deployment_file_path}')

if self._verify_service_contracts_deployment_data(
if self.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=user_deposit_whole_balance_limit,
deployment_data=deployment_data,
deployed_contracts_info=deployment_data,
):
print(
f'Deployment info from {deployment_file_path} has been verified '
Expand All @@ -89,39 +89,27 @@ def verify_deployed_service_contracts_in_filesystem(
def store_and_verify_deployment_info_raiden(
self,
deployed_contracts_info: DeployedContracts,
save_info: bool,
):
if save_info:
self._store_deployment_info(
deployment_info=deployed_contracts_info,
services=False,
)
self.verify_deployed_contracts_in_filesystem()
else:
self._verify_deployment_data(deployed_contracts_info)
self._store_deployment_info(
deployment_info=deployed_contracts_info,
services=False,
)
self.verify_deployed_contracts_in_filesystem()

def store_and_verify_deployment_info_services(
self,
deployed_contracts_info: DeployedContracts,
save_info: bool,
token_address: str,
user_deposit_whole_limit: int,
user_deposit_whole_balance_limit: int,
):
if save_info:
self._store_deployment_info(
services=True,
deployment_info=deployed_contracts_info,
)
self.verify_deployed_service_contracts_in_filesystem(
token_address=token_address,
user_deposit_whole_balance_limit=user_deposit_whole_limit,
)
else:
self._verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=user_deposit_whole_limit,
deployment_data=deployed_contracts_info,
)
self._store_deployment_info(
services=True,
deployment_info=deployed_contracts_info,
)
self.verify_deployed_service_contracts_in_filesystem(
token_address=token_address,
user_deposit_whole_balance_limit=user_deposit_whole_balance_limit,
)

def _store_deployment_info(
self,
Expand All @@ -141,7 +129,7 @@ def _store_deployment_info(
f' has been updated at {deployment_file_path}.',
)

def _verify_deployment_data(
def verify_deployment_data(
self,
deployment_data: DeployedContracts,
):
Expand Down Expand Up @@ -239,29 +227,29 @@ def _verify_deployed_contract(

return contract_instance, contracts[contract_name]['constructor_arguments']

def _verify_service_contracts_deployment_data(
def verify_service_contracts_deployment_data(
self,
token_address: str,
user_deposit_whole_balance_limit: int,
deployment_data: DeployedContracts,
deployed_contracts_info: DeployedContracts,
):
chain_id = int(self.web3.version.network)
assert deployment_data is not None
assert deployed_contracts_info is not None

if self.contract_manager.version_string != deployment_data['contracts_version']:
if self.contract_manager.version_string != deployed_contracts_info['contracts_version']:
raise RuntimeError('Version string mismatch')
if chain_id != deployment_data['chain_id']:
if chain_id != deployed_contracts_info['chain_id']:
raise RuntimeError('chain_id mismatch')

service_bundle, constructor_arguments = self._verify_deployed_contract(
deployment_data=deployment_data,
deployment_data=deployed_contracts_info,
contract_name=CONTRACT_SERVICE_REGISTRY,
)
assert to_checksum_address(service_bundle.functions.token().call()) == token_address
assert token_address == constructor_arguments[0]

user_deposit, constructor_arguments = self._verify_deployed_contract(
deployment_data=deployment_data,
deployment_data=deployed_contracts_info,
contract_name=CONTRACT_USER_DEPOSIT,
)
assert len(constructor_arguments) == 2
Expand All @@ -272,7 +260,7 @@ def _verify_service_contracts_deployment_data(
assert user_deposit_whole_balance_limit == constructor_arguments[1]

monitoring_service, constructor_arguments = self._verify_deployed_contract(
deployment_data,
deployed_contracts_info,
CONTRACT_MONITORING_SERVICE,
)
assert len(constructor_arguments) == 3
Expand All @@ -290,7 +278,7 @@ def _verify_service_contracts_deployment_data(
assert user_deposit.address == constructor_arguments[2]

one_to_n, constructor_arguments = self._verify_deployed_contract(
deployment_data=deployment_data,
deployment_data=deployed_contracts_info,
contract_name=CONTRACT_ONE_TO_N,
)
assert to_checksum_address(
Expand Down
15 changes: 15 additions & 0 deletions raiden_contracts/tests/test_deploy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,18 @@ def test_verify_nonexistent_deployment(
token_address=FAKE_ADDRESS,
user_deposit_whole_balance_limit=user_deposit_whole_balance_limit,
)


def test_verify_existent_deployment():
""" Test verify_deployed_contracts_in_filesystem() with an existent deployment data. """
web3_mock = Mock()
web3_mock.version.network = 42
verifier = ContractVerifier(web3=web3_mock, contracts_version='0.11.1')
# The Mock doesn't return the runtime code, so the code comparison fails.
with pytest.raises(AssertionError):
verifier.verify_deployed_contracts_in_filesystem()
with pytest.raises(AssertionError):
verifier.verify_deployed_service_contracts_in_filesystem(
token_address='0x3Aa761BcDB064179a1e37748D8A5F577a177Be5c',
user_deposit_whole_balance_limit=2 ** 256 - 1,
)
44 changes: 20 additions & 24 deletions raiden_contracts/tests/test_deploy_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ def test_deploy_script_raiden(
"""
deployed_contracts_info = deployed_raiden_info

deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployment_data=deployed_contracts_info,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['contracts_version'] = '0.0.0'
with pytest.raises(RuntimeError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployment_data=deployed_contracts_info_fail,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['chain_id'] = 0
with pytest.raises(RuntimeError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployment_data=deployed_contracts_info_fail,
)

Expand All @@ -155,14 +155,14 @@ def test_deploy_script_raiden(
CONTRACT_ENDPOINT_REGISTRY
]['address'] = EMPTY_ADDRESS
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY]['address'] = EMPTY_ADDRESS
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

Expand All @@ -171,28 +171,28 @@ def test_deploy_script_raiden(
CONTRACT_TOKEN_NETWORK_REGISTRY
]['address'] = EMPTY_ADDRESS
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['contracts'][CONTRACT_ENDPOINT_REGISTRY]['block_number'] = 0
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY]['block_number'] = 0
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail['contracts'][CONTRACT_TOKEN_NETWORK_REGISTRY]['block_number'] = 0
with pytest.raises(AssertionError):
deployer._verify_deployment_data(
deployer.verify_deployment_data(
deployed_contracts_info_fail,
)

Expand Down Expand Up @@ -447,28 +447,28 @@ def test_deploy_script_service(
deposit_limit = token_supply // 2

deployed_service_contracts = deployed_service_info
deployer._verify_service_contracts_deployment_data(
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
deployment_data=deployed_service_contracts,
deployed_contracts_info=deployed_service_contracts,
)

deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail['contracts_version'] = '0.0.0'
with pytest.raises(RuntimeError):
deployer._verify_service_contracts_deployment_data(
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
deployment_data=deployed_info_fail,
deployed_contracts_info=deployed_info_fail,
)

deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail['chain_id'] = deployed_service_contracts['chain_id'] + 1
with pytest.raises(RuntimeError):
deployer._verify_service_contracts_deployment_data(
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
deployment_data=deployed_info_fail,
deployed_contracts_info=deployed_info_fail,
)

def test_missing_deployment(contract_name):
Expand All @@ -477,10 +477,10 @@ def test_missing_deployment(contract_name):
contract_name
]['address'] = EMPTY_ADDRESS
with pytest.raises(AssertionError):
deployer._verify_service_contracts_deployment_data(
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
deployment_data=deployed_info_fail,
deployed_contracts_info=deployed_info_fail,
)

for contract_name in [
Expand Down Expand Up @@ -534,11 +534,9 @@ def test_store_and_verify_raiden(fs_reload_deployer, deployed_raiden_info, deplo
deployed_contracts_info = deployed_raiden_info
deployer.store_and_verify_deployment_info_raiden(
deployed_contracts_info=deployed_contracts_info,
save_info=False,
)
deployer.store_and_verify_deployment_info_raiden(
deployed_contracts_info=deployed_contracts_info,
save_info=True,
)


Expand All @@ -554,17 +552,15 @@ def test_store_and_verify_services(
version=None,
).parent)
deployed_contracts_info = deployed_service_info
deployer.store_and_verify_deployment_info_services(
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
deployed_contracts_info=deployed_contracts_info,
save_info=False,
user_deposit_whole_limit=DEPOSIT_LIMIT,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
)
deployer.store_and_verify_deployment_info_services(
token_address=token_address,
deployed_contracts_info=deployed_contracts_info,
save_info=True,
user_deposit_whole_limit=DEPOSIT_LIMIT,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
)


Expand Down

0 comments on commit 7240b2c

Please sign in to comment.