forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_layer.py
executable file
·612 lines (497 loc) · 18.3 KB
/
execution_layer.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/env python3
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0
import argparse
from os import chdir, remove
from pathlib import Path
import re
from shutil import which, rmtree
import subprocess
from sys import stderr, stdout
from typing import TextIO, Union
def parse_args():
parser = argparse.ArgumentParser(
prog="execution-layer",
)
subparsers = parser.add_subparsers(
description="Tools for managing cuts of the execution-layer.",
)
cut = subparsers.add_parser(
"cut",
help=(
"Create a new copy of execution-related crates, and add them to "
"the workspace."
),
)
cut.set_defaults(cmd="Cut", do=do_cut)
cut.add_argument(
"feature",
type=feature,
help="The name of the new cut to make.",
)
cut.add_argument(
"--dry-run",
action="store_true",
help="Print the operations, without running them.",
)
generate_lib = subparsers.add_parser(
"generate-lib",
help=(
"Generate `sui-execution/src/lib.rs` based on the current set of "
"execution crates (i.e. without adding a new one). Prints out "
"what the contents of `lib.rs` would be without actually writing "
"out to the file, if --dry-run flag is supplied."
),
)
generate_lib.set_defaults(cmd="Generating lib.rs", do=do_generate_lib)
generate_lib.add_argument(
"--dry-run",
action="store_true",
help="Print the operations, without running them.",
)
merge = subparsers.add_parser(
"merge",
help="Apply the changes made to FEATURE since it was cut, to BASE.",
)
merge.set_defaults(cmd="Merge", do=do_merge)
merge.add_argument("base", type=feature, help="The cut to merge into.")
merge.add_argument("feature", type=feature, help="The cut to take from.")
merge.add_argument(
"--dry-run",
action="store_true",
help="Print the patch file to apply to BASE without applying it.",
)
merge.add_argument(
"-f",
"--force",
action="store_true",
help=(
"Merge regardless of warnings that the working directory is "
"not clean. If the merge fails at this point, it may be "
"difficult to recover from."
),
)
patch = subparsers.add_parser(
"patch",
help=(
"Print a patch file representing the changes made to FEATURE "
"after the commit in which it was cut."
),
)
patch.set_defaults(do=do_patch)
patch.add_argument(
"feature",
type=feature,
help="The name of the cut to generate the patch for.",
)
rebase = subparsers.add_parser(
"rebase",
help=(
"Rebase the changes to FEATURE against the current contents of "
"'latest'."
),
)
rebase.set_defaults(cmd="Rebase", do=do_rebase)
rebase.add_argument(
"feature",
type=feature,
help="The name of the cut to rebase against 'latest'.",
)
rebase.add_argument(
"-f",
"--force",
action="store_true",
help=(
"Rebase regardless of warnings that the working directory is "
"not clean. If the rebase fails at this point, it may be "
"difficult to recover from."
),
)
return parser.parse_args()
def feature(f):
if re.match(r"[a-z][a-zA-Z0-9_]*", f):
return f
else:
raise argparse.ArgumentTypeError(f"Invalid feature name: '{f}'")
def do_cut(args):
"""Perform the actions of the 'cut' sub-command.
Accepts the parsed command-line arguments as a parameter."""
ensure_cut_binary()
cmd = cut_command(args.feature)
if args.dry_run:
cmd.append("--dry-run")
print(run(cmd))
return
impl_module = impl(args.feature)
if impl_module.is_file():
raise Exception(
f"Impl for '{args.feature}' already exists at '{impl_module}'"
)
print("Cutting new release...", file=stderr)
result = subprocess.run(cmd, stdout=stdout, stderr=stderr)
if result.returncode != 0:
print("Cut failed", file=stderr)
exit(result.returncode)
update_toml(args.feature, Path() / "sui-execution" / "Cargo.toml")
generate_impls(args.feature, impl_module)
with open(Path() / "sui-execution" / "src" / "lib.rs", mode="w") as lib:
generate_lib(lib)
def do_generate_lib(args):
if args.dry_run:
generate_lib(stdout)
else:
lib_path = Path() / "sui-execution" / "src" / "lib.rs"
with open(lib_path, mode="w") as lib:
generate_lib(lib)
def do_merge(args):
from_module = impl(args.feature)
if not from_module.is_file():
raise Exception(f"'{args.feature}' does not exist.")
to_module = impl(args.base)
if not to_module.is_file():
raise Exception(f"'{args.base}' does not exist.")
print("Calculating change...", file=stderr)
changes = patch(args.feature)
if not changes:
print("No changes.", file=stderr)
return
print("Porting feature to base...", file=stderr)
adapted = change_patch_directories(changes, args.feature, args.base)
if args.dry_run:
print(adapted)
return
if not args.force and not is_repo_clean():
raise Exception(
"Working directory or index is not clean, not merging. Re-run "
"with --force to ignore warning."
)
print("Applying changes...", file=stderr)
subprocess.run(
["git", "apply", "-"],
input=adapted,
text=True,
stdout=stdout,
stderr=stderr,
)
def do_patch(args):
print(patch(args.feature))
def do_rebase(args):
impl_module = impl(args.feature)
if not impl_module.is_file():
raise Exception(f"'{args.feature}' does not exist.")
if not args.force and not is_repo_clean():
raise Exception(
"Working directory or index is not clean, not rebasing. Re-run "
"with --force to ignore warning."
)
# Need to do this before we delete the existing crates, because it
# will leave the workspace in an inconsistent state, so we can't
# build the `cut` binary at that point.
ensure_cut_binary()
print("Preserving changes...", file=stderr)
changes = patch(args.feature) or ""
print("Cleaning feature...", file=stderr)
delete_cut_crates(args.feature)
print("Re-generating feature...", file=stderr)
cmd = cut_command(args.feature)
cmd.append("--no-workspace-update")
result = subprocess.run(cmd, stdout=stdout, stderr=stderr)
if result.returncode != 0:
print("Re-generation failed.", file=stderr)
exit(result.returncode)
print("Re-applying changes...", file=stderr)
subprocess.run(
["git", "am", "-3", "-"],
input=changes,
text=True,
stdout=stdout,
stderr=stderr,
)
def run(command):
"""Run command, and return its stdout as a UTF-8 string."""
result = subprocess.run(command, stdout=subprocess.PIPE)
return result.stdout.decode("utf-8")
def repo_root():
"""Find the repository root, using git."""
return run(["git", "rev-parse", "--show-toplevel"]).strip()
def origin_commit(feature):
"""Find the commit that introduced cut with name `feature`.
Returns the commit hash as a string if one can be found. Returns
`None` if the cut exists but hasn't been committed, and raises an
`Exception` if the cut does not exist.
"""
impl_module = impl(feature)
if not impl_module.is_file():
raise Exception(f"Cut '{feature}' does not exist.")
commit = run(
[
*["git", "log"],
"--pretty=format:%H",
"--diff-filter=A",
*["--", impl_module],
]
).strip()
return None if commit == "" else commit
def is_repo_clean():
"""Checks whether the repo is in a clean state."""
return run(["git", "status", "--porcelain"]).strip() == ""
def patch(feature):
sha = origin_commit(args.feature)
if sha is None:
return
return run(
[
*["git", "diff", f"{sha}...HEAD", "--"],
*cut_directories(args.feature),
]
)
def change_patch_directories(patch, feature, base):
"""Fix-up patch referring to `feature` to refer to `base`.
Look for references to directories in cut `feature` in the
metadata of patch and replace them with references to the
equivalent directories in `base`.
"""
def is_metadata(line):
return (
line.startswith("+++")
or line.startswith("---")
or line.startswith("diff --git")
)
# Mapping from directories that might appear in the patch file to
# the directories they should be replaced by. Represented as a
# list of pairs as it will be iterated over.
mapping = list(
zip(
map(str, cut_directories(feature)),
map(str, cut_directories(base)),
)
)
def sub(line):
for feat, base in mapping:
line = line.replace(feat, base)
return line
return "".join(
(sub(line) if is_metadata(line) else line) + "\n"
for line in patch.splitlines()
if not line.startswith("index")
)
def ensure_cut_binary():
"""Ensure a build of the cut binary exists."""
result = subprocess.run(
["cargo", "build", "--bin", "cut"],
stdout=stdout,
stderr=stderr,
)
if result.returncode != 0:
return Exception("Failed to build 'cut' binary.")
def cut_command(f):
"""Arguments for creating the cut for 'feature'."""
return [
*["./target/debug/cut", "--feature", f],
*["-d", f"sui-execution/latest:sui-execution/{f}:-latest"],
*["-d", f"external-crates/move:external-crates/move/move-execution/{f}"],
*["-p", "sui-adapter-latest"],
*["-p", "sui-move-natives-latest"],
*["-p", "sui-verifier-latest"],
*["-p", "move-abstract-interpreter"],
*["-p", "move-bytecode-verifier"],
*["-p", "move-stdlib-natives"],
*["-p", "move-vm-runtime"],
*["-p", "bytecode-verifier-tests"],
]
def cut_directories(f):
"""Directories containing crates for `feature`."""
sui_base = Path() / "sui-execution"
external = Path() / "external-crates"
crates = [
sui_base / f / "sui-adapter",
sui_base / f / "sui-move-natives",
sui_base / f / "sui-verifier",
]
if f == "latest":
crates.extend(
[
external / "move" / "crates" / "move-abstract-interpreter",
external / "move" / "crates" / "move-bytecode-verifier",
external / "move" / "crates" / "move-stdlib-natives",
external / "move" / "crates" / "move-vm-runtime",
external / "move" / "crates" / "bytecode-verifier-tests",
]
)
else:
crates.extend(
[
external / "move" / "move-execution" / f / "crates" / "move-abstract-interpreter",
external / "move" / "move-execution" / f / "crates" / "move-bytecode-verifier",
external / "move" / "move-execution" / f / "crates" / "move-stdlib-natives",
external / "move" / "move-execution" / f / "crates" / "move-vm-runtime",
external / "move" / "move-execution" / f / "crates" / "bytecode-verifier-tests",
]
)
return crates
def impl(feature):
"""Path to the impl module for this feature"""
return Path() / "sui-execution" / "src" / (feature.replace("-", "_") + ".rs")
def delete_cut_crates(feature):
"""Delete `feature`-specific crates."""
if feature == "latest":
raise Exception("Can't delete 'latest'")
for module in cut_directories(feature):
rmtree(module)
def update_toml(feature, toml_path):
"""Add dependencies for 'feature' to sui-execution's manifest."""
# Read all the lines
with open(toml_path) as toml:
lines = toml.readlines()
# Write them back, looking for template comment lines
with open(toml_path, mode="w") as toml:
for line in lines:
if line.startswith("# ") and "$CUT" in line:
toml.write(line[2:].replace("$CUT", feature))
toml.write(line)
def generate_impls(feature, copy):
"""Create the implementations of the `Executor` and `Verifier`.
Copies the implementation for the `latest` cut and updates its imports."""
orig = Path() / "sui-execution" / "src" / "latest.rs"
with open(orig, mode="r") as orig, open(copy, mode="w") as copy:
for line in orig:
line = re.sub(r"^use (.*)_latest::", rf"use \1_{feature.replace('-', '_')}::", line)
copy.write(line)
def generate_lib(output_file: TextIO):
"""Expose all `Executor` and `Verifier` impls via lib.rs
Generates the contents of sui-execution/src/lib.rs to assign a numeric
execution version for every module that implements an execution version.
Version snapshots (whose names follow the pattern `/v[0-9]+/`) are assigned
versions according to their names (v0 gets 0, v1 gets 1, etc).
`latest` gets the next version after all version snapshots.
Feature snapshots (all other snapshots) are assigned versions starting with
`u64::MAX` and going down, in the order they were created (as measured by
git commit timestamps)
The generated contents are written out to `output_file` (an IO device).
"""
template_path = Path() / "sui-execution" / "src" / "lib.template.rs"
cuts = discover_cuts()
with open(template_path, mode="r") as template_file:
template = template_file.read()
def substitute(m):
spc = m.group(1)
var = m.group(2)
if var == "GENERATED_MESSAGE":
cmd = "./scripts/execution-layer"
return f"{spc}// DO NOT MODIFY, Generated by {cmd}"
elif var == "MOD_CUTS":
return "".join(sorted(f"{spc}mod {cut};" for (_, _, cut) in cuts))
elif var == "FEATURE_CONSTS":
return "".join(
f"{spc}pub const {feature}: u64 = {version};"
for (version, feature, _) in cuts
if feature is not None
)
elif var == "EXECUTOR_CUTS":
executor = (
"{spc}{version} => Arc::new({cut}::Executor::new(\n"
"{spc} protocol_config,\n"
"{spc} silent,\n"
"{spc} enable_profiler,\n"
"{spc})?),\n"
)
return "\n".join(
executor.format(spc=spc, version=feature or version, cut=cut)
for (version, feature, cut) in cuts
)
elif var == "VERIFIER_CUTS":
call = "Verifier::new(config, metrics)"
return "\n".join(
f"{spc}{feature or version} => Box::new({cut}::{call}),"
for (version, feature, cut) in cuts
)
else:
raise Exception(f"Don't know how to substitute {var}")
rust_code = re.sub(
r"^(\s*)// \$([A-Z_]+)$",
substitute,
template,
flags=re.MULTILINE,
)
try:
result = subprocess.run(['rustfmt'], input=rust_code, text=True, capture_output=True, check=True)
formatted_code = result.stdout
output_file.write(formatted_code)
except subprocess.CalledProcessError as e:
print(f"rustfmt failed with error code {e.returncode}")
print("stderr:", e.stderr)
except Exception as e:
print(f"An error occurred: {e}")
# Modules in `sui-execution` that don't count as "cuts" (they are
# other supporting modules)
NOT_A_CUT = {
"executor",
"lib",
"lib.template",
"tests",
"verifier",
}
def discover_cuts():
"""Find all modules corresponding to execution layer cuts.
Finds all modules within the `sui-execution` crate that count as
entry points for an execution layer cut. Returns a list of
3-tuples, where:
- The 0th element is a string representing the version number.
- The 1st element is an (optional) constant name for the version,
used to easily export the versions for features.
- The 2nd element is the name of the module.
Snapshot cuts (with names following the pattern /latest|v[0-9]+/)
are assigned version numbers according to their name (with latest
getting the version one higher than the highest occupied snapshot
version).
Feature cuts (all other cuts) are assigned versions starting with
`u64::MAX` and counting down, ordering features first by commit
time, and then by name.
"""
snapshots = []
features = []
src = Path() / "sui-execution" / "src"
for f in src.iterdir():
if not f.is_file() or f.stem in NOT_A_CUT:
continue
elif re.match(r"latest|v[0-9]+", f.stem):
snapshots.append(f)
else:
features.append(f)
def snapshot_key(path):
if path.stem == "latest":
return float("inf")
else:
return int(path.stem[1:])
def feature_key(path):
return path.stem
snapshots.sort(key=snapshot_key)
features.sort(key=feature_key)
cuts = []
for snapshot in snapshots:
mod = snapshot.stem
if mod != "latest":
cuts.append((mod[1:], None, mod))
continue
# Latest gets one higher version than any other snapshot
# version we've assigned so far
ver = 1 + max(int(v) for (v, _, _) in cuts)
cuts.append((str(ver), None, "latest"))
# "Feature" cuts are not intended to be used on production
# networks, so stability is not as important for them, they are
# assigned versions in lexicographical order.
for i, feature in enumerate(features):
version = f"u64::MAX - {i}" if i > 0 else "u64::MAX"
feature_stem = feature.stem.replace("-", "_")
cuts.append((version, feature_stem.upper(), feature_stem))
return cuts
if __name__ == "__main__":
for bin in ["git", "cargo"]:
if not which(bin):
print(f"Please install '{bin}'", file=stderr)
args = parse_args()
chdir(repo_root())
try:
args.do(args)
except Exception as e:
print(f"{args.cmd} failed! {e}", file=stderr)