1
0

Fixed parsing of AvlTree
Some checks failed
Python Package / Package (push) Failing after 25s

This commit is contained in:
Jon Michael Aanes 2024-05-03 10:18:54 +02:00
parent ae1c7d984a
commit b212b0136d
5 changed files with 54 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
__pycache__/
*.egg-info/
.mypy_cache/
/build/

View File

@ -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)

19
test/test_address.py Normal file
View File

@ -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

18
test/test_binaryreader.py Normal file
View File

@ -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)