Skip to content

Commit

Permalink
Merge pull request #29 from skalenetwork/enhancement/SKALE-1820-skale…
Browse files Browse the repository at this point in the history
…-py-v2-support

Enhancement/skale 1802 skale py v2 support, sgx, transactions manager and more
  • Loading branch information
dmytrotkk authored Nov 28, 2019
2 parents dada422 + 468f526 commit 325e9aa
Show file tree
Hide file tree
Showing 50 changed files with 427 additions and 609 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 100
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,venv,node_modules
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.0
0.6.0
57 changes: 25 additions & 32 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,27 @@
# 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
import logging

from flask import Flask, render_template, session, g
from peewee import SqliteDatabase

import sentry_sdk

from skale import Skale
from skale.wallets import RPCWallet

from core.node import Node
from core.local_wallet import LocalWallet
from core.node_config import NodeConfig
from core.schains.monitor import SchainsMonitor
from core.schains.cleaner import SChainsCleaner

from tools.helper import get_sentry_env_name
from tools.configs import NODE_CONFIG_FILEPATH, FLASK_SECRET_KEY_FILE
from tools.configs.web3 import ENDPOINT, ABI_FILEPATH
from tools.configs import FLASK_SECRET_KEY_FILE
from tools.configs.web3 import ENDPOINT, ABI_FILEPATH, TM_URL
from tools.configs.db import DB_FILE
from tools.logger import init_admin_logger
from tools.config_storage import ConfigStorage
from tools.token_utils import TokenUtils
from tools.docker_utils import DockerUtils
from tools.str_formatters import arguments_list_string
from tools.sgx_utils import generate_sgx_key
from tools.sgx_utils import generate_sgx_key, sgx_server_text
from tools.token_utils import init_user_token

from tools.configs.flask import FLASK_APP_HOST, FLASK_APP_PORT, FLASK_DEBUG_MODE
from web.user import User
Expand All @@ -61,39 +58,31 @@
werkzeug_logger = logging.getLogger('werkzeug') # todo: remove
werkzeug_logger.setLevel(logging.WARNING) # todo: remove

rpc_wallet = RPCWallet(os.environ['TM_URL'])
rpc_wallet = RPCWallet(TM_URL)
skale = Skale(ENDPOINT, ABI_FILEPATH, rpc_wallet)
wallet = LocalWallet(skale, rpc_wallet)
config = ConfigStorage(NODE_CONFIG_FILEPATH)

docker_utils = DockerUtils()
node = Node(skale, config, wallet)
token_utils = TokenUtils()
user_session = UserSession(session)

SENTRY_URL = os.environ.get('SENTRY_URL', None)
if SENTRY_URL:
sentry_env_name = get_sentry_env_name(skale.manager.address)
sentry_sdk.init(SENTRY_URL, environment=sentry_env_name)


if not token_utils.get_token():
token_utils.add_token()
token = token_utils.get_token()
node_config = NodeConfig()
node = Node(skale, node_config)
schains_monitor = SchainsMonitor(skale, node_config)
schains_cleaner = SChainsCleaner(skale, node_config)

token = init_user_token()
database = SqliteDatabase(DB_FILE)

app = Flask(__name__)
app.register_blueprint(construct_auth_bp(user_session, token))
app.register_blueprint(web_logs)
app.register_blueprint(construct_nodes_bp(skale, node, docker_utils))
app.register_blueprint(construct_schains_bp(skale, wallet, docker_utils, node))
app.register_blueprint(construct_wallet_bp(wallet))
app.register_blueprint(construct_node_info_bp(skale, wallet, docker_utils))
app.register_blueprint(construct_schains_bp(skale, node_config, docker_utils))
app.register_blueprint(construct_wallet_bp(skale))
app.register_blueprint(construct_node_info_bp(skale, docker_utils))
app.register_blueprint(construct_security_bp())
app.register_blueprint(construct_validators_bp(skale, config, wallet))
app.register_blueprint(construct_metrics_bp(skale, config))
app.register_blueprint(construct_validators_bp(skale, node_config))
app.register_blueprint(construct_metrics_bp(skale, node_config))

wallet.get_or_generate()

@app.before_request
def before_request():
Expand All @@ -113,9 +102,13 @@ def main():


