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/025FA781D389D7C7CAAF836E5E47ABED6CEFD2D928.abi new file mode 100644 index 0000000..5209198 Binary files /dev/null and b/test/example-abis/025FA781D389D7C7CAAF836E5E47ABED6CEFD2D928.abi differ 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) +