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
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

use anchor_lang::{prelude::*, AnchorDeserialize};

pub const SEED_REGISTRY: &[u8] = b"registry";

/// Registry

#[account]
#[derive(Debug)]
pub struct Registry {
    pub current_epoch: u64,
    pub locked: bool,
    pub nonce: u64,
    pub total_pools: u64,
    pub total_unstakes: u64,
    pub total_workers: u64,
}

impl Registry {
    pub fn pubkey() -> Pubkey {
        Pubkey::find_program_address(&[SEED_REGISTRY], &crate::ID).0
    }
}

/**
 * RegistryAccount
 */

pub trait RegistryAccount {
    fn init(&mut self) -> Result<()>;

    fn hash_nonce(&mut self) -> Result<()>;
}

impl RegistryAccount for Account<'_, Registry> {
    fn init(&mut self) -> Result<()> {
        self.current_epoch = 0;
        self.locked = false;
        self.total_workers = 0;
        Ok(())
    }

    fn hash_nonce(&mut self) -> Result<()> {
        let mut hasher = DefaultHasher::new();
        Clock::get().unwrap().slot.hash(&mut hasher);
        self.nonce.hash(&mut hasher);
        self.nonce = hasher.finish();
        Ok(())
    }
}