Skip to content

Commit

Permalink
Showing 9 changed files with 176 additions and 36 deletions.
45 changes: 26 additions & 19 deletions core/schains/config/generator.py
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
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.limits import get_schain_type

@@ -155,20 +156,6 @@ def generate_schain_config(

originator_address = get_schain_originator(schain)

predeployed_accounts = generate_predeployed_accounts(
schain_name=schain['name'],
schain_type=schain_type,
schain_nodes=schain_nodes_with_schains,
on_chain_owner=on_chain_owner,
mainnet_owner=mainnet_owner,
originator_address=originator_address,
generation=generation
)

precompiled_accounts = generate_precompiled_accounts(
on_chain_owner=on_chain_owner
)

skale_config = generate_skale_section(
schain=schain,
on_chain_etherbase=on_chain_etherbase,
@@ -187,6 +174,30 @@ def generate_schain_config(
catchup=catchup
)

accounts = {}
if is_static_accounts(schain['name']):
logger.info(f'Found static account for {schain["name"]}, going to use in config')
accounts = static_accounts(schain['name'])['accounts']
else:
logger.info('Static accounts not found, generating regular accounts section')
predeployed_accounts = generate_predeployed_accounts(
schain_name=schain['name'],
schain_type=schain_type,
schain_nodes=schain_nodes_with_schains,
on_chain_owner=on_chain_owner,
mainnet_owner=mainnet_owner,
originator_address=originator_address,
generation=generation
)
precompiled_accounts = generate_precompiled_accounts(
on_chain_owner=on_chain_owner
)
accounts = {
**base_config.config['accounts'],
**predeployed_accounts,
**precompiled_accounts,
}

schain_config = SChainConfig(
seal_engine=base_config.config['sealEngine'],
params={
@@ -195,11 +206,7 @@ def generate_schain_config(
},
unddos=base_config.config['unddos'],
genesis=base_config.config['genesis'],
accounts={
**base_config.config['accounts'],
**predeployed_accounts,
**precompiled_accounts,
},
accounts=accounts,
skale_config=skale_config
)
return schain_config
38 changes: 38 additions & 0 deletions core/schains/config/static_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
#
# This file is part of SKALE Admin
#
# Copyright (C) 2023-Present SKALE Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import os

from tools.helper import read_json
from tools.configs import STATIC_ACCOUNTS_FOLDER, ENV_TYPE


def static_accounts(schain_name: str) -> dict:
return read_json(static_accounts_filepath(schain_name))


def is_static_accounts(schain_name: str) -> bool:
return os.path.isfile(static_accounts_filepath(schain_name))


def static_accounts_filepath(schain_name: str) -> str:
static_accounts_env_path = os.path.join(STATIC_ACCOUNTS_FOLDER, ENV_TYPE)
if not os.path.isdir(static_accounts_env_path):
return ''
return os.path.join(static_accounts_env_path, f'schain-{schain_name}.json')
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -5,4 +5,4 @@ mock==4.0.2
blinker==1.4

pytest-cov==2.9.0
codecov==2.1.9
codecov==2.1.13
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ simple-crypt==4.1.7
pycryptodome==3.12.0
python-iptables==1.0.0

skale.py==5.8dev4
skale.py==5.8b2

ima-predeployed==1.3.5b1
etherbase-predeployed==1.1.0b1
@@ -34,3 +34,5 @@ cryptography==35.0.0
python-dateutil==2.8.1
python-telegram-bot==12.8
sh==1.14.1

urllib3<2
35 changes: 20 additions & 15 deletions tests/routes/ssl_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import datetime
import filecmp
import json
import os
import pathlib
import time
from contextlib import contextmanager
from datetime import datetime

import mock

@@ -47,31 +46,37 @@ def cert_key_pair_host():
cert_path = os.path.join(CONFIG_FOLDER, 'temp_ssl_cert')
key_path = os.path.join(CONFIG_FOLDER, 'temp_ssl_key')
generate_cert(cert_path, key_path)
yield cert_path, key_path
pathlib.Path(cert_path).unlink(missing_ok=True)
pathlib.Path(key_path).unlink(missing_ok=True)
# Ensure uploaded certs are removed
cert_path = os.path.join(SSL_CERTIFICATES_FILEPATH, 'ssl_cert')
key_path = os.path.join(SSL_CERTIFICATES_FILEPATH, 'ssl_key')
pathlib.Path(cert_path).unlink(missing_ok=True)
pathlib.Path(key_path).unlink(missing_ok=True)
try:
yield cert_path, key_path
finally:
pathlib.Path(cert_path).unlink(missing_ok=True)
pathlib.Path(key_path).unlink(missing_ok=True)
# Ensure uploaded certs are removed
cert_path = os.path.join(SSL_CERTIFICATES_FILEPATH, 'ssl_cert')
key_path = os.path.join(SSL_CERTIFICATES_FILEPATH, 'ssl_key')
pathlib.Path(cert_path).unlink(missing_ok=True)
pathlib.Path(key_path).unlink(missing_ok=True)


@pytest.fixture
def bad_cert_host(cert_key_pair_host):
cert, key = cert_key_pair_host
with open(cert, 'w') as cert_file:
cert_file.write('WRONG CERT')
yield cert, key
try:
yield cert, key
finally:
os.remove(cert)


def test_status(skale_bp, cert_key_pair):
year = int(datetime.utcfromtimestamp(time.time()).strftime('%Y'))
month = datetime.utcfromtimestamp(time.time()).strftime('%m')
day = datetime.utcfromtimestamp(time.time()).strftime('%d')
year += 1
dt = datetime.datetime.now() + datetime.timedelta(days=365)
year = dt.strftime('%Y')
month = dt.strftime('%m')
day = dt.strftime('%d')
expire_day_line = f'{year}-{month}-{day}'
data = get_bp_data(skale_bp, get_api_url(BLUEPRINT_NAME, 'status'))
print(expire_day_line, data['payload']['expiration_date'])
assert data['status'] == 'ok'
assert data['payload']['issued_to'] is None
assert data['payload']['expiration_date'].startswith(expire_day_line)
14 changes: 14 additions & 0 deletions tests/schains/config/static_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from core.schains.config.static_accounts import is_static_accounts, static_accounts

SCHAIN_NAME = 'test'


def test_is_static_accounts():
assert is_static_accounts(SCHAIN_NAME)
assert not is_static_accounts('qwerty')


def test_static_accounts():
accounts = static_accounts(SCHAIN_NAME)
assert isinstance(accounts, dict)
assert accounts.get('accounts', None)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"accounts": {
"0x9d8C194E07c628C15C82f06a53cCCf3c7b68810D": {
"balance": "1000000000000000000000000000000"
}
}
}
65 changes: 65 additions & 0 deletions tests/test_generate_config_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import json
import pytest
from skale.schain_config.rotation_history import get_previous_schain_groups

from core.schains.config.predeployed import generate_predeployed_accounts
from core.schains.config.precompiled import generate_precompiled_accounts

from core.schains.limits import get_schain_type
from core.schains.config.generator import (
get_on_chain_owner, get_schain_originator, SChainBaseConfig)

from tools.helper import is_address_contract
from tools.configs.schains import BASE_SCHAIN_CONFIG_FILEPATH


CHAINS = []


@pytest.mark.skip(reason="test only used to generate static accounts for a sync node")
def test_generate_config(skale):
for schain_name in CHAINS:

schain = skale.schains.get_by_name(schain_name)
schain_type = get_schain_type(schain['partOfNode'])

node_groups = get_previous_schain_groups(skale, schain_name)
original_group = node_groups[0]['nodes']

schain_nodes_with_schains = []
for key, value in original_group.items():
schain_nodes_with_schains.append({
'id': int(key),
'publicKey': value[2]
})

is_owner_contract = is_address_contract(skale.web3, schain['mainnetOwner'])
on_chain_owner = get_on_chain_owner(schain, schain['generation'], is_owner_contract)

mainnet_owner = schain['mainnetOwner']

originator_address = get_schain_originator(schain)

precompiled_accounts = generate_precompiled_accounts(
on_chain_owner=on_chain_owner
)

base_config = SChainBaseConfig(BASE_SCHAIN_CONFIG_FILEPATH)

predeployed_accounts = generate_predeployed_accounts(
schain_name=schain['name'],
schain_type=schain_type,
schain_nodes=schain_nodes_with_schains,
on_chain_owner=on_chain_owner,
mainnet_owner=mainnet_owner,
originator_address=originator_address,
generation=schain['generation']
)

accounts = {
**base_config.config['accounts'],
**predeployed_accounts,
**precompiled_accounts,
}
with open(f'accounts/schain-{schain_name}.json', 'w') as outfile:
json.dump({'accounts': accounts}, outfile, indent=4)
2 changes: 2 additions & 0 deletions tools/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -29,6 +29,8 @@
CONTRACTS_INFO_FOLDER = os.path.join(SKALE_VOLUME_PATH, CONTRACTS_INFO_FOLDER_NAME)
CONFIG_FOLDER = os.path.join(SKALE_VOLUME_PATH, CONFIG_FOLDER_NAME)

STATIC_ACCOUNTS_FOLDER = os.path.join(CONFIG_FOLDER, 'schain_accounts')

FLASK_SECRET_KEY_FILENAME = 'flask_db_key.txt'
FLASK_SECRET_KEY_FILE = os.path.join(NODE_DATA_PATH, FLASK_SECRET_KEY_FILENAME)

0 comments on commit 06d542b

Please sign in to comment.