Skip to content

Commit

Permalink
Add one more tool to get the runtime upgrade block
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypan committed Jan 7, 2025
1 parent 32e3708 commit 990e6b3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tools/get_runtime_version_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sys
sys.path.append('./')

import argparse
from substrateinterface import SubstrateInterface
from peaq.utils import get_block_hash
from peaq.utils import get_block_height


def get_runtime_version(substrate, block_number):
block_hash = get_block_hash(substrate, block_number)
return substrate.get_block_runtime_version(block_hash)['specVersion']


def find_first_occurrence(substrate, version):
# Binary search for the block number,
low = 1
high = get_block_height(substrate)
result = 0
while low < high:
mid = (low + high) // 2
value = get_runtime_version(substrate, mid)
# 0: equal, 1: mid > version, -1: mid < version
if value == version:
result = mid
high = mid - 1
print(f'Found runtime version {version} at block number {mid}')
elif value < version:
print(f'Runtime version {version} is already reached at block number {mid}')
low = mid + 1
else:
high = mid - 1
print(f'Runtime version {version} is not reached yet at block number {mid}')
return result + 1


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get storage and constants from a Substrate chain')
parser.add_argument('-r', '--runtime', type=str, required=True, help='Your runtime websocket endpoint')
parser.add_argument('-v', '--version', type=int, required=True, help='The runtime version you want to check')
args = parser.parse_args()

substrate = SubstrateInterface(
url=args.runtime,
)

runtime_version = args.version
print(f'Block number for runtime version {runtime_version}: {find_first_occurrence(substrate, runtime_version)}')

0 comments on commit 990e6b3

Please sign in to comment.