spl_slashing/
state.rs

1//! Program state
2use {
3    crate::{duplicate_block_proof::DuplicateBlockProofData, error::SlashingError},
4    solana_program::{clock::Slot, pubkey::Pubkey},
5};
6
7const PACKET_DATA_SIZE: usize = 1232;
8
9/// Types of slashing proofs
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum ProofType {
12    /// Invalid proof type
13    InvalidType,
14    /// Proof consisting of 2 shreds signed by the leader indicating the leader
15    /// submitted a duplicate block.
16    DuplicateBlockProof,
17}
18
19impl ProofType {
20    /// Size of the proof account to create in order to hold the proof data
21    /// header and contents
22    pub const fn proof_account_length(&self) -> usize {
23        match self {
24            Self::InvalidType => panic!("Cannot determine size of invalid proof type"),
25            Self::DuplicateBlockProof => {
26                // Duplicate block proof consists of 2 shreds that can be `PACKET_DATA_SIZE`.
27                DuplicateBlockProofData::size_of(PACKET_DATA_SIZE)
28            }
29        }
30    }
31
32    /// Display string for this proof type's violation
33    pub fn violation_str(&self) -> &str {
34        match self {
35            Self::InvalidType => "invalid",
36            Self::DuplicateBlockProof => "duplicate block",
37        }
38    }
39}
40
41impl From<ProofType> for u8 {
42    fn from(value: ProofType) -> Self {
43        match value {
44            ProofType::InvalidType => 0,
45            ProofType::DuplicateBlockProof => 1,
46        }
47    }
48}
49
50impl From<u8> for ProofType {
51    fn from(value: u8) -> Self {
52        match value {
53            1 => Self::DuplicateBlockProof,
54            _ => Self::InvalidType,
55        }
56    }
57}
58
59/// Trait that proof accounts must satisfy in order to verify via the slashing
60/// program
61pub trait SlashingProofData<'a> {
62    /// The type of proof this data represents
63    const PROOF_TYPE: ProofType;
64
65    /// Zero copy from raw data buffer
66    fn unpack(data: &'a [u8]) -> Result<Self, SlashingError>
67    where
68        Self: Sized;
69
70    /// Verification logic for this type of proof data
71    fn verify_proof(self, slot: Slot, pubkey: &Pubkey) -> Result<(), SlashingError>;
72}
73
74#[cfg(test)]
75mod tests {
76    use crate::state::PACKET_DATA_SIZE;
77
78    #[test]
79    fn test_packet_size_parity() {
80        assert_eq!(PACKET_DATA_SIZE, solana_sdk::packet::PACKET_DATA_SIZE);
81    }
82}