forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-test-node-config.py
207 lines (169 loc) · 6.45 KB
/
generate-test-node-config.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/python3.7
import subprocess
import json
import pprint
import base64
import hashlib
configname="server.list"
binary="../ton-build/generate-random-id"
ids=[]
catchains=[]
public_overlays=[]
def ip2str(ip):
b = ip.split(".")
v = (int(b[0]) << 24) + (int(b[1]) << 16) + (int(b[2]) << 8) + int(b[3])
if (v >= (1 << 31)):
return '-' + str((1 << 32) - v)
else:
return str(v)
def get_addr_list(node):
return '{"@type":"adnl.addressList","version":0,"addrs":[{"@type":"adnl.address.udp","ip":'+ \
ip2str(node['ip'])+',"port":'+node['port']+'}]}'
def generate_dht_node(node):
global binary
addr_list = get_addr_list(node)
r = subprocess.run([binary, '-k', node['spk'], '-a', addr_list, '-m', 'dht'], capture_output=True)
s = r.stdout.decode("utf-8").split("\n")
return json.loads(s[0])
def add_id(s):
global binary
if len(s) < 1 or s[0] == '#':
return
t = s.split(" ")
assert(len(t) >= 3)
ip = t[0]
port = t[1]
pk = t[2]
options = t[3:]
pub = '-'
short = '-'
rand = None
spk = pk
if (pk[0] != '-'):
r = subprocess.run([binary, '-k', pk, '-m', 'id'], capture_output=True)
s = r.stdout.decode("utf-8").split("\n")
pk = json.loads(pk)
pub = json.loads(s[1])
short = json.loads(s[2])
if '+dhtstatic' in options:
if not '+dht' in options:
options.append('+dht')
else:
if '+dhtstatic' in options:
print("cannot use dht static on random node")
sys.exit(2)
if any(opt.startswith('+catchain') for opt in options):
print("cannot use catchain on random node")
sys.exit(2)
rand=int(pk[1:])
global ids
if rand == None:
ids.append({'ip' : ip, 'port' : port, 'options' : options, 'pk' : pk, 'spk' : spk, 'pub' : pub, 'short' : short, 'rand' : rand})
else:
for x in range(0, rand):
r = subprocess.run([binary, '-m', 'id'], capture_output=True)
s = r.stdout.decode("utf-8").split("\n")
pk = json.loads(s[0])
pub = json.loads(s[1])
short = json.loads(s[2])
ids.append({'ip' : ip, 'port' : port, 'options' : options, 'pk' : pk, 'spk' : spk, 'pub' : pub, 'short' : short, 'rand' : None})
def readconfig(name):
with open(name) as f:
for s in f.readlines():
add_id(s.strip())
def generate_global_config():
global ids
config={'@type':'config.global'}
dht={'@type':'dht.config.global','k':6,'a':3}
dht_nodes=[]
for node in ids:
if '+dhtstatic' in node['options']:
dht_nodes.append(generate_dht_node(node))
dht['static_nodes'] = {'@type':'dht.nodes','nodes':dht_nodes}
config['dht'] = dht
catchains = []
for x in ids:
for y in x['options']:
if y.startswith('+catchain'):
assert(x['rand'] == None)
name=y[9:]
if not name in catchains:
catchains.append(name)
cc=[]
for name in catchains:
catchain={'@type':'catchain.config.global','tag':base64.b64encode(hashlib.sha256(name.encode("utf-8")).digest()).decode("utf-8")}
catchain_nodes=[]
for node in ids:
if ('+catchain'+name) in node['options']:
catchain_nodes.append(node['pub'])
catchain['nodes'] = catchain_nodes
cc.append(catchain)
config['catchains'] = cc
liteservers=[]
for node in ids:
for opt in node['options']:
if opt.startswith('+liteserver'):
assert(node['rand'] == None)
name=opt[9:]
liteservers.append({'@type':'liteservers.config.global','ip':int(ip2str(node['ip'])), 'port':4924,'id':node['pub']})
config['liteservers'] = liteservers
validators = {"@type": "validator.config.global", "zero_state": {
"workchain" : -1,
"shard" : -9223372036854775808,
"seqno" : 0,
"root_hash": "DXduYJkakj2d+rB7Qpj2SCkjTCG7AGXAA9G7EHQEyG0=",
"file_hash": "4xASfDALy6Hk5Tg01IyBoFUepur9YJg2pg3cm0zocJk="
}}
config['validator'] = validators
with open('ton-global.config.json', 'w') as outfile:
json.dump(config, outfile, indent=2)
def generate_local_config(ip,port):
global ids
config = {'@type' : 'config.local'}
ports=[]
for node in ids:
if node['ip'] == ip and node['port'] == port:
ports.append(int(node['port']))
ports = sorted(set(ports))
config['udp_ports'] = ports
ids_config=[]
for node in ids:
if node['rand'] == None and node['ip'] == ip and node['port'] == port:
ids_config.append({'@type':'id.config.local','id':node['pk']})
config['local_ids'] = ids_config
dht_config=[]
for node in ids:
if node['ip'] == ip and node['port'] == port and ('+dht' in node['options']):
if node['rand'] == None:
dht_config.append({'@type':'dht.config.local', 'id':node['short']})
else:
cnt=node['rand']
dht_config.append({'@type':'dht.config.random.local', 'cnt':cnt})
config['dht'] = dht_config
liteservers=[]
for node in ids:
for opt in node['options']:
if opt.startswith('+liteserver') and node['ip'] == ip and node['port'] == port:
assert(node['rand'] == None)
name=opt[9:]
liteservers.append({'@type':'liteserver.config.local','port':4924,'id':node['pk']})
config['liteservers'] = liteservers
validators=[]
for node in ids:
for opt in node['options']:
if opt == '+validator' and node['ip'] == ip and node['port'] == port:
assert(node['rand'] == None)
validators.append({'@type':'validator.config.local', 'id':node['short']})
config['validators'] = validators
controlserver = {'@type':'control.config.local', 'priv':{"@type":"pk.ed25519","key":"jRbqvPhSr3/xylof9zQyeqbplvWPSIGiHSft3ovKVc4="}, \
'pub':'Fv8DAtv6nqnrHIPpmv4LGIw0D9cMoF40JXQdM2WVMQM=', 'port':(int(port) + 1000)}
config['control'] = [controlserver]
with open('ton-local.' + ip + '.' + port + '.config.json', 'w') as outfile:
json.dump(config, outfile, indent=2)
readconfig(configname)
generate_global_config()
ips=["" + node['ip'] + ":" + node['port'] for node in ids]
ips=set(ips)
for ip in ips:
b = ip.split(":")
generate_local_config(b[0], b[1])