From 5a5b7f39c1d6b46f98d15d5cec017c6c178efcaa Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 15 Oct 2020 21:43:18 -0600 Subject: [PATCH] CLI: Put `deploy` ephemeral keypair behind a flag --- cli/src/cli.rs | 36 +++++++++++++++++++++++++++++------- cli/tests/deploy.rs | 2 ++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 911b6f5515..b0b80ac15d 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -177,6 +177,7 @@ pub enum CliCommand { program_location: String, address: Option, use_deprecated_loader: bool, + random_address: bool, }, // Stake Commands CreateStakeAccount { @@ -609,12 +610,14 @@ pub fn parse_command( 1 }); let use_deprecated_loader = matches.is_present("use_deprecated_loader"); + let random_address = matches.is_present("random_address"); Ok(CliCommandInfo { command: CliCommand::Deploy { program_location: matches.value_of("program_location").unwrap().to_string(), address, use_deprecated_loader, + random_address, }, signers, }) @@ -1131,12 +1134,16 @@ fn process_deploy( program_location: &str, address: Option, use_deprecated_loader: bool, + random_address: bool, ) -> ProcessResult { let new_keypair = Keypair::new(); // Create ephemeral keypair to use for program address, if not provided let program_id = if let Some(i) = address { config.signers[i] - } else { + } else if random_address { &new_keypair + } else { + // Clap will enforce one of the previous two conditions + unreachable!(); }; let mut file = File::open(program_location).map_err(|err| { CliError::DynamicProgramError(format!("Unable to open program file: {}", err)) @@ -1570,12 +1577,14 @@ pub fn process_command(config: &CliConfig) -> ProcessResult { program_location, address, use_deprecated_loader, + random_address, } => process_deploy( &rpc_client, config, program_location, *address, *use_deprecated_loader, + *random_address, ), // Stake Commands @@ -2192,10 +2201,17 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, ' .arg( Arg::with_name("address_signer") .index(2) - .value_name("SIGNER_KEYPAIR") + .value_name("ADDRESS_KEYPAIR") .takes_value(true) .validator(is_valid_signer) - .help("The signer for the desired address of the program [default: new random address]") + .required_unless("random_address") + .help("The signer for the desired program address. See also: --random-address") + ) + .arg( + Arg::with_name("random_address") + .long("random-address") + .takes_value(false) + .help("Deploy at a random address. WARNING: Deployment cannot be retried!") ) .arg( Arg::with_name("use_deprecated_loader") @@ -2558,10 +2574,12 @@ mod tests { ); // Test Deploy Subcommand - let test_deploy = - test_commands - .clone() - .get_matches_from(vec!["test", "deploy", "/Users/test/program.o"]); + let test_deploy = test_commands.clone().get_matches_from(vec![ + "test", + "deploy", + "/Users/test/program.o", + "--random-address", + ]); assert_eq!( parse_command(&test_deploy, &default_signer, &mut None).unwrap(), CliCommandInfo { @@ -2569,6 +2587,7 @@ mod tests { program_location: "/Users/test/program.o".to_string(), address: None, use_deprecated_loader: false, + random_address: true, }, signers: vec![read_keypair_file(&keypair_file).unwrap().into()], } @@ -2590,6 +2609,7 @@ mod tests { program_location: "/Users/test/program.o".to_string(), address: Some(1), use_deprecated_loader: false, + random_address: false, }, signers: vec![ read_keypair_file(&keypair_file).unwrap().into(), @@ -2903,6 +2923,7 @@ mod tests { program_location: pathbuf.to_str().unwrap().to_string(), address: None, use_deprecated_loader: false, + random_address: true, }; let result = process_command(&config); let json: Value = serde_json::from_str(&result.unwrap()).unwrap(); @@ -2921,6 +2942,7 @@ mod tests { program_location: "bad/file/location.so".to_string(), address: None, use_deprecated_loader: false, + random_address: true, }; assert!(process_command(&config).is_err()); } diff --git a/cli/tests/deploy.rs b/cli/tests/deploy.rs index 3161e17f8d..bb5e578e14 100644 --- a/cli/tests/deploy.rs +++ b/cli/tests/deploy.rs @@ -64,6 +64,7 @@ fn test_cli_deploy_program() { program_location: pathbuf.to_str().unwrap().to_string(), address: None, use_deprecated_loader: false, + random_address: true, }; let response = process_command(&config); @@ -98,6 +99,7 @@ fn test_cli_deploy_program() { program_location: pathbuf.to_str().unwrap().to_string(), address: Some(1), use_deprecated_loader: false, + random_address: false, }; process_command(&config).unwrap(); let account1 = rpc_client