drive/verify/voting/verify_identity_votes_given_proof/
mod.rs

1mod v0;
2
3use crate::error::drive::DriveError;
4use crate::query::ContractLookupFn;
5use crate::verify::RootHash;
6use dpp::identifier::Identifier;
7
8use crate::error::Error;
9
10use crate::query::contested_resource_votes_given_by_identity_query::ContestedResourceVotesGivenByIdentityQuery;
11use dpp::version::PlatformVersion;
12use dpp::voting::votes::resource_vote::ResourceVote;
13
14impl ContestedResourceVotesGivenByIdentityQuery {
15    /// Verifies a proof for the vote poll vote state proof.
16    ///
17    /// This function verifies a given serialized proof and returns the root hash along with a collection of deserialized identifiers and their corresponding resource votes.
18    ///
19    /// # Arguments
20    ///
21    /// * `proof` - A byte slice representing the serialized proof to be verified.
22    /// * `contract_lookup_fn` - Function that retrieves data contract based on its identifier.
23    /// * `platform_version` - A reference to the platform version to be used for verification.
24    ///
25    /// # Returns
26    ///
27    /// A `Result` containing:
28    /// * A tuple with the root hash and a collection of `(Identifier, ResourceVote)` pairs if the proof is valid. The collection type is flexible and determined by the generic parameter `I`.
29    /// * An `Error` variant if the proof verification fails or a deserialization error occurs.
30    ///
31    /// # Errors
32    ///
33    /// This function will return an `Error` if:
34    /// * The proof verification fails.
35    /// * A deserialization error occurs when parsing the serialized document(s).
36    pub fn verify_identity_votes_given_proof<I>(
37        &self,
38        proof: &[u8],
39        contract_lookup_fn: &ContractLookupFn,
40        platform_version: &PlatformVersion,
41    ) -> Result<(RootHash, I), Error>
42    where
43        I: FromIterator<(Identifier, ResourceVote)>,
44    {
45        match platform_version
46            .drive
47            .methods
48            .verify
49            .voting
50            .verify_identity_votes_given_proof
51        {
52            0 => self.verify_identity_votes_given_proof_v0(
53                proof,
54                contract_lookup_fn,
55                platform_version,
56            ),
57            version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
58                method: "verify_identity_votes_given_proof".to_string(),
59                known_versions: vec![0],
60                received: version,
61            })),
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::error::drive::DriveError;
70    use crate::error::Error;
71    use dpp::version::PlatformVersion;
72    use std::collections::BTreeMap;
73
74    #[test]
75    fn test_verify_identity_votes_given_proof_unknown_version() {
76        let mut platform_version = PlatformVersion::latest().clone();
77        platform_version
78            .drive
79            .methods
80            .verify
81            .voting
82            .verify_identity_votes_given_proof = 255;
83
84        let query = ContestedResourceVotesGivenByIdentityQuery {
85            identity_id: Identifier::default(),
86            offset: None,
87            limit: None,
88            start_at: None,
89            order_ascending: true,
90        };
91
92        let contract_lookup_fn: &ContractLookupFn = &|_id| Ok(None);
93
94        let result: Result<(RootHash, BTreeMap<Identifier, ResourceVote>), Error> =
95            query.verify_identity_votes_given_proof(&[], contract_lookup_fn, &platform_version);
96
97        assert!(
98            matches!(result, Err(Error::Drive(DriveError::UnknownVersionMismatch { method, known_versions, received }))
99                if method == "verify_identity_votes_given_proof" && known_versions == vec![0] && received == 255
100            )
101        );
102    }
103}