espresso_types/v0/
nsproof.rs

1use hotshot_query_service::VidCommon;
2use hotshot_types::data::VidCommitment;
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    v0::{NamespaceId, NsIndex, NsPayload, NsTable, Payload, Transaction},
7    v0_1::ADVZNsProof,
8    v0_3::AvidMNsProof,
9};
10
11#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
12pub struct NamespaceProofQueryData {
13    pub proof: Option<NsProof>,
14    pub transactions: Vec<Transaction>,
15}
16
17#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
18pub struct ADVZNamespaceProofQueryData {
19    pub proof: Option<ADVZNsProof>,
20    pub transactions: Vec<Transaction>,
21}
22
23/// Each variant represents a specific version of a namespace proof.
24#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25pub enum NsProof {
26    V0(ADVZNsProof),
27    V1(AvidMNsProof),
28}
29
30impl NsProof {
31    pub fn new(payload: &Payload, index: &NsIndex, common: &VidCommon) -> Option<NsProof> {
32        match common {
33            VidCommon::V0(common) => Some(NsProof::V0(ADVZNsProof::new(payload, index, common)?)),
34            VidCommon::V1(common) => Some(NsProof::V1(AvidMNsProof::new(payload, index, common)?)),
35        }
36    }
37
38    pub fn verify(
39        &self,
40        ns_table: &NsTable,
41        commit: &VidCommitment,
42        common: &VidCommon,
43    ) -> Option<(Vec<Transaction>, NamespaceId)> {
44        match (self, common) {
45            (Self::V0(proof), VidCommon::V0(common)) => proof.verify(ns_table, commit, common),
46            (Self::V1(proof), VidCommon::V1(common)) => proof.verify(ns_table, commit, common),
47            _ => {
48                tracing::error!("Incompatible version of VidCommon and NsProof.");
49                None
50            },
51        }
52    }
53
54    pub fn export_all_txs(&self, ns_id: &NamespaceId) -> Vec<Transaction> {
55        match self {
56            Self::V0(proof) => proof.export_all_txs(ns_id),
57            Self::V1(proof) => {
58                NsPayload::from_bytes_slice(&proof.0.ns_payload).export_all_txs(ns_id)
59            },
60        }
61    }
62}