reth_db_api/models/
snap.rs1use alloy_eips::BlockNumHash;
4use alloy_primitives::B256;
5use core::fmt;
6use serde::{Deserialize, Serialize};
7
8pub const SNAP_ATTEMPT_VERSION: u32 = 1;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17pub struct SnapAttempt {
18 version: u32,
20 id: SnapAttemptId,
22 pivot: BlockNumHash,
24 state_root: B256,
26 state_version: u64,
28 status: SnapBootstrapStatus,
30}
31
32impl SnapAttempt {
33 pub const fn start(previous: Option<Self>, pivot: BlockNumHash, state_root: B256) -> Self {
35 let id = match previous {
36 Some(previous) => previous.id.next(),
37 None => SnapAttemptId::FIRST,
38 };
39 Self {
40 version: SNAP_ATTEMPT_VERSION,
41 id,
42 pivot,
43 state_root,
44 state_version: 0,
45 status: SnapBootstrapStatus::Unfinished,
46 }
47 }
48
49 pub const fn id(&self) -> SnapAttemptId {
51 self.id
52 }
53
54 pub const fn pivot(&self) -> BlockNumHash {
56 self.pivot
57 }
58
59 pub const fn state_root(&self) -> B256 {
61 self.state_root
62 }
63
64 pub const fn state_version(&self) -> u64 {
66 self.state_version
67 }
68
69 pub const fn is_unfinished(&self) -> bool {
71 matches!(self.status, SnapBootstrapStatus::Unfinished)
72 }
73
74 pub const fn is_verified(&self) -> bool {
76 matches!(self.status, SnapBootstrapStatus::Verified)
77 }
78
79 pub const fn re_anchor(&mut self, pivot: BlockNumHash, state_root: B256) {
81 self.pivot = pivot;
82 self.state_root = state_root;
83 self.state_version = self.state_version.saturating_add(1);
84 }
85
86 pub const fn verify(&mut self) {
88 self.status = SnapBootstrapStatus::Verified;
89 }
90
91 pub const fn abandon(&mut self) {
93 self.status = SnapBootstrapStatus::Abandoned;
94 }
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
102pub struct SnapAttemptId(u64);
103
104impl SnapAttemptId {
105 pub const FIRST: Self = Self(0);
107
108 pub const fn next(self) -> Self {
110 Self(self.0.saturating_add(1))
111 }
112}
113
114impl From<SnapAttemptId> for u64 {
115 fn from(id: SnapAttemptId) -> Self {
116 id.0
117 }
118}
119
120impl fmt::Display for SnapAttemptId {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "{}", self.0)
123 }
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
128enum SnapBootstrapStatus {
129 Unfinished,
131 Verified,
133 Abandoned,
135}