zksvm_api_types/
common.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4use solana_sdk::{account::Account, pubkey::Pubkey};
5use uuid::Uuid;
6
7#[derive(Debug, Copy, Clone, Deserialize, Serialize, Hash, Eq, PartialEq, PartialOrd, Ord)]
8#[serde(transparent)]
9pub struct PubkeyWrapper {
10    #[serde(with = "svm_engine_keys::pubkey::single")]
11    inner: Pubkey,
12}
13
14impl From<&PubkeyWrapper> for Pubkey {
15    fn from(wrapper: &PubkeyWrapper) -> Self {
16        wrapper.inner
17    }
18}
19
20impl From<&Pubkey> for PubkeyWrapper {
21    fn from(inner: &Pubkey) -> Self {
22        Self { inner: *inner }
23    }
24}
25
26#[derive(Deserialize, Serialize, Clone, Debug)]
27#[serde(rename_all = "camelCase")]
28pub struct AccountProxy {
29    pub lamports: u64,
30    pub data: Vec<u8>,
31    pub owner: PubkeyWrapper,
32    pub executable: bool,
33    pub rent_epoch: u64,
34}
35
36impl From<&AccountProxy> for Account {
37    fn from(proxy: &AccountProxy) -> Self {
38        let AccountProxy {
39            lamports,
40            data,
41            owner,
42            executable,
43            rent_epoch,
44        } = proxy;
45        Account {
46            lamports: *lamports,
47            data: data.clone(),
48            owner: owner.into(),
49            executable: *executable,
50            rent_epoch: *rent_epoch,
51        }
52    }
53}
54
55impl From<&Account> for AccountProxy {
56    fn from(account: &Account) -> Self {
57        let Account {
58            lamports,
59            data,
60            owner,
61            executable,
62            rent_epoch,
63        } = account;
64        AccountProxy {
65            lamports: *lamports,
66            data: data.clone(),
67            owner: owner.into(),
68            executable: *executable,
69            rent_epoch: *rent_epoch,
70        }
71    }
72}
73
74#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq, sqlx::Type)]
75#[serde(rename_all = "lowercase")]
76#[sqlx(type_name = "proof_type", rename_all = "lowercase")]
77pub enum ProofType {
78    Core,
79    Compressed,
80    Groth16,
81}
82
83impl Display for ProofType {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(
86            f,
87            "{}",
88            serde_json::to_string(self).unwrap().trim_matches('"')
89        )
90    }
91}
92
93#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq, sqlx::Type)]
94#[serde(rename_all = "snake_case")]
95#[sqlx(type_name = "proof_state", rename_all = "snake_case")]
96pub enum ProofState {
97    Uninitiated,
98    InProgress,
99    Success,
100    Failed,
101}
102
103#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, Clone)]
104pub struct ZKProof {
105    #[sqlx(rename = "proof_state")]
106    pub state: ProofState,
107    #[sqlx(rename = "proof_type")]
108    pub sp1_type: Option<ProofType>,
109    // this is storing milliseconds
110    pub proving_time: Option<i32>,
111    pub cycle_count: Option<i64>,
112    pub proof: Option<Vec<u8>>,
113    pub verification_key: Option<Vec<u8>>,
114}
115
116impl Default for ZKProof {
117    fn default() -> Self {
118        Self {
119            state: ProofState::InProgress,
120            sp1_type: None,
121            proving_time: None,
122            cycle_count: None,
123            proof: None,
124            verification_key: None,
125        }
126    }
127}
128
129impl ZKProof {
130    pub fn failed(proof_type: ProofType) -> Self {
131        Self {
132            state: ProofState::Failed,
133            sp1_type: Some(proof_type),
134            proving_time: None,
135            cycle_count: None,
136            proof: None,
137            verification_key: None,
138        }
139    }
140}
141
142#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
143pub struct Session {
144    pub session_id: Uuid,
145    pub api_key: Uuid,
146    pub start_time: chrono::NaiveDateTime,
147    pub end_time: Option<chrono::NaiveDateTime>,
148    #[sqlx(flatten)]
149    pub proof: ZKProof,
150}
151
152impl Session {
153    pub fn is_initiated(&self) -> bool {
154        self.proof.state != ProofState::Uninitiated
155    }
156}