From b212b0136d4add7c4fc06ae898d470cd842ab08e Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 3 May 2024 10:18:54 +0200 Subject: [PATCH] Fixed parsing of AvlTree --- .gitignore | 2 ++ pbcabi/model.py | 15 ++++++++++++++ ...A781D389D7C7CAAF836E5E47ABED6CEFD2D928.abi | Bin 0 -> 4007 bytes test/test_address.py | 19 ++++++++++++++++++ test/test_binaryreader.py | 18 +++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 test/example-abis/025FA781D389D7C7CAAF836E5E47ABED6CEFD2D928.abi create mode 100644 test/test_address.py create mode 100644 test/test_binaryreader.py diff --git a/.gitignore b/.gitignore index 9c882b7..bade9fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ __pycache__/ *.egg-info/ +.mypy_cache/ +/build/ diff --git a/pbcabi/model.py b/pbcabi/model.py index 793083b..b25c023 100644 --- a/pbcabi/model.py +++ b/pbcabi/model.py @@ -14,6 +14,9 @@ from enum import Enum import enforce_typing from .binaryreader import BinaryReader import pbcabi.data as data +import logging + +logger = logging.getLogger(__name__) Identifier = str @@ -242,6 +245,16 @@ class AvlTreeTypeSpec(MapTypeSpec): ''' DISCRIMINANT = 0x19 + @staticmethod + def read_from(reader: BinaryReader): + ''' + Deserialize this 'TypeSpec' type from 'BinaryReader'. + ''' + type_key = TypeSpec.read_from(reader) + type_value = TypeSpec.read_from(reader) + type_spec = AvlTreeTypeSpec(type_key, type_value) + return type_spec + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True, slots=True) class InlineMapTypeSpec(MapTypeSpec): @@ -528,6 +541,8 @@ class StructTypeSpec(NamedTypeSpec): ''' Deserialize this 'TypeSpec' type from 'BinaryReader'. ''' + + logger.debug('Reading Struct') name = reader.readString() variants = reader.readList(FieldAbi.read_from) type_spec = StructTypeSpec(name, type_index, variants) diff --git a/test/example-abis/025FA781D389D7C7CAAF836E5E47ABED6CEFD2D928.abi b/test/example-abis/025FA781D389D7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qTTv^Ho-cmus$Zbc9GVNi3*@mb{DJR$*p18JuK`g z!62;Swp<&wCz*}~eJi?{EuUp-njp+&icmrd^(y+-99GSv`c2<1&*>CbFFqx}GNq3I zaYf^CIsLz&Z9LP8J*&Pa%K3fOtPa@&%1Af+hh^*M`j`dWJLZeR`EK YnzAzh%R^$hX{AfG%K(0@0!FO&4;N_2O8@`> literal 0 HcmV?d00001 diff --git a/test/test_address.py b/test/test_address.py new file mode 100644 index 0000000..17c3898 --- /dev/null +++ b/test/test_address.py @@ -0,0 +1,19 @@ +import pytest +from pathlib import Path +from pbcabi.binaryreader import BinaryReader +import pbcabi.model +from pbcabi.data import BlockchainAddress + +EXAMPLE_SHARDS = [ + ("Shard1", 2, "000000000000000000000000000000000000000001"), + ("Shard0", 2, "000000000000000000000000000000000000000002"), + ("Shard0", 3, "025FA781D389D7C7CAAF836E5E47ABED6CEFD2D928"), + ("Shard1", 3, "04FE17D1009372C8ED3AC5B790B32E349359C2C7E9"), + ("Shard0", 3, "01A2020BB33EF9E0323C7A3210D5CB7FD492AA0D65"), +] + +@pytest.mark.parametrize('shard_id,num_shards,address', EXAMPLE_SHARDS) +def test_parse_abi(shard_id: int, num_shards:int, address: str): + address = BlockchainAddress.from_hex_hash(address) + assert address.shard_id(num_shards) == shard_id + diff --git a/test/test_binaryreader.py b/test/test_binaryreader.py new file mode 100644 index 0000000..27e8c93 --- /dev/null +++ b/test/test_binaryreader.py @@ -0,0 +1,18 @@ +import pytest +from pathlib import Path +from pbcabi.binaryreader import BinaryReader +import pbcabi.model + +def assert_parse(hex, value): + derp = bytes.fromhex(hex) + assert BinaryReader(derp).readSignedIntBigEndian(1) == value + +def test_parse(): + assert_parse('00', 0) + assert_parse('01', 1) + assert_parse('10', 0x10) + assert_parse('79', 0x79) + assert_parse('80', -0x80) + assert_parse('F0', -16) + assert_parse('FF', -1) +