miclockwork_network_program/state/
registry.rs1use std::{
2 collections::hash_map::DefaultHasher,
3 hash::{Hash, Hasher},
4};
5
6use anchor_lang::{prelude::*, AnchorDeserialize};
7
8pub const SEED_REGISTRY: &[u8] = b"registry";
9
10#[account]
13#[derive(Debug)]
14pub struct Registry {
15 pub current_epoch: u64,
16 pub locked: bool,
17 pub nonce: u64,
18 pub total_pools: u64,
19 pub total_unstakes: u64,
20 pub total_workers: u64,
21}
22
23impl Registry {
24 pub fn pubkey() -> Pubkey {
25 Pubkey::find_program_address(&[SEED_REGISTRY], &crate::ID).0
26 }
27}
28
29pub trait RegistryAccount {
34 fn init(&mut self) -> Result<()>;
35
36 fn hash_nonce(&mut self) -> Result<()>;
37}
38
39impl RegistryAccount for Account<'_, Registry> {
40 fn init(&mut self) -> Result<()> {
41 self.current_epoch = 0;
42 self.locked = false;
43 self.total_workers = 0;
44 Ok(())
45 }
46
47 fn hash_nonce(&mut self) -> Result<()> {
48 let mut hasher = DefaultHasher::new();
49 Clock::get().unwrap().slot.hash(&mut hasher);
50 self.nonce.hash(&mut hasher);
51 self.nonce = hasher.finish();
52 Ok(())
53 }
54}