Skip to content

Commit

Permalink
Merge pull request #476 from skalenetwork/hotfix/SKALE-4942-fix-rotat…
Browse files Browse the repository at this point in the history
…ion-conditions

SKALE-4942 Add is_rotation_active function, add tests
  • Loading branch information
dmytrotkk authored Feb 15, 2022
2 parents 6e0d46c + 8c5d34e commit 3d7871f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 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.9.0-develop.1"
MANAGER_TAG: "1.9.0-develop.8"
steps:
- uses: actions/checkout@v2
with:
Expand Down
13 changes: 12 additions & 1 deletion skale/contracts/manager/node_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,21 @@ def get_schain_finish_ts(self, node_id: int, schain_name: str) -> int:
finish_ts += rotation_delay
return finish_ts

def is_rotation_in_progress(self, schain_name):
def is_rotation_in_progress(self, schain_name) -> bool:
schain_id = self.schains.name_to_id(schain_name)
return self.contract.functions.isRotationInProgress(schain_id).call()

def is_new_node_found(self, schain_name) -> bool:
schain_id = self.schains.name_to_id(schain_name)
return self.contract.functions.isNewNodeFound(schain_id).call()

def is_rotation_active(self, schain_name) -> bool:
"""
The public function that tells whether rotation is in the active phase - the new group is
already generated
"""
return self.is_rotation_in_progress(schain_name) and self.is_new_node_found(schain_name)

def wait_for_new_node(self, schain_name):
schain_id = self.schains.name_to_id(schain_name)
return self.contract.functions.waitForNewNode(schain_id).call()
Expand Down
14 changes: 14 additions & 0 deletions skale/utils/contracts_provision/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ def create_clean_schain(skale):
return create_schain(skale, random_name=True)


def create_node(skale) -> str:
cleanup_nodes_schains(skale)
ip, public_ip, port, name = generate_random_node_data()
skale.manager.create_node(
ip=ip,
port=port,
name=name,
domain_name=DEFAULT_DOMAIN_NAME,
public_ip=public_ip,
wait_for=True
)
return name


def validator_exist(skale):
return skale.validator_service.number_of_validators() > 0

Expand Down
33 changes: 33 additions & 0 deletions tests/manager/node_rotation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import mock

from skale.contracts.manager.node_rotation import Rotation
from skale.utils.contracts_provision.main import (
cleanup_nodes_schains, create_schain, add_test4_schain_type
)
from tests.constants import DEFAULT_SCHAIN_ID, DEFAULT_SCHAIN_NAME, DEFAULT_SCHAIN_INDEX
from tests.rotation_history.utils import set_up_nodes, run_dkg


def test_get_rotation(skale):
Expand Down Expand Up @@ -50,3 +54,32 @@ def test_is_rotation_in_progress(skale):

def test_wait_for_new_node(skale):
assert skale.node_rotation.wait_for_new_node(DEFAULT_SCHAIN_NAME) is False


def test_is_rotation_active(skale):
cleanup_nodes_schains(skale)
nodes, skale_instances = set_up_nodes(skale, 4)
add_test4_schain_type(skale)
name = create_schain(skale, random_name=True)
group_index = skale.web3.sha3(text=name)

run_dkg(nodes, skale_instances, group_index)

exiting_node_index = 1
set_up_nodes(skale, 1)

assert not skale.node_rotation.is_new_node_found(name)
assert not skale.node_rotation.is_rotation_in_progress(name)
assert not skale.node_rotation.is_rotation_active(name)

skale.nodes.init_exit(nodes[exiting_node_index]['node_id'])

assert not skale.node_rotation.is_new_node_found(name)
assert skale.node_rotation.is_rotation_in_progress(name)
assert not skale.node_rotation.is_rotation_active(name)

skale_instances[exiting_node_index].manager.node_exit(nodes[exiting_node_index]['node_id'])

assert skale.node_rotation.is_new_node_found(name)
assert skale.node_rotation.is_rotation_in_progress(name)
assert skale.node_rotation.is_rotation_active(name)
18 changes: 12 additions & 6 deletions tests/manager/nodes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from skale.contracts.manager.nodes import FIELDS, NodeStatus
from skale.utils.exceptions import InvalidNodeIdError
from skale.utils.contracts_provision import DEFAULT_DOMAIN_NAME
from skale.utils.contracts_provision.main import create_node

from tests.constants import DEFAULT_NODE_HASH, DEFAULT_NODE_NAME, NOT_EXISTING_ID

Expand All @@ -28,12 +29,14 @@ def public_key_from_private(key):


def test_get(skale):
node = skale.nodes.get_by_name(DEFAULT_NODE_NAME)
node_id = skale.nodes.node_name_to_index(DEFAULT_NODE_NAME)
name = create_node(skale)

node = skale.nodes.get_by_name(name)
node_id = skale.nodes.node_name_to_index(name)
node_by_id = skale.nodes.get(node_id)
assert list(node.keys()) == FIELDS
assert [k for k, v in node.items() if v is None] == []
assert node_by_id['name'] == DEFAULT_NODE_NAME
assert node_by_id['name'] == name
socket.inet_ntoa(node_by_id['ip'])
socket.inet_ntoa(node_by_id['publicIP'])

Expand Down Expand Up @@ -104,14 +107,16 @@ def test_name_to_id(skale):


def test_is_node_name_available(skale):
node = skale.nodes.get_by_name(DEFAULT_NODE_NAME)
name = create_node(skale)
node = skale.nodes.get_by_name(name)
unused_name = 'unused_name'
assert skale.nodes.is_node_name_available(node['name']) is False
assert skale.nodes.is_node_name_available(unused_name) is True


def test_is_node_ip_available(skale):
node = skale.nodes.get_by_name(DEFAULT_NODE_NAME)
name = create_node(skale)
node = skale.nodes.get_by_name(name)
node_ip = Helper.ip_from_bytes(node['ip'])

unused_ip = '123.123.231.123'
Expand All @@ -136,7 +141,8 @@ def test_get_node_public_key(skale):


def test_node_in_maintenance(skale):
node_id = skale.nodes.node_name_to_index(DEFAULT_NODE_NAME)
name = create_node(skale)
node_id = skale.nodes.node_name_to_index(name)
assert skale.nodes.get_node_status(node_id) == NodeStatus.ACTIVE.value

skale.nodes.set_node_in_maintenance(node_id)
Expand Down
2 changes: 2 additions & 0 deletions tests/manager/schains_internal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_get_schains_number(skale):


def test_get_schain_list_size(skale, empty_account):
create_clean_schain(skale)
list_size = skale.schains_internal.get_schain_list_size(skale.wallet.address)
empty_list_size = skale.schains_internal.get_schain_list_size(empty_account.address)

Expand All @@ -30,6 +31,7 @@ def test_get_schain_list_size(skale, empty_account):


def test_get_schain_id_by_index_for_owner(skale):
create_clean_schain(skale)
schain_id = skale.schains_internal.get_schain_id_by_index_for_owner(
skale.wallet.address, 0
)
Expand Down

0 comments on commit 3d7871f

Please sign in to comment.