gemachain_sdk/
commitment_config.rs

1#![allow(deprecated)]
2#![cfg(feature = "full")]
3
4use std::str::FromStr;
5use thiserror::Error;
6
7#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
8#[serde(rename_all = "camelCase")]
9pub struct CommitmentConfig {
10    pub commitment: CommitmentLevel,
11}
12
13impl CommitmentConfig {
14    #[deprecated(
15        since = "1.5.5",
16        note = "Please use CommitmentConfig::processed() instead"
17    )]
18    pub fn recent() -> Self {
19        Self {
20            commitment: CommitmentLevel::Recent,
21        }
22    }
23
24    #[deprecated(
25        since = "1.5.5",
26        note = "Please use CommitmentConfig::finalized() instead"
27    )]
28    pub fn max() -> Self {
29        Self {
30            commitment: CommitmentLevel::Max,
31        }
32    }
33
34    #[deprecated(
35        since = "1.5.5",
36        note = "Please use CommitmentConfig::finalized() instead"
37    )]
38    pub fn root() -> Self {
39        Self {
40            commitment: CommitmentLevel::Root,
41        }
42    }
43
44    #[deprecated(
45        since = "1.5.5",
46        note = "Please use CommitmentConfig::confirmed() instead"
47    )]
48    pub fn single() -> Self {
49        Self {
50            commitment: CommitmentLevel::Single,
51        }
52    }
53
54    #[deprecated(
55        since = "1.5.5",
56        note = "Please use CommitmentConfig::confirmed() instead"
57    )]
58    pub fn single_gossip() -> Self {
59        Self {
60            commitment: CommitmentLevel::SingleGossip,
61        }
62    }
63
64    pub fn finalized() -> Self {
65        Self {
66            commitment: CommitmentLevel::Finalized,
67        }
68    }
69
70    pub fn confirmed() -> Self {
71        Self {
72            commitment: CommitmentLevel::Confirmed,
73        }
74    }
75
76    pub fn processed() -> Self {
77        Self {
78            commitment: CommitmentLevel::Processed,
79        }
80    }
81
82    pub fn ok(self) -> Option<Self> {
83        if self == Self::default() {
84            None
85        } else {
86            Some(self)
87        }
88    }
89
90    pub fn is_finalized(&self) -> bool {
91        matches!(
92            &self.commitment,
93            CommitmentLevel::Finalized | CommitmentLevel::Max | CommitmentLevel::Root
94        )
95    }
96
97    pub fn is_confirmed(&self) -> bool {
98        matches!(
99            &self.commitment,
100            CommitmentLevel::Confirmed | CommitmentLevel::SingleGossip | CommitmentLevel::Single
101        )
102    }
103
104    pub fn is_processed(&self) -> bool {
105        matches!(
106            &self.commitment,
107            CommitmentLevel::Processed | CommitmentLevel::Recent
108        )
109    }
110
111    pub fn is_at_least_confirmed(&self) -> bool {
112        self.is_confirmed() || self.is_finalized()
113    }
114
115    pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
116        match commitment.commitment {
117            CommitmentLevel::Finalized => CommitmentConfig::max(),
118            CommitmentLevel::Confirmed => CommitmentConfig::single_gossip(),
119            CommitmentLevel::Processed => CommitmentConfig::recent(),
120            _ => commitment,
121        }
122    }
123}
124
125impl FromStr for CommitmentConfig {
126    type Err = ParseCommitmentLevelError;
127
128    fn from_str(s: &str) -> Result<Self, Self::Err> {
129        CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
130    }
131}
132
133#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
134#[serde(rename_all = "camelCase")]
135/// An attribute of a slot. It describes how finalized a block is at some point in time. For example, a slot
136/// is said to be at the max level immediately after the cluster recognizes the block at that slot as
137/// finalized. When querying the ledger state, use lower levels of commitment to report progress and higher
138/// levels to ensure state changes will not be rolled back.
139pub enum CommitmentLevel {
140    /// (DEPRECATED) The highest slot having reached max vote lockout, as recognized by a supermajority of the cluster.
141    #[deprecated(
142        since = "1.5.5",
143        note = "Please use CommitmentLevel::Finalized instead"
144    )]
145    Max,
146
147    /// (DEPRECATED) The highest slot of the heaviest fork. Ledger state at this slot is not derived from a finalized
148    /// block, but if multiple forks are present, is from the fork the validator believes is most likely
149    /// to finalize.
150    #[deprecated(
151        since = "1.5.5",
152        note = "Please use CommitmentLevel::Processed instead"
153    )]
154    Recent,
155
156    /// (DEPRECATED) The highest slot having reached max vote lockout.
157    #[deprecated(
158        since = "1.5.5",
159        note = "Please use CommitmentLevel::Finalized instead"
160    )]
161    Root,
162
163    /// (DEPRECATED) The highest slot having reached 1 confirmation by supermajority of the cluster.
164    #[deprecated(
165        since = "1.5.5",
166        note = "Please use CommitmentLevel::Confirmed instead"
167    )]
168    Single,
169
170    /// (DEPRECATED) The highest slot that has been voted on by supermajority of the cluster
171    /// This differs from `single` in that:
172    /// 1) It incorporates votes from gossip and replay.
173    /// 2) It does not count votes on descendants of a block, only direct votes on that block.
174    /// 3) This confirmation level also upholds "optimistic confirmation" guarantees in
175    /// release 1.3 and onwards.
176    #[deprecated(
177        since = "1.5.5",
178        note = "Please use CommitmentLevel::Confirmed instead"
179    )]
180    SingleGossip,
181
182    /// The highest slot of the heaviest fork processed by the node. Ledger state at this slot is
183    /// not derived from a confirmed or finalized block, but if multiple forks are present, is from
184    /// the fork the validator believes is most likely to finalize.
185    Processed,
186
187    /// The highest slot that has been voted on by supermajority of the cluster, ie. is confirmed.
188    /// Confirmation incorporates votes from gossip and replay. It does not count votes on
189    /// descendants of a block, only direct votes on that block, and upholds "optimistic
190    /// confirmation" guarantees in release 1.3 and onwards.
191    Confirmed,
192
193    /// The highest slot having reached max vote lockout, as recognized by a supermajority of the
194    /// cluster.
195    Finalized,
196}
197
198impl Default for CommitmentLevel {
199    fn default() -> Self {
200        Self::Finalized
201    }
202}
203
204impl FromStr for CommitmentLevel {
205    type Err = ParseCommitmentLevelError;
206
207    fn from_str(s: &str) -> Result<Self, Self::Err> {
208        match s {
209            "max" => Ok(CommitmentLevel::Max),
210            "recent" => Ok(CommitmentLevel::Recent),
211            "root" => Ok(CommitmentLevel::Root),
212            "single" => Ok(CommitmentLevel::Single),
213            "singleGossip" => Ok(CommitmentLevel::SingleGossip),
214            "processed" => Ok(CommitmentLevel::Processed),
215            "confirmed" => Ok(CommitmentLevel::Confirmed),
216            "finalized" => Ok(CommitmentLevel::Finalized),
217            _ => Err(ParseCommitmentLevelError::Invalid),
218        }
219    }
220}
221
222impl std::fmt::Display for CommitmentLevel {
223    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
224        let s = match self {
225            CommitmentLevel::Max => "max",
226            CommitmentLevel::Recent => "recent",
227            CommitmentLevel::Root => "root",
228            CommitmentLevel::Single => "single",
229            CommitmentLevel::SingleGossip => "singleGossip",
230            CommitmentLevel::Processed => "processed",
231            CommitmentLevel::Confirmed => "confirmed",
232            CommitmentLevel::Finalized => "finalized",
233        };
234        write!(f, "{}", s)
235    }
236}
237
238#[derive(Error, Debug)]
239pub enum ParseCommitmentLevelError {
240    #[error("invalid variant")]
241    Invalid,
242}