From 47ed7b3d126ba4a4e7a184698f72bff8cf094983 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 25 Apr 2024 11:41:54 +0000 Subject: [PATCH 1/4] Add commonBLSPublic key to sync node config. Fix node_id --- core/schains/config/generator.py | 21 ++++++++- core/schains/config/node_info.py | 65 +++++++++++++++++----------- core/schains/config/skale_section.py | 10 +++-- core/schains/dkg/utils.py | 18 +++++--- core/schains/monitor/main.py | 2 +- sync_node.py | 17 +++++++- tools/node_options.py | 4 ++ 7 files changed, 97 insertions(+), 40 deletions(-) diff --git a/core/schains/config/generator.py b/core/schains/config/generator.py index 5dcb1eda5..acf123fc2 100644 --- a/core/schains/config/generator.py +++ b/core/schains/config/generator.py @@ -22,6 +22,7 @@ from skale import Skale from skale.schain_config.generator import get_schain_nodes_with_schains +from skale.schain_config.ports_allocation import get_schain_base_port_on_node from skale.schain_config.rotation_history import get_previous_schain_groups from etherbase_predeployed import ETHERBASE_ADDRESS @@ -34,6 +35,7 @@ from core.schains.config.generation import Gen from core.schains.config.static_accounts import is_static_accounts, static_accounts from core.schains.config.helper import get_chain_id, get_schain_id +from core.schains.dkg.utils import get_common_bls_public_key from core.schains.limits import get_schain_type from tools.helper import read_json @@ -130,7 +132,8 @@ def generate_schain_config( schain: dict, node_id: int, node: dict, ecdsa_key_name: str, schains_on_node: list, rotation_id: int, schain_nodes_with_schains: list, node_groups: list, generation: int, is_owner_contract: bool, - skale_manager_opts: SkaleManagerOpts, sync_node: bool = False, + skale_manager_opts: SkaleManagerOpts, schain_base_port: int, common_bls_public_keys: list[str], + sync_node: bool = False, archive=None, catchup=None ) -> SChainConfig: """Main function that is used to generate sChain config""" @@ -171,6 +174,8 @@ def generate_schain_config( rotation_id=rotation_id, node_groups=node_groups, skale_manager_opts=skale_manager_opts, + schain_base_port=schain_base_port, + common_bls_public_keys=common_bls_public_keys, sync_node=sync_node, archive=archive, catchup=catchup @@ -233,19 +238,31 @@ def generate_schain_config_with_skale( is_owner_contract = is_address_contract(skale.web3, schain['mainnetOwner']) skale_manager_opts = init_skale_manager_opts(skale) + group_index = skale.schains.name_to_id(schain_name) + common_bls_public_keys = get_common_bls_public_key(skale, group_index) + + if sync_node: + schain_base_port = node_options.schain_base_port + else: + schain_base_port = get_schain_base_port_on_node( + schains_on_node, + schain['name'], + node['port'] + ) return generate_schain_config( schain=schain, node=node, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_data['rotation_id'], schain_nodes_with_schains=schain_nodes_with_schains, node_groups=node_groups, generation=generation, is_owner_contract=is_owner_contract, skale_manager_opts=skale_manager_opts, + schain_base_port=schain_base_port, + common_bls_public_keys=common_bls_public_keys, sync_node=sync_node, archive=node_options.archive, catchup=node_options.catchup diff --git a/core/schains/config/node_info.py b/core/schains/config/node_info.py index 1d5542961..e4bb3386b 100644 --- a/core/schains/config/node_info.py +++ b/core/schains/config/node_info.py @@ -22,7 +22,6 @@ from skale.dataclasses.node_info import NodeInfo from skale.dataclasses.skaled_ports import SkaledPorts -from skale.schain_config.ports_allocation import get_schain_base_port_on_node from core.schains.config.skale_manager_opts import SkaleManagerOpts from tools.configs import SGX_SSL_KEY_FILEPATH, SGX_SSL_CERT_FILEPATH @@ -74,24 +73,28 @@ def to_dict(self): def generate_current_node_info( node: dict, node_id: int, ecdsa_key_name: str, static_node_info: dict, - schain: dict, schains_on_node: list, rotation_id: int, skale_manager_opts: SkaleManagerOpts, + schain: dict, rotation_id: int, + nodes_in_schain: int, + skale_manager_opts: SkaleManagerOpts, + schain_base_port: int, + common_bls_public_keys: list[str], sync_node: bool = False, archive: bool = False, catchup: bool = False ) -> CurrentNodeInfo: - schain_base_port_on_node = get_schain_base_port_on_node( - schains_on_node, + wallets = generate_wallets_config( schain['name'], - node['port'] + rotation_id, + sync_node, + nodes_in_schain, + common_bls_public_keys ) - wallets = {} if sync_node else generate_wallets_config(schain['name'], rotation_id) - if ecdsa_key_name is None: ecdsa_key_name = '' return CurrentNodeInfo( node_id=node_id, name=node['name'], - base_port=schain_base_port_on_node, + base_port=schain_base_port, ecdsa_key_name=ecdsa_key_name, wallets=wallets, skale_manager_opts=skale_manager_opts, @@ -103,28 +106,40 @@ def generate_current_node_info( ) -def generate_wallets_config(schain_name: str, rotation_id: int) -> dict: - secret_key_share_filepath = get_secret_key_share_filepath(schain_name, rotation_id) - secret_key_share_config = read_json(secret_key_share_filepath) +def generate_wallets_config( + schain_name: str, + rotation_id: int, + sync_node: bool, + nodes_in_schain: int, + common_bls_public_keys: str +) -> dict: + wallets = {'ima': {}} + formatted_common_pk = {} + + for (i, value) in enumerate(common_bls_public_keys): + name = 'commonBLSPublicKey' + str(i) + formatted_common_pk[name] = str(value) + + wallets['ima'].update({ + 'n': nodes_in_schain, + **formatted_common_pk + }) + + if not sync_node: + secret_key_share_filepath = get_secret_key_share_filepath(schain_name, rotation_id) + secret_key_share_config = read_json(secret_key_share_filepath) - wallets = { - 'ima': { + wallets['ima'].update({ 'keyShareName': secret_key_share_config['key_share_name'], 't': secret_key_share_config['t'], - 'n': secret_key_share_config['n'], 'certFile': SGX_SSL_CERT_FILEPATH, - 'keyFile': SGX_SSL_KEY_FILEPATH - } - } - common_public_keys = secret_key_share_config['common_public_key'] - for (i, value) in enumerate(common_public_keys): - name = 'commonBLSPublicKey' + str(i) - wallets['ima'][name] = str(value) + 'keyFile': SGX_SSL_KEY_FILEPATH, + }) - public_keys = secret_key_share_config['public_key'] - for (i, value) in enumerate(public_keys): - name = 'BLSPublicKey' + str(i) - wallets['ima'][name] = str(value) + public_keys = secret_key_share_config['public_key'] + for (i, value) in enumerate(public_keys): + name = 'BLSPublicKey' + str(i) + wallets['ima'][name] = str(value) return wallets diff --git a/core/schains/config/skale_section.py b/core/schains/config/skale_section.py index badbe403d..a83fd58fa 100644 --- a/core/schains/config/skale_section.py +++ b/core/schains/config/skale_section.py @@ -48,9 +48,10 @@ def to_dict(self): def generate_skale_section( schain: dict, on_chain_etherbase: str, on_chain_owner: str, schain_id: int, node_id: int, - node: dict, ecdsa_key_name: str, schains_on_node: list, schain_nodes_with_schains: list, + node: dict, ecdsa_key_name: str, schain_nodes_with_schains: list, rotation_id: int, node_groups: dict, skale_manager_opts: SkaleManagerOpts, - sync_node: bool = False, archive=None, catchup=None + schain_base_port: int, + common_bls_public_keys: list[str], sync_node: bool = False, archive=None, catchup=None ) -> SkaleConfig: contract_settings = generate_contract_settings( on_chain_owner=on_chain_owner, @@ -60,6 +61,7 @@ def generate_skale_section( schain_type = get_schain_type(schain['partOfNode']) static_node_info = get_static_node_info(schain_type) static_schain_info = get_static_schain_info() + nodes_in_schain = len(schain_nodes_with_schains) node_info = generate_current_node_info( node_id=node_id, @@ -67,9 +69,11 @@ def generate_skale_section( ecdsa_key_name=ecdsa_key_name, static_node_info=static_node_info, schain=schain, - schains_on_node=schains_on_node, rotation_id=rotation_id, skale_manager_opts=skale_manager_opts, + schain_base_port=schain_base_port, + nodes_in_schain=nodes_in_schain, + common_bls_public_keys=common_bls_public_keys, sync_node=sync_node, archive=archive, catchup=catchup diff --git a/core/schains/dkg/utils.py b/core/schains/dkg/utils.py index 76ec0eefc..585c36ce7 100644 --- a/core/schains/dkg/utils.py +++ b/core/schains/dkg/utils.py @@ -177,17 +177,12 @@ def generate_bls_keys(dkg_client): dkg_client.fetch_bls_public_key() bls_public_keys = dkg_client.get_bls_public_keys() - common_public_key = skale.key_storage.get_common_public_key(dkg_client.group_index) - formatted_common_public_key = [ - elem - for coord in common_public_key - for elem in coord - ] + common_public_key = get_common_bls_public_key(skale, dkg_client.group_index) except Exception as err: raise DKGKeyGenerationError(err) dkg_client.last_completed_step = DKGStep.KEY_GENERATION return { - 'common_public_key': formatted_common_public_key, + 'common_public_key': common_public_key, 'public_key': dkg_client.public_key, 'bls_public_keys': bls_public_keys, 't': dkg_client.t, @@ -196,6 +191,15 @@ def generate_bls_keys(dkg_client): } +def get_common_bls_public_key(skale, group_index: str) -> list[str]: + raw_common_public_key = skale.key_storage.get_common_public_key(group_index) + return [ + elem + for coord in raw_common_public_key + for elem in coord + ] + + def send_complaint(dkg_client: DKGClient, index: int, reason: ComplaintReason): channel_started_time = dkg_client.skale.dkg.get_channel_started_time(dkg_client.group_index) reason_to_missing = { diff --git a/core/schains/monitor/main.py b/core/schains/monitor/main.py index 11298a7f8..9921962e1 100644 --- a/core/schains/monitor/main.py +++ b/core/schains/monitor/main.py @@ -198,7 +198,7 @@ def create_and_execute_tasks( is_rotation_active = skale.node_rotation.is_rotation_active(name) - leaving_chain = not is_node_part_of_chain(skale, name, node_config.id) + leaving_chain = not SYNC_NODE and not is_node_part_of_chain(skale, name, node_config.id) if leaving_chain and not is_rotation_active: logger.info('Not on node (%d), finishing process', node_config.id) return True diff --git a/sync_node.py b/sync_node.py index edb696e90..62b4d5f84 100644 --- a/sync_node.py +++ b/sync_node.py @@ -23,11 +23,13 @@ from typing import Dict from skale import Skale, SkaleIma +from skale.schain_config.ports_allocation import get_schain_base_port_on_node from core.schains.process_manager import run_pm_schain from core.node_config import NodeConfig from core.ima.schain import update_predeployed_ima +from tools.node_options import NodeOptions from tools.logger import init_sync_logger from tools.configs.web3 import ENDPOINT, ABI_FILEPATH from tools.configs.ima import MAINNET_IMA_ABI_FILEPATH @@ -66,9 +68,20 @@ def worker(schain_name: str): exit(1) schain = skale.schains.get_by_name(schain_name) - schain_nodes = skale.schains_internal.get_node_ids_for_schain(schain_name) node_config = NodeConfig() - node_config.id = schain_nodes[0] + node_options = NodeOptions() + + schain_nodes = skale.schains_internal.get_node_ids_for_schain(schain_name) + if not node_config.id: + node_config.id = schain_nodes[0] + + node = skale.nodes.get(node_config.id) + if node_options.schain_base_port == -1: + node_options.schain_base_port = get_schain_base_port_on_node( + schain_nodes, + schain_name, + node['port'] + ) logger.info(f'Node {node_config.id} will be used as a current node') monitor(skale, skale_ima, node_config, schain) diff --git a/tools/node_options.py b/tools/node_options.py index 351218f12..d9b694364 100644 --- a/tools/node_options.py +++ b/tools/node_options.py @@ -41,3 +41,7 @@ def catchup(self) -> bool: @property def historic_state(self) -> bool: return self._get('historic_state') + + @property + def schain_base_port(self) -> int: + return self._get('schain_base_port') or -1 From 0417b906cde47210f34099f26c9cccf0aabe08f3 Mon Sep 17 00:00:00 2001 From: badrogger Date: Fri, 26 Apr 2024 10:44:40 +0000 Subject: [PATCH 2/4] Move schain_base_port to NodeConfig. Fix generator tests --- core/node_config.py | 8 +++ core/schains/config/generator.py | 14 +++--- core/schains/config/main.py | 5 +- core/schains/monitor/action.py | 2 +- sync_node.py | 11 +++-- tests/schains/config/generator_test.py | 68 +++++++++++++++----------- tools/node_options.py | 4 -- 7 files changed, 64 insertions(+), 48 deletions(-) diff --git a/core/node_config.py b/core/node_config.py index 5ab3d6bfc..189498150 100644 --- a/core/node_config.py +++ b/core/node_config.py @@ -60,3 +60,11 @@ def sgx_key_name(self) -> int: @sgx_key_name.setter def sgx_key_name(self, sgx_key_name: int) -> None: return self._set('sgx_key_name', sgx_key_name) + + @property + def schain_base_port(self) -> int: + return self._get('schain_base_port') or -1 + + @schain_base_port.setter + def schain_base_port(self, schain_port: int) -> None: + return self._set('schain_base_port', schain_port) diff --git a/core/schains/config/generator.py b/core/schains/config/generator.py index acf123fc2..fe4eef631 100644 --- a/core/schains/config/generator.py +++ b/core/schains/config/generator.py @@ -28,6 +28,7 @@ from etherbase_predeployed import ETHERBASE_ADDRESS from marionette_predeployed import MARIONETTE_ADDRESS +from core.node_config import NodeConfig from core.schains.config.skale_manager_opts import SkaleManagerOpts, init_skale_manager_opts from core.schains.config.skale_section import SkaleConfig, generate_skale_section from core.schains.config.predeployed import generate_predeployed_accounts @@ -130,7 +131,7 @@ def get_schain_originator(schain: dict): def generate_schain_config( schain: dict, node_id: int, node: dict, ecdsa_key_name: str, - schains_on_node: list, rotation_id: int, schain_nodes_with_schains: list, + rotation_id: int, schain_nodes_with_schains: list, node_groups: list, generation: int, is_owner_contract: bool, skale_manager_opts: SkaleManagerOpts, schain_base_port: int, common_bls_public_keys: list[str], sync_node: bool = False, @@ -169,7 +170,6 @@ def generate_schain_config( node_id=node_id, node=node, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, schain_nodes_with_schains=schain_nodes_with_schains, rotation_id=rotation_id, node_groups=node_groups, @@ -223,16 +223,16 @@ def generate_schain_config_with_skale( skale: Skale, schain_name: str, generation: int, - node_id: int, + node_config: NodeConfig, rotation_data: dict, ecdsa_key_name: str, sync_node: bool = False, node_options: NodeOptions = NodeOptions() ) -> SChainConfig: schain_nodes_with_schains = get_schain_nodes_with_schains(skale, schain_name) - schains_on_node = skale.schains.get_schains_for_node(node_id) + schains_on_node = skale.schains.get_schains_for_node(node_config.id) schain = skale.schains.get_by_name(schain_name) - node = skale.nodes.get(node_id) + node = skale.nodes.get(node_config.id) node_groups = get_previous_schain_groups(skale, schain_name) is_owner_contract = is_address_contract(skale.web3, schain['mainnetOwner']) @@ -242,7 +242,7 @@ def generate_schain_config_with_skale( common_bls_public_keys = get_common_bls_public_key(skale, group_index) if sync_node: - schain_base_port = node_options.schain_base_port + schain_base_port = node_config.schain_base_port else: schain_base_port = get_schain_base_port_on_node( schains_on_node, @@ -253,7 +253,7 @@ def generate_schain_config_with_skale( return generate_schain_config( schain=schain, node=node, - node_id=node_id, + node_id=node_config.id, ecdsa_key_name=ecdsa_key_name, rotation_id=rotation_data['rotation_id'], schain_nodes_with_schains=schain_nodes_with_schains, diff --git a/core/schains/config/main.py b/core/schains/config/main.py index ed50fc407..4dd1a1838 100644 --- a/core/schains/config/main.py +++ b/core/schains/config/main.py @@ -23,6 +23,7 @@ from skale import Skale from core.node import get_skale_node_version +from core.node_config import NodeConfig from core.schains.config.directory import get_files_with_prefix, schain_config_dir from core.schains.config.file_manager import ConfigFileManager, SkaledConfigFilename from core.schains.config.generator import generate_schain_config_with_skale @@ -39,7 +40,7 @@ def create_new_upstream_config( skale: Skale, - node_id: int, + node_config: NodeConfig, schain_name: str, generation: int, ecdsa_sgx_key_name: str, @@ -57,7 +58,7 @@ def create_new_upstream_config( skale=skale, schain_name=schain_name, generation=generation, - node_id=node_id, + node_config=node_config, rotation_data=rotation_data, ecdsa_key_name=ecdsa_sgx_key_name, sync_node=sync_node, diff --git a/core/schains/monitor/action.py b/core/schains/monitor/action.py index 61f52ff4f..62bc479c2 100644 --- a/core/schains/monitor/action.py +++ b/core/schains/monitor/action.py @@ -216,7 +216,7 @@ def upstream_config(self) -> bool: ) new_config = create_new_upstream_config( skale=self.skale, - node_id=self.node_config.id, + node_config=self.node_config, schain_name=self.name, generation=self.generation, ecdsa_sgx_key_name=self.node_config.sgx_key_name, diff --git a/sync_node.py b/sync_node.py index 62b4d5f84..dbe59a58b 100644 --- a/sync_node.py +++ b/sync_node.py @@ -29,7 +29,6 @@ from core.node_config import NodeConfig from core.ima.schain import update_predeployed_ima -from tools.node_options import NodeOptions from tools.logger import init_sync_logger from tools.configs.web3 import ENDPOINT, ABI_FILEPATH from tools.configs.ima import MAINNET_IMA_ABI_FILEPATH @@ -69,16 +68,18 @@ def worker(schain_name: str): schain = skale.schains.get_by_name(schain_name) node_config = NodeConfig() - node_options = NodeOptions() schain_nodes = skale.schains_internal.get_node_ids_for_schain(schain_name) + logger.info('HERE sn %s', schain_nodes) if not node_config.id: node_config.id = schain_nodes[0] node = skale.nodes.get(node_config.id) - if node_options.schain_base_port == -1: - node_options.schain_base_port = get_schain_base_port_on_node( - schain_nodes, + if node_config.schain_base_port == -1: + schains_on_node = skale.schains.get_schains_for_node(node_config.id) + logger.info('HERE sonn %s', schains_on_node) + node_config.schain_base_port = get_schain_base_port_on_node( + schains_on_node, schain_name, node['port'] ) diff --git a/tests/schains/config/generator_test.py b/tests/schains/config/generator_test.py index 6b0168928..a0c7042e4 100644 --- a/tests/schains/config/generator_test.py +++ b/tests/schains/config/generator_test.py @@ -24,12 +24,13 @@ NODE_ID = 1 ECDSA_KEY_NAME = 'TEST:KEY:NAME' +COMMON_BLS_PUBLIC_KEY = [123, 456, 789, 123], SECRET_KEY = { "key_share_name": "BLS_KEY:SCHAIN_ID:1:NODE_ID:0:DKG_ID:0", "t": 3, "n": 4, - "common_public_key": [123, 456, 789, 123], + "common_public_key": COMMON_BLS_PUBLIC_KEY, "public_key": [ "123", "456", @@ -195,16 +196,18 @@ def check_config(node_id, all_node_ids, config): def test_generate_schain_config_with_skale( skale, + node_config, schain_on_contracts, schain_secret_key_file ): schain_name = schain_on_contracts node_ids = skale.schains_internal.get_node_ids_for_schain(schain_name) current_node_id = node_ids[0] + node_config.id = current_node_id schain_config = generate_schain_config_with_skale( skale=skale, schain_name=schain_name, - node_id=current_node_id, + node_config=node_config, rotation_data={'rotation_id': 0, 'leaving_node': 1}, ecdsa_key_name=ECDSA_KEY_NAME, generation=0, @@ -225,7 +228,6 @@ def test_generate_schain_config_gen0(schain_secret_key_file_default_chain, skale node_id, generation, rotation_id = 1, 0, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -233,13 +235,14 @@ def test_generate_schain_config_gen0(schain_secret_key_file_default_chain, skale node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() @@ -250,7 +253,6 @@ def test_generate_schain_config_gen0(schain_secret_key_file_default_chain, skale def test_generate_schain_config_gen1(schain_secret_key_file_default_chain, skale_manager_opts): node_id, generation, rotation_id = 1, 1, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -258,13 +260,14 @@ def test_generate_schain_config_gen1(schain_secret_key_file_default_chain, skale node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=True, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() @@ -296,7 +299,6 @@ def test_generate_schain_config_gen1_pk_owner( ): node_id, generation, rotation_id = 1, 1, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -304,13 +306,14 @@ def test_generate_schain_config_gen1_pk_owner( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() @@ -324,7 +327,6 @@ def test_generate_schain_config_gen2_schain_id( ): node_id, generation, rotation_id = 1, 2, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -332,13 +334,14 @@ def test_generate_schain_config_gen2_schain_id( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() assert config['skaleConfig']['sChain']['schainID'] == 2755779573749746 @@ -354,13 +357,14 @@ def test_generate_schain_config_gen1_schain_id( node=TEST_NODE, node_id=node_id, ecdsa_key_name='test', - schains_on_node=[{'name': 'test_schain'}], rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups={}, generation=generation, is_owner_contract=False, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() assert config['skaleConfig']['sChain']['schainID'] == 1 @@ -376,13 +380,14 @@ def test_generate_schain_config_gen0_schain_id( node=TEST_NODE, node_id=node_id, ecdsa_key_name='test', - schains_on_node=[{'name': 'test_schain'}], rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups={}, generation=generation, is_owner_contract=False, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000 ) config = schain_config.to_dict() assert config['skaleConfig']['sChain']['schainID'] == 1 @@ -391,15 +396,17 @@ def test_generate_schain_config_gen0_schain_id( def test_generate_schain_config_with_skale_gen2( skale, schain_on_contracts, - schain_secret_key_file + schain_secret_key_file, + node_config ): schain_name = schain_on_contracts node_ids = skale.schains_internal.get_node_ids_for_schain(schain_name) current_node_id = node_ids[0] + node_config.id = current_node_id schain_config = generate_schain_config_with_skale( skale=skale, schain_name=schain_name, - node_id=current_node_id, + node_config=node_config, rotation_data={'rotation_id': 0, 'leaving_node': 1}, ecdsa_key_name=ECDSA_KEY_NAME, generation=2 @@ -423,7 +430,6 @@ def test_generate_sync_node_config( ): node_id, generation, rotation_id = 1, 1, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -431,13 +437,14 @@ def test_generate_sync_node_config( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=True ) config = schain_config.to_dict() @@ -452,7 +459,6 @@ def test_generate_sync_node_config_archive_catchup( ): node_id, generation, rotation_id = 1, 1, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'test_schain'}] node_groups = {} schain_config = generate_schain_config( @@ -460,13 +466,14 @@ def test_generate_sync_node_config_archive_catchup( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=True ) config = schain_config.to_dict() @@ -479,13 +486,14 @@ def test_generate_sync_node_config_archive_catchup( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=True, archive=False, catchup=True @@ -500,13 +508,14 @@ def test_generate_sync_node_config_archive_catchup( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=False, archive=False, catchup=True @@ -523,7 +532,6 @@ def test_generate_sync_node_config_static_accounts( ): node_id, generation, rotation_id = 1, 1, 0 ecdsa_key_name = 'test' - schains_on_node = [{'name': 'static_chain'}] node_groups = {} schain_config = generate_schain_config( @@ -531,13 +539,14 @@ def test_generate_sync_node_config_static_accounts( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=schains_on_node, rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('static_chain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=True ) config = schain_config.to_dict() @@ -549,13 +558,14 @@ def test_generate_sync_node_config_static_accounts( node=TEST_NODE, node_id=node_id, ecdsa_key_name=ecdsa_key_name, - schains_on_node=[{'name': 'test_schain'}], rotation_id=rotation_id, schain_nodes_with_schains=get_schain_node_with_schains('test_schain'), node_groups=node_groups, generation=generation, is_owner_contract=False, skale_manager_opts=skale_manager_opts, + common_bls_public_keys=COMMON_BLS_PUBLIC_KEY, + schain_base_port=10000, sync_node=True ) config = schain_config.to_dict() diff --git a/tools/node_options.py b/tools/node_options.py index d9b694364..351218f12 100644 --- a/tools/node_options.py +++ b/tools/node_options.py @@ -41,7 +41,3 @@ def catchup(self) -> bool: @property def historic_state(self) -> bool: return self._get('historic_state') - - @property - def schain_base_port(self) -> int: - return self._get('schain_base_port') or -1 From 90059c0de6c29462ed579aee6e3f0758ee1424e2 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 29 Apr 2024 12:56:42 +0000 Subject: [PATCH 3/4] Fix schain config node_info tests --- tests/schains/config/node_info_test.py | 67 ++++++++++++++++++++------ 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/tests/schains/config/node_info_test.py b/tests/schains/config/node_info_test.py index 23a450dec..9b3278d41 100644 --- a/tests/schains/config/node_info_test.py +++ b/tests/schains/config/node_info_test.py @@ -5,12 +5,14 @@ from core.schains.types import SchainType from tools.configs import SGX_SSL_KEY_FILEPATH, SGX_SSL_CERT_FILEPATH +COMMON_PUBLIC_KEY = [1, 2, 3, 4] + SECRET_KEY_MOCK = { 'key_share_name': 'BLS_KEY:SCHAIN_ID:1:NODE_ID:0:DKG_ID:0', 't': 1, 'n': 2, - 'common_public_key': [1, 1, 1], - 'public_key': ['1', '1', '1'], + 'common_public_key': COMMON_PUBLIC_KEY, + 'public_key': ['4', '3', '2', '1'], } SCHAIN_NAME = 'test_schain' @@ -18,17 +20,48 @@ def test_generate_wallets_config(): with mock.patch('core.schains.config.node_info.read_json', return_value=SECRET_KEY_MOCK): - wallets = generate_wallets_config('test_schain', 0) + wallets = generate_wallets_config( + 'test_schain', + 0, + sync_node=False, + nodes_in_schain=4, + common_bls_public_keys=COMMON_PUBLIC_KEY + ) assert wallets['ima']['keyShareName'] == SECRET_KEY_MOCK['key_share_name'] assert wallets['ima']['certFile'] == SGX_SSL_CERT_FILEPATH assert wallets['ima']['keyFile'] == SGX_SSL_KEY_FILEPATH assert wallets['ima']['commonBLSPublicKey0'] == '1' - assert wallets['ima']['commonBLSPublicKey1'] == '1' - assert wallets['ima']['commonBLSPublicKey2'] == '1' - assert wallets['ima']['BLSPublicKey0'] == '1' - assert wallets['ima']['BLSPublicKey1'] == '1' - assert wallets['ima']['BLSPublicKey2'] == '1' + assert wallets['ima']['commonBLSPublicKey1'] == '2' + assert wallets['ima']['commonBLSPublicKey2'] == '3' + assert wallets['ima']['commonBLSPublicKey3'] == '4' + assert wallets['ima']['BLSPublicKey0'] == '4' + assert wallets['ima']['BLSPublicKey1'] == '3' + assert wallets['ima']['BLSPublicKey2'] == '2' + assert wallets['ima']['BLSPublicKey3'] == '1' + + +def test_generate_wallets_config_sync_node(): + with mock.patch('core.schains.config.node_info.read_json', return_value=SECRET_KEY_MOCK): + wallets = generate_wallets_config( + 'test_schain', + 0, + sync_node=True, + nodes_in_schain=4, + common_bls_public_keys=COMMON_PUBLIC_KEY + ) + + assert 'keyShareName' not in wallets['ima'] + assert 'certFile' not in wallets['ima'] + assert 'keyFile' not in wallets['ima'] + assert 'BLSPublicKey0' not in wallets['ima'] + assert 'BLSPublicKey1' not in wallets['ima'] + assert 'BLSPublicKey2' not in wallets['ima'] + assert 'BLSPublicKey3' not in wallets['ima'] + assert wallets['ima']['commonBLSPublicKey0'] == '1' + assert wallets['ima']['commonBLSPublicKey1'] == '2' + assert wallets['ima']['commonBLSPublicKey2'] == '3' + assert wallets['ima']['commonBLSPublicKey3'] == '4' def test_generate_current_node_info( @@ -45,9 +78,11 @@ def test_generate_current_node_info( ecdsa_key_name='123', static_node_info=static_node_info, schain={'name': _schain_name, 'partOfNode': 0}, - schains_on_node=[{'name': _schain_name, 'port': 10000}], rotation_id=0, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + nodes_in_schain=4, + schain_base_port=10000, + common_bls_public_keys=COMMON_PUBLIC_KEY ) current_node_info_dict = current_node_info.to_dict() assert current_node_info_dict['nodeID'] == 1 @@ -69,9 +104,11 @@ def test_generate_current_node_info( ecdsa_key_name='123', static_node_info=static_node_info, schain={'name': _schain_name, 'partOfNode': 0}, - schains_on_node=[{'name': _schain_name, 'port': 10000}], rotation_id=0, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + nodes_in_schain=4, + schain_base_port=10000, + common_bls_public_keys=COMMON_PUBLIC_KEY ) current_node_info_dict = current_node_info.to_dict() assert current_node_info_dict['maxCacheSize'] == 16000000 @@ -95,9 +132,11 @@ def test_skale_manager_opts( ecdsa_key_name='123', static_node_info=static_node_info, schain={'name': _schain_name, 'partOfNode': 0}, - schains_on_node=[{'name': _schain_name, 'port': 10000}], rotation_id=0, - skale_manager_opts=skale_manager_opts + skale_manager_opts=skale_manager_opts, + nodes_in_schain=4, + schain_base_port=10000, + common_bls_public_keys=COMMON_PUBLIC_KEY ) current_node_info_dict = current_node_info.to_dict() assert current_node_info_dict['skale-manager'] == { From 69b68244ac5cc0e27c4628faec09d84007a51448 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 2 May 2024 10:58:48 +0000 Subject: [PATCH 4/4] Add schain_base_port test --- sync_node.py | 2 -- tests/node_test.py | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sync_node.py b/sync_node.py index dbe59a58b..77746aad9 100644 --- a/sync_node.py +++ b/sync_node.py @@ -70,14 +70,12 @@ def worker(schain_name: str): node_config = NodeConfig() schain_nodes = skale.schains_internal.get_node_ids_for_schain(schain_name) - logger.info('HERE sn %s', schain_nodes) if not node_config.id: node_config.id = schain_nodes[0] node = skale.nodes.get(node_config.id) if node_config.schain_base_port == -1: schains_on_node = skale.schains.get_schains_for_node(node_config.id) - logger.info('HERE sonn %s', schains_on_node) node_config.schain_base_port = get_schain_base_port_on_node( schains_on_node, schain_name, diff --git a/tests/node_test.py b/tests/node_test.py index d664b2a86..76675ebbf 100644 --- a/tests/node_test.py +++ b/tests/node_test.py @@ -35,7 +35,7 @@ def node(node_skales, skale, nodes): node_data = skale.nodes.get(config.id) config.name = node_data['name'] config.ip = ip_from_bytes(node_data['ip']) - yield Node(node_skales[0], config) + return Node(node_skales[0], config) @pytest.fixture @@ -94,6 +94,7 @@ def test_create_insufficient_funds(unregistered_node): def test_register_info(unregistered_node): unregistered_node.config.id = None ip, public_ip, port, name = generate_random_node_data() + assert unregistered_node.config.schain_base_port == -1 # Register new node and check that it successfully created on contracts with mock.patch('core.node.update_filebeat_service'): @@ -104,6 +105,7 @@ def test_register_info(unregistered_node): name, domain_name=DEFAULT_DOMAIN_NAME ) + assert unregistered_node.config.schain_base_port == -1 assert res['status'] == 'ok' res_data = res.get('data')