Skip to content

Commit

Permalink
Merge pull request #175 from skalenetwork/enhancement/SKALE-2575-sm-1…
Browse files Browse the repository at this point in the history
….3-develop.0

SKALE-2575 Support 1.3.develop.0 skale manager
  • Loading branch information
badrogger authored May 14, 2020
2 parents 6a70781 + a364735 commit 4dce9b3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
ETH_PRIVATE_KEY: ${{ secrets.ETH_PRIVATE_KEY }}
ENDPOINT: ${{ secrets.ENDPOINT }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
MANAGER_TAG: "1.2.0-develop.1"
MANAGER_TAG: "1.3.0-develop.0"
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
79 changes: 25 additions & 54 deletions skale/contracts/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import logging
import socket

from eth_abi import encode_abi

from skale.contracts import BaseContract, transaction_method
from skale.utils import helper
from skale.utils.constants import GAS, OP_TYPES
from skale.utils.constants import GAS

logger = logging.getLogger(__name__)

Expand All @@ -37,41 +38,24 @@ def create_node(self, ip, port, name, public_ip=None):
logger.info(
f'create_node: {ip}:{port}, public ip: {public_ip} name: {name}')
skale_nonce = helper.generate_nonce()
pk = self.skale.wallet.public_key

if not public_ip:
public_ip = ip
pk = self.skale.wallet.public_key

transaction_data = self.create_node_data_to_bytes(
ip, public_ip, port, name, pk, skale_nonce)
return self.contract.functions.createNode(transaction_data)

def create_node_data_to_bytes(self, ip, public_ip, port, name, pk, nonce):
pk_fix = str(pk)[2:]

type_bytes = OP_TYPES['create_node'].to_bytes(1, byteorder='big')
port_bytes = port.to_bytes(2, byteorder='big')
nonce_bytes = nonce.to_bytes(2, byteorder='big') # todo
ip_bytes = socket.inet_aton(ip)
public_ip_bytes = socket.inet_aton(public_ip)
pk_bytes = bytes.fromhex(pk_fix)
name_bytes = name.encode()

data_bytes = (
type_bytes
+ port_bytes # noqa: W503
+ nonce_bytes # noqa: W503
+ ip_bytes # noqa: W503
+ public_ip_bytes # noqa: W503
+ pk_bytes # noqa: W503
+ name_bytes # noqa: W503
)
logger.info(
f'create_node_data_to_bytes bytes: {self.skale.web3.toHex(data_bytes)}'
# pk to bytes without 0x
pk_bytes = bytes.fromhex(str(pk)[2:])

return self.contract.functions.createNode(
port,
skale_nonce,
ip_bytes,
public_ip_bytes,
pk_bytes,
name
)

return data_bytes

def create_default_schain(self, name):
lifetime = 3600
nodes_type = 4
Expand All @@ -87,42 +71,29 @@ def create_schain(self, lifetime, type_of_nodes, deposit, name):

token = self.skale.get_contract_by_name('token')
skale_nonce = helper.generate_nonce()
transaction_data = self.create_schain_data_to_bytes(
lifetime, type_of_nodes, name, skale_nonce)

return token.contract.functions.send(self.address, deposit,
transaction_data)

def create_schain_data_to_bytes(self, lifetime, type_of_nodes, name,
nonce):
type_bytes = OP_TYPES['create_schain'].to_bytes(1, byteorder='big')

lifetime_hex = lifetime.to_bytes(32, byteorder='big')
type_of_nodes_hex = type_of_nodes.to_bytes(1, byteorder='big')
nonce_hex = nonce.to_bytes(2, byteorder='big')
name_hex = name.encode()

data_bytes = (type_bytes + lifetime_hex +
type_of_nodes_hex + nonce_hex + name_hex)
logger.info(
f'create_schain_data_to_bytes bytes:'
f'{self.skale.web3.toHex(data_bytes)}'
tx_data = encode_abi(
['uint', 'uint8', 'uint16', 'string'],
[lifetime, type_of_nodes, skale_nonce, name]
)
return data_bytes
return token.contract.functions.send(self.address, deposit, tx_data)

@transaction_method(gas_limit=GAS['get_bounty'])
def get_bounty(self, node_id):
return self.contract.functions.getBounty(node_id)

@transaction_method(gas_limit=GAS['send_verdict'])
def send_verdict(self, validator, node_id, downtime, latency):
return self.contract.functions.sendVerdict(validator, node_id, downtime,
latency)
return self.contract.functions.sendVerdict(
validator,
[node_id, downtime, latency]
)

@transaction_method(gas_limit=GAS['send_verdicts'])
def send_verdicts(self, validator, nodes_ids, downtimes, latencies):
return self.contract.functions.sendVerdicts(validator, nodes_ids,
downtimes, latencies)
return self.contract.functions.sendVerdicts(
validator,
[nodes_ids, downtimes, latencies]
)

@transaction_method(gas_limit=GAS['delete_node'])
def deregister(self, node_id):
Expand Down
2 changes: 0 additions & 2 deletions skale/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
'set_launch_timestamp': 200000,
}

OP_TYPES = {'create_node': 0x1, 'create_schain': 0x10}

LONG_LINE = '=' * 100

SCHAIN_TYPES = {
Expand Down
71 changes: 19 additions & 52 deletions tests/contracts/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,14 @@
from hexbytes import HexBytes
from mock import Mock

import skale.utils.helper as helper
from skale.dataclasses.tx_res import TransactionFailedError
from skale.utils.constants import GAS
from skale.utils.web3_utils import private_key_to_public

from skale.utils.contracts_provision.main import (
generate_random_node_data, generate_random_schain_data
)


def test_create_node_data_to_bytes(skale):
ip, public_ip, port, name = generate_random_node_data()
skale_nonce = helper.generate_nonce()
pk = private_key_to_public(skale.wallet._private_key)

bytes_data = skale.manager.create_node_data_to_bytes(
ip, public_ip, port, name, pk, skale_nonce)
name_bytes = name.encode()

assert type(bytes_data) is bytes
assert bytes_data.find(name_bytes) != -1


def test_create_schain_data_to_bytes(skale):
type_of_nodes, lifetime_seconds, name = generate_random_schain_data()
skale_nonce = helper.generate_nonce()

bytes_data = skale.manager.create_schain_data_to_bytes(
lifetime_seconds,
type_of_nodes,
name,
skale_nonce
)
name_bytes = name.encode()

assert type(bytes_data) is bytes
assert bytes_data.find(name_bytes) != -1


def test_get_bounty(skale):
node_id = 0
nonce = skale.web3.eth.getTransactionCount(skale.wallet.address)
Expand Down Expand Up @@ -78,7 +47,7 @@ def test_send_verdict(skale):
'gas': 200000, 'nonce': nonce,
'to': contract_address,
'data': (
'0xd77de31c000000000000000000000000000000000000000'
'0x96a1ce46000000000000000000000000000000000000000'
'0000000000000000000000000000000000000000000000000'
'000000000000000000000000000000000000007b000000000'
'0000000000000000000000000000000000000000000000000'
Expand Down Expand Up @@ -110,26 +79,24 @@ def test_send_verdicts(skale):
'value': 0, 'gasPrice': skale.gas_price, 'chainId': chain_id,
'gas': 8000000, 'nonce': nonce,
'to': contract_address,
'data': ('0x25b2114b000000000000000000000000000000000000000000'
'0000000000000000000000000000000000000000000000000000'
'0000000000000000000000000000000080000000000000000000'
'0000000000000000000000000000000000000000000100000000'
'0000000000000000000000000000000000000000000000000000'
'0001800000000000000000000000000000000000000000000000'
'0000000000000000030000000000000000000000000000000000'
'00000000000000000000000000007b0000000000000000000000'
'0000000000000000000000000000000000000000e70000000000'
'0000000000000000000000000000000000000000000000000001'
'c300000000000000000000000000000000000000000000000000'
'0000000000000300000000000000000000000000000000000000'
'0000000000000000000000000100000000000000000000000000'
'0000000000000000000000000000000000000200000000000000'
'0000000000000000000000000000000000000000000000000300'
'0000000000000000000000000000000000000000000000000000'
'0000000003000000000000000000000000000000000000000000'
'000000000000000000000a000000000000000000000000000000'
'0000000000000000000000000000000014000000000000000000'
'000000000000000000000000000000000000000000001e')

'data': (
'0x42c81d610000000000000000000000000000000000000000'
'00000000000000000000000000000000000000000000000000'
'0000000000000000000000000000000000004000000000000'
'00000000000000000000000000000000000000000000000000'
'00300000000000000000000000000000000000000000000000'
'0000000000000007b000000000000000000000000000000000'
'00000000000000000000000000000e70000000000000000000'
'0000000000000000000000000000000000000000001c300000'
'00000000000000000000000000000000000000000000000000'
'00000000100000000000000000000000000000000000000000'
'00000000000000000000002000000000000000000000000000'
'00000000000000000000000000000000000030000000000000'
'00000000000000000000000000000000000000000000000000'
'a0000000000000000000000000000000000000000000000000'
'00000000000001400000000000000000000000000000000000'
'0000000000000000000000000001e')
}
exp = skale.web3.eth.account.signTransaction(
expected_txn, skale.wallet._private_key).rawTransaction
Expand Down

0 comments on commit 4dce9b3

Please sign in to comment.