diff --git a/raiden/network/proxies/token_network_registry.py b/raiden/network/proxies/token_network_registry.py index d8d01e04bd..827d1be825 100644 --- a/raiden/network/proxies/token_network_registry.py +++ b/raiden/network/proxies/token_network_registry.py @@ -27,6 +27,7 @@ TYPE_CHECKING, Address, BlockSpecification, + ChainID, Dict, T_TargetAddress, TokenAddress, @@ -122,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 @@ -132,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, @@ -211,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") @@ -267,3 +282,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..9dd186acd4 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,21 @@ 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 + )