Merge create-stake-account into delegate-stake (#5280)
This commit is contained in:
parent
19e4f70244
commit
d1eff5d607
|
@ -105,15 +105,7 @@ setup_validator_accounts() {
|
||||||
create-vote-account "$vote_pubkey" "$identity_pubkey" 1 --commission 127
|
create-vote-account "$vote_pubkey" "$identity_pubkey" 1 --commission 127
|
||||||
) || return $?
|
) || return $?
|
||||||
|
|
||||||
echo "Fund the stake account from the node's identity pubkey"
|
|
||||||
(
|
|
||||||
set -x
|
|
||||||
$solana_wallet --keypair "$identity_keypair_path" --url "http://$entrypoint_ip:8899" \
|
|
||||||
create-stake-account "$stake_pubkey" "$stake_lamports"
|
|
||||||
) || return $?
|
|
||||||
|
|
||||||
echo "Delegate the stake account to the node's vote account"
|
echo "Delegate the stake account to the node's vote account"
|
||||||
# transaction must be signed by the stake_keypair
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
$solana_wallet --keypair "$identity_keypair_path" --url "http://$entrypoint_ip:8899" \
|
$solana_wallet --keypair "$identity_keypair_path" --url "http://$entrypoint_ip:8899" \
|
||||||
|
|
|
@ -50,7 +50,6 @@ pub enum WalletCommand {
|
||||||
AuthorizeVoter(Pubkey, Keypair, Pubkey),
|
AuthorizeVoter(Pubkey, Keypair, Pubkey),
|
||||||
CreateVoteAccount(Pubkey, Pubkey, u8, u64),
|
CreateVoteAccount(Pubkey, Pubkey, u8, u64),
|
||||||
ShowVoteAccount(Pubkey),
|
ShowVoteAccount(Pubkey),
|
||||||
CreateStakeAccount(Pubkey, u64),
|
|
||||||
DelegateStake(Keypair, Pubkey, u64),
|
DelegateStake(Keypair, Pubkey, u64),
|
||||||
WithdrawStake(Keypair, Pubkey, u64),
|
WithdrawStake(Keypair, Pubkey, u64),
|
||||||
DeactivateStake(Keypair),
|
DeactivateStake(Keypair),
|
||||||
|
@ -230,23 +229,15 @@ pub fn parse_command(
|
||||||
let voting_account_pubkey = value_of(matches, "voting_account_pubkey").unwrap();
|
let voting_account_pubkey = value_of(matches, "voting_account_pubkey").unwrap();
|
||||||
Ok(WalletCommand::ShowVoteAccount(voting_account_pubkey))
|
Ok(WalletCommand::ShowVoteAccount(voting_account_pubkey))
|
||||||
}
|
}
|
||||||
("create-stake-account", Some(matches)) => {
|
|
||||||
let staking_account_pubkey = value_of(matches, "staking_account_pubkey").unwrap();
|
|
||||||
let lamports = matches.value_of("lamports").unwrap().parse()?;
|
|
||||||
Ok(WalletCommand::CreateStakeAccount(
|
|
||||||
staking_account_pubkey,
|
|
||||||
lamports,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
("delegate-stake", Some(matches)) => {
|
("delegate-stake", Some(matches)) => {
|
||||||
let staking_account_keypair =
|
let staking_account_keypair =
|
||||||
keypair_of(matches, "staking_account_keypair_file").unwrap();
|
keypair_of(matches, "staking_account_keypair_file").unwrap();
|
||||||
let voting_account_pubkey = value_of(matches, "voting_account_pubkey").unwrap();
|
let voting_account_pubkey = value_of(matches, "voting_account_pubkey").unwrap();
|
||||||
let stake = matches.value_of("stake").unwrap().parse()?;
|
let lamports_to_stake = matches.value_of("lamports_to_stake").unwrap().parse()?;
|
||||||
Ok(WalletCommand::DelegateStake(
|
Ok(WalletCommand::DelegateStake(
|
||||||
staking_account_keypair,
|
staking_account_keypair,
|
||||||
voting_account_pubkey,
|
voting_account_pubkey,
|
||||||
stake,
|
lamports_to_stake,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
("withdraw-stake", Some(matches)) => {
|
("withdraw-stake", Some(matches)) => {
|
||||||
|
@ -569,28 +560,6 @@ fn process_show_vote_account(
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_create_stake_account(
|
|
||||||
rpc_client: &RpcClient,
|
|
||||||
config: &WalletConfig,
|
|
||||||
staking_account_pubkey: &Pubkey,
|
|
||||||
lamports: u64,
|
|
||||||
) -> ProcessResult {
|
|
||||||
let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
|
|
||||||
let ixs = stake_instruction::create_stake_account(
|
|
||||||
&config.keypair.pubkey(),
|
|
||||||
staking_account_pubkey,
|
|
||||||
lamports,
|
|
||||||
);
|
|
||||||
let mut tx = Transaction::new_signed_with_payer(
|
|
||||||
ixs,
|
|
||||||
Some(&config.keypair.pubkey()),
|
|
||||||
&[&config.keypair],
|
|
||||||
recent_blockhash,
|
|
||||||
);
|
|
||||||
let signature_str = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair])?;
|
|
||||||
Ok(signature_str.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_deactivate_stake_account(
|
fn process_deactivate_stake_account(
|
||||||
rpc_client: &RpcClient,
|
rpc_client: &RpcClient,
|
||||||
config: &WalletConfig,
|
config: &WalletConfig,
|
||||||
|
@ -614,14 +583,16 @@ fn process_delegate_stake(
|
||||||
config: &WalletConfig,
|
config: &WalletConfig,
|
||||||
staking_account_keypair: &Keypair,
|
staking_account_keypair: &Keypair,
|
||||||
voting_account_pubkey: &Pubkey,
|
voting_account_pubkey: &Pubkey,
|
||||||
stake: u64,
|
lamports: u64,
|
||||||
) -> ProcessResult {
|
) -> ProcessResult {
|
||||||
let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
|
let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
|
||||||
let ixs = vec![stake_instruction::delegate_stake(
|
|
||||||
|
let ixs = stake_instruction::create_stake_account_and_delegate_stake(
|
||||||
|
&config.keypair.pubkey(),
|
||||||
&staking_account_keypair.pubkey(),
|
&staking_account_keypair.pubkey(),
|
||||||
voting_account_pubkey,
|
voting_account_pubkey,
|
||||||
stake,
|
lamports,
|
||||||
)];
|
);
|
||||||
|
|
||||||
let mut tx = Transaction::new_signed_with_payer(
|
let mut tx = Transaction::new_signed_with_payer(
|
||||||
ixs,
|
ixs,
|
||||||
|
@ -691,12 +662,19 @@ fn process_show_stake_account(
|
||||||
let stake_account = rpc_client.get_account(staking_account_pubkey)?;
|
let stake_account = rpc_client.get_account(staking_account_pubkey)?;
|
||||||
match stake_account.state() {
|
match stake_account.state() {
|
||||||
Ok(StakeState::Stake(stake)) => {
|
Ok(StakeState::Stake(stake)) => {
|
||||||
println!("account lamports: {}", stake_account.lamports);
|
println!("total stake: {}", stake_account.lamports);
|
||||||
println!("voter pubkey: {}", stake.voter_pubkey);
|
|
||||||
println!("credits observed: {}", stake.credits_observed);
|
println!("credits observed: {}", stake.credits_observed);
|
||||||
println!("stake: {}", stake.stake);
|
println!("delegated stake: {}", stake.stake);
|
||||||
|
if stake.voter_pubkey != Pubkey::default() {
|
||||||
|
println!("delegated voter pubkey: {}", stake.voter_pubkey);
|
||||||
|
}
|
||||||
|
println!("stake activates at epoch: {}", stake.activated);
|
||||||
|
if stake.deactivated < std::u64::MAX {
|
||||||
|
println!("stake deactivates at epoch: {}", stake.deactivated);
|
||||||
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
|
Ok(StakeState::Uninitialized) => Ok("Stake account is uninitialized".to_string()),
|
||||||
_ => Err(WalletError::RpcRequestError(
|
_ => Err(WalletError::RpcRequestError(
|
||||||
"Account data could not be deserialized to stake state".to_string(),
|
"Account data could not be deserialized to stake state".to_string(),
|
||||||
))?,
|
))?,
|
||||||
|
@ -1063,11 +1041,6 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
|
||||||
process_show_vote_account(&rpc_client, config, &voting_account_pubkey)
|
process_show_vote_account(&rpc_client, config, &voting_account_pubkey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create stake account
|
|
||||||
WalletCommand::CreateStakeAccount(staking_account_pubkey, lamports) => {
|
|
||||||
process_create_stake_account(&rpc_client, config, &staking_account_pubkey, *lamports)
|
|
||||||
}
|
|
||||||
|
|
||||||
WalletCommand::DelegateStake(staking_account_keypair, voting_account_pubkey, lamports) => {
|
WalletCommand::DelegateStake(staking_account_keypair, voting_account_pubkey, lamports) => {
|
||||||
process_delegate_stake(
|
process_delegate_stake(
|
||||||
&rpc_client,
|
&rpc_client,
|
||||||
|
@ -1412,37 +1385,16 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
|
||||||
.help("Vote account pubkey"),
|
.help("Vote account pubkey"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("create-stake-account")
|
|
||||||
.about("Create staking account")
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("staking_account_pubkey")
|
|
||||||
.index(1)
|
|
||||||
.value_name("PUBKEY")
|
|
||||||
.takes_value(true)
|
|
||||||
.required(true)
|
|
||||||
.validator(is_pubkey)
|
|
||||||
.help("Staking account address to fund"),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("lamports")
|
|
||||||
.index(2)
|
|
||||||
.value_name("NUM")
|
|
||||||
.takes_value(true)
|
|
||||||
.required(true)
|
|
||||||
.help("The number of lamports to send to staking account"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("delegate-stake")
|
SubCommand::with_name("delegate-stake")
|
||||||
.about("Delegate the stake to some vote account")
|
.about("Delegate stake to a vote account")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("staking_account_keypair_file")
|
Arg::with_name("staking_account_keypair_file")
|
||||||
.index(1)
|
.index(1)
|
||||||
.value_name("KEYPAIR_FILE")
|
.value_name("KEYPAIR_FILE")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(true)
|
.required(true)
|
||||||
.help("Keypair file for the staking account, for signing the delegate transaction."),
|
.help("Keypair file for the new stake account"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("voting_account_pubkey")
|
Arg::with_name("voting_account_pubkey")
|
||||||
|
@ -1454,12 +1406,12 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
|
||||||
.help("The voting account to which the stake will be delegated"),
|
.help("The voting account to which the stake will be delegated"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("stake")
|
Arg::with_name("lamports_to_stake")
|
||||||
.index(3)
|
.index(3)
|
||||||
.value_name("NUM")
|
.value_name("NUM")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(true)
|
.required(true)
|
||||||
.help("The number of lamports to stake, must be less than the stake account's balance."),
|
.help("The number of lamports to stake"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
|
@ -1888,18 +1840,7 @@ mod tests {
|
||||||
WalletCommand::CreateVoteAccount(pubkey, node_pubkey, 0, 50)
|
WalletCommand::CreateVoteAccount(pubkey, node_pubkey, 0, 50)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test Create Stake Account
|
// Test DelegateStake Subcommand
|
||||||
let test_create_stake_account = test_commands.clone().get_matches_from(vec![
|
|
||||||
"test",
|
|
||||||
"create-stake-account",
|
|
||||||
&pubkey_string,
|
|
||||||
"50",
|
|
||||||
]);
|
|
||||||
assert_eq!(
|
|
||||||
parse_command(&pubkey, &test_create_stake_account).unwrap(),
|
|
||||||
WalletCommand::CreateStakeAccount(pubkey, 50)
|
|
||||||
);
|
|
||||||
|
|
||||||
fn make_tmp_path(name: &str) -> String {
|
fn make_tmp_path(name: &str) -> String {
|
||||||
let out_dir = std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
|
let out_dir = std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
|
||||||
let keypair = Keypair::new();
|
let keypair = Keypair::new();
|
||||||
|
@ -1917,7 +1858,7 @@ mod tests {
|
||||||
let keypair_file = make_tmp_path("keypair_file");
|
let keypair_file = make_tmp_path("keypair_file");
|
||||||
gen_keypair_file(&keypair_file).unwrap();
|
gen_keypair_file(&keypair_file).unwrap();
|
||||||
let keypair = read_keypair(&keypair_file).unwrap();
|
let keypair = read_keypair(&keypair_file).unwrap();
|
||||||
// Test Delegate Stake Subcommand
|
|
||||||
let test_delegate_stake = test_commands.clone().get_matches_from(vec![
|
let test_delegate_stake = test_commands.clone().get_matches_from(vec![
|
||||||
"test",
|
"test",
|
||||||
"delegate-stake",
|
"delegate-stake",
|
||||||
|
@ -1930,10 +1871,7 @@ mod tests {
|
||||||
WalletCommand::DelegateStake(keypair, pubkey, 42)
|
WalletCommand::DelegateStake(keypair, pubkey, 42)
|
||||||
);
|
);
|
||||||
|
|
||||||
let keypair_file = make_tmp_path("keypair_file");
|
// Test WithdrawStake Subcommand
|
||||||
gen_keypair_file(&keypair_file).unwrap();
|
|
||||||
let keypair = read_keypair(&keypair_file).unwrap();
|
|
||||||
// Test Withdraw from Stake Account
|
|
||||||
let test_withdraw_stake = test_commands.clone().get_matches_from(vec![
|
let test_withdraw_stake = test_commands.clone().get_matches_from(vec![
|
||||||
"test",
|
"test",
|
||||||
"withdraw-stake",
|
"withdraw-stake",
|
||||||
|
@ -1941,6 +1879,7 @@ mod tests {
|
||||||
&pubkey_string,
|
&pubkey_string,
|
||||||
"42",
|
"42",
|
||||||
]);
|
]);
|
||||||
|
let keypair = read_keypair(&keypair_file).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_command(&pubkey, &test_withdraw_stake).unwrap(),
|
parse_command(&pubkey, &test_withdraw_stake).unwrap(),
|
||||||
WalletCommand::WithdrawStake(keypair, pubkey, 42)
|
WalletCommand::WithdrawStake(keypair, pubkey, 42)
|
||||||
|
@ -2118,10 +2057,6 @@ mod tests {
|
||||||
let signature = process_command(&config);
|
let signature = process_command(&config);
|
||||||
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
|
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
|
||||||
|
|
||||||
config.command = WalletCommand::CreateStakeAccount(bob_pubkey, 10);
|
|
||||||
let signature = process_command(&config);
|
|
||||||
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
|
|
||||||
|
|
||||||
let bob_keypair = Keypair::new();
|
let bob_keypair = Keypair::new();
|
||||||
let node_pubkey = Pubkey::new_rand();
|
let node_pubkey = Pubkey::new_rand();
|
||||||
config.command = WalletCommand::DelegateStake(bob_keypair.into(), node_pubkey, 100);
|
config.command = WalletCommand::DelegateStake(bob_keypair.into(), node_pubkey, 100);
|
||||||
|
|
Loading…
Reference in New Issue