Skip to content

Commit

Permalink
Merge pull request #1066 from skalenetwork/sync-bls-config
Browse files Browse the repository at this point in the history
Make sure node id is constant for sync node. Add common_bls_key to the schain config
  • Loading branch information
badrogger authored May 15, 2024
2 parents 0ed6b90 + b9157e9 commit bb8d657
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 93 deletions.
8 changes: 8 additions & 0 deletions core/node_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 25 additions & 8 deletions core/schains/config/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@

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
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
from core.schains.config.precompiled import generate_precompiled_accounts
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
Expand Down Expand Up @@ -128,9 +131,10 @@ 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, 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"""
Expand Down Expand Up @@ -166,11 +170,12 @@ 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,
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
Expand Down Expand Up @@ -218,34 +223,46 @@ 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'])

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_config.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,
node_id=node_config.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
Expand Down
5 changes: 3 additions & 2 deletions core/schains/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
65 changes: 40 additions & 25 deletions core/schains/config/node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
10 changes: 7 additions & 3 deletions core/schains/config/skale_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -60,16 +61,19 @@ 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(schain['name'])
nodes_in_schain = len(schain_nodes_with_schains)

node_info = generate_current_node_info(
node_id=node_id,
node=node,
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
Expand Down
18 changes: 11 additions & 7 deletions core/schains/dkg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion core/schains/monitor/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/schains/monitor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions sync_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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
Expand Down Expand Up @@ -66,9 +67,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]

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_config.schain_base_port == -1:
schains_on_node = skale.schains.get_schains_for_node(node_config.id)
node_config.schain_base_port = get_schain_base_port_on_node(
schains_on_node,
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)
Expand Down
4 changes: 3 additions & 1 deletion tests/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'):
Expand All @@ -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')

Expand Down
Loading

0 comments on commit bb8d657

Please sign in to comment.