solana_sdk/
commitment_config.rs1#![allow(deprecated)]
2#![cfg(feature = "full")]
3
4use {std::str::FromStr, thiserror::Error};
5
6#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
7#[serde(rename_all = "camelCase")]
8pub struct CommitmentConfig {
9 pub commitment: CommitmentLevel,
10}
11
12impl CommitmentConfig {
13 #[deprecated(
14 since = "1.5.5",
15 note = "Please use CommitmentConfig::processed() instead"
16 )]
17 pub fn recent() -> Self {
18 Self {
19 commitment: CommitmentLevel::Recent,
20 }
21 }
22
23 #[deprecated(
24 since = "1.5.5",
25 note = "Please use CommitmentConfig::finalized() instead"
26 )]
27 pub fn max() -> Self {
28 Self {
29 commitment: CommitmentLevel::Max,
30 }
31 }
32
33 #[deprecated(
34 since = "1.5.5",
35 note = "Please use CommitmentConfig::finalized() instead"
36 )]
37 pub fn root() -> Self {
38 Self {
39 commitment: CommitmentLevel::Root,
40 }
41 }
42
43 #[deprecated(
44 since = "1.5.5",
45 note = "Please use CommitmentConfig::confirmed() instead"
46 )]
47 pub fn single() -> Self {
48 Self {
49 commitment: CommitmentLevel::Single,
50 }
51 }
52
53 #[deprecated(
54 since = "1.5.5",
55 note = "Please use CommitmentConfig::confirmed() instead"
56 )]
57 pub fn single_gossip() -> Self {
58 Self {
59 commitment: CommitmentLevel::SingleGossip,
60 }
61 }
62
63 pub fn finalized() -> Self {
64 Self {
65 commitment: CommitmentLevel::Finalized,
66 }
67 }
68
69 pub fn confirmed() -> Self {
70 Self {
71 commitment: CommitmentLevel::Confirmed,
72 }
73 }
74
75 pub fn processed() -> Self {
76 Self {
77 commitment: CommitmentLevel::Processed,
78 }
79 }
80
81 pub fn ok(self) -> Option<Self> {
82 if self == Self::default() {
83 None
84 } else {
85 Some(self)
86 }
87 }
88
89 pub fn is_finalized(&self) -> bool {
90 matches!(
91 &self.commitment,
92 CommitmentLevel::Finalized | CommitmentLevel::Max | CommitmentLevel::Root
93 )
94 }
95
96 pub fn is_confirmed(&self) -> bool {
97 matches!(
98 &self.commitment,
99 CommitmentLevel::Confirmed | CommitmentLevel::SingleGossip | CommitmentLevel::Single
100 )
101 }
102
103 pub fn is_processed(&self) -> bool {
104 matches!(
105 &self.commitment,
106 CommitmentLevel::Processed | CommitmentLevel::Recent
107 )
108 }
109
110 pub fn is_at_least_confirmed(&self) -> bool {
111 self.is_confirmed() || self.is_finalized()
112 }
113
114 pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
115 match commitment.commitment {
116 CommitmentLevel::Finalized => CommitmentConfig::max(),
117 CommitmentLevel::Confirmed => CommitmentConfig::single_gossip(),
118 CommitmentLevel::Processed => CommitmentConfig::recent(),
119 _ => commitment,
120 }
121 }
122}
123
124impl FromStr for CommitmentConfig {
125 type Err = ParseCommitmentLevelError;
126
127 fn from_str(s: &str) -> Result<Self, Self::Err> {
128 CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
129 }
130}
131
132#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
133#[serde(rename_all = "camelCase")]
134pub enum CommitmentLevel {
139 #[deprecated(
141 since = "1.5.5",
142 note = "Please use CommitmentLevel::Finalized instead"
143 )]
144 Max,
145
146 #[deprecated(
150 since = "1.5.5",
151 note = "Please use CommitmentLevel::Processed instead"
152 )]
153 Recent,
154
155 #[deprecated(
157 since = "1.5.5",
158 note = "Please use CommitmentLevel::Finalized instead"
159 )]
160 Root,
161
162 #[deprecated(
164 since = "1.5.5",
165 note = "Please use CommitmentLevel::Confirmed instead"
166 )]
167 Single,
168
169 #[deprecated(
176 since = "1.5.5",
177 note = "Please use CommitmentLevel::Confirmed instead"
178 )]
179 SingleGossip,
180
181 Processed,
185
186 Confirmed,
191
192 Finalized,
195}
196
197impl Default for CommitmentLevel {
198 fn default() -> Self {
199 Self::Finalized
200 }
201}
202
203impl FromStr for CommitmentLevel {
204 type Err = ParseCommitmentLevelError;
205
206 fn from_str(s: &str) -> Result<Self, Self::Err> {
207 match s {
208 "max" => Ok(CommitmentLevel::Max),
209 "recent" => Ok(CommitmentLevel::Recent),
210 "root" => Ok(CommitmentLevel::Root),
211 "single" => Ok(CommitmentLevel::Single),
212 "singleGossip" => Ok(CommitmentLevel::SingleGossip),
213 "processed" => Ok(CommitmentLevel::Processed),
214 "confirmed" => Ok(CommitmentLevel::Confirmed),
215 "finalized" => Ok(CommitmentLevel::Finalized),
216 _ => Err(ParseCommitmentLevelError::Invalid),
217 }
218 }
219}
220
221impl std::fmt::Display for CommitmentLevel {
222 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
223 let s = match self {
224 CommitmentLevel::Max => "max",
225 CommitmentLevel::Recent => "recent",
226 CommitmentLevel::Root => "root",
227 CommitmentLevel::Single => "single",
228 CommitmentLevel::SingleGossip => "singleGossip",
229 CommitmentLevel::Processed => "processed",
230 CommitmentLevel::Confirmed => "confirmed",
231 CommitmentLevel::Finalized => "finalized",
232 };
233 write!(f, "{}", s)
234 }
235}
236
237#[derive(Error, Debug)]
238pub enum ParseCommitmentLevelError {
239 #[error("invalid variant")]
240 Invalid,
241}