Skip to content

Commit d0889db

Browse files
committed
Uses metadata v14 for events
1 parent 35728ab commit d0889db

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ async def decode_scale(
999999
else:
10001000
if not runtime:
10011001
runtime = await self.init_runtime(block_hash=block_hash)
1002-
if runtime.metadata_v15 is not None or force_legacy is True:
1002+
if runtime.metadata_v15 is not None and force_legacy is False:
10031003
obj = decode_by_type_string(type_string, runtime.registry, scale_bytes)
10041004
if self.decode_ss58:
10051005
try:
@@ -1930,7 +1930,13 @@ def convert_event_data(data):
19301930
if key == "who":
19311931
who = ss58_encode(bytes(value[0]), self.ss58_format)
19321932
attributes["who"] = who
1933-
if isinstance(value, dict):
1933+
elif key == "from":
1934+
who_from = ss58_encode(bytes(value[0]), self.ss58_format)
1935+
attributes["from"] = who_from
1936+
elif key == "to":
1937+
who_to = ss58_encode(bytes(value[0]), self.ss58_format)
1938+
attributes["to"] = who_to
1939+
elif isinstance(value, dict):
19341940
# Convert nested single-key dictionaries to their keys as strings
19351941
for sub_key, sub_value in value.items():
19361942
if isinstance(sub_value, dict):
@@ -1958,16 +1964,12 @@ def convert_event_data(data):
19581964
block_hash = await self.get_chain_head()
19591965

19601966
storage_obj = await self.query(
1961-
module="System", storage_function="Events", block_hash=block_hash
1967+
module="System", storage_function="Events", block_hash=block_hash, force_legacy_decode=True
19621968
)
1969+
# bt-decode Metadata V15 is not ideal for events. Force legacy decoding for this
19631970
if storage_obj:
19641971
for item in list(storage_obj):
1965-
try:
1966-
events.append(convert_event_data(item))
1967-
except (
1968-
AttributeError
1969-
): # indicates this was legacy decoded with scalecodec
1970-
events.append(item)
1972+
events.append(item)
19711973
return events
19721974

19731975
async def get_metadata(self, block_hash=None) -> MetadataV15:
@@ -2175,6 +2177,7 @@ async def _process_response(
21752177
storage_item: Optional[ScaleType] = None,
21762178
result_handler: Optional[ResultHandler] = None,
21772179
runtime: Optional[Runtime] = None,
2180+
force_legacy_decode: bool = False
21782181
) -> tuple[Any, bool]:
21792182
"""
21802183
Processes the RPC call response by decoding it, returning it as is, or setting a handler for subscriptions,
@@ -2187,6 +2190,7 @@ async def _process_response(
21872190
storage_item: The ScaleType object used for decoding ScaleBytes results
21882191
result_handler: the result handler coroutine used for handling longer-running subscriptions
21892192
runtime: Optional Runtime to use for decoding. If not specified, the currently-loaded `self.runtime` is used
2193+
force_legacy_decode: Whether to force the use of the legacy Metadata V14 decoder
21902194
21912195
Returns:
21922196
(decoded response, completion)
@@ -2208,7 +2212,7 @@ async def _process_response(
22082212
q = bytes(query_value)
22092213
else:
22102214
q = query_value
2211-
result = await self.decode_scale(value_scale_type, q, runtime=runtime)
2215+
result = await self.decode_scale(value_scale_type, q, runtime=runtime, force_legacy=force_legacy_decode)
22122216
if asyncio.iscoroutinefunction(result_handler):
22132217
# For multipart responses as a result of subscriptions.
22142218
message, bool_result = await result_handler(result, subscription_id)
@@ -2223,6 +2227,7 @@ async def _make_rpc_request(
22232227
result_handler: Optional[ResultHandler] = None,
22242228
attempt: int = 1,
22252229
runtime: Optional[Runtime] = None,
2230+
force_legacy_decode: bool = False
22262231
) -> RequestManager.RequestResults:
22272232
request_manager = RequestManager(payloads)
22282233

@@ -2267,6 +2272,7 @@ async def _make_rpc_request(
22672272
storage_item,
22682273
result_handler,
22692274
runtime=runtime,
2275+
force_legacy_decode=force_legacy_decode
22702276
)
22712277

22722278
request_manager.add_response(
@@ -2298,6 +2304,7 @@ async def _make_rpc_request(
22982304
storage_item,
22992305
result_handler,
23002306
attempt + 1,
2307+
force_legacy_decode
23012308
)
23022309

23032310
return request_manager.get_results()
@@ -3323,6 +3330,7 @@ async def query(
33233330
subscription_handler=None,
33243331
reuse_block_hash: bool = False,
33253332
runtime: Optional[Runtime] = None,
3333+
force_legacy_decode: bool = False
33263334
) -> Optional[Union["ScaleObj", Any]]:
33273335
"""
33283336
Queries substrate. This should only be used when making a single request. For multiple requests,
@@ -3355,6 +3363,7 @@ async def query(
33553363
storage_item,
33563364
result_handler=subscription_handler,
33573365
runtime=runtime,
3366+
force_legacy_decode=force_legacy_decode
33583367
)
33593368
result = responses[preprocessed.queryable][0]
33603369
if isinstance(result, (list, tuple, int, float)):

async_substrate_interface/sync_substrate.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def decode_scale(
679679
type_string: str,
680680
scale_bytes: bytes,
681681
return_scale_obj=False,
682+
force_legacy: bool = False,
682683
) -> Union[ScaleObj, Any]:
683684
"""
684685
Helper function to decode arbitrary SCALE-bytes (e.g. 0x02000000) according to given RUST type_string
@@ -689,6 +690,7 @@ def decode_scale(
689690
type_string: the type string of the SCALE object for decoding
690691
scale_bytes: the bytes representation of the SCALE object to decode
691692
return_scale_obj: Whether to return the decoded value wrapped in a SCALE-object-like wrapper, or raw.
693+
force_legacy: Whether to force the use of the legacy Metadata V14 decoder
692694
693695
Returns:
694696
Decoded object
@@ -697,7 +699,7 @@ def decode_scale(
697699
# Decode AccountId bytes to SS58 address
698700
return ss58_encode(scale_bytes, self.ss58_format)
699701
else:
700-
if self.runtime.metadata_v15 is not None:
702+
if self.runtime.metadata_v15 is not None and force_legacy is False:
701703
obj = decode_by_type_string(
702704
type_string, self.runtime.registry, scale_bytes
703705
)
@@ -1631,16 +1633,12 @@ def convert_event_data(data):
16311633
block_hash = self.get_chain_head()
16321634

16331635
storage_obj = self.query(
1634-
module="System", storage_function="Events", block_hash=block_hash
1636+
module="System", storage_function="Events", block_hash=block_hash, force_legacy_decode=True
16351637
)
1638+
# bt-decode Metadata V15 is not ideal for events. Force legacy decoding for this
16361639
if storage_obj:
16371640
for item in list(storage_obj):
1638-
try:
1639-
events.append(convert_event_data(item))
1640-
except (
1641-
AttributeError
1642-
): # indicates this was legacy decoded with scalecodec
1643-
events.append(item)
1641+
events.append(item)
16441642
return events
16451643

16461644
def get_metadata(self, block_hash=None) -> MetadataV15:
@@ -1822,6 +1820,7 @@ def _process_response(
18221820
value_scale_type: Optional[str] = None,
18231821
storage_item: Optional[ScaleType] = None,
18241822
result_handler: Optional[ResultHandler] = None,
1823+
force_legacy_decode: bool = False
18251824
) -> tuple[Any, bool]:
18261825
"""
18271826
Processes the RPC call response by decoding it, returning it as is, or setting a handler for subscriptions,
@@ -1833,6 +1832,7 @@ def _process_response(
18331832
value_scale_type: Scale Type string used for decoding ScaleBytes results
18341833
storage_item: The ScaleType object used for decoding ScaleBytes results
18351834
result_handler: the result handler coroutine used for handling longer-running subscriptions
1835+
force_legacy_decode: Whether to force legacy Metadata V14 decoding of the response
18361836
18371837
Returns:
18381838
(decoded response, completion)
@@ -1854,7 +1854,7 @@ def _process_response(
18541854
q = bytes(query_value)
18551855
else:
18561856
q = query_value
1857-
result = self.decode_scale(value_scale_type, q)
1857+
result = self.decode_scale(value_scale_type, q, force_legacy=force_legacy_decode)
18581858
if isinstance(result_handler, Callable):
18591859
# For multipart responses as a result of subscriptions.
18601860
message, bool_result = result_handler(result, subscription_id)
@@ -1868,6 +1868,7 @@ def _make_rpc_request(
18681868
storage_item: Optional[ScaleType] = None,
18691869
result_handler: Optional[ResultHandler] = None,
18701870
attempt: int = 1,
1871+
force_legacy_decode: bool = False
18711872
) -> RequestManager.RequestResults:
18721873
request_manager = RequestManager(payloads)
18731874
_received = {}
@@ -1901,6 +1902,7 @@ def _make_rpc_request(
19011902
storage_item,
19021903
result_handler,
19031904
attempt + 1,
1905+
force_legacy_decode
19041906
)
19051907
if "id" in response:
19061908
_received[response["id"]] = response
@@ -1932,6 +1934,7 @@ def _make_rpc_request(
19321934
value_scale_type,
19331935
storage_item,
19341936
result_handler,
1937+
force_legacy_decode
19351938
)
19361939
request_manager.add_response(
19371940
item_id, decoded_response, complete
@@ -2870,6 +2873,7 @@ def query(
28702873
raw_storage_key: Optional[bytes] = None,
28712874
subscription_handler=None,
28722875
reuse_block_hash: bool = False,
2876+
force_legacy_decode: bool = False
28732877
) -> Optional[Union["ScaleObj", Any]]:
28742878
"""
28752879
Queries substrate. This should only be used when making a single request. For multiple requests,
@@ -2895,6 +2899,7 @@ def query(
28952899
value_scale_type,
28962900
storage_item,
28972901
result_handler=subscription_handler,
2902+
force_legacy_decode=force_legacy_decode
28982903
)
28992904
result = responses[preprocessed.queryable][0]
29002905
if isinstance(result, (list, tuple, int, float)):

0 commit comments

Comments
 (0)