Skip to content

Commit

Permalink
Merge pull request #146 from skalenetwork/fix/update-for-the-latest-sm
Browse files Browse the repository at this point in the history
SKALE-2455 Update code and tests for the latest SM beta
  • Loading branch information
badrogger authored Apr 23, 2020
2 parents 93a4bb2 + 4c084f0 commit 1c0ffeb
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
ETH_PRIVATE_KEY: ${{ secrets.ETH_PRIVATE_KEY }}
ENDPOINT: ${{ secrets.ENDPOINT }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
MANAGER_TAG: "1.1.1-stable.0"
MANAGER_TAG: "beta-latest"
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
5 changes: 4 additions & 1 deletion skale/contracts/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def transaction_method(gas_limit):
def real_decorator(transaction):
@wraps(transaction)
def wrapper(self, *args, wait_for=False, timeout=4, blocks_to_wait=50, retries=1,
gas_price=None, nonce=None, dry_run=False, **kwargs):
gas_price=None, nonce=None, dry_run=False, raise_for_status=True, **kwargs):
method = transaction(self, *args, **kwargs)
if dry_run:
opts = {
Expand Down Expand Up @@ -66,6 +66,9 @@ def wrapper(self, *args, wait_for=False, timeout=4, blocks_to_wait=50, retries=1
)
if tx_res.receipt['status'] == 1:
return tx_res
else:
if raise_for_status:
tx_res.raise_for_status()
else:
return tx_res
return tx_res
Expand Down
24 changes: 10 additions & 14 deletions skale/contracts/delegation/validator_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from skale.dataclasses.tx_res import TxRes
from skale.utils.constants import GAS


FIELDS = [
'name', 'validator_address', 'requested_address', 'description', 'fee_rate',
'registration_time', 'minimum_delegation_amount', 'trusted'
Expand Down Expand Up @@ -189,28 +190,23 @@ def register_validator(self, name: str, description: str, fee_rate: int,
return self.contract.functions.registerValidator(
name, description, fee_rate, min_delegation_amount)

def get_link_node_signature(self, validator_id: int) -> str:
unsigned_data = Web3.soliditySha3(['uint256'], [validator_id])
unsigned_message = messages.encode_defunct(hexstr=unsigned_data.hex())
signed_message = self.skale.wallet.sign_message(unsigned_message)
return signed_message.signature.hex()

@transaction_method(GAS['link_node_address'])
def link_node_address(self, node_address: str, validator_id: int) -> TxRes:
def link_node_address(self, node_address: str, signature: str) -> TxRes:
"""Link node address to your validator account.
:param node_address: Address of the node to link
:type node_address: str
:param validator_id: ID of the validator to link
:type node_address: int
:param signature: Signature - reuslt of the get_link_node_signature function
:type signature: str
:returns: Transaction results
:rtype: TxRes
"""
unsigned_hash = Web3.soliditySha3(['uint256', 'address'], [validator_id, node_address])
message = messages.encode_defunct(hexstr=unsigned_hash.hex())

signed_message = self.skale.web3.eth.account.sign_message(
message,
private_key=self.skale.wallet._private_key
)
print('unsigned_hash')
print(unsigned_hash)
print("signature =", signed_message.signature.hex())
signature = signed_message.signature.hex()
return self.contract.functions.linkNodeAddress(node_address, signature)

@transaction_method(GAS['unlink_node_address'])
Expand Down
2 changes: 1 addition & 1 deletion skale/utils/contracts_provision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
D_VALIDATOR_NAME = 'test'
D_VALIDATOR_DESC = 'test'
D_VALIDATOR_FEE = 10
D_VALIDATOR_MIN_DEL = 1000
D_VALIDATOR_MIN_DEL = 1000000

D_DELEGATION_PERIOD = 3
D_DELEGATION_INFO = 'test'
Expand Down
12 changes: 8 additions & 4 deletions skale/utils/contracts_provision/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ def setup_validator(skale):
enable_validator(skale)
else:
print('Skipping default validator creation')
delegation_id = skale.delegation_controller._get_delegation_ids_len_by_validator(D_VALIDATOR_ID)
set_test_msr(skale)

delegate_to_validator(skale)
accept_pending_delegation(skale, delegation_id)
delegations = skale.delegation_controller.get_all_delegations_by_validator(D_VALIDATOR_ID)
accept_pending_delegation(skale, delegations[-1]['id'])


def link_address_to_validator(skale):
print('Linking address to validator')
skale.validator_service.link_node_address(
signature = skale.validator_service.get_link_node_signature(D_VALIDATOR_ID)
tx_res = skale.validator_service.link_node_address(
node_address=skale.wallet.address,
signature=signature,
wait_for=True
)
tx_res.raise_for_status()


def skip_delegation_delay(skale, delegation_id):
Expand All @@ -88,7 +92,7 @@ def accept_pending_delegation(skale, delegation_id):

def get_test_delegation_amount(skale):
msr = skale.constants_holder.msr()
return msr * 10
return msr * 30


def set_test_msr(skale):
Expand Down
6 changes: 6 additions & 0 deletions skale/wallets/web3_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def sign(self, tx_dict):
private_key=self._private_key
)

def sign_message(self, unsigned_hash: str):
return self._web3.eth.account.sign_message(
unsigned_hash,
private_key=self._private_key
)

def sign_and_send(self, tx_dict) -> str:
signed_tx = self.sign(tx_dict)
return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex()
Expand Down
7 changes: 3 additions & 4 deletions tests/contracts/delegation/delegation_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ def _delegate_and_activate(skale):
info=D_DELEGATION_INFO,
wait_for=True
)
delegation_ids = skale.delegation_controller._get_delegation_ids_by_validator(
validator_id=D_VALIDATOR_ID
)
delegations = skale.delegation_controller.get_all_delegations_by_validator(D_VALIDATOR_ID)
skale.delegation_controller.accept_pending_delegation(
delegation_ids[-1],
delegations[-1]['id'],
wait_for=True
)
_skip_evm_time(skale.web3, MONTH_IN_SECONDS)
# skale.time_helpers_with_debug.skip_time(MONTH_IN_SECONDS, wait_for=True)


def test_get_delegated_to_validator_now(skale):
Expand Down
58 changes: 27 additions & 31 deletions tests/contracts/delegation/validator_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
)


def link_node_address(skale, wallet):
main_wallet = skale.wallet
skale.wallet = wallet
signature = skale.validator_service.get_link_node_signature(
validator_id=D_VALIDATOR_ID
)
skale.wallet = main_wallet
skale.validator_service.link_node_address(
node_address=wallet.address,
signature=signature,
wait_for=True
)


def test_get_raw_not_exist(skale):
empty_struct = skale.validator_service._ValidatorService__get_raw(NOT_EXISTING_ID)
assert empty_struct[0] == ''
Expand Down Expand Up @@ -51,14 +65,10 @@ def test_get_linked_addresses_by_validator_address(skale):
addresses = skale.validator_service.get_linked_addresses_by_validator_address(
address=skale.wallet.address
)
assert skale.wallet.address in addresses
# assert skale.wallet.address in addresses # todo: can't link main address for now

wallet = generate_wallet(skale.web3)
tx_res = skale.validator_service.link_node_address(
node_address=wallet.address,
wait_for=True
)
check_receipt(tx_res.receipt)
link_node_address(skale, wallet)

assert wallet.address not in addresses
addresses = skale.validator_service.get_linked_addresses_by_validator_address(
Expand All @@ -70,19 +80,15 @@ def test_get_linked_addresses_by_validator_address(skale):
def test_get_linked_addresses_by_validator_id(skale):
addresses = skale.validator_service.get_linked_addresses_by_validator_id(D_VALIDATOR_ID)
assert isinstance(addresses, list)
assert skale.wallet.address in addresses
# assert skale.wallet.address in addresses # todo: can't link main address for now


def test_is_main_address(skale):
is_main_address = skale.validator_service.is_main_address(skale.wallet.address)
assert is_main_address

wallet = generate_wallet(skale.web3)
tx_res = skale.validator_service.link_node_address(
node_address=wallet.address,
wait_for=True
)
check_receipt(tx_res.receipt)
link_node_address(skale, wallet)

is_main_address = skale.validator_service.is_main_address(wallet.address)
assert not is_main_address
Expand Down Expand Up @@ -157,16 +163,14 @@ def test_is_validator_trusted(skale):


def test_register_existing_validator(skale):
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()
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
)


def _generate_new_validator(skale):
Expand Down Expand Up @@ -200,11 +204,7 @@ def test_link_node_address(skale):
)
assert wallet.address not in addresses

