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);
if let Ok(Some(vote_account_user_data)) = 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(());
}
}

View File

@ -619,8 +619,7 @@ mod tests {
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;
}

View File

@ -110,18 +110,9 @@ mod tests {
#[test]
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 mut vote_account = vote_state::create_vote_account(100);
vote_state::initialize_and_deserialize(
&staker_id,
&mut staker_account,
&vote_id,
&mut vote_account,
)
.unwrap();
vote_state::initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
let vote = Vote::new(i as u64);

View File

@ -19,8 +19,7 @@ impl Vote {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum VoteInstruction {
/// Initialize the VoteState for this `vote account`
/// * Transaction::keys[0] - the staker id that is also assumed to be the delegate by default
/// * Transaction::keys[1] - the new "vote account" to be associated with the delegate
/// * Instruction::keys[0] - the new "vote account" to be associated with the delegate
InitializeAccount,
/// `Delegate` or `Assign` a vote account to a particular node
DelegateStake(Pubkey),
@ -41,11 +40,11 @@ impl VoteInstruction {
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(
id(),
&VoteInstruction::InitializeAccount,
vec![(staker_id, true), (vote_id, false)],
vec![(vote_id, false)],
)
}
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
/// that the transaction must be signed by the staker's keys
pub fn initialize_account(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
if !check_id(&keyed_accounts[1].account.owner) {
error!("account[1] is not assigned to the VOTE_PROGRAM");
if !check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the VOTE_PROGRAM");
Err(ProgramError::InvalidArgument)?;
}
if keyed_accounts[0].signer_key().is_none() {
error!("account[0] should sign the transaction");
Err(ProgramError::InvalidArgument)?;
}
let staker_id = keyed_accounts[0].signer_key().unwrap();
let vote_state = VoteState::deserialize(&keyed_accounts[1].account.userdata);
let staker_id = keyed_accounts[0].unsigned_key();
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
if let Ok(vote_state) = vote_state {
if vote_state.delegate_id == Pubkey::default() {
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 {
error!("account[1] userdata already initialized");
error!("account[0] userdata already initialized");
Err(ProgramError::InvalidUserdata)?;
}
} else {
error!("account[1] does not have valid userdata");
error!("account[0] does not have valid userdata");
Err(ProgramError::InvalidUserdata)?;
}
@ -240,15 +235,10 @@ pub fn create_vote_account(tokens: u64) -> Account {
}
pub fn initialize_and_deserialize(
from_id: &Pubkey,
from_account: &mut Account,
vote_id: &Pubkey,
vote_account: &mut Account,
) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [
KeyedAccount::new(from_id, true, from_account),
KeyedAccount::new(vote_id, false, vote_account),
];
let mut keyed_accounts = [KeyedAccount::new(vote_id, false, vote_account)];
initialize_account(&mut keyed_accounts)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state)
@ -272,32 +262,20 @@ mod tests {
#[test]
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 mut vote_account = create_vote_account(100);
let bogus_account_id = Keypair::new().pubkey();
let mut bogus_account = Account::new(100, 0, id());
let mut keyed_accounts = [
KeyedAccount::new(&staker_id, false, &mut bogus_staker_account),
KeyedAccount::new(&bogus_account_id, 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));
let mut keyed_accounts = [KeyedAccount::new(
&bogus_account_id,
false,
&mut bogus_account,
)];
//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);
assert_eq!(res, Ok(()));
@ -319,27 +297,19 @@ mod tests {
#[test]
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 mut vote_account = create_vote_account(100);
let vote_state =
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
.unwrap();
assert_eq!(vote_state.delegate_id, from_id);
let vote_state = initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
assert_eq!(vote_state.delegate_id, vote_id);
assert!(vote_state.votes.is_empty());
}
#[test]
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 mut vote_account = create_vote_account(100);
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
.unwrap();
initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
let vote = Vote::new(1);
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
@ -349,12 +319,9 @@ mod tests {
#[test]
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 mut vote_account = create_vote_account(100);
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
.unwrap();
initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
let vote = Vote::new(1);
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![
Instruction::new(0, &create_tx, vec![0, 1]),
Instruction::new(1, &VoteInstruction::InitializeAccount, vec![0, 1]),
Instruction::new(1, &VoteInstruction::InitializeAccount, vec![1]),
],
)
}