Skip to content

Commit 24a35a5

Browse files
committed
add processing hardforks
1 parent 2ed5f6b commit 24a35a5

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

mytoninstaller/mytoninstaller.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ def Refresh(local):
9393
ton_work_dir = "/var/ton-work/"
9494
ton_bin_dir = bin_dir + "ton/"
9595
ton_src_dir = src_dir + "ton/"
96+
mtc_src_dir = src_dir + "mytonctrl/"
9697
local.buffer.bin_dir = bin_dir
9798
local.buffer.src_dir = src_dir
9899
local.buffer.ton_work_dir = ton_work_dir
99100
local.buffer.ton_bin_dir = ton_bin_dir
100101
local.buffer.ton_src_dir = ton_src_dir
102+
local.buffer.mtc_src_dir = mtc_src_dir
101103
ton_db_dir = ton_work_dir + "db/"
102104
keys_dir = ton_work_dir + "keys/"
103105
local.buffer.ton_db_dir = ton_db_dir
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import argparse
2+
import json
3+
import subprocess
4+
import sys
5+
import time
6+
import logging
7+
8+
9+
def run_vc(cmd: str):
10+
if binary:
11+
return subprocess.run([binary, '-k', client, '-p', server, '-a', address, '--cmd', cmd],
12+
stdout=subprocess.PIPE).stdout.decode()
13+
else: # use symlink
14+
return subprocess.run(['bash', 'validator-console', '--cmd', cmd],
15+
stdout=subprocess.PIPE).stdout.decode()
16+
17+
def get_last_mc_seqno():
18+
stats = run_vc('getstats')
19+
for line in stats.split('\n'):
20+
if line.startswith('masterchainblock'):
21+
return int(line.split()[1].split(',')[2].split(')')[0])
22+
23+
def restart_node():
24+
return subprocess.run(['systemctl', 'restart', 'validator']).returncode
25+
26+
def get_config():
27+
with open(config_path) as f:
28+
return json.load(f)
29+
30+
def set_config(config: dict):
31+
with open(config_path, 'w') as f:
32+
f.write(json.dumps(config, indent=4))
33+
34+
def set_hardforks(hfks: list):
35+
c = get_config()
36+
c['validator']['hardforks'] = hfks
37+
set_config(c)
38+
39+
def add_hardfork(hfk: dict):
40+
c = get_config()
41+
c['validator']['hardforks'].append(hfk)
42+
set_config(c)
43+
44+
45+
logger = logging.getLogger(__name__)
46+
logging.basicConfig(
47+
level=logging.DEBUG,
48+
format='%(asctime)s - %(levelname)s - %(message)s',
49+
datefmt='%Y-%m-%d %H:%M:%S'
50+
)
51+
52+
parser = argparse.ArgumentParser(description="Python script to process hardforks when syncing archive node.")
53+
parser.add_argument('--from-seqno', type=int, required=True, help='Global config path')
54+
parser.add_argument('--config-path', type=str, required=True, help='Global config path')
55+
parser.add_argument('--bin', type=str, help='VC bin')
56+
parser.add_argument('--client', type=str, help='VC Client')
57+
parser.add_argument('--server', type=str, help='VC Server')
58+
parser.add_argument('--address', type=str, help='VC Address')
59+
60+
args = parser.parse_args()
61+
config_path = args.config_path
62+
binary = args.bin
63+
client = args.client
64+
server = args.server,
65+
address = args.address
66+
from_seqno = args.from_seqno
67+
68+
69+
hardforks = get_config()['validator']['hardforks']
70+
keep_hardforks = [h for h in hardforks if h['seqno'] <= from_seqno]
71+
hardforks = [h for h in hardforks if h['seqno'] > from_seqno]
72+
if len(hardforks) == 0:
73+
logger.info("No hardforks to process.")
74+
sys.exit(0)
75+
set_hardforks(keep_hardforks)
76+
77+
while True:
78+
if len(hardforks) == 0:
79+
break
80+
try:
81+
last_mc_seqno = get_last_mc_seqno()
82+
if not last_mc_seqno:
83+
logger.info("Waiting for last mc seqno...")
84+
time.sleep(300)
85+
continue
86+
if last_mc_seqno != hardforks[0]['seqno'] - 1:
87+
logger.info(f"Waiting for hardfork {hardforks[0]['seqno']}...")
88+
time.sleep(300)
89+
continue
90+
hardfork = hardforks.pop(0)
91+
logger.info(f"Processing hardfork {hardfork['seqno']}.")
92+
add_hardfork(hardfork)
93+
logger.info(f"Hardfork {hardfork['seqno']} has been added.")
94+
restart_node()
95+
logger.info(f"Node is restarted")
96+
except Exception as e:
97+
import traceback
98+
logger.error(f"Exception occurred: {e}\n{traceback.format_exc()}")
99+
time.sleep(60)
100+
101+
logger.info(f"All done.")

mytoninstaller/settings.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ def parse_block_value(local, block: str):
188188
return int(data['seqno'])
189189

190190

191+
def process_hardforks(local):
192+
193+
pass
194+
195+
196+
191197
def download_archive_from_ts(local):
192198
archive_blocks = os.getenv('ARCHIVE_BLOCKS')
193199
if archive_blocks is None:
@@ -274,6 +280,33 @@ def download_archive_from_ts(local):
274280
if block_to is not None:
275281
set_node_argument(local, ['--sync-shards-upto', str(block_to)])
276282

283+
with open(local.buffer.global_config_path, 'r') as f:
284+
c = json.loads(f.read())
285+
if c['validator']['hardforks'] and c['validator']['hardforks'][-1]['seqno'] > block_from:
286+
run_process_hardforks(local, block_from)
287+
288+
289+
def run_process_hardforks(local, from_seqno: int):
290+
script_path = os.path.join(local.buffer.mtc_src_dir, 'mytoninstaller', 'scripts', 'process_hardforks.py')
291+
log_path = "/tmp/process_hardforks_logs.txt"
292+
log_file = open(log_path, "a")
293+
294+
p = subprocess.Popen([
295+
"python3", script_path,
296+
"--from-seqno", str(from_seqno),
297+
"--config-path", local.buffer.global_config_path,
298+
# "--bin", local.buffer.ton_bin_dir + "validator-engine-console/validator-engine-console",
299+
# "--client", local.buffer.keys_dir + "client",
300+
# "--server", local.buffer.keys_dir + "server.pub",
301+
# "--address", "127.0.0.1:"
302+
],
303+
stdout=log_file,
304+
stderr=log_file,
305+
stdin=subprocess.DEVNULL,
306+
start_new_session=True # run in background
307+
)
308+
local.add_log(f"process_hardforks process is running in background, PID: {p.pid}, log file: {log_path}", "info")
309+
277310

278311
def DownloadDump(local):
279312
dump = local.buffer.dump

0 commit comments

Comments
 (0)