trident_fuzz/accounts_storage/
vote_store.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use solana_sdk::{
    account::{AccountSharedData, WritableAccount},
    clock::Clock,
    pubkey::Pubkey,
    rent::Rent,
    signature::Keypair,
    signer::Signer,
};
use solana_vote_program::vote_state::{VoteInit, VoteState, VoteStateVersions};

use crate::{fuzz_client::FuzzClient, AccountId};

use super::AccountsStorage;

pub struct VoteStore {
    pub pubkey: Pubkey,
}

impl From<Pubkey> for VoteStore {
    fn from(pubkey: Pubkey) -> Self {
        VoteStore { pubkey }
    }
}

impl AccountsStorage<VoteStore> {
    #[allow(clippy::too_many_arguments)]
    pub fn get_or_create_account(
        &mut self,
        account_id: AccountId,
        client: &mut impl FuzzClient,
        node_pubkey: &Pubkey,
        authorized_voter: &Pubkey,
        authorized_withdrawer: &Pubkey,
        commission: u8,
        clock: &Clock,
    ) -> Pubkey {
        let key = self.accounts.entry(account_id).or_insert_with(|| {
            let vote_account = Keypair::new();

            let rent = Rent::default();
            let lamports = rent.minimum_balance(VoteState::size_of());
            let mut account = AccountSharedData::new(
                lamports,
                VoteState::size_of(),
                &solana_sdk::vote::program::ID,
            );

            let vote_state = VoteState::new(
                &VoteInit {
                    node_pubkey: *node_pubkey,
                    authorized_voter: *authorized_voter,
                    authorized_withdrawer: *authorized_withdrawer,
                    commission,
                },
                clock,
            );

            VoteState::serialize(
                &VoteStateVersions::Current(Box::new(vote_state)),
                account.data_as_mut_slice(),
            )
            .unwrap();

            client.set_account_custom(&vote_account.pubkey(), &account);

            VoteStore {
                pubkey: vote_account.pubkey(),
            }
        });
        key.pubkey
    }
    pub fn get(&self, account_id: AccountId) -> Pubkey {
        match self.accounts.get(&account_id) {
            Some(v) => v.pubkey,
            None => Pubkey::new_unique(),
        }
    }
}