From 9f63b1539d321c5d84ded49095376116c64ae69b Mon Sep 17 00:00:00 2001 From: badroger Date: Wed, 13 Nov 2019 20:12:07 +0200 Subject: [PATCH 1/2] SKALE-1696 Add dkg contract to skale.py --- setup.py | 1 + skale/contracts/__init__.py | 2 + skale/contracts/dkg.py | 46 +++++++++++++ skale/contracts_info.py | 3 +- skale/transactions/tools.py | 1 + skale/utils/constants.py | 6 +- tests/contracts/dkg_test.py | 125 ++++++++++++++++++++++++++++++++++++ 7 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 skale/contracts/dkg.py create mode 100644 tests/contracts/dkg_test.py diff --git a/setup.py b/setup.py index 0d852dc9..c3028cfb 100644 --- a/setup.py +++ b/setup.py @@ -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" diff --git a/skale/contracts/__init__.py b/skale/contracts/__init__.py index a9b355a3..aa5d2f87 100644 --- a/skale/contracts/__init__.py +++ b/skale/contracts/__init__.py @@ -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 diff --git a/skale/contracts/dkg.py b/skale/contracts/dkg.py new file mode 100644 index 00000000..6a7cf57b --- /dev/null +++ b/skale/contracts/dkg.py @@ -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 . + +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']) diff --git a/skale/contracts_info.py b/skale/contracts_info.py index 63fcf529..3ed39c95 100644 --- a/skale/contracts_info.py +++ b/skale/contracts_info.py @@ -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), ] diff --git a/skale/transactions/tools.py b/skale/transactions/tools.py index f855e901..1e450e11 100644 --- a/skale/transactions/tools.py +++ b/skale/transactions/tools.py @@ -40,6 +40,7 @@ def send_to_tx_manager(tx_manager, operation, gas_amount): def sign_and_send(web3, method, gas_amount, wallet): nonce = get_eth_nonce(web3, wallet.address) tx_dict = build_tx_dict(method, gas_amount, nonce) + print(f'IVD tx_dict {tx_dict}') signed_tx = wallet.sign(tx_dict) tx = web3.eth.sendRawTransaction(signed_tx.rawTransaction) return tx diff --git a/skale/utils/constants.py b/skale/utils/constants.py index 426482c4..82b8a7a6 100644 --- a/skale/utils/constants.py +++ b/skale/utils/constants.py @@ -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} diff --git a/tests/contracts/dkg_test.py b/tests/contracts/dkg_test.py new file mode 100644 index 00000000..16b82f3c --- /dev/null +++ b/tests/contracts/dkg_test.py @@ -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 . + +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)) From a8c1ade3d3e65215e1af2c8f48027bf29d54e9aa Mon Sep 17 00:00:00 2001 From: badroger Date: Fri, 15 Nov 2019 16:53:05 +0200 Subject: [PATCH 2/2] Remove redundant print debug --- skale/transactions/tools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/skale/transactions/tools.py b/skale/transactions/tools.py index 1e450e11..f855e901 100644 --- a/skale/transactions/tools.py +++ b/skale/transactions/tools.py @@ -40,7 +40,6 @@ def send_to_tx_manager(tx_manager, operation, gas_amount): def sign_and_send(web3, method, gas_amount, wallet): nonce = get_eth_nonce(web3, wallet.address) tx_dict = build_tx_dict(method, gas_amount, nonce) - print(f'IVD tx_dict {tx_dict}') signed_tx = wallet.sign(tx_dict) tx = web3.eth.sendRawTransaction(signed_tx.rawTransaction) return tx