From e2f81d668e56bca3e6c69b604f7878fb03597c43 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Sat, 14 Sep 2019 17:51:09 +0200 Subject: [PATCH 1/2] Add a getter for chain_id This getter will be useful for precondition checks. --- raiden/network/proxies/token_network_registry.py | 5 +++++ .../proxies/test_token_network_registry.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/raiden/network/proxies/token_network_registry.py b/raiden/network/proxies/token_network_registry.py index d8d01e04bd..396972af3e 100644 --- a/raiden/network/proxies/token_network_registry.py +++ b/raiden/network/proxies/token_network_registry.py @@ -26,6 +26,7 @@ from raiden.utils.typing import ( TYPE_CHECKING, Address, + ChainID, BlockSpecification, Dict, T_TargetAddress, @@ -267,3 +268,7 @@ def get_max_token_networks(self, to_block: BlockSpecification) -> int: token network registry. """ return self.proxy.contract.functions.max_token_networks().call(block_identifier=to_block) + + def get_chain_id(self, to_block: BlockSpecification) -> ChainID: + """ Returns the value of chain_id stored in the TokenNetworkRegistry contract """ + return ChainID(self.proxy.contract.functions.chain_id().call(block_identifier=to_block)) diff --git a/raiden/tests/integration/network/proxies/test_token_network_registry.py b/raiden/tests/integration/network/proxies/test_token_network_registry.py index 2891a1386e..19f0be0822 100644 --- a/raiden/tests/integration/network/proxies/test_token_network_registry.py +++ b/raiden/tests/integration/network/proxies/test_token_network_registry.py @@ -132,3 +132,19 @@ def test_token_network_registry_max_token_networks( blockchain_service=blockchain_service, ) assert token_network_registry_proxy.get_max_token_networks(to_block="latest") == UINT256_MAX + + +def test_token_network_registry_chain_id( + deploy_client, token_network_registry_address, contract_manager +): + """ get_chain_id() should return the chain ID """ + blockchain_service = BlockChainService( + jsonrpc_client=deploy_client, contract_manager=contract_manager + ) + token_network_registry_proxy = TokenNetworkRegistry( + jsonrpc_client=deploy_client, + registry_address=to_canonical_address(token_network_registry_address), + contract_manager=contract_manager, + blockchain_service=blockchain_service, + ) + assert token_network_registry_proxy.get_chain_id(to_block="latest") == int(deploy_client.web3.version.network) From a361a52dd860d5017b35116e52d90e86202b1056 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Sat, 14 Sep 2019 18:02:41 +0200 Subject: [PATCH 2/2] Add checks against chain_id == 0 When TokenNetworkRegistry's createERC20TokenNetwork() tries to deploy a TokenNetwork contract, the constructor of TokenNetwork fails if chain_id stored in TokenNetworkRegistry is zero. This commit adds checks against this case in the proxy of TokenNetworkRegistry. --- raiden/network/proxies/token_network_registry.py | 16 +++++++++++++++- .../proxies/test_token_network_registry.py | 4 +++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/raiden/network/proxies/token_network_registry.py b/raiden/network/proxies/token_network_registry.py index 396972af3e..827d1be825 100644 --- a/raiden/network/proxies/token_network_registry.py +++ b/raiden/network/proxies/token_network_registry.py @@ -26,8 +26,8 @@ from raiden.utils.typing import ( TYPE_CHECKING, Address, - ChainID, BlockSpecification, + ChainID, Dict, T_TargetAddress, TokenAddress, @@ -123,6 +123,7 @@ def add_token( already_registered = self.get_token_network( token_address=token_address, block_identifier=block_identifier ) + chain_id = self.get_chain_id(to_block=block_identifier) except ValueError: # If `block_identifier` has been pruned the checks cannot be performed pass @@ -133,6 +134,13 @@ def add_token( raise BrokenPreconditionError( "The token is already registered in the TokenNetworkRegistry." ) + if chain_id == 0: + raise RaidenUnrecoverableError( + "The TokenNetworkRegistry was deployed for chain_id == 0. " + "But the constructor of TokenNetworkRegistry usually refuses this value. " + "Moreover, the constructor of TokenNetwork will refuse this value." + "Probably the node is talking to a wrong smart contract." + ) return self._add_token( token_address=token_address, @@ -212,6 +220,12 @@ def _add_token( if self.get_token_network(token_address, block): raise RaidenRecoverableError(f"{error_prefix}. Token already registered") + if self.get_chain_id(block) == 0: + raise RaidenUnrecoverableError( + f"{error_prefix}. TokenNetworkRegistry's chain_id changed. " + "Probably the node is talking to a wrong smart contract." + ) + raise RaidenUnrecoverableError(error_prefix) token_network_address = self.get_token_network(token_address, "latest") diff --git a/raiden/tests/integration/network/proxies/test_token_network_registry.py b/raiden/tests/integration/network/proxies/test_token_network_registry.py index 19f0be0822..9dd186acd4 100644 --- a/raiden/tests/integration/network/proxies/test_token_network_registry.py +++ b/raiden/tests/integration/network/proxies/test_token_network_registry.py @@ -147,4 +147,6 @@ def test_token_network_registry_chain_id( contract_manager=contract_manager, blockchain_service=blockchain_service, ) - assert token_network_registry_proxy.get_chain_id(to_block="latest") == int(deploy_client.web3.version.network) + assert token_network_registry_proxy.get_chain_id(to_block="latest") == int( + deploy_client.web3.version.network + )