Skip to content

Commit b84c4c2

Browse files
committed
Sync optimised actually
1 parent 7b88b08 commit b84c4c2

File tree

2 files changed

+79
-85
lines changed

2 files changed

+79
-85
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,17 @@ async def get_runtime_for_version(
10331033
async def _get_runtime_for_version(
10341034
self, runtime_version: int, block_hash: Optional[str] = None
10351035
) -> Runtime:
1036-
runtime_block_hash, block_number = await asyncio.gather(
1037-
self.get_parent_block_hash(block_hash), self.get_block_number(block_hash)
1038-
)
1036+
if not block_hash:
1037+
block_hash, runtime_block_hash, block_number = await asyncio.gather(
1038+
self.get_chain_head(),
1039+
self.get_parent_block_hash(block_hash),
1040+
self.get_block_number(block_hash),
1041+
)
1042+
else:
1043+
runtime_block_hash, block_number = await asyncio.gather(
1044+
self.get_parent_block_hash(block_hash),
1045+
self.get_block_number(block_hash),
1046+
)
10391047
runtime_info, metadata, (metadata_v15, registry) = await asyncio.gather(
10401048
self.get_block_runtime_info(runtime_block_hash),
10411049
self.get_block_metadata(block_hash=runtime_block_hash, decode=True),

async_substrate_interface/sync_substrate.py

Lines changed: 68 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ss58_encode,
1414
MultiAccountId,
1515
)
16-
from scalecodec.base import RuntimeConfigurationObject, ScaleBytes, ScaleType
16+
from scalecodec.base import ScaleBytes, ScaleType
1717
from websockets.sync.client import connect, ClientConnection
1818
from websockets.exceptions import ConnectionClosed
1919

@@ -553,6 +553,13 @@ def initialize(self):
553553
chain = self.rpc_request("system_chain", [])
554554
self._chain = chain.get("result")
555555
self.init_runtime()
556+
if self.ss58_format is None:
557+
# Check and apply runtime constants
558+
ss58_prefix_constant = self.get_constant(
559+
"System", "SS58Prefix", block_hash=self.last_block_hash
560+
)
561+
if ss58_prefix_constant:
562+
self.ss58_format = ss58_prefix_constant
556563
self.initialized = True
557564

558565
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -721,11 +728,19 @@ def init_runtime(
721728
if block_id and block_hash:
722729
raise ValueError("Cannot provide block_hash and block_id at the same time")
723730

724-
if block_id:
731+
if block_id is not None:
732+
if runtime := self.runtime_cache.retrieve(block=block_id):
733+
self.runtime = runtime
734+
return runtime
725735
block_hash = self.get_block_hash(block_id)
726736

727737
if not block_hash:
728738
block_hash = self.get_chain_head()
739+
else:
740+
self.last_block_hash = block_hash
741+
if runtime := self.runtime_cache.retrieve(block_hash=block_hash):
742+
self.runtime = runtime
743+
return runtime
729744

730745
runtime_version = self.get_block_runtime_version_for(block_hash)
731746
if runtime_version is None:
@@ -736,56 +751,62 @@ def init_runtime(
736751
if self.runtime and runtime_version == self.runtime.runtime_version:
737752
return self.runtime
738753

739-
runtime = self.runtime_cache.retrieve(runtime_version=runtime_version)
740-
if not runtime:
741-
self.last_block_hash = block_hash
742-
743-
runtime_block_hash = self.get_parent_block_hash(block_hash)
744-
745-
runtime_info = self.get_block_runtime_info(runtime_block_hash)
754+
if runtime := self.runtime_cache.retrieve(runtime_version=runtime_version):
755+
self.runtime = runtime
756+
return runtime
757+
else:
758+
return self.get_runtime_for_version(runtime_version, block_hash)
746759

747-
metadata = self.get_block_metadata(
748-
block_hash=runtime_block_hash, decode=True
749-
)
750-
if metadata is None:
751-
# does this ever happen?
752-
raise SubstrateRequestException(
753-
f"No metadata for block '{runtime_block_hash}'"
754-
)
755-
logger.debug(
756-
"Retrieved metadata for {} from Substrate node".format(runtime_version)
757-
)
760+
def get_runtime_for_version(
761+
self, runtime_version: int, block_hash: Optional[str] = None
762+
) -> Runtime:
763+
"""
764+
Retrieves the `Runtime` for a given runtime version at a given block hash.
765+
Args:
766+
runtime_version: version of the runtime (from `get_block_runtime_version_for`)
767+
block_hash: hash of the block to query
758768
759-
metadata_v15, registry = self._load_registry_at_block(
760-
block_hash=runtime_block_hash
761-
)
762-
logger.debug(
763-
f"Retrieved metadata v15 for {runtime_version} from Substrate node"
764-
)
769+
Returns:
770+
Runtime object for the given runtime version
771+
"""
772+
if not block_hash:
773+
block_hash = self.get_chain_head()
774+
runtime_block_hash = self.get_parent_block_hash(block_hash)
775+
block_number = self.get_block_number(block_hash)
776+
runtime_info = self.get_block_runtime_info(runtime_block_hash)
765777

766-
runtime = Runtime(
767-
chain=self.chain,
768-
runtime_config=self.runtime_config,
769-
metadata=metadata,
770-
type_registry=self.type_registry,
771-
metadata_v15=metadata_v15,
772-
runtime_info=runtime_info,
773-
registry=registry,
774-
)
775-
self.runtime_cache.add_item(
776-
runtime_version=runtime_version, runtime=runtime
778+
metadata = self.get_block_metadata(block_hash=runtime_block_hash, decode=True)
779+
if metadata is None:
780+
# does this ever happen?
781+
raise SubstrateRequestException(
782+
f"No metadata for block '{runtime_block_hash}'"
777783
)
784+
logger.debug(
785+
"Retrieved metadata for {} from Substrate node".format(runtime_version)
786+
)
778787

779-
self.load_runtime(runtime)
780-
781-
if self.ss58_format is None:
782-
# Check and apply runtime constants
783-
ss58_prefix_constant = self.get_constant(
784-
"System", "SS58Prefix", block_hash=block_hash
785-
)
788+
metadata_v15, registry = self._load_registry_at_block(
789+
block_hash=runtime_block_hash
790+
)
791+
logger.debug(
792+
f"Retrieved metadata v15 for {runtime_version} from Substrate node"
793+
)
786794

787-
if ss58_prefix_constant:
788-
self.ss58_format = ss58_prefix_constant
795+
runtime = Runtime(
796+
chain=self.chain,
797+
runtime_config=self.runtime_config,
798+
metadata=metadata,
799+
type_registry=self.type_registry,
800+
metadata_v15=metadata_v15,
801+
runtime_info=runtime_info,
802+
registry=registry,
803+
)
804+
self.runtime_cache.add_item(
805+
block=block_number,
806+
block_hash=block_hash,
807+
runtime_version=runtime_version,
808+
runtime=runtime,
809+
)
789810
return runtime
790811

791812
def create_storage_key(
@@ -1061,6 +1082,7 @@ def get_metadata_runtime_call_function(
10611082
Args:
10621083
api: Name of the runtime API e.g. 'TransactionPaymentApi'
10631084
method: Name of the method e.g. 'query_fee_details'
1085+
block_hash: block hash whose metadata to query
10641086
10651087
Returns:
10661088
runtime call function
@@ -1087,41 +1109,6 @@ def get_metadata_runtime_call_function(
10871109

10881110
return runtime_call_def_obj
10891111

1090-
def get_metadata_runtime_call_function(
1091-
self, api: str, method: str
1092-
) -> GenericRuntimeCallDefinition:
1093-
"""
1094-
Get details of a runtime API call
1095-
1096-
Args:
1097-
api: Name of the runtime API e.g. 'TransactionPaymentApi'
1098-
method: Name of the method e.g. 'query_fee_details'
1099-
1100-
Returns:
1101-
GenericRuntimeCallDefinition
1102-
"""
1103-
self.init_runtime()
1104-
1105-
try:
1106-
runtime_call_def = self.runtime_config.type_registry["runtime_api"][api][
1107-
"methods"
1108-
][method]
1109-
runtime_call_def["api"] = api
1110-
runtime_call_def["method"] = method
1111-
runtime_api_types = self.runtime_config.type_registry["runtime_api"][
1112-
api
1113-
].get("types", {})
1114-
except KeyError:
1115-
raise ValueError(f"Runtime API Call '{api}.{method}' not found in registry")
1116-
1117-
# Add runtime API types to registry
1118-
self.runtime_config.update_type_registry_types(runtime_api_types)
1119-
1120-
runtime_call_def_obj = self.create_scale_object("RuntimeCallDefinition")
1121-
runtime_call_def_obj.encode(runtime_call_def)
1122-
1123-
return runtime_call_def_obj
1124-
11251112
def _get_block_handler(
11261113
self,
11271114
block_hash: str,
@@ -2887,7 +2874,6 @@ def query_map(
28872874
Returns:
28882875
QueryMapResult object
28892876
"""
2890-
hex_to_bytes_ = hex_to_bytes
28912877
params = params or []
28922878
block_hash = self._get_current_block_hash(block_hash, reuse_block_hash)
28932879
if block_hash:

0 commit comments

Comments
 (0)