|
| 1 | +use crate::builders::BuilderMode; |
| 2 | +use clap::Parser; |
| 3 | +pub use op::OpRbuilderArgs; |
| 4 | +use playground::PlaygroundOptions; |
| 5 | +use reth_optimism_cli::{chainspec::OpChainSpecParser, commands::Commands}; |
| 6 | + |
1 | 7 | mod op; |
2 | 8 | mod playground; |
3 | 9 |
|
4 | | -pub use op::OpRbuilderArgs; |
5 | | -pub use playground::CliExt; |
| 10 | +/// This trait is used to extend Reth's CLI with additional functionality that |
| 11 | +/// are specific to the OP builder, such as populating default values for CLI arguments |
| 12 | +/// when running in the playground mode or checking the builder mode. |
| 13 | +/// |
| 14 | +pub trait CliExt { |
| 15 | + /// Populates the default values for the CLI arguments when the user specifies |
| 16 | + /// the `--builder.playground` flag. |
| 17 | + fn populate_defaults(self) -> Self; |
| 18 | + |
| 19 | + /// Returns the builder mode that the node is started with. |
| 20 | + fn builder_mode(&self) -> BuilderMode; |
| 21 | + |
| 22 | + /// Returns the Cli instance with the parsed command line arguments |
| 23 | + /// and defaults populated if applicable. |
| 24 | + fn parsed() -> Self; |
| 25 | +} |
| 26 | + |
| 27 | +pub type Cli = reth_optimism_cli::Cli<OpChainSpecParser, OpRbuilderArgs>; |
| 28 | + |
| 29 | +impl CliExt for Cli { |
| 30 | + /// Checks if the node is started with the `--builder.playground` flag, |
| 31 | + /// and if so, populates the default values for the CLI arguments from the |
| 32 | + /// playground configuration. |
| 33 | + /// |
| 34 | + /// The `--builder.playground` flag is used to populate the CLI arguments with |
| 35 | + /// default values for running the builder against the playground environment. |
| 36 | + /// |
| 37 | + /// The values are populated from the default directory of the playground |
| 38 | + /// configuration, which is `$HOME/.playground/devnet/` by default. |
| 39 | + /// |
| 40 | + /// Any manually specified CLI arguments by the user will override the defaults. |
| 41 | + fn populate_defaults(self) -> Self { |
| 42 | + let Commands::Node(ref node_command) = self.command else { |
| 43 | + // playground defaults are only relevant if running the node commands. |
| 44 | + return self; |
| 45 | + }; |
| 46 | + |
| 47 | + let Some(ref playground_dir) = node_command.ext.playground else { |
| 48 | + // not running in playground mode. |
| 49 | + return self; |
| 50 | + }; |
| 51 | + |
| 52 | + let options = match PlaygroundOptions::new(playground_dir) { |
| 53 | + Ok(options) => options, |
| 54 | + Err(e) => exit(e), |
| 55 | + }; |
| 56 | + |
| 57 | + options.apply(self) |
| 58 | + } |
| 59 | + |
| 60 | + fn parsed() -> Self { |
| 61 | + Cli::parse().populate_defaults() |
| 62 | + } |
| 63 | + |
| 64 | + /// Returns the type of builder implementation that the node is started with. |
| 65 | + /// Currently supports `Standard` and `Flashblocks` modes. |
| 66 | + fn builder_mode(&self) -> BuilderMode { |
| 67 | + if let Commands::Node(ref node_command) = self.command { |
| 68 | + if node_command.ext.enable_flashblocks { |
| 69 | + return BuilderMode::Flashblocks; |
| 70 | + } |
| 71 | + } |
| 72 | + BuilderMode::Standard |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Following clap's convention, a failure to parse the command line arguments |
| 77 | +/// will result in terminating the program with a non-zero exit code. |
| 78 | +fn exit(error: eyre::Report) -> ! { |
| 79 | + eprintln!("{error}"); |
| 80 | + std::process::exit(-1); |
| 81 | +} |
0 commit comments