Skip to content

Commit

Permalink
feat: rename secondary node's transfer submission function/task
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslevonyak committed Jun 28, 2024
1 parent 9ffc5e3 commit 4a82d0f
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 109 deletions.
19 changes: 10 additions & 9 deletions pantos/validatornode/business/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def get_validator_nonce(self, source_blockchain: Blockchain,
source_blockchain=source_blockchain,
source_transaction_id=source_transaction_id)

def submit_transfer_offchain(self, internal_transfer_id: int,
transfer: CrossChainTransfer) -> bool:
def submit_transfer_to_primary_node(self, internal_transfer_id: int,
transfer: CrossChainTransfer) -> bool:
"""Submit the signature for a cross-chain token transfer after
its successful validation to the primary validator node.
Expand Down Expand Up @@ -436,8 +436,8 @@ def validate_transfer(self, internal_transfer_id: int,
submit_transfer_onchain_task.delay(internal_transfer_id,
transfer.to_dict())
else:
submit_transfer_offchain_task.delay(internal_transfer_id,
transfer.to_dict())
submit_transfer_to_primary_node_task.delay(
internal_transfer_id, transfer.to_dict())
return True
except TransferInteractor.__TransferValidationError as error:
return error.is_permanent()
Expand Down Expand Up @@ -795,7 +795,7 @@ def confirm_transfer_task(self, internal_transfer_id: int,


@celery_app.task(bind=True, max_retries=None)
def submit_transfer_offchain_task(
def submit_transfer_to_primary_node_task(
self, internal_transfer_id: int,
transfer_dict: CrossChainTransferDict) -> bool:
"""Celery task for submitting the signature for a cross-chain token
Expand All @@ -817,20 +817,21 @@ def submit_transfer_offchain_task(
"""
transfer = CrossChainTransfer.from_dict(transfer_dict)
try:
submission_completed = TransferInteractor().submit_transfer_offchain(
internal_transfer_id, transfer)
submission_completed = \
TransferInteractor().submit_transfer_to_primary_node(
internal_transfer_id, transfer)
except Exception as error:
_logger.error(
'unable to submit the signature for a token transfer to the '
'primary validator node', extra=vars(transfer) | {
'internal_transfer_id': internal_transfer_id,
'task_id': self.request.id
}, exc_info=True)
retry_interval = config['tasks']['submit_transfer_offchain'][
retry_interval = config['tasks']['submit_transfer_to_primary_node'][
'retry_interval_after_error_in_seconds']
raise self.retry(countdown=retry_interval, exc=error)
if not submission_completed:
retry_interval = config['tasks']['submit_transfer_offchain'][
retry_interval = config['tasks']['submit_transfer_to_primary_node'][
'retry_interval_in_seconds']
raise self.retry(countdown=retry_interval)
return True
Expand Down
2 changes: 1 addition & 1 deletion pantos/validatornode/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@
'required': True,
'schema': {
'confirm_transfer': _VALIDATION_SCHEMA_TASK,
'submit_transfer_offchain': _VALIDATION_SCHEMA_TASK,
'submit_transfer_onchain': _VALIDATION_SCHEMA_TASK,
'submit_transfer_to_primary_node': _VALIDATION_SCHEMA_TASK,
'validate_transfer': _VALIDATION_SCHEMA_TASK
}
},
Expand Down
59 changes: 0 additions & 59 deletions tests/business/transfers/test_submit_transfer_offchain.py

This file was deleted.

58 changes: 58 additions & 0 deletions tests/business/transfers/test_submit_transfer_to_primary_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest.mock

import celery.exceptions # type: ignore
import pytest

from pantos.validatornode.business.transfers import TransferInteractorError
from pantos.validatornode.business.transfers import \
submit_transfer_to_primary_node_task


@pytest.mark.parametrize('submission_completed', [True, False])
@unittest.mock.patch(
'pantos.validatornode.business.transfers.config', {
'tasks': {
'submit_transfer_to_primary_node': {
'retry_interval_in_seconds': 120
}
}
})
@unittest.mock.patch(
'pantos.validatornode.business.transfers.TransferInteractor')
def test_submit_transfer_to_primary_node_task_correct(
mock_transfer_interactor, submission_completed, internal_transfer_id,
cross_chain_transfer, cross_chain_transfer_dict):
mock_transfer_interactor().submit_transfer_to_primary_node.return_value = \
submission_completed
if submission_completed:
submit_transfer_to_primary_node_task(internal_transfer_id,
cross_chain_transfer_dict)
else:
with pytest.raises(celery.exceptions.Retry):
submit_transfer_to_primary_node_task(internal_transfer_id,
cross_chain_transfer_dict)
mock_transfer_interactor().submit_transfer_to_primary_node.\
assert_called_once_with(internal_transfer_id, cross_chain_transfer)


@unittest.mock.patch(
'pantos.validatornode.business.transfers.config', {
'tasks': {
'submit_transfer_to_primary_node': {
'retry_interval_after_error_in_seconds': 300
}
}
})
@unittest.mock.patch(
'pantos.validatornode.business.transfers.TransferInteractor')
def test_submit_transfer_to_primary_node_task_error(mock_transfer_interactor,
internal_transfer_id,
cross_chain_transfer,
cross_chain_transfer_dict):
mock_transfer_interactor().submit_transfer_to_primary_node.side_effect = \
TransferInteractorError('')
with pytest.raises(TransferInteractorError):
submit_transfer_to_primary_node_task(internal_transfer_id,
cross_chain_transfer_dict)
mock_transfer_interactor().submit_transfer_to_primary_node.\
assert_called_once_with(internal_transfer_id, cross_chain_transfer)
71 changes: 39 additions & 32 deletions tests/business/transfers/test_validate_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@pytest.mark.parametrize('recipient_address_valid', [True, False])
@pytest.mark.parametrize('in_other_source_transaction', [True, False])
@pytest.mark.parametrize('is_primary_node', [True, False])
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
Expand All @@ -26,8 +26,9 @@
@unittest.mock.patch('pantos.validatornode.business.base.config')
def test_validate_transfer_confirmed_correct(
mock_config, mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
is_primary_node, in_other_source_transaction, recipient_address_valid,
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, is_primary_node,
in_other_source_transaction, recipient_address_valid,
destination_token_active, external_token_address_correct,
token_decimals_correct, transfer_interactor, internal_transfer_id,
cross_chain_transfer):
Expand Down Expand Up @@ -69,24 +70,26 @@ def test_validate_transfer_confirmed_correct(
if is_primary_node:
mock_submit_transfer_onchain_task.delay.assert_called_once_with(
internal_transfer_id, transfer_in_source_transaction.to_dict())
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()
else:
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_called_once_with(
internal_transfer_id, transfer_in_source_transaction.to_dict())
mock_submit_transfer_to_primary_node_task.delay.\
assert_called_once_with(internal_transfer_id,
transfer_in_source_transaction.to_dict())


@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.get_blockchain_client')
def test_validate_transfer_unconfirmed_correct(
mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
transfer_interactor, internal_transfer_id, cross_chain_transfer):
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, transfer_interactor,
internal_transfer_id, cross_chain_transfer):
_initialize_mock_blockchain_client(mock_get_blockchain_client,
TransactionStatus.UNCONFIRMED,
cross_chain_transfer, True, True, True,
Expand All @@ -99,20 +102,21 @@ def test_validate_transfer_unconfirmed_correct(
mock_database_access.update_reversal_transfer.assert_not_called()
mock_database_access.update_transfer_status.assert_not_called()
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()


@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.get_blockchain_client')
def test_validate_transfer_reverted_correct(
mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
transfer_interactor, internal_transfer_id, cross_chain_transfer):
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, transfer_interactor,
internal_transfer_id, cross_chain_transfer):
_initialize_mock_blockchain_client(mock_get_blockchain_client,
TransactionStatus.REVERTED,
cross_chain_transfer, True, True, True,
Expand All @@ -126,20 +130,21 @@ def test_validate_transfer_reverted_correct(
mock_database_access.update_transfer_status.assert_called_once_with(
internal_transfer_id, TransferStatus.SOURCE_TRANSACTION_REVERTED)
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()


@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.get_blockchain_client')
def test_validate_transfer_invalid_correct(
mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
transfer_interactor, internal_transfer_id, cross_chain_transfer):
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, transfer_interactor,
internal_transfer_id, cross_chain_transfer):
_initialize_mock_blockchain_client(mock_get_blockchain_client,
TransactionStatus.CONFIRMED,
cross_chain_transfer, True, False, True,
Expand All @@ -153,20 +158,21 @@ def test_validate_transfer_invalid_correct(
mock_database_access.update_transfer_status.assert_called_once_with(
internal_transfer_id, TransferStatus.SOURCE_TRANSACTION_INVALID)
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()


@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.get_blockchain_client')
def test_validate_transfer_not_found_in_transaction_error(
mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
transfer_interactor, internal_transfer_id, cross_chain_transfer):
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, transfer_interactor,
internal_transfer_id, cross_chain_transfer):
_initialize_mock_blockchain_client(mock_get_blockchain_client,
TransactionStatus.CONFIRMED, None, True,
True, True, True, True)
Expand All @@ -181,20 +187,21 @@ def test_validate_transfer_not_found_in_transaction_error(
mock_database_access.update_reversal_transfer.assert_not_called()
mock_database_access.update_transfer_status.assert_not_called()
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()


@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_offchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.'
'submit_transfer_to_primary_node_task')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.submit_transfer_onchain_task')
@unittest.mock.patch('pantos.validatornode.business.transfers.database_access')
@unittest.mock.patch(
'pantos.validatornode.business.transfers.get_blockchain_client')
def test_validate_transfer_other_error(
mock_get_blockchain_client, mock_database_access,
mock_submit_transfer_onchain_task, mock_submit_transfer_offchain_task,
transfer_interactor, internal_transfer_id, cross_chain_transfer):
mock_submit_transfer_onchain_task,
mock_submit_transfer_to_primary_node_task, transfer_interactor,
internal_transfer_id, cross_chain_transfer):
mock_get_blockchain_client.side_effect = Exception

with pytest.raises(TransferInteractorError) as exception_info:
Expand All @@ -207,7 +214,7 @@ def test_validate_transfer_other_error(
mock_database_access.update_reversal_transfer.assert_not_called()
mock_database_access.update_transfer_status.assert_not_called()
mock_submit_transfer_onchain_task.delay.assert_not_called()
mock_submit_transfer_offchain_task.delay.assert_not_called()
mock_submit_transfer_to_primary_node_task.delay.assert_not_called()


@pytest.mark.parametrize('validation_completed', [True, False])
Expand Down
4 changes: 2 additions & 2 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
confirm_transfer:
retry_interval_in_seconds: 60
retry_interval_after_error_in_seconds: 300
submit_transfer_offchain:
submit_transfer_onchain:
retry_interval_in_seconds: 60
retry_interval_after_error_in_seconds: 300
submit_transfer_onchain:
submit_transfer_to_primary_node:
retry_interval_in_seconds: 60
retry_interval_after_error_in_seconds: 300
validate_transfer:
Expand Down
6 changes: 3 additions & 3 deletions validator-node-config.env
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ CELERY_BACKEND=db+postgresql://pantos-validator-node:${DATABASE_PASSWORD}@localh
##### Section: confirm_transfer #####
# TASKS_CONFIRM_TRANSFER_INTERVAL=
# TASKS_CONFIRM_TRANSFER_RETRY_INTERVAL_AFTER_ERROR=
##### Section: submit_transfer_offchain #####
# TASKS_SUBMIT_TRANSFER_OFFCHAIN_RETRY_INTERVAL=
# TASKS_SUBMIT_TRANSFER_OFFCHAIN_RETRY_INTERVAL_AFTER_ERROR=
##### Section: submit_transfer_onchain #####
# TASKS_SUBMIT_TRANSFER_ONCHAIN_RETRY_INTERVAL=
# TASKS_SUBMIT_TRANSFER_ONCHAIN_RETRY_INTERVAL_AFTER_ERROR=
##### Section: submit_transfer_to_primary_node #####
# TASKS_SUBMIT_TRANSFER_TO_PRIMARY_NODE_RETRY_INTERVAL=
# TASKS_SUBMIT_TRANSFER_TO_PRIMARY_NODE_RETRY_INTERVAL_AFTER_ERROR=
##### Section: validate_transfer #####
# TASKS_VALIDATE_TRANSFER_RETRY_INTERVAL=
# TASKS_VALIDATE_TRANSFER_RETRY_INTERVAL_AFTER_ERROR=
Expand Down
Loading

0 comments on commit 4a82d0f

Please sign in to comment.