forked from sfffaaa/peaq-bc-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add one more tool to get the runtime upgrade block
- Loading branch information
jaypan
committed
Jan 7, 2025
1 parent
32e3708
commit 990e6b3
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}') |