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
6 changes: 5 additions & 1 deletion jamcodec/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json

from jamcodec.base import JamCodecTypeDef, JamCodecType, JamBytes
from jamcodec.types import Struct, Option, Vec, Enum
from jamcodec.types import Struct, StructObject, Option, Vec, Enum

T = TypeVar('T')

Expand Down Expand Up @@ -69,6 +69,10 @@ def from_codec_type(cls: Type[T], scale_type: JamCodecType) -> T:

def to_jam_bytes(self) -> JamBytes:
scale_obj = self.to_codec_type()
if type(scale_obj) in (JamCodecType, StructObject):
# The value is already normalized; only the encoded bytes are needed.
return scale_obj.type_def.encode(scale_obj.value_serialized)
# Preserve encoding and validation hooks on custom codec wrappers.
return scale_obj.encode()

@classmethod
Expand Down
35 changes: 35 additions & 0 deletions test/test_collection_buffers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from jamcodec.base import JamBytes
from jamcodec.types import Array, Map, Struct, Tuple, U16, Vec


@pytest.mark.parametrize("codec,value,expected", [
(Struct(first=U16, second=U16), {"first": 1, "second": 2}, b"\x01\x00\x02\x00"),
(Tuple(U16, U16), (1, 2), b"\x01\x00\x02\x00"),
(Array(U16, 2), [1, 2], b"\x01\x00\x02\x00"),
(Vec(U16), [1, 2], b"\x02\x01\x00\x02\x00"),
(Map(U16, U16), {2: 1, 1: 2}, b"\x02\x01\x00\x02\x00\x02\x00\x01\x00"),
])
def test_collection_encoding_has_complete_readable_buffer(codec, value, expected):
encoded = codec.encode(value)

assert encoded.to_bytes() == expected
assert encoded.offset == 0
assert encoded.length == len(expected)
codec.new().decode(encoded, check_remaining=True)
assert encoded.get_remaining_length() == 0


def test_vec_does_not_mutate_or_share_preencoded_item_buffer():
item = JamBytes(b"\x01\x00")
item.get_next_bytes(1)
encoded = Vec(U16).encode([item, item])

assert item.to_bytes() == b"\x01\x00"
assert item.offset == 1
assert encoded.to_bytes() == b"\x02\x01\x00\x01\x00"
item.data[0] = 9
assert encoded.to_bytes() == b"\x02\x01\x00\x01\x00"
encoded.data[1] = 7
assert item.to_bytes() == b"\x09\x00"
88 changes: 87 additions & 1 deletion test/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from typing import Optional, Type, Union, List

from jamcodec.base import JamBytes, JamCodecType
from jamcodec.exceptions import ScaleDeserializeException, ScaleEncodeException
from jamcodec.mixins import Serializable, T
from jamcodec.types import H256, U8, Array, Enum, Option, Null, Bytes
from jamcodec.types import H256, U8, U16, Array, Enum, Option, Null, Bytes, Map, Struct, StructObject, Vec


# Test definitions
Expand Down Expand Up @@ -151,6 +152,91 @@ def test_from_to_scale_bytes(self):

self.assertEqual(self.test_obj, validator_obj)

def test_binary_enum_encodings(self):
cases = (
(CustomErrorCode.bad_slot, b'\x00'),
(CustomErrorCode.duplicate_ticket, b'\x06'),
(Output(ok=OutputMarks(epoch_mark=None)), b'\x00\x00'),
(Output(err=CustomErrorCode.duplicate_ticket), b'\x01\x06'),
(WorkExecResult(ok=b''), b'\x00\x00'),
(WorkExecResult(ok=b'abc'), b'\x00\x03abc'),
(WorkExecResult(out_of_gas=Null), b'\x01'),
)
for value, expected in cases:
with self.subTest(value=value):
serialized = value.serialize()
encoded = value.to_jam_bytes()
self.assertEqual(expected, encoded.to_bytes())
self.assertEqual(0, encoded.offset)
self.assertEqual(serialized, value.serialize())

def test_binary_nested_collections_roundtrip(self):
@dataclass
class Packet(Serializable):
payload: bytes = field(metadata={'codec': Bytes})
numbers: list = field(metadata={'codec': Vec(Option(U16))})
labels: dict = field(metadata={'codec': Map(U8, Bytes)})

packet = Packet(b'abc', [None, 258], {2: b'b', 1: b'a'})
encoded = packet.to_jam_bytes()
self.assertEqual(b'\x03abc\x02\x00\x01\x02\x01\x02\x01\x01a\x02\x01b', encoded.to_bytes())
self.assertEqual(packet, Packet.from_jam_bytes(encoded))
self.assertEqual(encoded.length, encoded.offset)

def test_binary_encoding_returns_independent_buffers(self):
value = Output(ok=OutputMarks(epoch_mark=None))
first = value.to_jam_bytes()
first.get_next_bytes(1)
second = value.to_jam_bytes()

self.assertEqual(1, first.offset)
self.assertEqual(0, second.offset)
second.data[0] = 99
self.assertEqual(b'\x00\x00', first.to_bytes())
self.assertEqual(b'\x00\x00', value.to_jam_bytes().to_bytes())

value.ok.epoch_mark = EpochMark(bytes(32), [bytes(32)] * 6)
self.assertEqual(b'\x00\x01' + bytes(32 * 7), value.to_jam_bytes().to_bytes())
self.assertEqual(b'\x00\x00', first.to_bytes())

def test_binary_encoding_preserves_validation(self):
@dataclass
class Packet(Serializable):
number: int = field(metadata={'codec': U8})
digest: bytes = field(metadata={'codec': H256})

for value in (Packet(256, bytes(32)), Packet(1, b'short')):
with self.subTest(value=value):
with self.assertRaises(ScaleEncodeException):
value.to_jam_bytes()

def test_binary_encoding_preserves_custom_encoder(self):
class PrefixedCodec(StructObject):
def encode(self, value=None):
return JamBytes(b'\xaa' + super().encode(value).to_bytes())

@dataclass
class Packet(Serializable):
number: int
_codec_type_def = Struct(number=U8).impl(PrefixedCodec)

self.assertEqual(b'\xaa\x07', Packet(7).to_jam_bytes().to_bytes())

def test_binary_encoding_preserves_custom_validation_hook(self):
class ValidatedCodec(StructObject):
def deserialize(self, value):
if self._data is not None:
raise ScaleDeserializeException('custom post-encode validation')
return super().deserialize(value)

@dataclass
class Packet(Serializable):
number: int
_codec_type_def = Struct(number=U8).impl(ValidatedCodec)

with self.assertRaisesRegex(ScaleDeserializeException, 'custom post-encode validation'):
Packet(7).to_jam_bytes()


if __name__ == '__main__':
unittest.main()
Loading