Skip to content

Commit

Permalink
remove length encoding/decoding, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Jun 20, 2024
1 parent f799f7d commit 2d6c65e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
8 changes: 2 additions & 6 deletions multiversx_sdk/abi/array_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from typing import Any, Callable, List, Optional

from multiversx_sdk.abi.interface import ISingleValue
from multiversx_sdk.abi.shared import (convert_native_value_to_list,
decode_length, encode_length)
from multiversx_sdk.abi.shared import convert_native_value_to_list


class ArrayValue:
Expand All @@ -20,17 +19,14 @@ def __init__(self,
self.item_creator = item_creator

def encode_nested(self, writer: io.BytesIO):
encode_length(writer, len(self.items))
self._encode_list_items(writer)

def encode_top_level(self, writer: io.BytesIO):
self._encode_list_items(writer)

def decode_nested(self, reader: io.BytesIO):
length = decode_length(reader)

self.items = []
for _ in range(length):
for _ in range(self.length):
self._decode_list_item(reader)

def decode_top_level(self, data: bytes):
Expand Down
39 changes: 38 additions & 1 deletion multiversx_sdk/abi/array_value_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from multiversx_sdk.abi.array_value import ArrayValue
from multiversx_sdk.abi.biguint_value import BigUIntValue
from multiversx_sdk.abi.codec import Codec
from multiversx_sdk.abi.fields import Field
from multiversx_sdk.abi.small_int_values import U32Value
from multiversx_sdk.abi.small_int_values import U16Value, U32Value
from multiversx_sdk.abi.struct_value import StructValue


Expand Down Expand Up @@ -69,3 +70,39 @@ def test_set_payload_and_get_payload():
with pytest.raises(ValueError, match="cannot convert native value to list, because of: 'int' object is not iterable"):
value = ArrayValue(length=1, item_creator=lambda: U32Value())
value.set_payload(42)


def test_encode_top_level():
codec = Codec()
array_value = ArrayValue(length=3, items=[U16Value(1), U16Value(2), U16Value(3)])

encoded = codec.encode_top_level(array_value)
assert encoded.hex() == "000100020003"


def test_encode_nested():
codec = Codec()
array_value = ArrayValue(length=3, items=[U16Value(1), U16Value(2), U16Value(3)])

encoded = codec.encode_nested(array_value)
assert encoded.hex() == "000100020003"


def test_decode_top_level():
codec = Codec()
data = bytes.fromhex("000100020003")

destinattion = ArrayValue(length=3, item_creator=lambda: U16Value())
codec.decode_top_level(data, destinattion)

assert destinattion.items == [U16Value(1), U16Value(2), U16Value(3)]


def test_decode_nested():
codec = Codec()
data = bytes.fromhex("000100020003")

destinattion = ArrayValue(length=3, item_creator=lambda: U16Value())
codec.decode_nested(data, destinattion)

assert destinattion.items == [U16Value(1), U16Value(2), U16Value(3)]

0 comments on commit 2d6c65e

Please sign in to comment.