From c2806aa2f92c52b2ef7f6f732411db09c65bf1a1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 16 Oct 2020 06:09:40 +0000 Subject: [PATCH] CLI: Put `deploy` ephemeral keypair behind a flag (#12941) (cherry picked from commit 5a5b7f39c1d6b46f98d15d5cec017c6c178efcaa) Co-authored-by: Trent Nelson --- 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 5baeed55e3..dfc6a64732 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -193,6 +193,7 @@ pub enum CliCommand { program_location: String, address: Option, use_deprecated_loader: bool, + random_address: bool, }, // Stake Commands CreateStakeAccount { @@ -629,12 +630,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, }) @@ -1239,12 +1242,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)) @@ -1941,12 +1948,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 @@ -2610,10 +2619,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") @@ -3070,10 +3086,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 { @@ -3081,6 +3099,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()], } @@ -3102,6 +3121,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(), @@ -3830,6 +3850,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(); @@ -3848,6 +3869,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