drive/verify/voting/verify_vote_poll_votes_proof/
mod.rs

1mod v0;
2
3use crate::error::drive::DriveError;
4use crate::verify::RootHash;
5use dpp::identifier::Identifier;
6
7use crate::error::Error;
8
9use crate::query::vote_poll_contestant_votes_query::ResolvedContestedDocumentVotePollVotesDriveQuery;
10use dpp::version::PlatformVersion;
11
12impl ResolvedContestedDocumentVotePollVotesDriveQuery<'_> {
13    /// Verifies a proof for the vote poll vote state proof.
14    ///
15    /// This function takes a byte slice representing the serialized proof, verifies it, and returns a tuple consisting of the root hash
16    /// and a vector of deserialized contenders.
17    ///
18    /// # Arguments
19    ///
20    /// * `proof` - A byte slice representing the proof to be verified.
21    /// * `platform_version` - The platform version against which to verify the proof.
22    ///
23    /// # Returns
24    ///
25    /// A `Result` containing:
26    /// * A tuple with the root hash and a vector of deserialized `Document`s if the proof is valid.
27    /// * An `Error` variant, in case the proof verification fails or a deserialization error occurs.
28    ///
29    /// # Errors
30    ///
31    /// This function will return an `Error` variant if:
32    /// 1. The proof verification fails.
33    /// 2. A deserialization error occurs when parsing the serialized document(s).
34    pub fn verify_vote_poll_votes_proof(
35        &self,
36        proof: &[u8],
37        platform_version: &PlatformVersion,
38    ) -> Result<(RootHash, Vec<Identifier>), Error> {
39        match platform_version
40            .drive
41            .methods
42            .verify
43            .voting
44            .verify_vote_poll_votes_proof
45        {
46            0 => self.verify_vote_poll_votes_proof_v0(proof, platform_version),
47            version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
48                method: "verify_vote_poll_votes_proof".to_string(),
49                known_versions: vec![0],
50                received: version,
51            })),
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfoAllowBorrowed;
60    use crate::error::drive::DriveError;
61    use crate::error::Error;
62    use crate::util::object_size_info::DataContractResolvedInfo;
63    use dpp::tests::json_document::json_document_to_contract;
64    use dpp::version::PlatformVersion;
65    use std::sync::Arc;
66
67    #[test]
68    fn test_verify_vote_poll_votes_proof_unknown_version() {
69        let platform_version = PlatformVersion::latest();
70        let data_contract = json_document_to_contract(
71            "tests/supporting_files/contract/dpns/dpns-contract.json",
72            false,
73            platform_version,
74        )
75        .expect("expected to create a data contract");
76
77        let query = ResolvedContestedDocumentVotePollVotesDriveQuery {
78            vote_poll: ContestedDocumentResourceVotePollWithContractInfoAllowBorrowed {
79                contract: DataContractResolvedInfo::ArcDataContract(Arc::new(data_contract)),
80                document_type_name: String::new(),
81                index_name: String::new(),
82                index_values: vec![],
83            },
84            contestant_id: Identifier::default(),
85            offset: None,
86            limit: None,
87            start_at: None,
88            order_ascending: true,
89        };
90
91        let mut platform_version = platform_version.clone();
92        platform_version
93            .drive
94            .methods
95            .verify
96            .voting
97            .verify_vote_poll_votes_proof = 255;
98
99        let result = query.verify_vote_poll_votes_proof(&[], &platform_version);
100
101        assert!(
102            matches!(result, Err(Error::Drive(DriveError::UnknownVersionMismatch { method, known_versions, received }))
103                if method == "verify_vote_poll_votes_proof" && known_versions == vec![0] && received == 255
104            )
105        );
106    }
107}