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
use {
super::Config,
crate::pda::PDA,
anchor_lang::{prelude::*, AnchorDeserialize},
std::{collections::VecDeque, convert::TryFrom},
};
pub const SEED_POOL: &[u8] = b"pool";
#[account]
#[derive(Debug)]
pub struct Pool {
pub delegates: VecDeque<Pubkey>,
}
impl Pool {
pub fn pda() -> PDA {
Pubkey::find_program_address(&[SEED_POOL], &crate::ID)
}
}
impl TryFrom<Vec<u8>> for Pool {
type Error = Error;
fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Pool::try_deserialize(&mut data.as_slice())
}
}
pub trait PoolAccount {
fn new(&mut self) -> Result<()>;
fn cycle(&mut self, config: &Account<Config>, delegate: Pubkey) -> Result<()>;
}
impl PoolAccount for Account<'_, Pool> {
fn new(&mut self) -> Result<()> {
self.delegates = VecDeque::new();
Ok(())
}
fn cycle(&mut self, config: &Account<Config>, delegate: Pubkey) -> Result<()> {
self.delegates.pop_front();
self.delegates.push_back(delegate);
while self.delegates.len() > config.pool_size {
self.delegates.pop_front();
}
Ok(())
}
}