solana_commitment_config/
lib.rs

1//! Definitions of commitment levels.
2
3use core::{fmt, str::FromStr};
4
5#[cfg_attr(
6    feature = "serde",
7    derive(serde_derive::Serialize, serde_derive::Deserialize),
8    serde(rename_all = "camelCase")
9)]
10#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub struct CommitmentConfig {
12    pub commitment: CommitmentLevel,
13}
14
15impl CommitmentConfig {
16    pub const fn finalized() -> Self {
17        Self {
18            commitment: CommitmentLevel::Finalized,
19        }
20    }
21
22    pub const fn confirmed() -> Self {
23        Self {
24            commitment: CommitmentLevel::Confirmed,
25        }
26    }
27
28    pub const fn processed() -> Self {
29        Self {
30            commitment: CommitmentLevel::Processed,
31        }
32    }
33
34    pub fn ok(self) -> Option<Self> {
35        if self == Self::default() {
36            None
37        } else {
38            Some(self)
39        }
40    }
41
42    pub fn is_finalized(&self) -> bool {
43        self.commitment == CommitmentLevel::Finalized
44    }
45
46    pub fn is_confirmed(&self) -> bool {
47        self.commitment == CommitmentLevel::Confirmed
48    }
49
50    pub fn is_processed(&self) -> bool {
51        self.commitment == CommitmentLevel::Processed
52    }
53
54    pub fn is_at_least_confirmed(&self) -> bool {
55        self.is_confirmed() || self.is_finalized()
56    }
57
58    #[deprecated(
59        since = "2.0.2",
60        note = "Returns self. Please do not use. Will be removed in the future."
61    )]
62    pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
63        commitment
64    }
65}
66
67impl FromStr for CommitmentConfig {
68    type Err = ParseCommitmentLevelError;
69
70    fn from_str(s: &str) -> Result<Self, Self::Err> {
71        CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
72    }
73}
74
75#[cfg_attr(
76    feature = "serde",
77    derive(serde_derive::Serialize, serde_derive::Deserialize),
78    serde(rename_all = "camelCase")
79)]
80#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
81/// An attribute of a slot. It describes how finalized a block is at some point in time. For example, a slot
82/// is said to be at the max level immediately after the cluster recognizes the block at that slot as
83/// finalized. When querying the ledger state, use lower levels of commitment to report progress and higher
84/// levels to ensure state changes will not be rolled back.
85pub enum CommitmentLevel {
86    /// The highest slot of the heaviest fork processed by the node. Ledger state at this slot is
87    /// not derived from a confirmed or finalized block, but if multiple forks are present, is from
88    /// the fork the validator believes is most likely to finalize.
89    Processed,
90
91    /// The highest slot that has been voted on by supermajority of the cluster, ie. is confirmed.
92    /// Confirmation incorporates votes from gossip and replay. It does not count votes on
93    /// descendants of a block, only direct votes on that block, and upholds "optimistic
94    /// confirmation" guarantees in release 1.3 and onwards.
95    Confirmed,
96
97    /// The highest slot having reached max vote lockout, as recognized by a supermajority of the
98    /// cluster.
99    Finalized,
100}
101
102impl Default for CommitmentLevel {
103    fn default() -> Self {
104        Self::Finalized
105    }
106}
107
108impl FromStr for CommitmentLevel {
109    type Err = ParseCommitmentLevelError;
110
111    fn from_str(s: &str) -> Result<Self, Self::Err> {
112        match s {
113            "processed" => Ok(CommitmentLevel::Processed),
114            "confirmed" => Ok(CommitmentLevel::Confirmed),
115            "finalized" => Ok(CommitmentLevel::Finalized),
116            _ => Err(ParseCommitmentLevelError::Invalid),
117        }
118    }
119}
120
121impl std::fmt::Display for CommitmentLevel {
122    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
123        let s = match self {
124            CommitmentLevel::Processed => "processed",
125            CommitmentLevel::Confirmed => "confirmed",
126            CommitmentLevel::Finalized => "finalized",
127        };
128        write!(f, "{s}")
129    }
130}
131
132#[derive(Debug)]
133pub enum ParseCommitmentLevelError {
134    Invalid,
135}
136
137impl std::error::Error for ParseCommitmentLevelError {}
138
139impl fmt::Display for ParseCommitmentLevelError {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        match self {
142            Self::Invalid => f.write_str("invalid variant"),
143        }
144    }
145}