miclockwork_network_program/state/
penalty.rs

1use anchor_lang::{prelude::*, AnchorDeserialize};
2
3pub const SEED_PENALTY: &[u8] = b"penalty";
4
5/// Escrows the lamport balance owed to a particular worker.
6#[account]
7#[derive(Debug)]
8pub struct Penalty {
9    /// The worker who was penalized.
10    pub worker: Pubkey,
11}
12
13impl Penalty {
14    /// Derive the pubkey of a fee account.
15    pub fn pubkey(worker: Pubkey) -> Pubkey {
16        Pubkey::find_program_address(&[SEED_PENALTY, worker.as_ref()], &crate::ID).0
17    }
18}
19
20/// Trait for reading and writing to a penalty account.
21pub trait PenaltyAccount {
22    /// Get the pubkey of the penalty account.
23    fn pubkey(&self) -> Pubkey;
24
25    /// Initialize the account to hold penalty object.
26    fn init(&mut self, worker: Pubkey) -> Result<()>;
27}
28
29impl PenaltyAccount for Account<'_, Penalty> {
30    fn pubkey(&self) -> Pubkey {
31        Penalty::pubkey(self.worker)
32    }
33
34    fn init(&mut self, worker: Pubkey) -> Result<()> {
35        self.worker = worker;
36        Ok(())
37    }
38}