Skip to content

Commit

Permalink
Merge pull request #125 from skalenetwork/enhancement/SKALE-2244-hand…
Browse files Browse the repository at this point in the history
…le-transaction-error-in-transaction-method

SKALE-2244 Handle transaction error in transaction method
  • Loading branch information
dmytrotkk authored Apr 7, 2020
2 parents 597204b + c523b81 commit 5c7a5c3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions scripts/deploy_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ docker rm -f ganache || true
docker network create testnet || true

docker run -d --network testnet -p 8545:8545 -p 8546:8546 \
--name ganache trufflesuite/ganache-cli:v6.8.1-beta.0 \
--account="0x${ETH_PRIVATE_KEY},100000000000000000000000000" -l 80000000
--name ganache trufflesuite/ganache-cli:beta \
--account="0x${ETH_PRIVATE_KEY},100000000000000000000000000" -l 80000000 -b 1

docker pull skalenetwork/skale-manager:$MANAGER_BRANCH-latest
docker run \
Expand Down
8 changes: 2 additions & 6 deletions skale/contracts/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from web3 import Web3

from skale.transactions.tools import post_transaction, make_call
from skale.utils.web3_utils import (TransactionFailedError,
wait_for_receipt_by_blocks)
from skale.utils.web3_utils import wait_for_receipt_by_blocks


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -69,10 +68,7 @@ def wrapper(self, *args, wait_for=False, timeout=4, blocks_to_wait=50, retries=1
return tx_res
else:
return tx_res
raise TransactionFailedError(
f'transaction_method failed: {transaction.__name__}, '
f'receipt: {tx_res.receipt}'
)
return tx_res
return wrapper
return real_decorator

Expand Down
2 changes: 1 addition & 1 deletion skale/contracts/delegation/validator_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def ls(self, trusted_only=False):
for val_id in self.get_trusted_validator_ids()
] if trusted_only else [
self.get_with_id(val_id)
for val_id in range(1, number_of_validators+1)
for val_id in range(1, number_of_validators + 1)
]
return validators

Expand Down
9 changes: 9 additions & 0 deletions skale/dataclasses/tx_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# along with SKALE.py. If not, see <https://www.gnu.org/licenses/>.


class TransactionFailedError(Exception):
pass


class TxRes():
def __init__(self, tx_hash=None, data=None, receipt=None):
self._hash = tx_hash
Expand All @@ -39,3 +43,8 @@ def receipt(self):
@receipt.setter
def receipt(self, receipt: dict) -> None:
self._receipt = receipt

def raise_for_status(self) -> None:
if self._receipt and self._receipt['status'] != 1:
raise TransactionFailedError(f'Transaction failed with receipt '
f'{self._receipt}')
4 changes: 0 additions & 4 deletions skale/utils/web3_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
logger = logging.getLogger(__name__)


class TransactionFailedError(Exception):
pass


WS_MAX_MESSAGE_DATA_BYTES = 5 * 1024 * 1024


Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/base_contract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_dry_run(skale):
active_node_ids_after = skale.nodes_data.get_active_node_ids()
assert len(active_node_ids_after) == len(active_node_ids_before)

tx_res = skale.manager.create_node(ip, port, name, dry_run=False)
tx_res = skale.manager.create_node(ip, port, name, dry_run=False, wait_for=True)

assert tx_res.hash is not None

Expand Down
21 changes: 12 additions & 9 deletions tests/contracts/delegation/validator_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import pytest

from skale.contracts.delegation.validator_service import FIELDS
from skale.wallets.web3_wallet import generate_wallet
from skale.dataclasses.tx_res import TransactionFailedError
from skale.utils.web3_utils import check_receipt
from skale.utils.account_tools import send_ether
from skale.wallets.web3_wallet import generate_wallet

from tests.constants import (
NOT_EXISTING_ID, D_VALIDATOR_ID, D_VALIDATOR_NAME, D_VALIDATOR_DESC,
Expand Down Expand Up @@ -157,14 +158,16 @@ def test_is_validator_trusted(skale):


def test_register_existing_validator(skale):
with pytest.raises(ValueError):
skale.validator_service.register_validator(
name=D_VALIDATOR_NAME,
description=D_VALIDATOR_DESC,
fee_rate=D_VALIDATOR_FEE,
min_delegation_amount=D_VALIDATOR_MIN_DEL,
wait_for=True
)
tx_res = skale.validator_service.register_validator(
name=D_VALIDATOR_NAME,
description=D_VALIDATOR_DESC,
fee_rate=D_VALIDATOR_FEE,
min_delegation_amount=D_VALIDATOR_MIN_DEL,
wait_for=True
)
assert tx_res.receipt['status'] != 1
with pytest.raises(TransactionFailedError):
tx_res.raise_for_status()


def _generate_new_validator(skale):
Expand Down
7 changes: 5 additions & 2 deletions tests/contracts/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from hexbytes import HexBytes

import skale.utils.helper as helper
from skale.dataclasses.tx_res import TransactionFailedError
from skale.utils.constants import GAS
from skale.utils.web3_utils import (private_key_to_public, TransactionFailedError)
from skale.utils.web3_utils import private_key_to_public

from skale.utils.contracts_provision.main import (
generate_random_node_data, generate_random_schain_data
Expand Down Expand Up @@ -248,5 +249,7 @@ def test_create_node_status_0(skale):
'skale.contracts.base_contract.wait_for_receipt_by_blocks',
return_value={'status': 0}
):
tx_res = skale.manager.create_node(ip, port, name, wait_for=True)
assert tx_res.receipt['status'] == 0
with pytest.raises(TransactionFailedError):
skale.manager.create_node(ip, port, name, wait_for=True)
tx_res.raise_for_status()

0 comments on commit 5c7a5c3

Please sign in to comment.