clockwork_network/state/
node.rs

1use {
2    anchor_lang::{prelude::*, AnchorDeserialize},
3    anchor_spl::token::TokenAccount,
4    std::{collections::HashSet, convert::TryFrom},
5};
6
7pub const SEED_NODE: &[u8] = b"node";
8
9/**
10 * Node
11 */
12
13#[account]
14#[derive(Debug)]
15pub struct Node {
16    pub authority: Pubkey, // The node's authority (controls the stake)
17    pub id: u64,
18    pub stake: Pubkey,                    // The associated token account
19    pub worker: Pubkey,                   // The node's worker address (used to sign txs)
20    pub supported_pools: HashSet<Pubkey>, // The set pools that this node supports
21}
22
23impl Node {
24    pub fn pubkey(id: u64) -> Pubkey {
25        Pubkey::find_program_address(&[SEED_NODE, id.to_be_bytes().as_ref()], &crate::ID).0
26    }
27}
28
29impl TryFrom<Vec<u8>> for Node {
30    type Error = Error;
31    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
32        Node::try_deserialize(&mut data.as_slice())
33    }
34}
35
36/**
37 * NodeSettings
38 */
39#[derive(AnchorSerialize, AnchorDeserialize)]
40pub struct NodeSettings {
41    pub supported_pools: HashSet<Pubkey>,
42}
43
44/**
45 * NodeAccount
46 */
47
48pub trait NodeAccount {
49    fn new(
50        &mut self,
51        authority: &mut Signer,
52        id: u64,
53        stake: &mut Account<TokenAccount>,
54        worker: &Signer,
55    ) -> Result<()>;
56
57    fn update(&mut self, settings: NodeSettings) -> Result<()>;
58}
59
60impl NodeAccount for Account<'_, Node> {
61    fn new(
62        &mut self,
63        authority: &mut Signer,
64        id: u64,
65        stake: &mut Account<TokenAccount>,
66        worker: &Signer,
67    ) -> Result<()> {
68        self.authority = authority.key();
69        self.id = id;
70        self.stake = stake.key();
71        self.worker = worker.key();
72        Ok(())
73    }
74
75    fn update(&mut self, settings: NodeSettings) -> Result<()> {
76        self.supported_pools = settings.supported_pools;
77        Ok(())
78    }
79}