21
21
)
22
22
23
23
from bt_decode import MetadataV15 , PortableRegistry , decode as decode_by_type_string
24
- from scalecodec .base import ScaleBytes , ScaleType
24
+ from scalecodec .base import ScaleBytes , ScaleType , RuntimeConfigurationObject
25
+ from scalecodec .type_registry import load_type_registry_preset
25
26
from scalecodec .types import (
26
27
GenericCall ,
27
28
GenericExtrinsic ,
@@ -1113,6 +1114,10 @@ async def get_runtime_for_version(
1113
1114
async def _get_runtime_for_version (
1114
1115
self , runtime_version : int , block_hash : Optional [str ] = None
1115
1116
) -> Runtime :
1117
+ runtime_config = RuntimeConfigurationObject ()
1118
+ runtime_config .clear_type_registry ()
1119
+ runtime_config .update_type_registry (load_type_registry_preset (name = "core" ))
1120
+
1116
1121
if not block_hash :
1117
1122
block_hash , runtime_block_hash , block_number = await asyncio .gather (
1118
1123
self .get_chain_head (),
@@ -1126,7 +1131,11 @@ async def _get_runtime_for_version(
1126
1131
)
1127
1132
runtime_info , metadata , (metadata_v15 , registry ) = await asyncio .gather (
1128
1133
self .get_block_runtime_info (runtime_block_hash ),
1129
- self .get_block_metadata (block_hash = runtime_block_hash , decode = True ),
1134
+ self .get_block_metadata (
1135
+ block_hash = runtime_block_hash ,
1136
+ runtime_config = runtime_config ,
1137
+ decode = True ,
1138
+ ),
1130
1139
self ._load_registry_at_block (block_hash = runtime_block_hash ),
1131
1140
)
1132
1141
if metadata is None :
@@ -1147,6 +1156,7 @@ async def _get_runtime_for_version(
1147
1156
chain = self .chain ,
1148
1157
metadata = metadata ,
1149
1158
type_registry = self .type_registry ,
1159
+ runtime_config = runtime_config ,
1150
1160
metadata_v15 = metadata_v15 ,
1151
1161
runtime_info = runtime_info ,
1152
1162
registry = registry ,
@@ -2102,7 +2112,10 @@ async def _get_block_runtime_version_for(self, block_hash: str):
2102
2112
return runtime_info ["specVersion" ]
2103
2113
2104
2114
async def get_block_metadata (
2105
- self , block_hash : Optional [str ] = None , decode : bool = True
2115
+ self ,
2116
+ block_hash : Optional [str ] = None ,
2117
+ runtime_config : Optional [RuntimeConfigurationObject ] = None ,
2118
+ decode : bool = True ,
2106
2119
) -> Optional [Union [dict , ScaleType ]]:
2107
2120
"""
2108
2121
A pass-though to existing JSONRPC method `state_getMetadata`.
@@ -2116,7 +2129,7 @@ async def get_block_metadata(
2116
2129
from the server
2117
2130
"""
2118
2131
params = None
2119
- if decode and not self . runtime_config :
2132
+ if decode and not runtime_config :
2120
2133
raise ValueError (
2121
2134
"Cannot decode runtime configuration without a supplied runtime_config"
2122
2135
)
@@ -2129,7 +2142,7 @@ async def get_block_metadata(
2129
2142
raise SubstrateRequestException (response ["error" ]["message" ])
2130
2143
2131
2144
if (result := response .get ("result" )) and decode :
2132
- metadata_decoder = self . runtime_config .create_scale_object (
2145
+ metadata_decoder = runtime_config .create_scale_object (
2133
2146
"MetadataVersioned" , data = ScaleBytes (result )
2134
2147
)
2135
2148
metadata_decoder .decode ()
@@ -2645,10 +2658,12 @@ async def generate_signature_payload(
2645
2658
tip : int = 0 ,
2646
2659
tip_asset_id : Optional [int ] = None ,
2647
2660
include_call_length : bool = False ,
2661
+ runtime : Optional [Runtime ] = None ,
2648
2662
) -> ScaleBytes :
2649
2663
# Retrieve genesis hash
2650
2664
genesis_hash = await self .get_block_hash (0 )
2651
- runtime = await self .init_runtime (block_hash = None )
2665
+ if runtime is None :
2666
+ runtime = await self .init_runtime (block_hash = None )
2652
2667
2653
2668
if not era :
2654
2669
era = "00"
@@ -2759,7 +2774,7 @@ async def generate_signature_payload(
2759
2774
)
2760
2775
2761
2776
if include_call_length :
2762
- length_obj = self .runtime_config .create_scale_object ("Bytes" )
2777
+ length_obj = runtime .runtime_config .create_scale_object ("Bytes" )
2763
2778
call_data = str (length_obj .encode (str (call .data )))
2764
2779
2765
2780
else :
@@ -2855,7 +2870,12 @@ async def create_signed_extrinsic(
2855
2870
else :
2856
2871
# Create signature payload
2857
2872
signature_payload = await self .generate_signature_payload (
2858
- call = call , era = era , nonce = nonce , tip = tip , tip_asset_id = tip_asset_id
2873
+ call = call ,
2874
+ era = era ,
2875
+ nonce = nonce ,
2876
+ tip = tip ,
2877
+ tip_asset_id = tip_asset_id ,
2878
+ runtime = runtime ,
2859
2879
)
2860
2880
2861
2881
# Set Signature version to crypto type of keypair
@@ -2867,7 +2887,7 @@ async def create_signed_extrinsic(
2867
2887
signature = await signature
2868
2888
2869
2889
# Create extrinsic
2870
- extrinsic = self .runtime_config .create_scale_object (
2890
+ extrinsic = runtime .runtime_config .create_scale_object (
2871
2891
type_string = "Extrinsic" , metadata = runtime .metadata
2872
2892
)
2873
2893
@@ -2907,7 +2927,7 @@ async def create_unsigned_extrinsic(self, call: GenericCall) -> GenericExtrinsic
2907
2927
runtime = await self .init_runtime ()
2908
2928
2909
2929
# Create extrinsic
2910
- extrinsic = self .runtime_config .create_scale_object (
2930
+ extrinsic = runtime .runtime_config .create_scale_object (
2911
2931
type_string = "Extrinsic" , metadata = runtime .metadata
2912
2932
)
2913
2933
@@ -3018,10 +3038,10 @@ async def runtime_call(
3018
3038
3019
3039
try :
3020
3040
if runtime .metadata_v15 is None :
3021
- _ = self .runtime_config .type_registry ["runtime_api" ][api ]["methods" ][
3041
+ _ = runtime .runtime_config .type_registry ["runtime_api" ][api ]["methods" ][
3022
3042
method
3023
3043
]
3024
- runtime_api_types = self .runtime_config .type_registry ["runtime_api" ][
3044
+ runtime_api_types = runtime .runtime_config .type_registry ["runtime_api" ][
3025
3045
api
3026
3046
].get ("types" , {})
3027
3047
runtime .runtime_config .update_type_registry_types (runtime_api_types )
@@ -3288,7 +3308,7 @@ async def get_type_registry(
3288
3308
else :
3289
3309
type_string = f"scale_info::{ scale_info_type .value ['id' ]} "
3290
3310
3291
- scale_cls = self .runtime_config .get_decoder_class (type_string )
3311
+ scale_cls = runtime .runtime_config .get_decoder_class (type_string )
3292
3312
type_registry [type_string ] = scale_cls .generate_type_decomposition (
3293
3313
max_recursion = max_recursion
3294
3314
)
0 commit comments