Skip to content

Commit

Permalink
Fixed data parsing for __kind junctions with multiple keys (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda authored Feb 1, 2025
1 parent cc50655 commit f47c1bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Releases prior to 7.0 has been removed from this file to declutter search result

- cli: Fixed help message on `CallbackError` reporting `batch` handler instead of actual one.
- substrate.subsquid: Fixed parsing nested structures in response.
- substrate.subsquid: Fixed data parsing for `__kind=GeneralKey`.
- substrate.subsquid: Fixed parsing for `__kind` junctions with multiple keys.

### Changed

Expand Down
14 changes: 3 additions & 11 deletions src/dipdup/runtimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ def extract_subsquid_payload(data: Any) -> Any:
return tuple(extract_subsquid_payload(item) for item in data)

if isinstance(data, dict):

if (kind := data.get('__kind')) is None:
return {key: extract_subsquid_payload(value) for key, value in data.items()}

if len(data) > 2:
return {kind: {key: value for key, value in data.items() if key != '__kind'}}

if 'value' in data:
value = data['value']
if isinstance(value, list | tuple):
Expand All @@ -289,16 +291,6 @@ def extract_subsquid_payload(data: Any) -> Any:
if 'key' in data:
return {kind: data['key']}

# See: https://github.com/galacticcouncil/hydration-node/blob/master/precompiles/utils/src/solidity/codec/xcm.rs#L294
if (
'data' in data and
'length' in data and
isinstance(data['data'], str) and
data['data'].startswith('0x') and
isinstance(data['length'], int)
):
return {kind: data['data'][:2+data['length']*2]}

return kind

return data
Expand Down
77 changes: 72 additions & 5 deletions tests/test_datasources/test_substrate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from dipdup.runtimes import extract_multilocation_payload
from dipdup.runtimes import extract_subsquid_payload

Expand Down Expand Up @@ -59,6 +61,35 @@
}
]

subsquid_general_key_with_value_payload_example = {
'parents': 1,
'interior': {
'__kind': 'X2',
'value': [
{'__kind': 'Parachain', 'value': 2030},
{
'__kind': 'GeneralKey',
'value': '0x02c80084af223c8b598536178d9361dc55bfda6818',
},
],
},
}
subsquid_general_key_with_data_and_length_payload_example = {
'parents': 1,
'interior': {
'__kind': 'X2',
'value': [
{'__kind': 'Parachain', 'value': 2030},
{
'__kind': 'GeneralKey',
'data': '0x0809000000000000000000000000000000000000000000000000000000000000',
'length': 2,
},
],
},
}


processed_path_1 = (
(
{
Expand Down Expand Up @@ -106,6 +137,29 @@
},
},
)
node_general_key_with_value_payload_example = {
'parents': 1,
'interior': {
'X2': (
{'Parachain': 2030},
{'GeneralKey': '0x02c80084af223c8b598536178d9361dc55bfda6818'},
),
},
}
node_general_key_with_data_and_length_payload_example = {
'parents': 1,
'interior': {
'X2': (
{'Parachain': 2030},
{
'GeneralKey': {
'data': '0x0809000000000000000000000000000000000000000000000000000000000000',
'length': 2,
}
},
),
},
}

extracted_path_1 = (
(
Expand All @@ -128,11 +182,24 @@
)


def test_extract_subsquid_payload() -> None:

assert extract_subsquid_payload(path_1) == processed_path_1
assert extract_subsquid_payload(path_2) == processed_path_2
assert extract_subsquid_payload(path_3) == processed_path_3
@pytest.mark.parametrize(
'subsquid_payload, expected_node_payload',
(
(path_1, processed_path_1),
(path_2, processed_path_2),
(path_3, processed_path_3),
(
subsquid_general_key_with_value_payload_example,
node_general_key_with_value_payload_example,
),
(
subsquid_general_key_with_data_and_length_payload_example,
node_general_key_with_data_and_length_payload_example,
),
),
)
def test_extract_subsquid_payload(subsquid_payload, expected_node_payload) -> None: # type: ignore
assert extract_subsquid_payload(subsquid_payload) == expected_node_payload


def test_extract_multilocation_payload() -> None:
Expand Down

0 comments on commit f47c1bd

Please sign in to comment.