snark/work_verify/
snark_work_verify_state.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5use openmina_core::{
6    requests::PendingRequests,
7    snark::{Snark, SnarkJobId},
8};
9
10use crate::{TransactionVerifier, VerifierSRS};
11
12use super::{SnarkWorkVerifyError, SnarkWorkVerifyId, SnarkWorkVerifyIdType};
13
14#[derive(Serialize, Deserialize, Clone)]
15pub struct SnarkWorkVerifyState {
16    pub verifier_index: TransactionVerifier,
17    pub verifier_srs: Arc<VerifierSRS>,
18    pub jobs: PendingRequests<SnarkWorkVerifyIdType, SnarkWorkVerifyStatus>,
19}
20
21impl SnarkWorkVerifyState {
22    pub fn new(verifier_index: TransactionVerifier, verifier_srs: Arc<VerifierSRS>) -> Self {
23        Self {
24            verifier_index,
25            verifier_srs,
26            jobs: Default::default(),
27        }
28    }
29
30    pub fn next_req_id(&self) -> SnarkWorkVerifyId {
31        self.jobs.next_req_id()
32    }
33}
34
35impl std::fmt::Debug for SnarkWorkVerifyState {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("SnarkWorkVerifyState")
38            // TODO(binier): display hashes instead.
39            .field("verifier_index", &"<content too big>")
40            .field("verifier_srs", &"<content too big>")
41            .field("jobs", &self.jobs)
42            .finish()
43    }
44}
45
46#[derive(Serialize, Deserialize, Debug, Clone)]
47pub enum SnarkWorkVerifyStatus {
48    Init {
49        time: redux::Timestamp,
50        batch: Vec<Snark>,
51        // TODO(binier): move p2p/src/identity to shared crate and use
52        // `PeerId` here.
53        sender: String,
54        on_success: redux::Callback<(SnarkWorkVerifyId, String, Vec<Snark>)>,
55        on_error: redux::Callback<(SnarkWorkVerifyId, String, Vec<SnarkJobId>)>,
56    },
57    Pending {
58        time: redux::Timestamp,
59        batch: Vec<Snark>,
60        sender: String,
61        on_success: redux::Callback<(SnarkWorkVerifyId, String, Vec<Snark>)>,
62        on_error: redux::Callback<(SnarkWorkVerifyId, String, Vec<SnarkJobId>)>,
63    },
64    Error {
65        time: redux::Timestamp,
66        batch: Vec<Snark>,
67        sender: String,
68        error: SnarkWorkVerifyError,
69    },
70    Success {
71        time: redux::Timestamp,
72        batch: Vec<Snark>,
73        sender: String,
74    },
75}
76
77impl SnarkWorkVerifyStatus {
78    pub fn is_init(&self) -> bool {
79        matches!(self, Self::Init { .. })
80    }
81
82    pub fn is_pending(&self) -> bool {
83        matches!(self, Self::Pending { .. })
84    }
85
86    pub fn is_finished(&self) -> bool {
87        matches!(self, Self::Error { .. } | Self::Success { .. })
88    }
89
90    pub fn batch(&self) -> &[Snark] {
91        match self {
92            Self::Init { batch, .. } => batch,
93            Self::Pending { batch, .. } => batch,
94            Self::Error { batch, .. } => batch,
95            Self::Success { batch, .. } => batch,
96        }
97    }
98
99    pub fn sender(&self) -> &str {
100        match self {
101            Self::Init { sender, .. } => sender,
102            Self::Pending { sender, .. } => sender,
103            Self::Error { sender, .. } => sender,
104            Self::Success { sender, .. } => sender,
105        }
106    }
107}