if __name__ == '__main__':
logger.info(arguments_list_string({'Root account token': token}, 'Starting Flask server'))
logger.info(arguments_list_string({
'Endpoint': ENDPOINT,
'Transaction manager': TM_URL,
'SGX Server': sgx_server_text()
}, 'Starting Flask server'))
if not User.table_exists():
User.create_table()
generate_sgx_key(config)
generate_sgx_key(node_config)
app.secret_key = FLASK_SECRET_KEY_FILE
app.run(debug=FLASK_DEBUG_MODE, port=FLASK_APP_PORT, host=FLASK_APP_HOST, use_reloader=False)
75 changes: 0 additions & 75 deletions core/local_wallet.py

This file was deleted.

111 changes: 111 additions & 0 deletions core/node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*-
#
# This file is part of SKALE Admin
#
# Copyright (C) 2019 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 logging
from enum import Enum
import docker

from core.filebeat import run_filebeat_service

from tools.str_formatters import arguments_list_string
from tools.wallet_utils import check_required_balance

from skale.utils.web3_utils import wait_receipt, check_receipt
from skale.utils.helper import ip_from_bytes
from skale.wallets.web3_wallet import public_key_to_address

docker_client = docker.from_env()
logger = logging.getLogger(__name__)


class NodeStatuses(Enum):
"""This class contains possible node statuses"""
NOT_CREATED = 0
REQUESTED = 1
CREATED = 2
ERROR = 3


class Node:
"""This class contains node registration logic"""
def __init__(self, skale, config):
self.skale = skale
self.config = config

def register(self, ip, public_ip, port, name):
"""
Main node registration function.
Parameters:
ip (str): P2P IP address that will be assigned to the node
public_ip (str): Public IP address that will be assigned to the node
port (int): Base port that will be used for sChains on the node
name (str): Node name
Returns:
dict: Execution status and node config
"""
self._log_node_info('Node create started', ip, public_ip, port, name)
if self.config.id is not None:
return self._node_already_exist()
if not check_required_balance(self.skale):
return self._insufficient_funds()
res = self.skale.manager.create_node(ip, int(port), name, public_ip)
receipt = wait_receipt(self.skale.web3, res['tx'])
try:
check_receipt(receipt)
except ValueError as err:
logger.error(arguments_list_string({'tx': res['tx']}, 'Node creation failed', 'error'))
return {'status': 0, 'errors': [err]}
self._log_node_info('Node successfully created', ip, public_ip, port, name)
res = self.skale.nodes_data.node_name_to_index(name)
self.config.id = self.skale.nodes_data.node_name_to_index(name)
# run_filebeat_service(public_ip, self.config.id, self.skale) # todo: uncomment!
return {'status': 1, 'data': self.config.all()}

def _insufficient_funds(self):
err_msg = f'Insufficient funds, re-check your wallet'
logger.error(err_msg)
return {'status': 0, 'errors': [err_msg]}

def _node_already_exist(self):
err_msg = f'Node is already installed on this machine. Node ID: {self.config.id}'
logger.error(err_msg)
return {'status': 0, 'errors': [err_msg]}

def _log_node_info(self, title, ip, public_ip, port, name):
log_params = {'IP': ip, 'Public IP': public_ip, 'Port': port, 'Name': name}
logger.info(arguments_list_string(log_params, title))

@property
def info(self):
_id = self.config.id
if _id is not None:
raw_info = self.skale.nodes_data.get(_id)
return self._transform_node_info(raw_info, _id)
return {'status': NodeStatuses.NOT_CREATED.value}

def _transform_node_info(self, node_info, node_id):
node_info['ip'] = ip_from_bytes(node_info['ip'])
node_info['publicIP'] = ip_from_bytes(node_info['publicIP'])
node_info['status'] = NodeStatuses.CREATED.value
node_info['id'] = node_id
node_info['publicKey'] = self.skale.web3.toHex(node_info['publicKey'])
node_info['owner'] = public_key_to_address(node_info['publicKey'])
return node_info
1 change: 0 additions & 1 deletion core/node/__init__.py

This file was deleted.

Loading

0 comments on commit 325e9aa

Please sign in to comment.