Skip to content

Commit

Permalink
Improve TxRes
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Nov 2, 2023
1 parent 6bf2d82 commit da41525
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
12 changes: 6 additions & 6 deletions skale/transactions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class TransactionWaitError(TimeoutError, TransactionError):
pass


class TransactionLogicError(TransactionError):
class TransactionLogicError(TransactionError, ContractLogicError):
"""
Raised when transaction executed with error
"""
pass


class DryRunFailedError(TransactionError):
class DryRunFailedError(TransactionLogicError):
"""
Raised when error occurred during dry run call
"""
Expand All @@ -60,19 +60,19 @@ class DryRunFailedError(TransactionError):

class TransactionFailedError(TransactionLogicError):
"""
Raised when transaction included in a block failed during execution
Raised when transaction included in the block failed during execution
"""
pass


class RevertError(TransactionLogicError, ContractLogicError):
class TransactionRevertError(TransactionFailedError):
"""
Raised when transaction was included in a block and reverted during execution
Raised when transaction included in the block failed with revert
"""
pass


class RevertDryRunError(DryRunFailedError):
class DryRunRevertError(DryRunFailedError):
"""
Raised when transaction reverted during dry run call
"""
Expand Down
23 changes: 13 additions & 10 deletions skale/transactions/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

from skale.transactions.exceptions import (
DryRunFailedError,
RevertError,
DryRunRevertError,
TransactionRevertError,
TransactionFailedError
)

Expand Down Expand Up @@ -68,15 +69,17 @@ def tx_failed(self) -> bool:
return not is_success_or_not_performed(self.receipt)

def raise_for_status(self) -> None:
# Check if tx was sent
print(self.dry_run_result)
if self.receipt is not None:
# Check if tx errored
if not is_success(self.receipt):
raise TransactionFailedError(self.receipt['error'])
else:
# Check if revert
error_msg = self.receipt['error']
if is_revert_error(self.receipt):
raise TransactionRevertError(error_msg)
else:
raise TransactionFailedError(error_msg)
elif self.dry_run_result is not None and not is_success(self.dry_run_result):
error_msg = self.dry_run_result['message']
if is_revert_error(self.dry_run_result):
raise RevertError(self.dry_run_result['message'])
# Check if dry run errored due to other reason
if not is_success(self.dry_run_result):
raise DryRunFailedError(self.dry_run_result['error'])
raise DryRunRevertError(error_msg)
else:
raise DryRunFailedError(error_msg)
4 changes: 2 additions & 2 deletions tests/manager/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


from tests.constants import NEW_REWARD_PERIOD, NEW_DELTA_PERIOD
from skale.transactions.result import RevertError
from skale.transactions.result import DryRunRevertError


def test_get_set_periods(skale):
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_get_set_latency(skale):
def test_get_set_launch_timestamp(skale):
launch_ts = skale.constants_holder.get_launch_timestamp()
assert isinstance(launch_ts, int)
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.constants_holder.set_launch_timestamp(launch_ts, wait_for=True)


Expand Down
4 changes: 2 additions & 2 deletions tests/manager/delegation/delegation_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from skale.contracts.manager.delegation.delegation_controller import FIELDS
from skale.transactions.exceptions import ContractLogicError
from skale.transactions.result import RevertError
from skale.transactions.result import DryRunRevertError
from skale.utils.contracts_provision.main import _skip_evm_time
from skale.utils.contracts_provision.utils import generate_random_name

Expand Down Expand Up @@ -204,7 +204,7 @@ def test_request_undelegate(skale, validator):
)

# Transaction failed if delegation period is in progress
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
tx_res = skale.delegation_controller.request_undelegation(
delegation_id,
wait_for=True,
Expand Down
8 changes: 4 additions & 4 deletions tests/manager/delegation/validator_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from skale.contracts.manager.delegation.validator_service import FIELDS
from skale.transactions.result import RevertError
from skale.transactions.result import DryRunRevertError
from skale.utils.account_tools import send_eth
from skale.wallets.web3_wallet import generate_wallet
from skale.utils.contracts_provision.main import _skip_evm_time, enable_validator
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_is_accepting_new_requests(skale, validator):


def test_register_existing_validator(skale):
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.validator_service.register_validator(
name=D_VALIDATOR_NAME,
description=D_VALIDATOR_DESC,
Expand Down Expand Up @@ -319,7 +319,7 @@ def test_request_confirm_new_address(skale):
validator = skale.validator_service.get(validator_id)
assert validator['requested_address'] == '0x0000000000000000000000000000000000000000'

with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.validator_service.request_for_new_address(
new_validator_address=main_wallet.address,
wait_for=True
Expand Down Expand Up @@ -402,7 +402,7 @@ def test_revert_reason(skale):
min_delegation_amount=D_VALIDATOR_MIN_DEL,
wait_for=True
)
except RevertError as e:
except DryRunRevertError as e:
assert no_validator_revert in e.message


Expand Down
4 changes: 2 additions & 2 deletions tests/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from unittest.mock import Mock

from skale.wallets.web3_wallet import generate_wallet
from skale.transactions.result import RevertError, TransactionFailedError
from skale.transactions.result import DryRunRevertError, TransactionFailedError

from skale.utils.contracts_provision.main import (
generate_random_node_data, generate_random_schain_data
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_node_exit_with_no_schains(skale, nodes):
def test_failed_node_exit(skale, block_in_seconds):
# block_in_seconds fixuture to return transaction revert in a same way as geth does
not_existed_node_id = 1
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.manager.node_exit(not_existed_node_id,
wait_for=True, gas_limit=TEST_GAS_LIMIT)

Expand Down
14 changes: 6 additions & 8 deletions tests/manager/sync_manager_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from web3.exceptions import ContractLogicError

from skale.transactions.exceptions import RevertError
from skale.transactions.exceptions import DryRunRevertError

import pytest

Expand Down Expand Up @@ -34,27 +32,27 @@ def test_add_get_remove_ip_range(skale, sync_manager_permissions, ip_range, bloc


def test_add_bad_ip_range(skale, sync_manager_permissions, block_in_seconds):
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.sync_manager.add_ip_range('brange', '0.0.0.0', '1.1.1.1')

with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.sync_manager.add_ip_range('brange', '2.2.2.2', '1.1.1.1')

with pytest.raises(OSError):
skale.sync_manager.add_ip_range('brange', '1.1.1.1', '256.256.256.256')


def test_remove_range_bad_params(skale, sync_manager_permissions, block_in_seconds):
with pytest.raises(RevertError):
with pytest.raises(DryRunRevertError):
skale.sync_manager.remove_ip_range('phantom')


def test_get_range_bad_params(skale, sync_manager_permissions, block_in_seconds):
num = skale.sync_manager.get_ip_ranges_number()
# TODO: Make dry run handle revert that has empty reason properly
with pytest.raises(ContractLogicError):
with pytest.raises(DryRunRevertError):
r = skale.sync_manager.get_ip_range_by_index(num)
with pytest.raises(ContractLogicError):
with pytest.raises(DryRunRevertError):
r = skale.sync_manager.get_ip_range_by_index(0)
r = skale.sync_manager.get_ip_range_by_name('phantom')
assert r.start_ip == '0.0.0.0' and r.end_ip == '0.0.0.0'

0 comments on commit da41525

Please sign in to comment.