Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Set correct state when creating task with no funds #5137

Merged
merged 4 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions golem/ethereum/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def single_currency(
])

def __str__(self) -> str:
return "Not enough funds available.\n" + self._missing_funds_to_str()
return "Not enough funds available." + self._missing_funds_to_str()

def to_dict(self) -> dict:
err_dict = super().to_dict()
Expand All @@ -54,15 +54,15 @@ def to_dict(self) -> dict:
def _missing_funds_to_str(self) -> str:
res = ""
for required, available, currency in self.missing_funds:
res += f"Required {currency}: {required / denoms.ether:f}, " \
f"available: {available / denoms.ether:f}\n"
res += f" Required {currency}: {required / denoms.ether:f}, " \
f"available: {available / denoms.ether:f}."

return res


class NotEnoughDepositFunds(NotEnoughFunds):
def __str__(self) -> str:
return "Not enough funds for Concent deposit." \
return "Not enough funds for Concent deposit. " \
"Top up your account or create the task with Concent disabled." \
+ self._missing_funds_to_str()

Expand Down
28 changes: 20 additions & 8 deletions golem/task/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,19 @@ def enqueue_new_task(client, task, force=False) \
return task


def _create_task_error(e, _self, task_dict, *args, **_kwargs) \
-> typing.Tuple[None, typing.Union[str, typing.Dict]]:
def _create_task_error(
e: Exception,
self: 'ClientProvider',
task_dict: dict,
*_args,
**kwargs
) -> typing.Tuple[None, typing.Union[str, typing.Dict]]:
logger.error("Cannot create task %r: %s", task_dict, e)

task_id = kwargs.get('task_id')
if task_id:
self.task_manager.task_creation_failed(task_id, str(e))

if hasattr(e, 'to_dict'):
return None, rpc_utils.int_to_string(e.to_dict())

Expand Down Expand Up @@ -528,12 +537,15 @@ def _create_legacy_task(
task = _create_task(self.client, task_dict)
task_id = task.header.task_id

self._validate_enough_funds_to_pay_for_task(
task.subtask_price,
task.get_total_tasks(),
task.header.concent_enabled,
force
)
try:
self._validate_enough_funds_to_pay_for_task(
task.subtask_price,
task.get_total_tasks(),
task.header.concent_enabled,
force
)
except eth_exceptions.NotEnoughFunds as e:
return _create_task_error(e, self, task_dict, task_id=task_id)

# Fire and forget the next steps after create_task
deferred = _prepare_task(client=self.client, task=task, force=force)
Expand Down
3 changes: 2 additions & 1 deletion golem/task/taskstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def is_active(self) -> bool:
TaskStatus.finished,
TaskStatus.aborted,
TaskStatus.timeout,
TaskStatus.restarted
TaskStatus.restarted,
TaskStatus.errorCreating
]


Expand Down
10 changes: 5 additions & 5 deletions tests/golem/task/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def test_create_task_fail_if_not_enough_gnt_available(self, mocked, *_):
error = result[1]
# noqa pylint:disable=unsubscriptable-object
self.assertEqual(error['error_type'], 'NotEnoughFunds')
self.assertEqual(error['error_msg'], 'Not enough funds available.\n'
self.assertEqual(error['error_msg'], 'Not enough funds available. '
'Required GNT: '
'0.166667, available: 0.000000\n')
'0.166667, available: 0.000000.')

@mock.patch('golem.task.rpc.ClientProvider.'
'_validate_enough_funds_to_pay_for_task')
Expand Down Expand Up @@ -277,11 +277,11 @@ def test_validate_lock_funds_possibility_raises_if_not_enough_funds(self):
subtask_price=required_gnt,
subtask_count=1
)
expected = f'Not enough funds available.\n' \
expected = f'Not enough funds available. ' \
f'Required GNT: {required_gnt / denoms.ether:f}, ' \
f'available: {available_gnt / denoms.ether:f}\n' \
f'available: {available_gnt / denoms.ether:f}. ' \
f'Required ETH: {required_eth / denoms.ether:f}, ' \
f'available: {available_eth / denoms.ether:f}\n'
f'available: {available_eth / denoms.ether:f}. '
self.assertIn(str(e.exception), expected)


Expand Down