miclockwork_network_program/state/
pool.rs1use std::collections::VecDeque;
2
3use anchor_lang::{prelude::*, AnchorDeserialize};
4
5pub const SEED_POOL: &[u8] = b"pool";
6
7const DEFAULT_POOL_SIZE: usize = 1;
8
9#[account]
14#[derive(Debug)]
15pub struct Pool {
16 pub id: u64,
17 pub size: usize,
18 pub workers: VecDeque<Pubkey>,
19}
20
21impl Pool {
22 pub fn pubkey(id: u64) -> Pubkey {
23 Pubkey::find_program_address(&[SEED_POOL, id.to_be_bytes().as_ref()], &crate::ID).0
24 }
25}
26
27#[derive(AnchorSerialize, AnchorDeserialize)]
32pub struct PoolSettings {
33 pub size: usize,
34}
35
36pub trait PoolAccount {
41 fn pubkey(&self) -> Pubkey;
42
43 fn init(&mut self, id: u64) -> Result<()>;
44
45 fn rotate(&mut self, worker: Pubkey) -> Result<()>;
46
47 fn update(&mut self, settings: &PoolSettings) -> Result<()>;
48}
49
50impl PoolAccount for Account<'_, Pool> {
51 fn pubkey(&self) -> Pubkey {
52 Pool::pubkey(self.id)
53 }
54
55 fn init(&mut self, id: u64) -> Result<()> {
56 self.id = id;
57 self.size = DEFAULT_POOL_SIZE;
58 self.workers = VecDeque::new();
59 Ok(())
60 }
61
62 fn rotate(&mut self, worker: Pubkey) -> Result<()> {
63 self.workers.push_back(worker);
65
66 while self.workers.len() > self.size {
68 self.workers.pop_front();
69 }
70
71 Ok(())
72 }
73
74 fn update(&mut self, settings: &PoolSettings) -> Result<()> {
75 self.size = settings.size;
76
77 while self.workers.len() > self.size {
79 self.workers.pop_front();
80 }
81
82 Ok(())
83 }
84}