Skip to content

Commit 88b6357

Browse files
committed
Needed to initiate the runtime config differently
1 parent c1856e2 commit 88b6357

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
)
2222

2323
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
2526
from scalecodec.types import (
2627
GenericCall,
2728
GenericExtrinsic,
@@ -1113,6 +1114,10 @@ async def get_runtime_for_version(
11131114
async def _get_runtime_for_version(
11141115
self, runtime_version: int, block_hash: Optional[str] = None
11151116
) -> Runtime:
1117+
runtime_config = RuntimeConfigurationObject()
1118+
runtime_config.clear_type_registry()
1119+
runtime_config.update_type_registry(load_type_registry_preset(name="core"))
1120+
11161121
if not block_hash:
11171122
block_hash, runtime_block_hash, block_number = await asyncio.gather(
11181123
self.get_chain_head(),
@@ -1126,7 +1131,11 @@ async def _get_runtime_for_version(
11261131
)
11271132
runtime_info, metadata, (metadata_v15, registry) = await asyncio.gather(
11281133
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+
),
11301139
self._load_registry_at_block(block_hash=runtime_block_hash),
11311140
)
11321141
if metadata is None:
@@ -1147,6 +1156,7 @@ async def _get_runtime_for_version(
11471156
chain=self.chain,
11481157
metadata=metadata,
11491158
type_registry=self.type_registry,
1159+
runtime_config=runtime_config,
11501160
metadata_v15=metadata_v15,
11511161
runtime_info=runtime_info,
11521162
registry=registry,
@@ -2102,7 +2112,10 @@ async def _get_block_runtime_version_for(self, block_hash: str):
21022112
return runtime_info["specVersion"]
21032113

21042114
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,
21062119
) -> Optional[Union[dict, ScaleType]]:
21072120
"""
21082121
A pass-though to existing JSONRPC method `state_getMetadata`.
@@ -2116,7 +2129,7 @@ async def get_block_metadata(
21162129
from the server
21172130
"""
21182131
params = None
2119-
if decode and not self.runtime_config:
2132+
if decode and not runtime_config:
21202133
raise ValueError(
21212134
"Cannot decode runtime configuration without a supplied runtime_config"
21222135
)
@@ -2129,7 +2142,7 @@ async def get_block_metadata(
21292142
raise SubstrateRequestException(response["error"]["message"])
21302143

21312144
if (result := response.get("result")) and decode:
2132-
metadata_decoder = self.runtime_config.create_scale_object(
2145+
metadata_decoder = runtime_config.create_scale_object(
21332146
"MetadataVersioned", data=ScaleBytes(result)
21342147
)
21352148
metadata_decoder.decode()
@@ -2645,10 +2658,12 @@ async def generate_signature_payload(
26452658
tip: int = 0,
26462659
tip_asset_id: Optional[int] = None,
26472660
include_call_length: bool = False,
2661+
runtime: Optional[Runtime] = None,
26482662
) -> ScaleBytes:
26492663
# Retrieve genesis hash
26502664
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)
26522667

26532668
if not era:
26542669
era = "00"
@@ -2759,7 +2774,7 @@ async def generate_signature_payload(
27592774
)
27602775

27612776
if include_call_length:
2762-
length_obj = self.runtime_config.create_scale_object("Bytes")
2777+
length_obj = runtime.runtime_config.create_scale_object("Bytes")
27632778
call_data = str(length_obj.encode(str(call.data)))
27642779

27652780
else:
@@ -2855,7 +2870,12 @@ async def create_signed_extrinsic(
28552870
else:
28562871
# Create signature payload
28572872
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,
28592879
)
28602880

28612881
# Set Signature version to crypto type of keypair
@@ -2867,7 +2887,7 @@ async def create_signed_extrinsic(
28672887
signature = await signature
28682888

28692889
# Create extrinsic
2870-
extrinsic = self.runtime_config.create_scale_object(
2890+
extrinsic = runtime.runtime_config.create_scale_object(
28712891
type_string="Extrinsic", metadata=runtime.metadata
28722892
)
28732893

@@ -2907,7 +2927,7 @@ async def create_unsigned_extrinsic(self, call: GenericCall) -> GenericExtrinsic
29072927
runtime = await self.init_runtime()
29082928

29092929
# Create extrinsic
2910-
extrinsic = self.runtime_config.create_scale_object(
2930+
extrinsic = runtime.runtime_config.create_scale_object(
29112931
type_string="Extrinsic", metadata=runtime.metadata
29122932
)
29132933

@@ -3018,10 +3038,10 @@ async def runtime_call(
30183038

30193039
try:
30203040
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"][
30223042
method
30233043
]
3024-
runtime_api_types = self.runtime_config.type_registry["runtime_api"][
3044+
runtime_api_types = runtime.runtime_config.type_registry["runtime_api"][
30253045
api
30263046
].get("types", {})
30273047
runtime.runtime_config.update_type_registry_types(runtime_api_types)
@@ -3288,7 +3308,7 @@ async def get_type_registry(
32883308
else:
32893309
type_string = f"scale_info::{scale_info_type.value['id']}"
32903310

3291-
scale_cls = self.runtime_config.get_decoder_class(type_string)
3311+
scale_cls = runtime.runtime_config.get_decoder_class(type_string)
32923312
type_registry[type_string] = scale_cls.generate_type_decomposition(
32933313
max_recursion=max_recursion
32943314
)

0 commit comments

Comments
 (0)