-
Notifications
You must be signed in to change notification settings - Fork 26
/
mkchain.py
364 lines (314 loc) · 12 KB
/
mkchain.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import argparse
import os
import sys
from datetime import datetime, timezone
import yaml
# https://stackoverflow.com/a/52424865/207209
class MyDumper(yaml.Dumper): # your force-indent dumper
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
class QuotedString(str): # just subclass the built-in str
pass
def quoted_scalar(dumper, data): # a representer to force quotations on scalars
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style='"')
# add the QuotedString custom type with a forced quotation representer to your dumper
MyDumper.add_representer(QuotedString, quoted_scalar)
# end https://stackoverflow.com/a/52424865/207209
from tqchain.keys import gen_key, set_use_docker
from ._version import get_versions
sys.path.insert(0, "tqchain")
__version__ = get_versions()["version"]
ARCHIVE_BAKER_NODE_NAME = "archive-baking-node"
ROLLING_REGULAR_NODE_NAME = "rolling-node"
cli_args = {
"should_generate_unsafe_deterministic_data": {
"help": (
"Should tezos-k8s generate deterministic account keys and genesis"
" block hash instead of mkchain using tezos-client to generate"
" random ones. This option is helpful for testing purposes."
),
"action": "store_true",
"default": False,
},
"number_of_nodes": {
"help": "number of peers in the cluster",
"default": 0,
"type": int,
},
"number_of_bakers": {
"help": "number of bakers in the cluster",
"default": 1,
"type": int,
},
"zerotier_network": {"help": "Zerotier network id for external chain access"},
"zerotier_token": {"help": "Zerotier token for external chain access"},
"expected_proof_of_work": {
"help": "Node identity generation difficulty",
"default": 0,
"type": int,
},
"bootstrap_peers": {
"help": "Bootstrap addresses to connect to. Can specify multiple.",
"action": "extend",
"nargs": "+",
},
"octez_docker_image": {
"help": "Version of the Octez docker image",
"default": "tezos/tezos:v14-release",
},
"use_docker": {
"action": "store_true",
"default": None,
},
"no_use_docker": {
"dest": "use_docker",
"action": "store_false",
},
}
# python versions < 3.8 doesn't have "extend" action
class ExtendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest) or []
items.extend(values)
setattr(namespace, self.dest, items)
def get_args():
parser = argparse.ArgumentParser(
description="Generate helm values for use with the tezos-chain helm chart"
)
parser.register("action", "extend", ExtendAction)
parser.add_argument("chain_name", action="store", help="Name of your chain")
parser.add_argument(
"-v",
"--version",
action="version",
version="%(prog)s {version}".format(version=__version__),
)
for k, v in cli_args.items():
parser.add_argument(*["--" + k.replace("_", "-")], **v)
return parser.parse_args()
def validate_args(args):
if args.number_of_nodes < 0:
print(
f"Invalid argument --number-of-nodes ({args.number_of_nodes}) "
f"must be non-negative"
)
exit(1)
if args.number_of_bakers < 0:
print(
f"Invalid argument --number-of-bakers ({args.number_of_bakers}) "
f"must be non-negative"
)
exit(1)
if args.number_of_nodes + args.number_of_bakers < 1:
print(
f"Invalid arguments: either "
f"--number-of-nodes ({args.number_of_nodes}) or "
f"--number-of-bakers ({args.number_of_bakers}) must be non-zero"
)
exit(1)
if (not args.zerotier_network and args.zerotier_token) or (
not args.zerotier_token and args.zerotier_network
):
print("Configuring Zerotier requires both a network id and access token.")
exit(1)
if args.zerotier_network and args.should_generate_unsafe_deterministic_data:
print(
"Configuring a Zerotier network and generating unsafe deterministic data is not allowed."
)
exit(1)
def node_config(name, n, is_baker):
ret = {
"is_bootstrap_node": False,
"config": {
"shell": {"history_mode": "rolling"},
"metrics_addr": [":9932"],
},
}
if is_baker:
ret["bake_using_accounts"] = [f"{name}-{n}"]
if n < 2:
ret["is_bootstrap_node"] = True
ret["config"]["shell"]["history_mode"] = "archive"
return ret
def main():
args = get_args()
validate_args(args)
set_use_docker(args.use_docker)
base_constants = {
"images": {
"octez": args.octez_docker_image,
},
"node_config_network": {"chain_name": args.chain_name},
"zerotier_config": {
"zerotier_network": args.zerotier_network,
"zerotier_token": args.zerotier_token,
},
# Custom chains should not pull snapshots or tarballs
"full_snapshot_url": None,
"rolling_snapshot_url": None,
"archive_tarball_url": None,
"rolling_tarball_url": None,
"node_globals": {
# Needs a quotedstring otherwise helm interprets "Y" as true and it does not work
"env": {
"all": {"TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER": QuotedString("Y")}
}
},
"protocols": [
{
"command": "014-PtKathma",
"vote": {"liquidity_baking_toggle_vote": "pass"},
}
],
}
# preserve pre-existing values, if any (in case of scale-up)
old_create_values = {}
old_invite_values = {}
files_path = f"{os.getcwd()}/{args.chain_name}"
if os.path.isfile(f"{files_path}_values.yaml"):
print(
"Found old values file. Some pre-existing values might remain\n"
"the same, e.g. public/private keys, and genesis block. Please\n"
"delete the values file to generate all new values.\n"
)
with open(f"{files_path}_values.yaml", "r") as yaml_file:
old_create_values = yaml.safe_load(yaml_file)
current_number_of_bakers = len(
old_create_values["nodes"][ARCHIVE_BAKER_NODE_NAME]["instances"]
)
if current_number_of_bakers != args.number_of_bakers:
print("ERROR: the number of bakers must not change on a pre-existing chain")
print(f"Current number of bakers: {current_number_of_bakers}")
print(f"Attempted change to {args.number_of_bakers} bakers")
exit(1)
if os.path.isfile(f"{files_path}_invite_values.yaml"):
with open(f"{files_path}_invite_values.yaml", "r") as yaml_file:
old_invite_values = yaml.safe_load(yaml_file)
if old_create_values.get("node_config_network", {}).get("genesis"):
print("Using existing genesis parameters")
base_constants["node_config_network"]["genesis"] = old_create_values[
"node_config_network"
]["genesis"]
else:
# create new chain genesis params if brand new chain
base_constants["node_config_network"]["genesis"] = {
"protocol": "Ps9mPmXaRzmzk35gbAYNCAw6UXdE2qoABTHbN2oEEc1qM7CwT9P",
"timestamp": datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(),
}
accounts = {"secret": {}, "public": {}}
if old_create_values.get("accounts"):
print("Using existing secret keys")
accounts["secret"] = old_create_values["accounts"]
if old_invite_values.get("accounts"):
print("Using existing public keys")
accounts["public"] = old_invite_values["accounts"]
elif not args.should_generate_unsafe_deterministic_data:
baking_accounts = {
f"{ARCHIVE_BAKER_NODE_NAME}-{n}": {} for n in range(args.number_of_bakers)
}
for account in baking_accounts:
print(f"Generating keys for account {account}")
keys = gen_key(args.octez_docker_image)
for key_type in keys:
accounts[key_type][account] = {
"key": keys[key_type],
"is_bootstrap_baker_account": True,
"bootstrap_balance": "4000000000000",
}
# First 2 bakers are acting as bootstrap nodes for the others, and run in
# archive mode. Any other bakers will be in rolling mode.
creation_nodes = {
ARCHIVE_BAKER_NODE_NAME: {
"runs": ["octez_node", "baker"],
"storage_size": "15Gi",
"instances": [
node_config(ARCHIVE_BAKER_NODE_NAME, n, is_baker=True)
for n in range(args.number_of_bakers)
],
},
ROLLING_REGULAR_NODE_NAME: None,
}
if args.number_of_nodes:
creation_nodes[ROLLING_REGULAR_NODE_NAME] = {
"storage_size": "15Gi",
"instances": [
node_config(ROLLING_REGULAR_NODE_NAME, n, is_baker=False)
for n in range(args.number_of_nodes)
],
}
signers = {
"tezos-signer-0": {
"sign_for_accounts": [
f"{ARCHIVE_BAKER_NODE_NAME}-{n}" for n in range(args.number_of_bakers)
]
}
}
activation_account_name = f"{ARCHIVE_BAKER_NODE_NAME}-0"
base_constants["node_config_network"][
"activation_account_name"
] = activation_account_name
with open(
f"{os.path.dirname(os.path.realpath(__file__))}/parameters.yaml", "r"
) as yaml_file:
parametersYaml = yaml.safe_load(yaml_file)
activation = {
"activation": {
"protocol_hash": "PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg",
"protocol_parameters": parametersYaml,
},
}
bootstrap_peers = args.bootstrap_peers if args.bootstrap_peers else []
creation_constants = {
"is_invitation": False,
"should_generate_unsafe_deterministic_data": args.should_generate_unsafe_deterministic_data,
"expected_proof_of_work": args.expected_proof_of_work,
**base_constants,
"bootstrap_peers": bootstrap_peers,
"accounts": accounts["secret"],
"signers": signers,
"nodes": creation_nodes,
**activation,
}
with open(f"{files_path}_values.yaml", "w") as yaml_file:
yaml.dump(
creation_constants,
yaml_file,
Dumper=MyDumper,
default_flow_style=False,
sort_keys=False,
)
print(f"Wrote chain creation constants to {files_path}_values.yaml")
# If there is a Zerotier configuration, create an invite file.
if not args.should_generate_unsafe_deterministic_data and base_constants.get(
"zerotier_config", {}
).get("zerotier_network"):
invite_nodes = {
ROLLING_REGULAR_NODE_NAME: {
"storage_size": "15Gi",
"instances": [
node_config(ROLLING_REGULAR_NODE_NAME, 0, is_baker=False)
],
},
ARCHIVE_BAKER_NODE_NAME: None,
}
invitation_constants = {
"is_invitation": True,
"expected_proof_of_work": args.expected_proof_of_work,
**base_constants,
"accounts": accounts["public"],
"bootstrap_peers": bootstrap_peers,
"nodes": invite_nodes,
}
with open(f"{files_path}_invite_values.yaml", "w") as yaml_file:
print(
f"Wrote chain invitation constants to {files_path}_invite_values.yaml"
)
yaml.dump(
invitation_constants,
yaml_file,
Dumper=MyDumper,
default_flow_style=False,
sort_keys=False,
)
if __name__ == "__main__":
main()