CLI: Put `deploy` ephemeral keypair behind a flag

This commit is contained in:
Trent Nelson 2020-10-15 21:43:18 -06:00 committed by mergify[bot]
parent 359707c85e
commit 5a5b7f39c1
2 changed files with 31 additions and 7 deletions

View File

@ -177,6 +177,7 @@ pub enum CliCommand {
program_location: String, program_location: String,
address: Option<SignerIndex>, address: Option<SignerIndex>,
use_deprecated_loader: bool, use_deprecated_loader: bool,
random_address: bool,
}, },
// Stake Commands // Stake Commands
CreateStakeAccount { CreateStakeAccount {
@ -609,12 +610,14 @@ pub fn parse_command(
1 1
}); });
let use_deprecated_loader = matches.is_present("use_deprecated_loader"); let use_deprecated_loader = matches.is_present("use_deprecated_loader");
let random_address = matches.is_present("random_address");
Ok(CliCommandInfo { Ok(CliCommandInfo {
command: CliCommand::Deploy { command: CliCommand::Deploy {
program_location: matches.value_of("program_location").unwrap().to_string(), program_location: matches.value_of("program_location").unwrap().to_string(),
address, address,
use_deprecated_loader, use_deprecated_loader,
random_address,
}, },
signers, signers,
}) })
@ -1131,12 +1134,16 @@ fn process_deploy(
program_location: &str, program_location: &str,
address: Option<SignerIndex>, address: Option<SignerIndex>,
use_deprecated_loader: bool, use_deprecated_loader: bool,
random_address: bool,
) -> ProcessResult { ) -> ProcessResult {
let new_keypair = Keypair::new(); // Create ephemeral keypair to use for program address, if not provided let new_keypair = Keypair::new(); // Create ephemeral keypair to use for program address, if not provided
let program_id = if let Some(i) = address { let program_id = if let Some(i) = address {
config.signers[i] config.signers[i]
} else { } else if random_address {
&new_keypair &new_keypair
} else {
// Clap will enforce one of the previous two conditions
unreachable!();
}; };
let mut file = File::open(program_location).map_err(|err| { let mut file = File::open(program_location).map_err(|err| {
CliError::DynamicProgramError(format!("Unable to open program file: {}", err)) CliError::DynamicProgramError(format!("Unable to open program file: {}", err))
@ -1570,12 +1577,14 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
program_location, program_location,
address, address,
use_deprecated_loader, use_deprecated_loader,
random_address,
} => process_deploy( } => process_deploy(
&rpc_client, &rpc_client,
config, config,
program_location, program_location,
*address, *address,
*use_deprecated_loader, *use_deprecated_loader,
*random_address,
), ),
// Stake Commands // Stake Commands
@ -2192,10 +2201,17 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.arg( .arg(
Arg::with_name("address_signer") Arg::with_name("address_signer")
.index(2) .index(2)
.value_name("SIGNER_KEYPAIR") .value_name("ADDRESS_KEYPAIR")
.takes_value(true) .takes_value(true)
.validator(is_valid_signer) .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(
Arg::with_name("use_deprecated_loader") Arg::with_name("use_deprecated_loader")
@ -2558,10 +2574,12 @@ mod tests {
); );
// Test Deploy Subcommand // Test Deploy Subcommand
let test_deploy = let test_deploy = test_commands.clone().get_matches_from(vec![
test_commands "test",
.clone() "deploy",
.get_matches_from(vec!["test", "deploy", "/Users/test/program.o"]); "/Users/test/program.o",
"--random-address",
]);
assert_eq!( assert_eq!(
parse_command(&test_deploy, &default_signer, &mut None).unwrap(), parse_command(&test_deploy, &default_signer, &mut None).unwrap(),
CliCommandInfo { CliCommandInfo {
@ -2569,6 +2587,7 @@ mod tests {
program_location: "/Users/test/program.o".to_string(), program_location: "/Users/test/program.o".to_string(),
address: None, address: None,
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: true,
}, },
signers: vec![read_keypair_file(&keypair_file).unwrap().into()], signers: vec![read_keypair_file(&keypair_file).unwrap().into()],
} }
@ -2590,6 +2609,7 @@ mod tests {
program_location: "/Users/test/program.o".to_string(), program_location: "/Users/test/program.o".to_string(),
address: Some(1), address: Some(1),
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: false,
}, },
signers: vec![ signers: vec![
read_keypair_file(&keypair_file).unwrap().into(), read_keypair_file(&keypair_file).unwrap().into(),
@ -2903,6 +2923,7 @@ mod tests {
program_location: pathbuf.to_str().unwrap().to_string(), program_location: pathbuf.to_str().unwrap().to_string(),
address: None, address: None,
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: true,
}; };
let result = process_command(&config); let result = process_command(&config);
let json: Value = serde_json::from_str(&result.unwrap()).unwrap(); let json: Value = serde_json::from_str(&result.unwrap()).unwrap();
@ -2921,6 +2942,7 @@ mod tests {
program_location: "bad/file/location.so".to_string(), program_location: "bad/file/location.so".to_string(),
address: None, address: None,
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: true,
}; };
assert!(process_command(&config).is_err()); assert!(process_command(&config).is_err());
} }

View File

@ -64,6 +64,7 @@ fn test_cli_deploy_program() {
program_location: pathbuf.to_str().unwrap().to_string(), program_location: pathbuf.to_str().unwrap().to_string(),
address: None, address: None,
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: true,
}; };
let response = process_command(&config); let response = process_command(&config);
@ -98,6 +99,7 @@ fn test_cli_deploy_program() {
program_location: pathbuf.to_str().unwrap().to_string(), program_location: pathbuf.to_str().unwrap().to_string(),
address: Some(1), address: Some(1),
use_deprecated_loader: false, use_deprecated_loader: false,
random_address: false,
}; };
process_command(&config).unwrap(); process_command(&config).unwrap();
let account1 = rpc_client let account1 = rpc_client