Skip to content

Commit 4978577

Browse files
committed
Revamps _load_registry_type_map
1 parent d597beb commit 4978577

File tree

1 file changed

+80
-51
lines changed

1 file changed

+80
-51
lines changed

async_substrate_interface/types.py

+80-51
Original file line numberDiff line numberDiff line change
@@ -626,40 +626,65 @@ def _load_registry_type_map(self, registry):
626626
registry_type_map = {}
627627
type_id_to_name = {}
628628
types = json.loads(registry.registry)["types"]
629+
type_by_id = {entry["id"]: entry for entry in types}
630+
631+
# Pass 1: Gather simple types
629632
for type_entry in types:
630-
type_type = type_entry["type"]
631633
type_id = type_entry["id"]
632-
type_def = type_type["def"]
633-
type_path = type_type.get("path")
634-
if type_path and type_path[-1] == "Option":
635-
self._handle_option_type(
636-
type_entry, type_id, registry_type_map, type_id_to_name
637-
)
634+
type_def = type_entry["type"]["def"]
635+
type_path = type_entry["type"].get("path")
636+
if type_id == 514:
637+
print(type_id)
638+
639+
if type_entry.get("params") or "variant" in type_def:
638640
continue
639-
if type_entry.get("params") or type_def.get("variant"):
640-
continue # has generics or is Enum
641+
641642
if type_path:
642643
type_name = type_path[-1]
643644
registry_type_map[type_name] = type_id
644645
type_id_to_name[type_id] = type_name
645646
else:
646-
# probably primitive
647-
if type_def.get("primitive"):
648-
type_name = type_def["primitive"]
649-
registry_type_map[type_name] = type_id
650-
type_id_to_name[type_id] = type_name
651-
for type_entry in types:
652-
type_type = type_entry["type"]
653-
type_id = type_entry["id"]
654-
type_def = type_type["def"]
655-
if type_def.get("sequence"):
647+
# Possibly a primitive
648+
if "primitive" in type_def:
649+
prim_name = type_def["primitive"]
650+
registry_type_map[prim_name] = type_id
651+
type_id_to_name[type_id] = prim_name
652+
653+
# Pass 2: Resolve remaining types
654+
pending_ids = set(type_by_id.keys()) - set(type_id_to_name.keys())
655+
656+
def resolve_type_definition(type_id):
657+
type_entry = type_by_id[type_id]
658+
type_def = type_entry["type"]["def"]
659+
type_path = type_entry["type"].get("path", [])
660+
type_params = type_entry["type"].get("params", [])
661+
662+
if type_id in type_id_to_name:
663+
return type_id_to_name[type_id]
664+
665+
# Resolve complex types with paths (including generics like Option etc)
666+
if type_path:
667+
type_name = type_path[-1]
668+
if type_params:
669+
inner_names = []
670+
for param in type_params:
671+
dep_id = param["type"]
672+
if dep_id not in type_id_to_name:
673+
return None
674+
inner_names.append(type_id_to_name[dep_id])
675+
return f"{type_name}<{', '.join(inner_names)}>"
676+
if "variant" in type_def:
677+
return None
678+
return type_name
679+
680+
elif "sequence" in type_def:
656681
sequence_type_id = type_def["sequence"]["type"]
657682
inner_type = type_id_to_name.get(sequence_type_id)
658683
if inner_type:
659684
type_name = f"Vec<{inner_type}>"
660-
type_id_to_name[type_id] = type_name
661-
registry_type_map[type_name] = type_id
662-
elif type_def.get("array"):
685+
return type_name
686+
687+
elif "array" in type_def:
663688
array_type_id = type_def["array"]["type"]
664689
inner_type = type_id_to_name.get(array_type_id)
665690
maybe_len = type_def["array"].get("len")
@@ -668,45 +693,46 @@ def _load_registry_type_map(self, registry):
668693
type_name = f"[{inner_type}; {maybe_len}]"
669694
else:
670695
type_name = f"[{inner_type}]"
671-
type_id_to_name[type_id] = type_name
672-
registry_type_map[type_name] = type_id
673-
elif type_def.get("compact"):
696+
return type_name
697+
698+
elif "compact" in type_def:
674699
compact_type_id = type_def["compact"]["type"]
675700
inner_type = type_id_to_name.get(compact_type_id)
676701
if inner_type:
677702
type_name = f"Compact<{inner_type}>"
678-
type_id_to_name[type_id] = type_name
679-
registry_type_map[type_name] = type_id
680-
elif type_def.get("tuple"):
703+
return type_name
704+
705+
elif "tuple" in type_def:
681706
tuple_type_ids = type_def["tuple"]
682707
type_names = []
683708
for inner_type_id in tuple_type_ids:
684-
inner_type = type_id_to_name.get(inner_type_id)
685-
if inner_type:
686-
type_names.append(inner_type)
709+
if inner_type_id not in type_id_to_name:
710+
return None
711+
type_names.append(type_id_to_name[inner_type_id])
687712
type_name = ", ".join(type_names)
688713
type_name = f"({type_name})"
689-
type_id_to_name[type_id] = type_name
690-
registry_type_map[type_name] = type_id
691-
self.registry_type_map = registry_type_map
692-
self.type_id_to_name = type_id_to_name
714+
return type_name
693715

694-
def _handle_option_type(
695-
self, type_entry, type_id, registry_type_map, type_id_to_name
696-
):
697-
params = type_entry["type"].get("params", [])
698-
if params:
699-
inner_names = []
700-
for param in params:
701-
inner_id = param["type"]
702-
inner_name = type_id_to_name.get(inner_id, f"Type{inner_id}")
703-
inner_names.append(inner_name)
704-
type_name = f"Option<{', '.join(inner_names)}>"
705-
else:
706-
type_name = "Option"
716+
elif "variant" in type_def:
717+
return None
718+
719+
return None
707720

708-
registry_type_map[type_name] = type_id
709-
type_id_to_name[type_id] = type_name
721+
resolved_type = True
722+
while resolved_type and pending_ids:
723+
resolved_type = False
724+
for type_id in list(pending_ids):
725+
if type_id == 514:
726+
print(type_id)
727+
name = resolve_type_definition(type_id)
728+
if name is not None:
729+
type_id_to_name[type_id] = name
730+
registry_type_map[name] = type_id
731+
pending_ids.remove(type_id)
732+
resolved_type = True
733+
734+
self.registry_type_map = registry_type_map
735+
self.type_id_to_name = type_id_to_name
710736

711737
def reload_type_registry(
712738
self, use_remote_preset: bool = True, auto_discover: bool = True
@@ -836,11 +862,14 @@ def _encode_scale(self, type_string, value: Any) -> bytes:
836862
)
837863
except KeyError:
838864
vec_acct_id = "scale_info::152"
865+
import json
839866

867+
with open("registry_final_pass_elif.json", "w") as json_file:
868+
json.dump(self.registry_type_map, json_file, indent=4)
840869
try:
841870
optional_acct_u16 = f"scale_info::{self.registry_type_map['Option<(AccountId32, u16)>']}"
842871
except KeyError:
843-
optional_acct_u16 = "scale_info::573"
872+
optional_acct_u16 = "scale_info::579"
844873

845874
if type_string == "scale_info::0": # Is an AccountId
846875
# encode string into AccountId

0 commit comments

Comments
 (0)