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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use {
    anchor_lang::{prelude::*, AnchorDeserialize},
    anchor_spl::token::TokenAccount,
    std::{collections::HashSet, convert::TryFrom},
};

pub const SEED_NODE: &[u8] = b"node";

/**
 * Node
 */

#[account]
#[derive(Debug)]
pub struct Node {
    pub authority: Pubkey, // The node's authority (controls the stake)
    pub id: u64,
    pub stake: Pubkey,                    // The associated token account
    pub worker: Pubkey,                   // The node's worker address (used to sign txs)
    pub supported_pools: HashSet<Pubkey>, // The set pools that this node supports
}

impl Node {
    pub fn pubkey(id: u64) -> Pubkey {
        Pubkey::find_program_address(&[SEED_NODE, id.to_be_bytes().as_ref()], &crate::ID).0
    }
}

impl TryFrom<Vec<u8>> for Node {
    type Error = Error;
    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
        Node::try_deserialize(&mut data.as_slice())
    }
}

/**
 * NodeSettings
 */
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct NodeSettings {
    pub supported_pools: HashSet<Pubkey>,
}

/**
 * NodeAccount
 */

pub trait NodeAccount {
    fn pubkey(&self) -> Pubkey;

    fn init(
        &mut self,
        authority: &mut Signer,
        id: u64,
        stake: &mut Account<TokenAccount>,
        worker: &Signer,
    ) -> Result<()>;

    fn update(&mut self, settings: NodeSettings) -> Result<()>;
}

impl NodeAccount for Account<'_, Node> {
    fn pubkey(&self) -> Pubkey {
        Node::pubkey(self.id)
    }

    fn init(
        &mut self,
        authority: &mut Signer,
        id: u64,
        stake: &mut Account<TokenAccount>,
        worker: &Signer,
    ) -> Result<()> {
        self.authority = authority.key();
        self.id = id;
        self.stake = stake.key();
        self.worker = worker.key();
        Ok(())
    }

    fn update(&mut self, settings: NodeSettings) -> Result<()> {
        self.supported_pools = settings.supported_pools;
        Ok(())
    }
}