The funder is not a staker

This commit is contained in:
Greg Fitzgerald 2019-03-05 10:53:10 -07:00
parent 6e9d803091
commit 52fc974cdf
6 changed files with 25 additions and 69 deletions

View File

@ -172,7 +172,7 @@ impl LocalCluster {
let vote_account_user_data = client.get_account_userdata(&vote_account); let vote_account_user_data = client.get_account_userdata(&vote_account);
if let Ok(Some(vote_account_user_data)) = vote_account_user_data { if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
if let Ok(vote_state) = VoteState::deserialize(&vote_account_user_data) { if let Ok(vote_state) = VoteState::deserialize(&vote_account_user_data) {
if vote_state.delegate_id == from_account.pubkey() { if vote_state.delegate_id == vote_account {
return Ok(()); return Ok(());
} }
} }

View File

@ -619,8 +619,7 @@ mod tests {
let vote_state = VoteState::deserialize(&account_user_data); let vote_state = VoteState::deserialize(&account_user_data);
if vote_state.map(|vote_state| vote_state.delegate_id) == Ok(validator_keypair.pubkey()) if vote_state.map(|vote_state| vote_state.delegate_id) == Ok(vote_account_id) {
{
break; break;
} }

View File

@ -110,18 +110,9 @@ mod tests {
#[test] #[test]
fn test_redeem_vote_credits_via_program() { fn test_redeem_vote_credits_via_program() {
let staker_id = Keypair::new().pubkey();
let mut staker_account = Account::new(100, 0, Pubkey::default());
let vote_id = Keypair::new().pubkey(); let vote_id = Keypair::new().pubkey();
let mut vote_account = vote_state::create_vote_account(100); let mut vote_account = vote_state::create_vote_account(100);
vote_state::initialize_and_deserialize( vote_state::initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
&staker_id,
&mut staker_account,
&vote_id,
&mut vote_account,
)
.unwrap();
for i in 0..vote_state::MAX_LOCKOUT_HISTORY { for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
let vote = Vote::new(i as u64); let vote = Vote::new(i as u64);

View File

@ -19,8 +19,7 @@ impl Vote {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum VoteInstruction { pub enum VoteInstruction {
/// Initialize the VoteState for this `vote account` /// Initialize the VoteState for this `vote account`
/// * Transaction::keys[0] - the staker id that is also assumed to be the delegate by default /// * Instruction::keys[0] - the new "vote account" to be associated with the delegate
/// * Transaction::keys[1] - the new "vote account" to be associated with the delegate
InitializeAccount, InitializeAccount,
/// `Delegate` or `Assign` a vote account to a particular node /// `Delegate` or `Assign` a vote account to a particular node
DelegateStake(Pubkey), DelegateStake(Pubkey),
@ -41,11 +40,11 @@ impl VoteInstruction {
vec![(vote_id, true)], vec![(vote_id, true)],
) )
} }
pub fn new_initialize_account(vote_id: Pubkey, staker_id: Pubkey) -> BuilderInstruction { pub fn new_initialize_account(vote_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new( BuilderInstruction::new(
id(), id(),
&VoteInstruction::InitializeAccount, &VoteInstruction::InitializeAccount,
vec![(staker_id, true), (vote_id, false)], vec![(vote_id, false)],
) )
} }
pub fn new_vote(vote_id: Pubkey, vote: Vote) -> BuilderInstruction { pub fn new_vote(vote_id: Pubkey, vote: Vote) -> BuilderInstruction {

View File

@ -177,28 +177,23 @@ pub fn delegate_stake(
/// Assumes that the account is being init as part of a account creation or balance transfer and /// Assumes that the account is being init as part of a account creation or balance transfer and
/// that the transaction must be signed by the staker's keys /// that the transaction must be signed by the staker's keys
pub fn initialize_account(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> { pub fn initialize_account(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
if !check_id(&keyed_accounts[1].account.owner) { if !check_id(&keyed_accounts[0].account.owner) {
error!("account[1] is not assigned to the VOTE_PROGRAM"); error!("account[0] is not assigned to the VOTE_PROGRAM");
Err(ProgramError::InvalidArgument)?; Err(ProgramError::InvalidArgument)?;
} }
if keyed_accounts[0].signer_key().is_none() { let staker_id = keyed_accounts[0].unsigned_key();
error!("account[0] should sign the transaction"); let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
Err(ProgramError::InvalidArgument)?;
}
let staker_id = keyed_accounts[0].signer_key().unwrap();
let vote_state = VoteState::deserialize(&keyed_accounts[1].account.userdata);
if let Ok(vote_state) = vote_state { if let Ok(vote_state) = vote_state {
if vote_state.delegate_id == Pubkey::default() { if vote_state.delegate_id == Pubkey::default() {
let vote_state = VoteState::new(*staker_id); let vote_state = VoteState::new(*staker_id);
vote_state.serialize(&mut keyed_accounts[1].account.userdata)?; vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
} else { } else {
error!("account[1] userdata already initialized"); error!("account[0] userdata already initialized");
Err(ProgramError::InvalidUserdata)?; Err(ProgramError::InvalidUserdata)?;
} }
} else { } else {
error!("account[1] does not have valid userdata"); error!("account[0] does not have valid userdata");
Err(ProgramError::InvalidUserdata)?; Err(ProgramError::InvalidUserdata)?;
} }
@ -240,15 +235,10 @@ pub fn create_vote_account(tokens: u64) -> Account {
} }
pub fn initialize_and_deserialize( pub fn initialize_and_deserialize(
from_id: &Pubkey,
from_account: &mut Account,
vote_id: &Pubkey, vote_id: &Pubkey,
vote_account: &mut Account, vote_account: &mut Account,
) -> Result<VoteState, ProgramError> { ) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [ let mut keyed_accounts = [KeyedAccount::new(vote_id, false, vote_account)];
KeyedAccount::new(from_id, true, from_account),
KeyedAccount::new(vote_id, false, vote_account),
];
initialize_account(&mut keyed_accounts)?; initialize_account(&mut keyed_accounts)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap(); let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state) Ok(vote_state)
@ -272,32 +262,20 @@ mod tests {
#[test] #[test]
fn test_initialize_vote_account() { fn test_initialize_vote_account() {
let staker_id = Keypair::new().pubkey();
let mut staker_account = Account::new(100, 0, Pubkey::default());
let mut bogus_staker_account = Account::new(100, 0, Pubkey::default());
let vote_account_id = Keypair::new().pubkey(); let vote_account_id = Keypair::new().pubkey();
let mut vote_account = create_vote_account(100); let mut vote_account = create_vote_account(100);
let bogus_account_id = Keypair::new().pubkey(); let bogus_account_id = Keypair::new().pubkey();
let mut bogus_account = Account::new(100, 0, id()); let mut bogus_account = Account::new(100, 0, id());
let mut keyed_accounts = [ let mut keyed_accounts = [KeyedAccount::new(
KeyedAccount::new(&staker_id, false, &mut bogus_staker_account), &bogus_account_id,
KeyedAccount::new(&bogus_account_id, false, &mut bogus_account), false,
]; &mut bogus_account,
)];
//staker is not signer
let res = initialize_account(&mut keyed_accounts);
assert_eq!(res, Err(ProgramError::InvalidArgument));
//space was never initialized, deserialize will fail
keyed_accounts[0] = KeyedAccount::new(&staker_id, true, &mut staker_account);
let res = initialize_account(&mut keyed_accounts);
assert_eq!(res, Err(ProgramError::InvalidUserdata));
//init should pass //init should pass
keyed_accounts[1] = KeyedAccount::new(&vote_account_id, false, &mut vote_account); keyed_accounts[0] = KeyedAccount::new(&vote_account_id, false, &mut vote_account);
let res = initialize_account(&mut keyed_accounts); let res = initialize_account(&mut keyed_accounts);
assert_eq!(res, Ok(())); assert_eq!(res, Ok(()));
@ -319,27 +297,19 @@ mod tests {
#[test] #[test]
fn test_voter_registration() { fn test_voter_registration() {
let from_id = Keypair::new().pubkey();
let mut from_account = Account::new(100, 0, Pubkey::default());
let vote_id = Keypair::new().pubkey(); let vote_id = Keypair::new().pubkey();
let mut vote_account = create_vote_account(100); let mut vote_account = create_vote_account(100);
let vote_state = let vote_state = initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account) assert_eq!(vote_state.delegate_id, vote_id);
.unwrap();
assert_eq!(vote_state.delegate_id, from_id);
assert!(vote_state.votes.is_empty()); assert!(vote_state.votes.is_empty());
} }
#[test] #[test]
fn test_vote() { fn test_vote() {
let from_id = Keypair::new().pubkey();
let mut from_account = Account::new(100, 0, Pubkey::default());
let vote_id = Keypair::new().pubkey(); let vote_id = Keypair::new().pubkey();
let mut vote_account = create_vote_account(100); let mut vote_account = create_vote_account(100);
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account) initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
.unwrap();
let vote = Vote::new(1); let vote = Vote::new(1);
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap(); let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
@ -349,12 +319,9 @@ mod tests {
#[test] #[test]
fn test_vote_signature() { fn test_vote_signature() {
let from_id = Keypair::new().pubkey();
let mut from_account = Account::new(100, 0, Pubkey::default());
let vote_id = Keypair::new().pubkey(); let vote_id = Keypair::new().pubkey();
let mut vote_account = create_vote_account(100); let mut vote_account = create_vote_account(100);
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account) initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
.unwrap();
let vote = Vote::new(1); let vote = Vote::new(1);
let mut keyed_accounts = [KeyedAccount::new(&vote_id, false, &mut vote_account)]; let mut keyed_accounts = [KeyedAccount::new(&vote_id, false, &mut vote_account)];

View File

@ -48,7 +48,7 @@ impl VoteTransaction {
vec![system_program::id(), id()], vec![system_program::id(), id()],
vec![ vec![
Instruction::new(0, &create_tx, vec![0, 1]), Instruction::new(0, &create_tx, vec![0, 1]),
Instruction::new(1, &VoteInstruction::InitializeAccount, vec![0, 1]), Instruction::new(1, &VoteInstruction::InitializeAccount, vec![1]),
], ],
) )
} }