Skip to content

Instantly share code, notes, and snippets.

@al3mart
Last active September 8, 2022 11:10
Show Gist options
  • Save al3mart/273da70db859730e37cf54a0d2831f95 to your computer and use it in GitHub Desktop.
Save al3mart/273da70db859730e37cf54a0d2831f95 to your computer and use it in GitHub Desktop.
Recover onboarded parachain state from a relay chain node.
# Usage python /parachain_info.py wss://relay-rpc.url [local_directory_name]
import sys
# install with `pip install substrate-interface
from substrateinterface import SubstrateInterface
def get_parachain_head(node_client, para_id):
return str(node_client.query('Paras', 'Heads', params=[para_id]))
def get_parachain_code_hash(node_client, para_id):
return str(node_client.query('Paras', 'CurrentCodeHash', params=[para_id]))
def get_parachain_wasm(node_client, code_hash):
return str(node_client.query('Paras', 'CodeByHash', params=[code_hash]))
def paras_list(node_client):
return node_client.query('Paras', 'Parachains', params=[])
def get_egress_channels(node_client, para_id):
return str(node_client.query('Hrmp', 'HrmpEgressChannelsIndex', params=[para_id]))
def get_ingress_channels(node_client, para_id):
return str(node_client.query('Hrmp', 'HrmpIngressChannelsIndex', params=[para_id]))
def get_dmp_queue_heads(node_client, para_id):
return str(node_client.query('Dmp', 'DownwardMessageQueueHeads', params=[para_id]))
def get_dmp_head_storage_key(node_client, para_id):
return str(node_client.generate_storage_hash(str('Dmp'), str('DownwardMessageQueueHeads'), [para_id], ['Twox64Concat']))
if len(sys.argv) > 1:
url = sys.argv[1]
file_path = sys.argv[2] if len(sys.argv) > 2 else '/tmp'
node_client = SubstrateInterface(url=url)
parachain_list = paras_list(node_client)
for para_id in parachain_list:
para_id = str(para_id)
print(f'Parachain ID={para_id}')
head = get_parachain_head(node_client, para_id)
if head == 'None':
continue
with open(f'{file_path}/state-{para_id}', 'w') as file:
file.write(head)
print(f'Chain state writen to {file_path}state-{para_id}')
code_hash = get_parachain_code_hash(node_client, para_id)
if code_hash == 'None':
continue
wasm = get_parachain_wasm(node_client, code_hash)
with open(f'{file_path}/wasm-{para_id}', 'w') as file:
file.write(wasm)
print(f'Chain wasm writen to {file_path}wasm-{para_id}')
ingress_channels = get_ingress_channels(node_client, para_id)
with open(f'{file_path}/ingress-channels', 'a') as file:
file.write(f'{para_id }{ingress_channels}\n')
egress_channels = get_egress_channels(node_client, para_id)
with open(f'{file_path}/egress-channels', 'a') as file:
file.write(f'{para_id }{egress_channels}\n')
dmp_head = get_dmp_queue_heads(node_client, para_id)
dmp_head_storage_key = get_dmp_head_storage_key(node_client, para_id)
with open(f'{file_path}/dmp_heads', 'a') as file:
file.write(f'{para_id }\nKey: {dmp_head_storage_key}\nValue: {dmp_head}\n')
else:
print('No WS URL provided as arg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment