solana-cli: selectively require keypair (#6477)
* Make parse_command consistent * Strip pubkey out of parse_stake_create_account * Move validator-info args into module * Strip pubkey out of parse_validator_info_command * Strip pubkey out of parse_vote_create_account * Strip pubkey out of balance parsing * Strip pubkey out of parse pay * Only verify keypair existence if command requires it * Use struct instead of tuple
This commit is contained in:
177
cli/src/vote.rs
177
cli/src/vote.rs
@@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
cli::{
|
||||
build_balance_message, check_account_for_fee, check_unique_pubkeys,
|
||||
log_instruction_custom_error, CliCommand, CliConfig, CliError, ProcessResult,
|
||||
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
||||
ProcessResult,
|
||||
},
|
||||
input_parsers::*,
|
||||
input_validators::*,
|
||||
@@ -159,51 +160,57 @@ impl VoteSubCommands for App<'_, '_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_vote_create_account(
|
||||
pubkey: &Pubkey,
|
||||
matches: &ArgMatches<'_>,
|
||||
) -> Result<CliCommand, CliError> {
|
||||
pub fn parse_vote_create_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
|
||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||
let node_pubkey = pubkey_of(matches, "node_pubkey").unwrap();
|
||||
let commission = value_of(&matches, "commission").unwrap_or(0);
|
||||
let authorized_voter = pubkey_of(matches, "authorized_voter").unwrap_or(vote_account_pubkey);
|
||||
let authorized_withdrawer = pubkey_of(matches, "authorized_withdrawer").unwrap_or(*pubkey);
|
||||
let authorized_voter = pubkey_of(matches, "authorized_voter");
|
||||
let authorized_withdrawer = pubkey_of(matches, "authorized_withdrawer");
|
||||
|
||||
Ok(CliCommand::CreateVoteAccount(
|
||||
vote_account_pubkey,
|
||||
VoteInit {
|
||||
Ok(CliCommandInfo {
|
||||
command: CliCommand::CreateVoteAccount {
|
||||
vote_account_pubkey,
|
||||
node_pubkey,
|
||||
authorized_voter,
|
||||
authorized_withdrawer,
|
||||
commission,
|
||||
},
|
||||
))
|
||||
require_keypair: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_vote_authorize(
|
||||
matches: &ArgMatches<'_>,
|
||||
vote_authorize: VoteAuthorize,
|
||||
) -> Result<CliCommand, CliError> {
|
||||
) -> Result<CliCommandInfo, CliError> {
|
||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||
let new_authorized_pubkey = pubkey_of(matches, "new_authorized_pubkey").unwrap();
|
||||
|
||||
Ok(CliCommand::VoteAuthorize(
|
||||
vote_account_pubkey,
|
||||
new_authorized_pubkey,
|
||||
vote_authorize,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn parse_vote_get_account_command(matches: &ArgMatches<'_>) -> Result<CliCommand, CliError> {
|
||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||
let use_lamports_unit = matches.is_present("lamports");
|
||||
Ok(CliCommand::ShowVoteAccount {
|
||||
pubkey: vote_account_pubkey,
|
||||
use_lamports_unit,
|
||||
Ok(CliCommandInfo {
|
||||
command: CliCommand::VoteAuthorize(
|
||||
vote_account_pubkey,
|
||||
new_authorized_pubkey,
|
||||
vote_authorize,
|
||||
),
|
||||
require_keypair: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_vote_uptime_command(matches: &ArgMatches<'_>) -> Result<CliCommand, CliError> {
|
||||
pub fn parse_vote_get_account_command(
|
||||
matches: &ArgMatches<'_>,
|
||||
) -> Result<CliCommandInfo, CliError> {
|
||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||
let use_lamports_unit = matches.is_present("lamports");
|
||||
Ok(CliCommandInfo {
|
||||
command: CliCommand::ShowVoteAccount {
|
||||
pubkey: vote_account_pubkey,
|
||||
use_lamports_unit,
|
||||
},
|
||||
require_keypair: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_vote_uptime_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
|
||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||
let aggregate = matches.is_present("aggregate");
|
||||
let span = if matches.is_present("span") {
|
||||
@@ -211,10 +218,13 @@ pub fn parse_vote_uptime_command(matches: &ArgMatches<'_>) -> Result<CliCommand,
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(CliCommand::Uptime {
|
||||
pubkey: vote_account_pubkey,
|
||||
aggregate,
|
||||
span,
|
||||
Ok(CliCommandInfo {
|
||||
command: CliCommand::Uptime {
|
||||
pubkey: vote_account_pubkey,
|
||||
aggregate,
|
||||
span,
|
||||
},
|
||||
require_keypair: false,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -222,11 +232,14 @@ pub fn process_create_vote_account(
|
||||
rpc_client: &RpcClient,
|
||||
config: &CliConfig,
|
||||
vote_account_pubkey: &Pubkey,
|
||||
vote_init: &VoteInit,
|
||||
node_pubkey: &Pubkey,
|
||||
authorized_voter: &Option<Pubkey>,
|
||||
authorized_withdrawer: &Option<Pubkey>,
|
||||
commission: u8,
|
||||
) -> ProcessResult {
|
||||
check_unique_pubkeys(
|
||||
(vote_account_pubkey, "vote_account_pubkey".to_string()),
|
||||
(&vote_init.node_pubkey, "node_pubkey".to_string()),
|
||||
(&node_pubkey, "node_pubkey".to_string()),
|
||||
)?;
|
||||
check_unique_pubkeys(
|
||||
(&config.keypair.pubkey(), "cli keypair".to_string()),
|
||||
@@ -239,10 +252,16 @@ pub fn process_create_vote_account(
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let vote_init = VoteInit {
|
||||
node_pubkey: *node_pubkey,
|
||||
authorized_voter: authorized_voter.unwrap_or(*vote_account_pubkey),
|
||||
authorized_withdrawer: authorized_withdrawer.unwrap_or(config.keypair.pubkey()),
|
||||
commission,
|
||||
};
|
||||
let ixs = vote_instruction::create_account(
|
||||
&config.keypair.pubkey(),
|
||||
vote_account_pubkey,
|
||||
vote_init,
|
||||
&vote_init,
|
||||
lamports,
|
||||
);
|
||||
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
||||
@@ -441,8 +460,11 @@ mod tests {
|
||||
&pubkey_string,
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_authorize_voter).unwrap(),
|
||||
CliCommand::VoteAuthorize(pubkey, pubkey, VoteAuthorize::Voter)
|
||||
parse_command(&test_authorize_voter).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::VoteAuthorize(pubkey, pubkey, VoteAuthorize::Voter),
|
||||
require_keypair: true
|
||||
}
|
||||
);
|
||||
|
||||
// Test CreateVoteAccount SubCommand
|
||||
@@ -457,16 +479,17 @@ mod tests {
|
||||
"10",
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account).unwrap(),
|
||||
CliCommand::CreateVoteAccount(
|
||||
pubkey,
|
||||
VoteInit {
|
||||
parse_command(&test_create_vote_account).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::CreateVoteAccount {
|
||||
vote_account_pubkey: pubkey,
|
||||
node_pubkey,
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: pubkey,
|
||||
commission: 10
|
||||
}
|
||||
)
|
||||
authorized_voter: None,
|
||||
authorized_withdrawer: None,
|
||||
commission: 10,
|
||||
},
|
||||
require_keypair: true
|
||||
}
|
||||
);
|
||||
let test_create_vote_account2 = test_commands.clone().get_matches_from(vec![
|
||||
"test",
|
||||
@@ -475,16 +498,17 @@ mod tests {
|
||||
&node_pubkey_string,
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account2).unwrap(),
|
||||
CliCommand::CreateVoteAccount(
|
||||
pubkey,
|
||||
VoteInit {
|
||||
parse_command(&test_create_vote_account2).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::CreateVoteAccount {
|
||||
vote_account_pubkey: pubkey,
|
||||
node_pubkey,
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: pubkey,
|
||||
commission: 0
|
||||
}
|
||||
)
|
||||
authorized_voter: None,
|
||||
authorized_withdrawer: None,
|
||||
commission: 0,
|
||||
},
|
||||
require_keypair: true
|
||||
}
|
||||
);
|
||||
// test init with an authed voter
|
||||
let authed = Pubkey::new_rand();
|
||||
@@ -497,16 +521,17 @@ mod tests {
|
||||
&authed.to_string(),
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account3).unwrap(),
|
||||
CliCommand::CreateVoteAccount(
|
||||
pubkey,
|
||||
VoteInit {
|
||||
parse_command(&test_create_vote_account3).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::CreateVoteAccount {
|
||||
vote_account_pubkey: pubkey,
|
||||
node_pubkey,
|
||||
authorized_voter: authed,
|
||||
authorized_withdrawer: pubkey,
|
||||
authorized_voter: Some(authed),
|
||||
authorized_withdrawer: None,
|
||||
commission: 0
|
||||
}
|
||||
)
|
||||
},
|
||||
require_keypair: true
|
||||
}
|
||||
);
|
||||
// test init with an authed withdrawer
|
||||
let test_create_vote_account4 = test_commands.clone().get_matches_from(vec![
|
||||
@@ -518,16 +543,17 @@ mod tests {
|
||||
&authed.to_string(),
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account4).unwrap(),
|
||||
CliCommand::CreateVoteAccount(
|
||||
pubkey,
|
||||
VoteInit {
|
||||
parse_command(&test_create_vote_account4).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::CreateVoteAccount {
|
||||
vote_account_pubkey: pubkey,
|
||||
node_pubkey,
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: authed,
|
||||
authorized_voter: None,
|
||||
authorized_withdrawer: Some(authed),
|
||||
commission: 0
|
||||
}
|
||||
)
|
||||
},
|
||||
require_keypair: true
|
||||
}
|
||||
);
|
||||
|
||||
// Test Uptime Subcommand
|
||||
@@ -541,11 +567,14 @@ mod tests {
|
||||
"--aggregate",
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &matches).unwrap(),
|
||||
CliCommand::Uptime {
|
||||
pubkey,
|
||||
aggregate: true,
|
||||
span: Some(4)
|
||||
parse_command(&matches).unwrap(),
|
||||
CliCommandInfo {
|
||||
command: CliCommand::Uptime {
|
||||
pubkey,
|
||||
aggregate: true,
|
||||
span: Some(4)
|
||||
},
|
||||
require_keypair: false
|
||||
}
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user