Skip to content

Commit

Permalink
Merge pull request #163 from skalenetwork/enhancement/SKALE-3350-new-…
Browse files Browse the repository at this point in the history
…tx-pipeline

SKALE-3350 Add RedisWalletAdapter
  • Loading branch information
dmytrotkk authored Jun 9, 2021
2 parents a988c70 + fc79197 commit 8e63bd9
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
DB_USER: ${{ secrets.DB_USER }}
DB_ROOT_PASSWORD: ${{ secrets.DB_ROOT_PASSWORD }}
DB_PORT: ${{ secrets.DB_PORT }}
MANAGER_TAG: "1.8.0-develop.15"
MANAGER_TAG: "1.8.0-develop.44"

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
Expand Down
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
path = helper-scripts
url = https://github.com/skalenetwork/helper-scripts.git
branch = develop

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
2.0.1
2 changes: 1 addition & 1 deletion bounty_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import tenacity
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED
from apscheduler.schedulers.background import BackgroundScheduler
from skale.transactions.result import TransactionError
from skale.transactions.exceptions import TransactionError
from web3.logs import DISCARD

from configs import (DELAY_AFTER_ERR, LONG_LINE, MISFIRE_GRACE_TIME,
Expand Down
14 changes: 14 additions & 0 deletions configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@
CONFIG_CHECK_PERIOD = 30 # in seconds
MISFIRE_GRACE_TIME = 365 * 24 * 60 * 60 # in seconds
DELAY_AFTER_ERR = 60 # in seconds

SGX_SERVER_URL = os.getenv('SGX_SERVER_URL')
SGX_CERTIFICATES_FOLDER_NAME = os.getenv('SGX_CERTIFICATES_DIR_NAME')

if SGX_CERTIFICATES_FOLDER_NAME:
SGX_CERTIFICATES_FOLDER = os.path.join(
NODE_DATA_PATH,
SGX_CERTIFICATES_FOLDER_NAME
)
else:
SGX_CERTIFICATES_FOLDER = os.getenv('SGX_CERTIFICATES_FOLDER')

DEFAULT_POOL = 'transactions'
REDIS_URI = os.getenv('REDIS_URI', 'redis://@127.0.0.1:6379')
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
skale.py==4.1b26
schedule==0.6.0
tenacity==6.2.0
apscheduler==3.6.3
peewee==3.14.0
PyMySQL==0.10.1
schedule==0.6.0
skale.py==5.1dev4
tenacity==6.2.0
3 changes: 3 additions & 0 deletions tests/prepare_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from skale import Skale
from skale.utils.web3_utils import init_web3
from skale.wallets import Web3Wallet
from skale.utils.contracts_provision.main import add_test_permissions


from tests.constants import (D_VALIDATOR_DESC, D_VALIDATOR_FEE, D_VALIDATOR_ID,
D_VALIDATOR_MIN_DEL, D_VALIDATOR_NAME, ENDPOINT,
Expand All @@ -28,6 +30,7 @@ def validator_exist(skale):

def setup_validator(skale):
"""Create and activate a validator"""
add_test_permissions(skale)
if not validator_exist(skale):
create_validator(skale)
enable_validator(skale)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_bounty_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

import pytest
from freezegun import freeze_time
from skale.transactions.result import TransactionError
from skale.transactions.exceptions import TransactionError

import bounty_agent
from configs import RETRY_INTERVAL
from tests.prepare_validator import get_active_ids, go_to_date
from tools.exceptions import NodeNotFoundException
from tools.helper import check_if_node_is_registered
Expand Down Expand Up @@ -106,8 +105,8 @@ def test_run_agent(skale, node_id):

with freeze_time(datetime.utcfromtimestamp(reward_date)):
bounty_collector.run()
print(f'Sleep for {RETRY_INTERVAL} sec')
time.sleep(RETRY_INTERVAL)
print('Sleep for 1 sec')
time.sleep(1)
bounty_collector.stop()

bounties = get_bounty_events(skale, bounty_collector.id)
Expand Down
54 changes: 49 additions & 5 deletions tools/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@

import json
import logging
import os
import re
from enum import Enum

import redis
import requests
import tenacity
from skale import Skale
from skale.wallets import RPCWallet

from configs import CONFIG_CHECK_PERIOD, NOTIFIER_URL, STATE_FILEPATH
from skale.utils.web3_utils import init_web3
from skale.wallets import RedisWalletAdapter, SgxWallet

from configs import (
CONFIG_CHECK_PERIOD,
DEFAULT_POOL,
NOTIFIER_URL,
NODE_CONFIG_FILEPATH,
REDIS_URI,
SGX_CERTIFICATES_FOLDER,
SGX_SERVER_URL,
STATE_FILEPATH
)
from configs.web3 import ABI_FILEPATH, ENDPOINT
from tools.exceptions import NodeNotFoundException

Expand All @@ -41,10 +51,24 @@


def init_skale():
wallet = RPCWallet(os.environ['TM_URL'])
wallet = init_wallet()
return Skale(ENDPOINT, ABI_FILEPATH, wallet, state_path=STATE_FILEPATH)


def init_wallet(pool=DEFAULT_POOL):
sgx_keyname = get_sgx_keyname_from_config(NODE_CONFIG_FILEPATH)
cpool = redis.ConnectionPool.from_url(REDIS_URI)
rs = redis.Redis(connection_pool=cpool)
web3 = init_web3(ENDPOINT)
sgx_wallet = SgxWallet(
web3=web3,
sgx_endpoint=SGX_SERVER_URL,
key_name=sgx_keyname,
path_to_cert=SGX_CERTIFICATES_FOLDER
)
return RedisWalletAdapter(rs, pool, sgx_wallet)


def get_agent_name(name):
name_parts = re.findall('[A-Z][^A-Z]*', name)
return '-'.join(name_parts).lower()
Expand Down Expand Up @@ -79,6 +103,26 @@ def get_id_from_config(node_config_filepath) -> int:
raise err


@tenacity.retry(
wait=tenacity.wait_fixed(CONFIG_CHECK_PERIOD),
retry=tenacity.retry_if_exception_type(KeyError) | tenacity.retry_if_exception_type(
FileNotFoundError))
def get_sgx_keyname_from_config(node_config_filepath) -> int:
"""Gets sgx keyname from config file."""
global _config_first_read
try:
logger.debug('Reading node id from config file...')
with open(node_config_filepath) as json_file:
data = json.load(json_file)
return data['sgx_key_name']
except (FileNotFoundError, KeyError) as err:
if _config_first_read:
logger.warning(
'Cannot read a sgx_key_name from config file?')
_config_first_read = False
raise err


class MsgIcon(Enum):
INFO = '\u2705'
WARNING = '\u26a0\ufe0f'
Expand Down

0 comments on commit 8e63bd9

Please sign in to comment.