Skip to content

Commit 09d330b

Browse files
authored
Merge pull request #305 from yungwine/mytonctrl2_dev
add collator config to mtc2.0
2 parents 7d46dc7 + ca211e9 commit 09d330b

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

modules/collator_config.py

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