Skip to content

Commit

Permalink
Merge pull request #45 from skalenetwork/enhacement/SKALE-1696-add-dk…
Browse files Browse the repository at this point in the history
…g-contract

Enhacement/skale 1696 add dkg contract
  • Loading branch information
badrogger authored Nov 15, 2019
2 parents c79126f + 55f30d1 commit a0ec5cb
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 2 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"pytest==5.2.1",
"click==7.0",
"twine==1.12.1",
"mock==3.0.5",
"when-changed",
"Random-Word==1.0.4",
"pytest-cov==2.8.1"
Expand Down
2 changes: 2 additions & 0 deletions skale/contracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
from skale.contracts.functionality.schains import SChains
from skale.contracts.functionality.nodes import Nodes
from skale.contracts.functionality.validators import Validators

from skale.contracts.dkg import DKG
46 changes: 46 additions & 0 deletions skale/contracts/dkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
#
# This file is part of SKALE.py
#
# Copyright (C) 2019 SKALE Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from skale.contracts import BaseContract
from skale.utils.constants import GAS


class DKG(BaseContract):
def broadcast(self, group_index, node_index,
verification_vector, secret_key_conribution):
op = self.contract.functions.broadcast(group_index, node_index,
verification_vector,
secret_key_conribution)
return self.skale.send_tx(op, GAS['dkg_broadcast'])

def response(self, group_index, from_node_index,
secret_number, multiplied_share):
op = self.contract.functions.response(group_index, from_node_index,
secret_number,
multiplied_share)
return self.skale.send_tx(op, GAS['dkg_response'])

def allright(self, group_index, from_node_index):
op = self.contract.functions.allright(group_index, from_node_index)
return self.skale.send_tx(op, GAS['dkg_allright'])

def complaint(self, group_index, from_node_index, to_node_index):
op = self.contract.functions.complaint(group_index, from_node_index,
to_node_index)
return self.skale.send_tx(op, GAS['dkg_complaint'])
3 changes: 2 additions & 1 deletion skale/contracts_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
ContractInfo('schains_data', 'SchainsData', contracts.SChainsData,
ContractTypes.DATA, True),
ContractInfo('validators_data', 'ValidatorsData', contracts.ValidatorsData,
ContractTypes.DATA, True)
ContractTypes.DATA, True),
ContractInfo('dkg', 'Dkg', contracts.DKG, ContractTypes.API, True),
]
6 changes: 5 additions & 1 deletion skale/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
'set_periods': 200000,
'delete_node': 6000000,
'delete_node_by_root': 6000000,
'delete_schain': 6000000
'delete_schain': 6000000,
'dkg_broadcast': 8000000,
'dkg_response': 8000000,
'dkg_allright': 1000000,
'dkg_complaint': 1000000,
}

OP_TYPES = {'create_node': 0x1, 'create_schain': 0x10}
Expand Down
125 changes: 125 additions & 0 deletions tests/contracts/dkg_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
#
# This file is part of SKALE.py
#
# Copyright (C) 2019 SKALE Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import mock
import web3
from hexbytes import HexBytes


def test_broadcast(skale):
nonce = skale.web3.eth.getTransactionCount(skale.wallet.address)
gas_price = skale.web3.eth.gasPrice
contract_address = skale.dkg.address
chain_id = skale.web3.eth.chainId
expected_txn = {
'value': 0, 'gasPrice': gas_price, 'chainId': chain_id,
'gas': 8000000, 'nonce': nonce,
'to': contract_address,
'data': (
'0x5ee180c165363239666136353938643733323736386637633732366234623632313238350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000001176616c69646174696f6e2d766563746f7200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000127365637265742d6b65792d636f6e747269620000000000000000000000000000' # noqa
)
}
group_index = b'e629fa6598d732768f7c726b4b621285'
node_index = 0
validation_vector = b'validation-vector'
secret_key_conribution = b'secret-key-contrib'

exp = skale.web3.eth.account.signTransaction(
expected_txn, skale.wallet._private_key).rawTransaction
with mock.patch.object(web3.eth.Eth, 'sendRawTransaction') as send_tx_mock:
send_tx_mock.return_value = b'hexstring'
skale.dkg.broadcast(group_index, node_index,
validation_vector, secret_key_conribution)
send_tx_mock.assert_called_with(HexBytes(exp))


def test_response(skale):
nonce = skale.web3.eth.getTransactionCount(skale.wallet.address)
gas_price = skale.web3.eth.gasPrice
contract_address = skale.dkg.address
chain_id = skale.web3.eth.chainId
expected_txn = {
'value': 0, 'gasPrice': gas_price, 'chainId': chain_id,
'gas': 8000000, 'nonce': nonce,
'to': contract_address,
'data': (
'0xce86d3ade629fa6598d732768f7c726b4b6212850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000106d756c7469706c6965645f736861726500000000000000000000000000000000' # noqa
)
}
group_index = 'e629fa6598d732768f7c726b4b621285'
from_node_index = 0
secret_number = 1
multiplied_share = b'multiplied_share'

exp = skale.web3.eth.account.signTransaction(
expected_txn, skale.wallet._private_key).rawTransaction
with mock.patch.object(web3.eth.Eth, 'sendRawTransaction') as send_tx_mock:
send_tx_mock.return_value = b'hexstring'
skale.dkg.response(group_index, from_node_index,
secret_number, multiplied_share)
send_tx_mock.assert_called_with(HexBytes(exp))


def test_allright(skale):
nonce = skale.web3.eth.getTransactionCount(skale.wallet.address)
gas_price = skale.web3.eth.gasPrice
contract_address = skale.dkg.address
chain_id = skale.web3.eth.chainId
expected_txn = {
'value': 0, 'gasPrice': gas_price, 'chainId': chain_id,
'gas': 1000000, 'nonce': nonce,
'to': contract_address,
'data': (
'0xce5807eee629fa6598d732768f7c726b4b621285000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' # noqa
)
}
group_index = 'e629fa6598d732768f7c726b4b621285'
from_node_index = 0

exp = skale.web3.eth.account.signTransaction(
expected_txn, skale.wallet._private_key).rawTransaction
with mock.patch.object(web3.eth.Eth, 'sendRawTransaction') as send_tx_mock:
send_tx_mock.return_value = b'hexstring'
skale.dkg.allright(group_index, from_node_index)
send_tx_mock.assert_called_with(HexBytes(exp))


def test_complaint(skale):
nonce = skale.web3.eth.getTransactionCount(skale.wallet.address)
gas_price = skale.web3.eth.gasPrice
contract_address = skale.dkg.address
chain_id = skale.web3.eth.chainId
expected_txn = {
'value': 0, 'gasPrice': gas_price, 'chainId': chain_id,
'gas': 1000000, 'nonce': nonce,
'to': contract_address,
'data': (
'0xd76c2c4fe629fa6598d732768f7c726b4b6212850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' # noqa
)
}
group_index = 'e629fa6598d732768f7c726b4b621285'
from_node_index = 0
to_node_index = 0

exp = skale.web3.eth.account.signTransaction(
expected_txn, skale.wallet._private_key).rawTransaction
with mock.patch.object(web3.eth.Eth, 'sendRawTransaction') as send_tx_mock:
send_tx_mock.return_value = b'hexstring'
skale.dkg.complaint(group_index, from_node_index, to_node_index)
send_tx_mock.assert_called_with(HexBytes(exp))

0 comments on commit a0ec5cb

Please sign in to comment.