Skip to content

Commit

Permalink
cleanup tuple type extraction, tiny test
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Jan 20, 2025
1 parent 1959e2d commit e095c16
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/dipdup/codegen/substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def scale_type_to_jsonschema(
schema['type'] = 'string'
# FIXME: We need to parse weird values like `Tuple:staging_xcm:v4:location:Locationstaging_xcm:v4:location:Location`; mind the missing delimeters
elif type_string.startswith('Tuple:'):
inner_types = extract_tuple_inner_types(type_string, 0, type_registry)
inner_types = extract_tuple_inner_types(type_string, type_registry)
schema['type'] = 'array'
schema['items'] = [scale_type_to_jsonschema(type_registry, t) for t in inner_types]

Expand Down
1 change: 0 additions & 1 deletion src/dipdup/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ async def _process_level_data(
matched_handlers = self._match_level_data(self._config.handlers, level_data)

total_matched = len(matched_handlers)
# metrics.set_index_handlers_matched(total_matched)
metrics.handlers_matched[self.name] += total_matched
metrics.time_in_matcher[self.name] += time.time() - started_at

Expand Down
15 changes: 6 additions & 9 deletions src/dipdup/runtimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_type_key(type_: str) -> str:
return type_.split(':')[-1].lower()


def extract_tuple_inner_types(type_: str, length: int, registry: dict[str, Any]) -> list[str]:
def extract_tuple_inner_types(type_: str, registry: dict[str, Any]) -> list[str]:
inner = type_[6:]
inner_types = []

Expand Down Expand Up @@ -222,7 +222,6 @@ def parse(value: Any, type_: str) -> Any:
if isinstance(value, list) and type_.startswith('Tuple:'):
inner_types = extract_tuple_inner_types(
type_=type_,
length=len(value),
registry=self.runtime_config.type_registry,
)
return [parse(v, t) for v, t in zip(value, inner_types, strict=True)]
Expand All @@ -238,15 +237,13 @@ def parse(value: Any, type_: str) -> Any:
else:
raise NotImplementedError('Unsupported Vec type')

try:
scale_obj = self.runtime_config.create_scale_object(
type_string=type_,
data=ScaleBytes(value) if isinstance(value, str) else value,
)
except (NotImplementedError, AssertionError) as e:
_logger.error('unsupported type `%s`: %s', type_, e)
if not isinstance(value, str):
return value

scale_obj = self.runtime_config.create_scale_object(
type_string=type_,
data=ScaleBytes(value),
)
return scale_obj.process()

for (key, value), type_ in zip(args.items(), arg_types, strict=True):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_datasources/test_substrate_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from dipdup.config.substrate_node import SubstrateNodeDatasourceConfig
from dipdup.datasources.substrate_node import SubstrateNodeDatasource
from dipdup.runtimes import extract_tuple_inner_types
from dipdup.runtimes import get_type_registry


def get_dummy_node() -> 'SubstrateNodeDatasource':
config = SubstrateNodeDatasourceConfig(
kind='substrate.node',
url='https://polkadot-asset-hub-rpc.polkadot.io',
)
config._name = 'test'
return SubstrateNodeDatasource(config)


async def test_extract_tuple_inner_types() -> None:
datasource = get_dummy_node()

runtime_config = datasource._interface.runtime_config
runtime_config.update_type_registry(get_type_registry('legacy'))
runtime_config.update_type_registry(get_type_registry('statemint'))

types = (
# 'Tuple:staging_xcm:v4:location:Locationstaging_xcm:v4:location:Location',
'Tuple:staging_xcm:v3:multilocation:MultiLocationstaging_xcm:v3:multilocation:MultiLocation',
'Tuple:U128U8U128',
)
expected_inner_types = (
# ['location', 'location'],
['multilocation', 'multilocation'],
['u128', 'u8', 'u128'],
)

for type_, expected_types in zip(types, expected_inner_types, strict=True):
result = extract_tuple_inner_types(type_, datasource._interface.runtime_config.type_registry)
assert result == expected_types

0 comments on commit e095c16

Please sign in to comment.