Skip to content
Closed
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
19 changes: 19 additions & 0 deletions raiden/network/proxies/token_network_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TYPE_CHECKING,
Address,
BlockSpecification,
ChainID,
Dict,
T_TargetAddress,
TokenAddress,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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
)