Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions hathorlib/nanocontracts/on_chain_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from enum import Enum
from enum import IntEnum, unique
from typing import NamedTuple

from hathorlib.conf import HathorSettings
Expand All @@ -25,14 +25,15 @@
ON_CHAIN_BLUEPRINT_VERSION: int = 1


class CodeKind(Enum):
@unique
class CodeKind(IntEnum):
""" Represents what type of code and format is being used, to allow new code/compression types in the future.
"""

PYTHON_GZIP = 'python+gzip'
PYTHON_ZLIB = 1

def __bytes__(self) -> bytes:
return self.value.encode()
return int_to_bytes(number=self.value, size=1)


class Code(NamedTuple):
Expand All @@ -47,11 +48,10 @@ class Code(NamedTuple):

def __bytes__(self) -> bytes:
# Code serialization format: [kind:variable bytes][null byte][data:variable bytes]
if self.kind is not CodeKind.PYTHON_GZIP:
if self.kind is not CodeKind.PYTHON_ZLIB:
raise ValueError('Invalid code kind value')
buf = bytearray()
buf.extend(bytes(self.kind))
buf.append(0)
buf.extend(self.data)
return bytes(buf)

Expand All @@ -63,11 +63,10 @@ def from_bytes(cls, data: bytes) -> 'Code':
check that.
"""
data = bytearray(data)
cut_at = data.index(0)
kind = CodeKind(data[0:cut_at].decode())
if kind is not CodeKind.PYTHON_GZIP:
kind = CodeKind(data[0])
if kind is not CodeKind.PYTHON_ZLIB:
raise ValueError('Code kind not supported')
compressed_code = data[cut_at + 1:]
compressed_code = data[1:]
return cls(kind, compressed_code)


Expand All @@ -83,7 +82,7 @@ def __init__(self) -> None:
self.nc_pubkey: bytes = b''
self.nc_signature: bytes = b''

self.code: Code = Code(CodeKind.PYTHON_GZIP, b'')
self.code: Code = Code(CodeKind.PYTHON_ZLIB, b'')

def serialize_code(self) -> bytes:
"""Serialization of self.code, to be used for the serialization of this transaction type."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

[tool.poetry]
name = "hathorlib"
version = "0.10.0"
version = "0.11.0"
description = "Hathor Network base objects library"
authors = ["Hathor Team <contact@hathor.network>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_on_chain_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _get_ocb(self):
b'\xf6\xf0`g\x1b0\xb6\xca\x1b\xed\x83:N\xa0\x98\xd2' \
b'\xdf\x02!\x00\xbe\xf85\xf6O`\xfed`Ip\xe2a\xc4\x03vv' \
b'\xec\x94\ny?\xde\x90\xc3\x12\x9c\xd8\xdd\xd8\xe5\r'
code = Code(CodeKind.PYTHON_GZIP, b'')
code = Code(CodeKind.PYTHON_ZLIB, b'')
ocb.code = code
return ocb

Expand Down