Skip to content

Commit

Permalink
Merge pull request #561 from skalenetwork/schain-allocation-type
Browse files Browse the repository at this point in the history
Add schain allocation type to SchainOptions
  • Loading branch information
badrogger authored Aug 19, 2024
2 parents 2a1651c + 2e2f8e1 commit 2531b0f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 87 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Launch hardhat node
working-directory: hardhat-node
run: |
docker-compose up -d && sleep 20
docker compose up -d && sleep 20
- name: Deploy manager
run: |
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
- name: Launch hardhat node
working-directory: hardhat-node
run: |
docker-compose up -d && sleep 20
docker compose up -d && sleep 20
- name: Deploy manager contracts
run: |
Expand Down
2 changes: 1 addition & 1 deletion scripts/calculate_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if [ -z $VERSION ]; then
fi


if [[ $BRANCH == 'master' ]]; then
if [[ $BRANCH == 'stable' ]]; then
echo $VERSION
exit 0
elif [[ $BRANCH == 'develop' ]]; then
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

setup(
name='skale.py',
version='6.2',
version='6.3',
description='SKALE client tools',
long_description_markdown_filename='README.md',
author='SKALE Labs',
Expand All @@ -43,10 +43,10 @@
install_requires=[
"asyncio==3.4.3",
"pyyaml==6.0",
"sgx.py==0.9dev2",
"redis==4.4.4",
"sgx.py==0.9dev3",
"redis==5.0.3",
"typing-extensions==4.9.0",
"web3==6.13.0"
"web3==6.20.2"
],

python_requires='>=3.7,<4',
Expand Down
2 changes: 1 addition & 1 deletion skale/contracts/manager/schains.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
FIELDS = [
'name', 'mainnetOwner', 'indexInOwnerList', 'partOfNode', 'lifetime', 'startDate', 'startBlock',
'deposit', 'index', 'generation', 'originator', 'chainId', 'multitransactionMode',
'thresholdEncryption'
'thresholdEncryption', 'allocationType'
]


Expand Down
51 changes: 43 additions & 8 deletions skale/dataclasses/schain_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,79 @@
# You should have received a copy of the GNU Affero General Public License
# along with SKALE.py. If not, see <https://www.gnu.org/licenses/>.

from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from skale.types.schain import SchainOption
from enum import Enum


class AllocationType(int, Enum):
DEFAULT = 0
NO_FILESTORAGE = 1
MAX_CONTRACT_STORAGE = 2
MAX_CONSENSUS_DB = 3
MAX_FILESTORAGE = 4


@dataclass
class SchainOptions:
multitransaction_mode: bool
threshold_encryption: bool
allocation_type: AllocationType

def to_tuples(self) -> list:
def to_tuples(self) -> list[SchainOption]:
return [
('multitr', bool_to_bytes(self.multitransaction_mode)),
('encrypt', bool_to_bytes(self.threshold_encryption))
('encrypt', bool_to_bytes(self.threshold_encryption)),
('alloc', int_to_bytes(self.allocation_type.value))
]


def parse_schain_options(raw_options: list) -> SchainOptions:
def parse_schain_options(raw_options: list[SchainOption]) -> SchainOptions:
"""
Parses raw sChain options from smart contracts (list of tuples).
Returns default values if nothing is set on contracts.
"""
if len(raw_options) == 0:
return get_default_schain_options()

multitransaction_mode = False
threshold_encryption = False
allocation_type = AllocationType.DEFAULT
if len(raw_options) > 0:
multitransaction_mode = bytes_to_bool(raw_options[0][1])
if len(raw_options) > 1:
threshold_encryption = bytes_to_bool(raw_options[1][1])
if len(raw_options) > 2:
allocation_type = AllocationType(bytes_to_int(raw_options[2][1]))

return SchainOptions(
multitransaction_mode=bytes_to_bool(raw_options[0][1]),
threshold_encryption=bytes_to_bool(raw_options[1][1])
multitransaction_mode=multitransaction_mode,
threshold_encryption=threshold_encryption,
allocation_type=allocation_type
)


def get_default_schain_options() -> SchainOptions:
return SchainOptions(
multitransaction_mode=False,
threshold_encryption=False
threshold_encryption=False,
allocation_type=AllocationType.DEFAULT
)


def bool_to_bytes(bool_value: bool) -> bytes:
return bool_value.to_bytes(1, byteorder='big')


def int_to_bytes(int_value: int) -> bytes:
return int.to_bytes(int_value, length=1, byteorder='big')


def bytes_to_int(bytes_value: bytes) -> int:
return int.from_bytes(bytes_value, byteorder='big')


def bytes_to_bool(bytes_value: bytes) -> bool:
return bool(int.from_bytes(bytes_value, 'big'))
2 changes: 1 addition & 1 deletion tests/manager/schains_internal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def test_get_raw(skale):
schain_arr = skale.schains_internal.get_raw(DEFAULT_SCHAIN_ID)
assert len(FIELDS) == len(schain_arr) + 3 # +1 for chainId + options
assert len(FIELDS) == len(schain_arr) + 4 # +1 for chainId + options


def test_get_raw_not_exist(skale):
Expand Down
Loading

0 comments on commit 2531b0f

Please sign in to comment.