snark/user_command_verify/
snark_user_command_verify_state.rs

1use std::sync::Arc;
2
3use ledger::scan_state::transaction_logic::{valid, verifiable, WithStatus};
4use redux::Callback;
5use serde::{Deserialize, Serialize};
6
7use openmina_core::{requests::PendingRequests, transaction::TransactionPoolMessageSource};
8
9use crate::{TransactionVerifier, VerifierSRS};
10
11use super::{SnarkUserCommandVerifyError, SnarkUserCommandVerifyId, SnarkUserCommandVerifyIdType};
12
13#[derive(Serialize, Deserialize, Clone)]
14pub struct SnarkUserCommandVerifyState {
15    pub verifier_index: TransactionVerifier,
16    pub verifier_srs: Arc<VerifierSRS>,
17    pub jobs: PendingRequests<SnarkUserCommandVerifyIdType, SnarkUserCommandVerifyStatus>,
18}
19
20impl SnarkUserCommandVerifyState {
21    pub fn new(verifier_index: TransactionVerifier, verifier_srs: Arc<VerifierSRS>) -> Self {
22        Self {
23            verifier_index,
24            verifier_srs,
25            jobs: Default::default(),
26        }
27    }
28
29    pub fn next_req_id(&self) -> SnarkUserCommandVerifyId {
30        self.jobs.next_req_id()
31    }
32}
33
34impl std::fmt::Debug for SnarkUserCommandVerifyState {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        f.debug_struct("SnarkUserCommandVerifyState")
37            // TODO(binier): display hashes instead.
38            .field("verifier_index", &"<content too big>")
39            .field("verifier_srs", &"<content too big>")
40            .field("jobs", &self.jobs)
41            .finish()
42    }
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone)]
46pub enum SnarkUserCommandVerifyStatus {
47    Init {
48        time: redux::Timestamp,
49        commands: Vec<WithStatus<verifiable::UserCommand>>,
50        from_source: TransactionPoolMessageSource,
51        on_success: super::OnSuccess,
52        on_error: Callback<(SnarkUserCommandVerifyId, Vec<String>)>,
53    },
54    Pending {
55        time: redux::Timestamp,
56        commands: Vec<WithStatus<verifiable::UserCommand>>,
57        from_source: TransactionPoolMessageSource,
58        on_success: super::OnSuccess,
59        on_error: Callback<(SnarkUserCommandVerifyId, Vec<String>)>,
60    },
61    Error {
62        time: redux::Timestamp,
63        commands: Vec<WithStatus<verifiable::UserCommand>>,
64        error: SnarkUserCommandVerifyError,
65    },
66    Success {
67        time: redux::Timestamp,
68        commands: Vec<valid::UserCommand>,
69    },
70}
71
72impl SnarkUserCommandVerifyStatus {
73    pub fn is_init(&self) -> bool {
74        matches!(self, Self::Init { .. })
75    }
76
77    pub fn is_pending(&self) -> bool {
78        matches!(self, Self::Pending { .. })
79    }
80
81    pub fn is_finished(&self) -> bool {
82        matches!(self, Self::Error { .. } | Self::Success { .. })
83    }
84}