-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheckmate.py
53 lines (36 loc) · 1.62 KB
/
checkmate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
import sys
import os
import subprocess
import json
from string import Template
def usage():
return """
Checkmate: your GetTreeState checkpoint mate.
checkmate.py LIGHTWALLETD_HOST --start-height HEIGHT [--interval BLOCK_INTERVAL] [--to-json]
--start-height the height you want to start pulling tree states from
--to-json exports the outputs to [height].json files instead of just stdout
"""
def main():
if len(sys.argv) < 4:
print(" Error: insufficient arguments")
print(usage())
return 1
lightwalletd_host = sys.argv[1]
start_height = int(sys.argv[3])
block_interval = 10000
if "--interval" in sys.argv:
block_interval = int(sys.argv[5])
to_json = "--to-json" in sys.argv
command_template = Template("grpcurl -d '{ \"height\" : $height }' $host cash.z.wallet.sdk.rpc.CompactTxStreamer/GetTreeState")
if to_json:
command_template = Template("grpcurl -d '{ \"height\" : $height }' $host cash.z.wallet.sdk.rpc.CompactTxStreamer/GetTreeState > $height.json")
latest_height_template = Template("grpcurl $host cash.z.wallet.sdk.rpc.CompactTxStreamer/GetLatestBlock")
output = subprocess.check_output(latest_height_template.substitute(host=lightwalletd_host), shell=True)
latest_block = json.loads(output)
latest_height = int(latest_block['height'])
for h in range(start_height,latest_height,block_interval):
os.system(command_template.substitute(height=h,host=lightwalletd_host))
os.system(command_template.substitute(height=latest_height,host=lightwalletd_host))
if __name__ == "__main__":
main()