Skip to content

Commit 622c3f6

Browse files
committed
add collator config
1 parent f511780 commit 622c3f6

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

modules/collator_config.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import json
2+
import requests
3+
4+
from mypylib.mypylib import color_print
5+
from modules.module import MtcModule
6+
from mytoncore.utils import hex2base64
7+
8+
9+
class CollatorConfigModule(MtcModule):
10+
11+
@staticmethod
12+
def check_config_url(url):
13+
try:
14+
r = requests.get(url, timeout=3)
15+
if r.status_code != 200:
16+
print(f'Failed to get config from {url}: {r.status_code} code; {r.text}')
17+
return
18+
return r.json()
19+
except Exception as e:
20+
print(f'Failed to get config from {url}: {e}')
21+
return
22+
23+
@staticmethod
24+
def check_config_file(path):
25+
try:
26+
with open(path, 'r') as f:
27+
return json.load(f)
28+
except Exception as e:
29+
print(f'Failed to read config from {path}: {e}')
30+
return
31+
32+
@staticmethod
33+
def get_config(path):
34+
if 'http' in path:
35+
config = CollatorConfigModule.check_config_url(path)
36+
else:
37+
config = CollatorConfigModule.check_config_file(path)
38+
if config is None:
39+
raise Exception(f'Failed to get config')
40+
return config
41+
42+
def add_collator_config_to_vc(self, config: dict):
43+
self.local.add_log(f"Adding collator options config to validator console", "debug")
44+
path = self.ton.tempDir + f'/collator_config.json'
45+
with open(path, 'w') as f:
46+
json.dump(config, f)
47+
result = self.ton.validatorConsole.Run(f"setcollatoroptionsjson {path}")
48+
return 'success' in result, result
49+
50+
def set_collator_config(self, args):
51+
if len(args) != 1:
52+
color_print("{red}Bad args. Usage:{endc} set_collator_config <path/url>")
53+
return
54+
location = args[0]
55+
config = self.get_config(location)
56+
self.ton.set_collator_config(location)
57+
added, msg = self.add_collator_config_to_vc(config)
58+
if not added:
59+
print(f'Failed to add collator config to validator console: {msg}')
60+
color_print("set_collator_config - {red}ERROR{endc}")
61+
return
62+
color_print("set_collator_config - {green}OK{endc}")
63+
64+
def get_collator_config(self, args):
65+
location = self.ton.get_collator_config_location()
66+
print(f'Collator config location: {location}')
67+
path = self.ton.tempDir + f'/current_collator_config.json'
68+
output = self.ton.validatorConsole.Run(f'getcollatoroptionsjson {path}')
69+
if 'saved config to' not in output:
70+
print(f'Failed to get collator config: {output}')
71+
color_print("get_collator_config - {red}ERROR{endc}")
72+
return
73+
with open(path, 'r') as f:
74+
config = json.load(f)
75+
print(f'Collator config:')
76+
print(json.dumps(config, indent=4))
77+
color_print("get_collator_config - {green}OK{endc}")
78+
79+
def update_collator_config(self, args):
80+
location = self.ton.get_collator_config()
81+
config = self.get_config(location)
82+
added, msg = self.add_collator_config_to_vc(config)
83+
if not added:
84+
print(f'Failed to add collator config to validator console: {msg}')
85+
color_print("update_collator_config - {red}ERROR{endc}")
86+
return
87+
color_print("update_collator_config - {green}OK{endc}")
88+
89+
def add_console_commands(self, console):
90+
console.AddItem("set_collator_config", self.set_collator_config, self.local.translate("set_collator_config_cmd"))
91+
console.AddItem("update_collator_config", self.update_collator_config, self.local.translate("update_collator_config_cmd"))
92+
console.AddItem("get_collator_config", self.get_collator_config, self.local.translate("get_collator_config_cmd"))

mytoncore/mytoncore.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,16 @@ def delete_custom_overlay(self, name: str):
39863986
del self.local.db['custom_overlays'][name]
39873987
self.local.save()
39883988

3989+
def set_collator_config(self, location: str):
3990+
self.local.db['collator_config'] = location
3991+
self.local.save()
3992+
3993+
def get_collator_config_location(self):
3994+
default = 'https://raw.githubusercontent.com/ton-blockchain/ton-blockchain.github.io/main/default_collator_options.json'
3995+
location = self.local.db.get('collator_config', default)
3996+
if location is None:
3997+
location = default
3998+
return location
39893999

39904000
def GetNetworkName(self):
39914001
data = self.local.read_db(self.liteClient.configPath)

mytonctrl/mytonctrl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def inject_globals(func):
128128
module = CustomOverlayModule(ton, local)
129129
module.add_console_commands(console)
130130

131+
from modules.collator_config import CollatorConfigModule
132+
module = CollatorConfigModule(ton, local)
133+
module.add_console_commands(console)
134+
131135
if ton.using_validator():
132136
from modules.validator import ValidatorModule
133137
module = ValidatorModule(ton, local)

0 commit comments

Comments
 (0)