tx_res = skale.validator_service.link_node_address(
node_address=wallet.address,
wait_for=True
)
check_receipt(tx_res.receipt)
link_node_address(skale, wallet)

addresses = skale.validator_service.get_linked_addresses_by_validator_address(
skale.wallet.address
Expand All @@ -214,11 +214,7 @@ def test_link_node_address(skale):

def test_unlink_node_address(skale):
wallet = generate_wallet(skale.web3)
tx_res = skale.validator_service.link_node_address(
node_address=wallet.address,
wait_for=True
)
check_receipt(tx_res.receipt)
link_node_address(skale, wallet)

addresses = skale.validator_service.get_linked_addresses_by_validator_address(
skale.wallet.address
Expand Down
3 changes: 2 additions & 1 deletion tests/contracts/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ 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)
tx_res = skale.manager.create_node(ip, port, name,
wait_for=True, raise_for_status=False)
assert tx_res.receipt['status'] == 0
with pytest.raises(TransactionFailedError):
tx_res.raise_for_status()
2 changes: 2 additions & 0 deletions tests/contracts/test/time_helpers_with_debug_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from tests.constants import MONTH_IN_SECONDS


Expand All @@ -7,6 +8,7 @@ def test_get_current_month(skale):
assert current_month <= 12


@pytest.mark.skip(reason="function is broken now")
def test_skip_time(skale):
current_month_before = skale.time_helpers_with_debug.get_current_month()
skale.time_helpers_with_debug.skip_time(MONTH_IN_SECONDS, wait_for=True)
Expand Down
16 changes: 12 additions & 4 deletions tests/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ def prepare_data(cleanup_only):
if not cleanup_only:
try:
setup_validator(skale)
skale.validator_service.link_node_address(
node_address=skale.wallet.address,
wait_for=True
)
# signature = skale.validator_service.get_link_node_signature(
# validator_id=D_VALIDATOR_ID
# )
# skale.validator_service.link_node_address(
# node_address=skale.wallet.address,
# signature=signature,
# wait_for=True
# )
# skale.time_helpers_with_debug.skip_time(MONTH_IN_SECONDS, wait_for=True)
_skip_evm_time(skale.web3, MONTH_IN_SECONDS)
create_nodes(skale)
create_schain(skale)

active_node_ids_before = skale.nodes_data.get_active_node_ids()
print(active_node_ids_before)
except Exception as err:
cleanup_nodes_schains(skale)
raise err
Expand Down

0 comments on commit 1c0ffeb

Please sign in to comment.