From c56052ff1628379f190800a92e9a2e452b50b338 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Mon, 3 Jun 2019 09:04:51 -0700 Subject: [PATCH] remove from_account from stake_instruction (#4502) --- core/src/local_cluster.rs | 6 +-- core/src/staking_utils.rs | 6 +-- programs/stake_api/src/stake_instruction.rs | 46 ++++++--------------- wallet/src/wallet.rs | 30 ++++++++++---- 4 files changed, 41 insertions(+), 47 deletions(-) diff --git a/core/src/local_cluster.rs b/core/src/local_cluster.rs index faef41d152..e0181585bb 100644 --- a/core/src/local_cluster.rs +++ b/core/src/local_cluster.rs @@ -444,13 +444,13 @@ impl LocalCluster { .wait_for_balance(&stake_account_pubkey, Some(amount)) .expect("get balance"); - let mut transaction = Transaction::new_signed_instructions( - &[from_account.as_ref(), &stake_account_keypair], + let mut transaction = Transaction::new_signed_with_payer( vec![stake_instruction::delegate_stake( - &from_account.pubkey(), &stake_account_pubkey, &vote_account_pubkey, )], + Some(&from_account.pubkey()), + &[from_account.as_ref(), &stake_account_keypair], client.get_recent_blockhash().unwrap().0, ); client diff --git a/core/src/staking_utils.rs b/core/src/staking_utils.rs index e94bb5b043..ed0a6d72ea 100644 --- a/core/src/staking_utils.rs +++ b/core/src/staking_utils.rs @@ -167,9 +167,10 @@ pub(crate) mod tests { keypairs: &[&T], ixs: Vec, ) { - bank.process_transaction(&Transaction::new_signed_instructions( - keypairs, + bank.process_transaction(&Transaction::new_signed_with_payer( ixs, + Some(&keypairs[0].pubkey()), + keypairs, bank.last_blockhash(), )) .unwrap(); @@ -204,7 +205,6 @@ pub(crate) mod tests { bank, &[from_account, &stake_account_keypair], vec![stake_instruction::delegate_stake( - &from_account.pubkey(), &stake_account_pubkey, vote_pubkey, )], diff --git a/programs/stake_api/src/stake_instruction.rs b/programs/stake_api/src/stake_instruction.rs index 947a9f1e33..6dd248b2cc 100644 --- a/programs/stake_api/src/stake_instruction.rs +++ b/programs/stake_api/src/stake_instruction.rs @@ -58,10 +58,7 @@ pub fn create_delegate_account( Instruction::new( id(), &StakeInstruction::InitializeDelegate, - vec![ - AccountMeta::new(*from_pubkey, true), - AccountMeta::new(*staker_pubkey, false), - ], + vec![AccountMeta::new(*staker_pubkey, false)], ), ] } @@ -82,22 +79,17 @@ pub fn create_mining_pool_account( Instruction::new( id(), &StakeInstruction::InitializeMiningPool, - vec![ - AccountMeta::new(*from_pubkey, true), - AccountMeta::new(*staker_pubkey, false), - ], + vec![AccountMeta::new(*staker_pubkey, false)], ), ] } pub fn redeem_vote_credits( - from_pubkey: &Pubkey, mining_pool_pubkey: &Pubkey, stake_pubkey: &Pubkey, vote_pubkey: &Pubkey, ) -> Instruction { let account_metas = vec![ - AccountMeta::new(*from_pubkey, true), AccountMeta::new(*mining_pool_pubkey, false), AccountMeta::new(*stake_pubkey, false), AccountMeta::new(*vote_pubkey, false), @@ -105,13 +97,8 @@ pub fn redeem_vote_credits( Instruction::new(id(), &StakeInstruction::RedeemVoteCredits, account_metas) } -pub fn delegate_stake( - from_pubkey: &Pubkey, - stake_pubkey: &Pubkey, - vote_pubkey: &Pubkey, -) -> Instruction { +pub fn delegate_stake(stake_pubkey: &Pubkey, vote_pubkey: &Pubkey) -> Instruction { let account_metas = vec![ - AccountMeta::new(*from_pubkey, true), AccountMeta::new(*stake_pubkey, true), AccountMeta::new(*vote_pubkey, false), ]; @@ -128,14 +115,12 @@ pub fn process_instruction( trace!("process_instruction: {:?}", data); trace!("keyed_accounts: {:?}", keyed_accounts); - if keyed_accounts.len() < 2 { + if keyed_accounts.is_empty() { Err(InstructionError::InvalidInstructionData)?; } - // 0th index is the account who paid for the transaction - // TODO: Remove the 0th index from the instruction. The stake program doesn't care who paid. - let (me, rest) = &mut keyed_accounts.split_at_mut(2); - let me = &mut me[1]; + let (me, rest) = &mut keyed_accounts.split_at_mut(1); + let me = &mut me[0]; // TODO: data-driven unpack and dispatch of KeyedAccounts match deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? { @@ -197,7 +182,6 @@ mod tests { fn test_stake_process_instruction() { assert_eq!( process_instruction(&redeem_vote_credits( - &Pubkey::default(), &Pubkey::default(), &Pubkey::default(), &Pubkey::default() @@ -205,11 +189,7 @@ mod tests { Err(InstructionError::InvalidAccountData), ); assert_eq!( - process_instruction(&delegate_stake( - &Pubkey::default(), - &Pubkey::default(), - &Pubkey::default() - )), + process_instruction(&delegate_stake(&Pubkey::default(), &Pubkey::default())), Err(InstructionError::InvalidAccountData), ); } @@ -236,10 +216,11 @@ mod tests { assert_eq!( super::process_instruction( &Pubkey::default(), - &mut [ - KeyedAccount::new(&Pubkey::default(), true, &mut Account::default()), - KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), - ], + &mut [KeyedAccount::new( + &Pubkey::default(), + false, + &mut Account::default() + ),], &serialize(&StakeInstruction::DelegateStake).unwrap(), ), Err(InstructionError::InvalidInstructionData), @@ -251,7 +232,6 @@ mod tests { &mut [ KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), - KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), ], &serialize(&StakeInstruction::RedeemVoteCredits).unwrap(), ), @@ -263,7 +243,6 @@ mod tests { super::process_instruction( &Pubkey::default(), &mut [ - KeyedAccount::new(&Pubkey::default(), true, &mut Account::default()), // from KeyedAccount::new(&Pubkey::default(), true, &mut Account::default()), KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), ], @@ -277,7 +256,6 @@ mod tests { super::process_instruction( &Pubkey::default(), &mut [ - KeyedAccount::new(&Pubkey::default(), true, &mut Account::default()), // from KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()), diff --git a/wallet/src/wallet.rs b/wallet/src/wallet.rs index c67cb1cbe2..c6db33efa7 100644 --- a/wallet/src/wallet.rs +++ b/wallet/src/wallet.rs @@ -544,7 +544,12 @@ fn process_create_stake_account( staking_account_pubkey, lamports, ); - let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, recent_blockhash); + 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()) } @@ -561,7 +566,12 @@ fn process_create_mining_pool_account( mining_pool_account_pubkey, lamports, ); - let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, recent_blockhash); + 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()) } @@ -574,15 +584,17 @@ fn process_delegate_stake( ) -> ProcessResult { let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?; let ixs = vec![stake_instruction::delegate_stake( - &config.keypair.pubkey(), &staking_account_keypair.pubkey(), voting_account_pubkey, )]; - let mut tx = Transaction::new_signed_instructions( - &[&config.keypair, &staking_account_keypair], + + let mut tx = Transaction::new_signed_with_payer( ixs, + Some(&config.keypair.pubkey()), + &[&config.keypair, &staking_account_keypair], recent_blockhash, ); + let signature_str = rpc_client .send_and_confirm_transaction(&mut tx, &[&config.keypair, &staking_account_keypair])?; Ok(signature_str.to_string()) @@ -597,12 +609,16 @@ fn process_redeem_vote_credits( ) -> ProcessResult { let (recent_blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?; let ixs = vec![stake_instruction::redeem_vote_credits( - &config.keypair.pubkey(), mining_pool_account_pubkey, staking_account_pubkey, voting_account_pubkey, )]; - let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, recent_blockhash); + 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()) }