-
Notifications
You must be signed in to change notification settings - Fork 26
/
roll.py
executable file
·442 lines (347 loc) · 13 KB
/
roll.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python3
"""
This is the entry point for roll-op system, responsible for parsing command line arguments and
invoking the appropriate commands.
"""
import os
import shutil
import block_explorer
import account_abstraction
import deps
import l1
import l2
import l2_batcher
import l2_deploy
import l2_engine
import l2_node
import l2_proposer
import libroll as lib
import logrotate
from argparsing import Argparser
from config import Config, use_production_config
from processes import PROCESS_MGR
import setup
import state
####################################################################################################
p = Argparser(
program_name="rollop",
description="R|Helps you spin up an op-stack rollup.\n"
"Use `rollop <command> --help` to get more detailed help for a command.",
)
# --------------------------------------------------------------------------------------------------
# Global Options
p.arg(
"--name",
help="name of the rollup deployment",
default=None,
dest="name")
p.arg(
"--preset",
help="use a preset rollup configuration",
choices=["dev", "prod"],
default=None,
dest="preset")
p.arg(
"--config",
help="path to the config file",
default=None,
dest="config_path")
p.arg(
"--clean",
help="clean command-related output before running the specified command",
default=False,
dest="clean_first",
action="store_true")
p.arg(
"--no-ansi-esc",
help="disable ANSI escape codes for terminal manipulation",
default=True,
dest="use_ansi_esc",
action="store_false")
p.arg(
"--yes",
default=False,
dest="always_yes",
action="store_true",
help="answer yes to all prompts (install all requested dependencies)")
# --------------------------------------------------------------------------------------------------
p.delimiter("MAIN COMMANDS")
p.command(
"help",
help="show this help message and exit")
p.command(
"setup",
help="installs prerequisites and builds the optimism repository")
cmd_devnet = p.command(
"devnet",
help="starts a local devnet, comprising an L1 node and all L2 components")
p.command(
"clean",
help="cleans up deployment outputs and databases")
cmd_l2 = p.command(
"l2",
help="starts an L2 blockchain, deploying the contracts if needed")
p.command(
"aa",
help="starts an ERC-4337 bundler and a paymaster signer service")
p.command(
"explorer",
help="starts a block explorer")
# --------------------------------------------------------------------------------------------------
p.delimiter("GRANULAR COMMANDS")
p.command(
"l1",
help="starts a local L1 node",
description="Starts a local L1 node, initializing it if needed.")
p.command(
"deploy-l2",
help="deploys but does not start an L2 chain",
description="Deploys but does not start an L2 chain: "
"creates the genesis and deploys the contracts to L1.")
p.command(
"l2-engine",
help="starts a local L2 execution engine (op-geth) node",
description="Starts a local L2 execution engine (op-geth) node, initializing it if needed.")
p.command(
"l2-sequencer",
help="starts a local L2 node (op-node) in sequencer mode")
p.command(
"l2-batcher",
help="starts a local L2 transaction batcher")
p.command(
"l2-proposer",
help="starts a local L2 output roots proposer")
# --------------------------------------------------------------------------------------------------
p.delimiter("CLEANUP")
p.command(
"clean-build",
help="cleans up build outputs (but not deployment outputs)",
description="Cleans up build outputs — leaves deployment outputs intact, "
"as well as anything that was downloaded. "
"Mostly used to get the the downloaded repos to rebuild. "
"Requires rerunning make setup after running!")
p.command(
"clean-l2",
help="cleans up L2 deployment outputs")
p.command(
"clean-aa",
help="cleans up deployment outputs for account abstraction",
)
p.command(
"clean-explorer",
help="deletes the block explorer databases, logs, and containers")
# --------------------------------------------------------------------------------------------------
# Command-Specific Options
for cmd in [cmd_l2, cmd_devnet]:
cmd.arg(
"--explorer",
help="deploys a blockscout explorer for the L2 chain (NOT FUNCTIONAL)",
default=False,
dest="explorer",
action="store_true")
cmd.arg(
"--aa",
help="starts an ERC4337 bundler and a paymaster signer service",
default=False,
dest="aa",
action="store_true")
####################################################################################################
def load_config() -> Config:
"""
Uses the program arguments (found at `state.args`) to create and populate a :py:class:`Config`
object.
"""
deployment_name = state.args.name
deployment_name = deployment_name if deployment_name else "rollup"
config = Config(deployment_name)
# Define config preset
if state.args.preset is None or state.args.preset == "dev":
pass
elif state.args.preset == "prod":
use_production_config(config)
else:
# Should never happen, as correct preset is validated by argparse.
raise Exception(f"Unknown preset: '{state.args.preset}'. Valid: 'dev', 'prod'.")
config.deployment_name = deployment_name
# Parse config file if specified
if state.args.config_path:
try:
import tomli
except Exception:
raise Exception(
"Missing dependencies. Try running `rollop setup` first.")
if os.path.exists(state.args.config_path):
with open(state.args.config_path, mode="rb") as f:
config_file = tomli.load(f)
else:
raise Exception(f"Cannot find config file at {state.args.config_path}")
try:
for key, value in config_file.items():
if hasattr(config, key):
setattr(config, key, value)
if config_file.get("batch_inbox_address") is None:
# Derive a unique batch inbox address from the chain id.
addr = "0xff69000000000000000000000000000000000000"
str_id = str(config.l2_chain_id)
config.batching_inbox_address = addr[:-len(str_id)] + str_id
recommended_options = [
"l1_chain_id",
"l2_chain_id",
"l1_rpc_url",
"contract_deployer_key",
"batcher_account",
"batcher_key",
"proposer_account",
"proposer_key",
"admin_account",
"admin_key",
"p2p_sequencer_account",
"p2p_sequencer_key",
]
for option in recommended_options:
if config_file.get(option) is None:
print(f"Warning: config file does not specify `{option}`.\n"
"It is highly recommended to specify this option, "
"especially for non-local deployments.")
if config_file.get("l1_rpc_url") is not None \
and config_file.get("l1_rpc_for_node_url") is None:
print("Config file specifies l1_rpc_url but not l1_rpc_for_node_url.\n"
"Automatically setting l1_rpc_for_node_url to the same value.")
config.l1_rpc_for_node_url = config.l1_rpc_url
except KeyError as e:
raise Exception(f"Missing config file value: {e}")
config.validate()
return config
####################################################################################################
def start_addons(config: Config):
"""
Starts a block explorer and/or an ERC4337 bundler and paymaster, if configured to do so.
"""
if getattr(state.args, "explorer", None):
block_explorer.launch_blockscout(config)
if getattr(state.args, "aa", None):
account_abstraction.setup(config)
account_abstraction.deploy(config)
account_abstraction.start(config)
####################################################################################################
def wait(config: Config):
"""
Prevents the main process from shutdowning while subprocesses are running, and also
starts the logrotate thread.
"""
logrotate.start_thread(config)
PROCESS_MGR.wait_all()
####################################################################################################
def main():
state.args = p.parse()
config = None
try:
if state.args.command is None or state.args.command == "help":
p.print_help()
exit()
deps.basic_setup()
config = load_config()
deps.create_paths(config)
if state.args.command == "setup":
setup.setup(config)
return
deps.post_setup()
if state.args.command == "devnet":
if state.args.clean_first:
l1.clean(config)
l2.clean(config)
deps.check_or_install_geth()
deps.check_or_install_foundry()
if config.run_devnet_l1:
l1.deploy_devnet_l1(config)
l2.deploy_and_start(config)
start_addons(config)
wait(config)
elif state.args.command == "clean":
if getattr(state.args, "aa", None):
account_abstraction.clean(config)
if getattr(state.args, "explorer", None):
block_explorer.clean(config)
l1.clean(config)
l2.clean(config)
shutil.rmtree(config.deployment_dir, ignore_errors=True)
elif state.args.command == "l2":
if state.args.clean_first:
l2.clean(config)
deps.check_or_install_foundry()
l2.deploy_and_start(config)
start_addons(config)
wait(config)
elif state.args.command == "aa":
if state.args.clean_first:
account_abstraction.clean(config)
account_abstraction.setup(config)
account_abstraction.deploy(config)
account_abstraction.start(config)
wait(config)
elif state.args.command == "explorer":
if state.args.clean_first:
block_explorer.clean(config)
block_explorer.launch_blockscout(config)
wait(config)
elif state.args.command == "l1":
if state.args.clean_first:
l1.clean(config)
deps.check_or_install_geth()
deps.check_or_install_foundry()
l1.deploy_devnet_l1(config)
wait(config)
elif state.args.command == "deploy-l2":
if state.args.clean_first:
l2.clean(config)
deps.check_or_install_foundry()
l2_deploy.deploy(config)
elif state.args.command == "l2-engine":
if state.args.clean_first:
l2_engine.clean(config)
l2_engine.start(config)
if hasattr(state.args, "explorer") and state.args.explorer:
block_explorer.launch_blockscout(config)
wait(config)
elif state.args.command == "l2-sequencer":
if state.args.clean_first:
l2_node.clean(config)
l2_node.start(config, sequencer=True)
wait(config)
elif state.args.command == "l2-batcher":
# nothing to clean
l2_batcher.start(config)
wait(config)
elif state.args.command == "l2-proposer":
# nothing to clean
config.deployments = lib.read_json_file(config.addresses_path)
l2_proposer.start(config)
wait(config)
elif state.args.command == "clean-build":
setup.clean_build()
elif state.args.command == "clean-l2":
l2.clean(config)
elif state.args.command == "clean-aa":
account_abstraction.clean(config)
elif state.args.command == "clean-explorer":
block_explorer.clean(config)
print("Done.")
except KeyboardInterrupt:
# Usually not triggered because we will exit via the exit hook handler.
print("Interrupted by user.")
except Exception as e:
if config is None:
raise e
else:
import traceback
trace_path = os.path.join(config.logs_dir, "trace.log")
with open(trace_path, "w") as f:
f.write(str(e))
f.write(traceback.format_exc())
print(f"Aborted with error: {e}\n"
f"See log file for full trace: {trace_path}")
exit(1)
####################################################################################################
if __name__ == "__main__":
main()
####################################################################################################