Skip to content

Commit

Permalink
Treat trying to deposit on a closed channel as recoverable error
Browse files Browse the repository at this point in the history
Maybe fix #4451

If a channel is not open, or is in a closing state (still open but
waiting on the closing transaction result) the `set_total_deposit`
function was throwing a `ValueError`.

That is a valid and recoverable race condition so this commit
introduces an appropriate error and handles it in all places where it
can be thrown.
  • Loading branch information
LefterisJP committed Sep 3, 2019
1 parent 4a503c0 commit 49b61dc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions raiden/api/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from raiden.constants import GENESIS_BLOCK_NUMBER, UINT256_MAX
from raiden.exceptions import (
AlreadyRegisteredTokenAddress,
ChannelNotOpenError,
DepositMismatch,
DepositOverLimit,
DuplicatedChannelError,
Expand Down Expand Up @@ -571,6 +572,7 @@ def set_total_channel_deposit(
AddressWithoutCode: The channel was settled during the deposit
execution.
DepositOverLimit: The total deposit amount is higher than the limit.
ChannelNotOpenError: The channel is no longer in an open state.
"""
chain_state = views.state_from_raiden(self.raiden)

Expand Down Expand Up @@ -642,8 +644,7 @@ def set_total_channel_deposit(
is_channel_open = channel.get_status(channel_state) == ChannelState.STATE_OPENED

if not is_channel_open:
msg = "Channel is not in an open state."
raise ValueError(msg)
raise ChannelNotOpenError("Channel is not in an open state.")

if safety_deprecation_switch:
msg = (
Expand Down
5 changes: 4 additions & 1 deletion raiden/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
AddressWithoutCode,
AlreadyRegisteredTokenAddress,
APIServerPortInUseError,
ChannelNotOpenError,
DepositMismatch,
DepositOverLimit,
DuplicatedChannelError,
Expand Down Expand Up @@ -646,7 +647,7 @@ def open(
return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED)
except (NonexistingChannel, UnknownTokenAddress) as e:
return api_error(errors=str(e), status_code=HTTPStatus.BAD_REQUEST)
except (DepositOverLimit, DepositMismatch) as e:
except (DepositOverLimit, DepositMismatch, ChannelNotOpenError) as e:
return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)

channel_state = views.get_channelstate_for(
Expand Down Expand Up @@ -1092,6 +1093,8 @@ def _deposit(
return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
except TokenNetworkDeprecated as e:
return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
except ChannelNotOpenError as e:
return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)

updated_channel_state = self.raiden_api.get_channel(
registry_address, channel_state.token_address, channel_state.partner_state.address
Expand Down
2 changes: 2 additions & 0 deletions raiden/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from raiden.api.python import RaidenAPI
from raiden.constants import Environment
from raiden.exceptions import (
ChannelNotOpenError,
DepositMismatch,
DepositOverLimit,
DuplicatedChannelError,
Expand Down Expand Up @@ -41,6 +42,7 @@
InsufficientFunds,
RaidenRecoverableError,
TransactionThrew,
ChannelNotOpenError,
)


Expand Down
4 changes: 4 additions & 0 deletions raiden/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ class DuplicatedChannelError(RaidenRecoverableError):
"""Raised if someone tries to create a channel that already exists."""


class ChannelNotOpenError(RaidenRecoverableError):
"""Raised if someone tries to work on a channel that is no longer in an open state."""


class ContractCodeMismatch(RaidenError):
"""Raised if the onchain code of the contract differs."""

Expand Down

0 comments on commit 49b61dc

Please sign in to comment.