cli: enforce rent-exemption balance for stake, vote and program accounts in cli (#6118)
* require minimum balance for stake, vote and program accounts
This commit is contained in:
@@ -367,6 +367,16 @@ pub fn process_create_stake_account(
|
||||
.into());
|
||||
}
|
||||
|
||||
let minimum_balance =
|
||||
rpc_client.get_minimum_balance_for_rent_exemption(std::mem::size_of::<StakeState>())?;
|
||||
|
||||
if lamports < minimum_balance {
|
||||
Err(WalletError::BadParameter(format!(
|
||||
"need atleast {} lamports for stake account to be rent exempt, provided lamports: {}",
|
||||
minimum_balance, lamports
|
||||
)))?;
|
||||
}
|
||||
|
||||
let ixs = stake_instruction::create_stake_account_with_lockup(
|
||||
&config.keypair.pubkey(),
|
||||
stake_account_pubkey,
|
||||
|
@@ -26,8 +26,6 @@ pub fn parse_vote_create_account(
|
||||
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 lamports = amount_of(matches, "amount", "unit").expect("Invalid amount");
|
||||
|
||||
Ok(WalletCommand::CreateVoteAccount(
|
||||
vote_account_pubkey,
|
||||
VoteInit {
|
||||
@@ -36,7 +34,6 @@ pub fn parse_vote_create_account(
|
||||
authorized_withdrawer,
|
||||
commission,
|
||||
},
|
||||
lamports,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -70,7 +67,6 @@ pub fn process_create_vote_account(
|
||||
config: &WalletConfig,
|
||||
vote_account_pubkey: &Pubkey,
|
||||
vote_init: &VoteInit,
|
||||
lamports: u64,
|
||||
) -> ProcessResult {
|
||||
check_unique_pubkeys(
|
||||
(vote_account_pubkey, "vote_account_pubkey".to_string()),
|
||||
@@ -80,11 +76,13 @@ pub fn process_create_vote_account(
|
||||
(&config.keypair.pubkey(), "wallet keypair".to_string()),
|
||||
(vote_account_pubkey, "vote_account_pubkey".to_string()),
|
||||
)?;
|
||||
let required_balance =
|
||||
rpc_client.get_minimum_balance_for_rent_exemption(VoteState::size_of())?;
|
||||
let ixs = vote_instruction::create_account(
|
||||
&config.keypair.pubkey(),
|
||||
vote_account_pubkey,
|
||||
vote_init,
|
||||
lamports,
|
||||
required_balance,
|
||||
);
|
||||
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
||||
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, recent_blockhash);
|
||||
@@ -303,10 +301,8 @@ mod tests {
|
||||
"create-vote-account",
|
||||
&pubkey_string,
|
||||
&node_pubkey_string,
|
||||
"50",
|
||||
"--commission",
|
||||
"10",
|
||||
"lamports",
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account).unwrap(),
|
||||
@@ -317,8 +313,7 @@ mod tests {
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: pubkey,
|
||||
commission: 10
|
||||
},
|
||||
50
|
||||
}
|
||||
)
|
||||
);
|
||||
let test_create_vote_account2 = test_commands.clone().get_matches_from(vec![
|
||||
@@ -326,7 +321,6 @@ mod tests {
|
||||
"create-vote-account",
|
||||
&pubkey_string,
|
||||
&node_pubkey_string,
|
||||
"50",
|
||||
]);
|
||||
assert_eq!(
|
||||
parse_command(&pubkey, &test_create_vote_account2).unwrap(),
|
||||
@@ -337,8 +331,7 @@ mod tests {
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: pubkey,
|
||||
commission: 0
|
||||
},
|
||||
858993459200
|
||||
}
|
||||
)
|
||||
);
|
||||
// test init with an authed voter
|
||||
@@ -348,8 +341,6 @@ mod tests {
|
||||
"create-vote-account",
|
||||
&pubkey_string,
|
||||
&node_pubkey_string,
|
||||
"50",
|
||||
"SOL",
|
||||
"--authorized-voter",
|
||||
&authed.to_string(),
|
||||
]);
|
||||
@@ -362,8 +353,7 @@ mod tests {
|
||||
authorized_voter: authed,
|
||||
authorized_withdrawer: pubkey,
|
||||
commission: 0
|
||||
},
|
||||
858993459200
|
||||
}
|
||||
)
|
||||
);
|
||||
// test init with an authed withdrawer
|
||||
@@ -372,7 +362,6 @@ mod tests {
|
||||
"create-vote-account",
|
||||
&pubkey_string,
|
||||
&node_pubkey_string,
|
||||
"0.5",
|
||||
"--authorized-withdrawer",
|
||||
&authed.to_string(),
|
||||
]);
|
||||
@@ -385,8 +374,7 @@ mod tests {
|
||||
authorized_voter: pubkey,
|
||||
authorized_withdrawer: authed,
|
||||
commission: 0
|
||||
},
|
||||
8589934592
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
@@ -65,7 +65,7 @@ pub enum WalletCommand {
|
||||
Cancel(Pubkey),
|
||||
Confirm(Signature),
|
||||
VoteAuthorize(Pubkey, Pubkey, VoteAuthorize),
|
||||
CreateVoteAccount(Pubkey, VoteInit, u64),
|
||||
CreateVoteAccount(Pubkey, VoteInit),
|
||||
ShowAccount {
|
||||
pubkey: Pubkey,
|
||||
output_file: Option<String>,
|
||||
@@ -655,11 +655,12 @@ fn process_deploy(
|
||||
// Build transactions to calculate fees
|
||||
let mut messages: Vec<&Message> = Vec::new();
|
||||
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
||||
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(program_data.len())?;
|
||||
let mut create_account_tx = system_transaction::create_account(
|
||||
&config.keypair,
|
||||
&program_id.pubkey(),
|
||||
blockhash,
|
||||
1,
|
||||
minimum_balance,
|
||||
program_data.len() as u64,
|
||||
&bpf_loader::id(),
|
||||
);
|
||||
@@ -1087,14 +1088,8 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
|
||||
WalletCommand::Confirm(signature) => process_confirm(&rpc_client, signature),
|
||||
|
||||
// Create vote account
|
||||
WalletCommand::CreateVoteAccount(vote_account_pubkey, vote_init, lamports) => {
|
||||
process_create_vote_account(
|
||||
&rpc_client,
|
||||
config,
|
||||
&vote_account_pubkey,
|
||||
&vote_init,
|
||||
*lamports,
|
||||
)
|
||||
WalletCommand::CreateVoteAccount(vote_account_pubkey, vote_init) => {
|
||||
process_create_vote_account(&rpc_client, config, &vote_account_pubkey, &vote_init)
|
||||
}
|
||||
|
||||
WalletCommand::VoteAuthorize(
|
||||
@@ -1546,22 +1541,6 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
|
||||
.validator(is_pubkey_or_keypair)
|
||||
.help("Validator that will vote with this account"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("amount")
|
||||
.index(3)
|
||||
.value_name("AMOUNT")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("The amount of send to the vote account (default unit SOL)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("unit")
|
||||
.index(4)
|
||||
.value_name("UNIT")
|
||||
.takes_value(true)
|
||||
.possible_values(&["SOL", "lamports"])
|
||||
.help("Specify unit to use for request"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("commission")
|
||||
.long("commission")
|
||||
@@ -2324,7 +2303,6 @@ mod tests {
|
||||
authorized_withdrawer: bob_pubkey,
|
||||
commission: 0,
|
||||
},
|
||||
10,
|
||||
);
|
||||
let signature = process_command(&config);
|
||||
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
|
||||
@@ -2344,7 +2322,7 @@ mod tests {
|
||||
withdrawer: config.keypair.pubkey(),
|
||||
},
|
||||
Lockup { slot: 0, custodian },
|
||||
10,
|
||||
1234,
|
||||
);
|
||||
let signature = process_command(&config);
|
||||
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
|
||||
@@ -2493,7 +2471,6 @@ mod tests {
|
||||
authorized_withdrawer: bob_pubkey,
|
||||
commission: 0,
|
||||
},
|
||||
10,
|
||||
);
|
||||
assert!(process_command(&config).is_err());
|
||||
|
||||
|
Reference in New Issue
Block a user