Skip to content

feat(test-bins): show config options #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-bins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition.workspace = true
default-run = "rpc"

[dependencies]
clap = { version = "4.5.36", features = ["derive", "env"] }
console-subscriber = { workspace = true, optional = true }
env_logger = { workspace = true }
log = { workspace = true }
Expand Down
64 changes: 60 additions & 4 deletions test-bins/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::Parser;
use log::*;
use magicblock_api::{
ledger,
Expand All @@ -16,6 +17,44 @@ const TEST_KEYPAIR_BYTES: [u8; 64] = [
202, 240, 105, 168, 157, 64, 233, 249, 100, 104, 210, 41, 83, 87,
];

/// MagicBlock Validator CLI arguments
#[derive(Parser, Debug)]
#[command(name = "MagicBlock Validator")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(about = "Runs a MagicBlock validator node")]
struct Cli {
/// Path to the configuration file
config: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: config parameter needs a long flag (--config) and better help text for discoverability

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clap shall support PathBuf. Also I would propose to consider config_path.


/// Base58 encoded validator private key
#[arg(
short = 'k',
long,
value_name = "KEYPAIR",
env = "VALIDATOR_KEYPAIR",
help = "Base58 encoded private key for the validator."
)]
keypair: Option<String>,

/// Disable geyser components (accounts,transactions)
#[arg(
long,
value_name = "COMPONENTS",
env = "GEYSER_DISABLE",
help = "Specifies geyser components to disable. [default: (accounts,transactions)]"
)]
disable_geyser: Option<String>,

/// Disable geyser cache components (accounts,transactions)
#[arg(
long,
value_name = "COMPONENTS",
env = "GEYSER_CACHE_DISABLE",
help = "Specifies geyser cache components to disable. [default: (accounts,transactions)]"
)]
disable_geyser_cache: Option<String>,
}

fn init_logger() {
if let Ok(style) = std::env::var("RUST_LOG_STYLE") {
use std::io::Write;
Expand Down Expand Up @@ -60,8 +99,24 @@ async fn main() {
#[cfg(feature = "tokio-console")]
console_subscriber::init();

let (file, config) = load_config_from_arg();
let cli = Cli::parse();

// Set environment variables from CLI arguments
if let Some(keypair) = cli.keypair {
std::env::set_var("VALIDATOR_KEYPAIR", keypair);
}

if let Some(disable_geyser) = cli.disable_geyser {
std::env::set_var("GEYSER_DISABLE", disable_geyser);
}

if let Some(disable_cache) = cli.disable_geyser_cache {
std::env::set_var("GEYSER_CACHE_DISABLE", disable_cache);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: setting env vars after parsing them from CLI args could cause issues with override_from_envs() later picking up these modified values unexpectedly


let (file, config) = load_config(cli.config);
let config = config.override_from_envs();

match file {
Some(file) => info!("Loading config from '{}'.", file),
None => info!("Using default config. Override it by passing the path to a config file."),
Expand Down Expand Up @@ -112,15 +167,16 @@ fn validator_keypair() -> Keypair {
if let Ok(keypair) = std::env::var("VALIDATOR_KEYPAIR") {
Keypair::from_base58_string(&keypair)
} else {
Comment on lines 130 to 131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: from_base58_string() can panic - needs error handling for invalid base58 strings

Suggested change
Keypair::from_base58_string(&keypair)
} else {
Keypair::from_base58_string(&keypair).unwrap_or_else(|_| {
panic!("Invalid base58 string provided for validator keypair");
})
} else {

warn!("Using default test keypair, provide one by setting 'VALIDATOR_KEYPAIR' env var to a base58 encoded private key");
warn!("Using default test keypair, provide one by setting the --keypair argument or the 'VALIDATOR_KEYPAIR' env var to a base58 encoded private key");
Keypair::from_bytes(&TEST_KEYPAIR_BYTES)
// SAFETY: these bytes are compiled into the code, thus we know it is valid
.unwrap()
}
}

fn load_config_from_arg() -> (Option<String>, EphemeralConfig) {
let config_file = std::env::args().nth(1);
fn load_config(
config_file: Option<String>,
) -> (Option<String>, EphemeralConfig) {
match config_file {
Some(config_file) => {
let config = EphemeralConfig::try_load_from_file(&config_file)
Expand Down