Skip to content

Commit f47c1bd

Browse files
authored
Fixed data parsing for __kind junctions with multiple keys (#1208)
1 parent cc50655 commit f47c1bd

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Releases prior to 7.0 has been removed from this file to declutter search result
1212

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

1717
### Changed
1818

src/dipdup/runtimes.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ def extract_subsquid_payload(data: Any) -> Any:
274274
return tuple(extract_subsquid_payload(item) for item in data)
275275

276276
if isinstance(data, dict):
277-
278277
if (kind := data.get('__kind')) is None:
279278
return {key: extract_subsquid_payload(value) for key, value in data.items()}
280279

280+
if len(data) > 2:
281+
return {kind: {key: value for key, value in data.items() if key != '__kind'}}
282+
281283
if 'value' in data:
282284
value = data['value']
283285
if isinstance(value, list | tuple):
@@ -289,16 +291,6 @@ def extract_subsquid_payload(data: Any) -> Any:
289291
if 'key' in data:
290292
return {kind: data['key']}
291293

292-
# See: https://github.com/galacticcouncil/hydration-node/blob/master/precompiles/utils/src/solidity/codec/xcm.rs#L294
293-
if (
294-
'data' in data and
295-
'length' in data and
296-
isinstance(data['data'], str) and
297-
data['data'].startswith('0x') and
298-
isinstance(data['length'], int)
299-
):
300-
return {kind: data['data'][:2+data['length']*2]}
301-
302294
return kind
303295

304296
return data

tests/test_datasources/test_substrate.py

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from dipdup.runtimes import extract_multilocation_payload
24
from dipdup.runtimes import extract_subsquid_payload
35

@@ -59,6 +61,35 @@
5961
}
6062
]
6163

64+
subsquid_general_key_with_value_payload_example = {
65+
'parents': 1,
66+
'interior': {
67+
'__kind': 'X2',
68+
'value': [
69+
{'__kind': 'Parachain', 'value': 2030},
70+
{
71+
'__kind': 'GeneralKey',
72+
'value': '0x02c80084af223c8b598536178d9361dc55bfda6818',
73+
},
74+
],
75+
},
76+
}
77+
subsquid_general_key_with_data_and_length_payload_example = {
78+
'parents': 1,
79+
'interior': {
80+
'__kind': 'X2',
81+
'value': [
82+
{'__kind': 'Parachain', 'value': 2030},
83+
{
84+
'__kind': 'GeneralKey',
85+
'data': '0x0809000000000000000000000000000000000000000000000000000000000000',
86+
'length': 2,
87+
},
88+
],
89+
},
90+
}
91+
92+
6293
processed_path_1 = (
6394
(
6495
{
@@ -106,6 +137,29 @@
106137
},
107138
},
108139
)
140+
node_general_key_with_value_payload_example = {
141+
'parents': 1,
142+
'interior': {
143+
'X2': (
144+
{'Parachain': 2030},
145+
{'GeneralKey': '0x02c80084af223c8b598536178d9361dc55bfda6818'},
146+
),
147+
},
148+
}
149+
node_general_key_with_data_and_length_payload_example = {
150+
'parents': 1,
151+
'interior': {
152+
'X2': (
153+
{'Parachain': 2030},
154+
{
155+
'GeneralKey': {
156+
'data': '0x0809000000000000000000000000000000000000000000000000000000000000',
157+
'length': 2,
158+
}
159+
},
160+
),
161+
},
162+
}
109163

110164
extracted_path_1 = (
111165
(
@@ -128,11 +182,24 @@
128182
)
129183

130184

131-
def test_extract_subsquid_payload() -> None:
132-
133-
assert extract_subsquid_payload(path_1) == processed_path_1
134-
assert extract_subsquid_payload(path_2) == processed_path_2
135-
assert extract_subsquid_payload(path_3) == processed_path_3
185+
@pytest.mark.parametrize(
186+
'subsquid_payload, expected_node_payload',
187+
(
188+
(path_1, processed_path_1),
189+
(path_2, processed_path_2),
190+
(path_3, processed_path_3),
191+
(
192+
subsquid_general_key_with_value_payload_example,
193+
node_general_key_with_value_payload_example,
194+
),
195+
(
196+
subsquid_general_key_with_data_and_length_payload_example,
197+
node_general_key_with_data_and_length_payload_example,
198+
),
199+
),
200+
)
201+
def test_extract_subsquid_payload(subsquid_payload, expected_node_payload) -> None: # type: ignore
202+
assert extract_subsquid_payload(subsquid_payload) == expected_node_payload
136203

137204

138205
def test_extract_multilocation_payload() -> None:

0 commit comments

Comments
 (0)