dpp/voting/contender_structs/
mod.rs

1mod contender;
2
3use crate::data_contract::document_type::DocumentTypeRef;
4use crate::document::serialization_traits::DocumentPlatformConversionMethodsV0;
5use crate::document::Document;
6use crate::voting::vote_choices::resource_vote_choice::ResourceVoteChoice;
7use crate::ProtocolError;
8use bincode::{Decode, Encode};
9use platform_value::Identifier;
10use platform_version::version::PlatformVersion;
11use std::fmt;
12
13pub use contender::v0::{ContenderV0, ContenderWithSerializedDocumentV0};
14pub use contender::{Contender, ContenderWithSerializedDocument};
15
16/// Represents a finalized contender in the contested document vote poll.
17/// This is for internal use where the document is in serialized form
18///
19/// This struct holds the identity ID of the contender, the serialized document,
20/// and the vote tally.
21#[derive(Debug, PartialEq, Eq, Clone, Default)]
22pub struct FinalizedContenderWithSerializedDocument {
23    /// The identity ID of the contender.
24    pub identity_id: Identifier,
25    /// The serialized document associated with the contender.
26    pub serialized_document: Vec<u8>,
27    /// The vote tally for the contender.
28    pub final_vote_tally: u32,
29}
30
31/// Represents a finalized contender in the contested document vote poll.
32/// This is for keeping information about previous vote polls
33///
34/// This struct holds the identity ID of the contender, the serialized document,
35/// and the vote tally.
36#[derive(Debug, PartialEq, Eq, Clone, Default, Encode, Decode)]
37pub struct FinalizedResourceVoteChoicesWithVoterInfo {
38    /// The resource vote choice.
39    pub resource_vote_choice: ResourceVoteChoice,
40    /// The pro_tx_hashes of the voters for this contender along with their strength
41    pub voters: Vec<(Identifier, u8)>,
42}
43impl fmt::Display for FinalizedResourceVoteChoicesWithVoterInfo {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        let voters_str: Vec<String> = self
46            .voters
47            .iter()
48            .map(|(id, strength)| format!("{}:{}", id, strength))
49            .collect();
50        write!(
51            f,
52            "FinalizedResourceVoteChoicesWithVoterInfo {{ resource_vote_choice: {}, voters: [{}] }}",
53            self.resource_vote_choice,
54            voters_str.join(", ")
55        )
56    }
57}
58
59/// Represents a finalized contender in the contested document vote poll.
60/// This is for internal use where the document is in serialized form
61///
62/// This struct holds the identity ID of the contender, the document,
63/// and the vote tally.
64#[derive(Debug, PartialEq, Clone)]
65pub struct FinalizedContender {
66    /// The identity ID of the contender.
67    pub identity_id: Identifier,
68    /// The document associated with the contender.
69    pub document: Document,
70    /// The still serialized document
71    pub serialized_document: Vec<u8>,
72    /// The vote tally for the contender.
73    pub final_vote_tally: u32,
74}
75
76impl FinalizedContender {
77    /// Try to get the finalized contender from a finalized contender with a serialized document
78    pub fn try_from_contender_with_serialized_document(
79        value: FinalizedContenderWithSerializedDocument,
80        document_type: DocumentTypeRef,
81        platform_version: &PlatformVersion,
82    ) -> Result<Self, ProtocolError> {
83        let FinalizedContenderWithSerializedDocument {
84            identity_id,
85            serialized_document,
86            final_vote_tally,
87        } = value;
88
89        Ok(FinalizedContender {
90            identity_id,
91            document: Document::from_bytes(&serialized_document, document_type, platform_version)?,
92            serialized_document,
93            final_vote_tally,
94        })
95    }
96}
97
98impl TryFrom<ContenderWithSerializedDocument> for FinalizedContenderWithSerializedDocument {
99    type Error = ProtocolError;
100
101    fn try_from(value: ContenderWithSerializedDocument) -> Result<Self, Self::Error> {
102        let (identity_id, serialized_document, vote_tally) = match value {
103            ContenderWithSerializedDocument::V0(v0) => {
104                let ContenderWithSerializedDocumentV0 {
105                    identity_id,
106                    serialized_document,
107                    vote_tally,
108                } = v0;
109                (identity_id, serialized_document, vote_tally)
110            }
111        };
112
113        Ok(FinalizedContenderWithSerializedDocument {
114            identity_id,
115            serialized_document: serialized_document.ok_or(
116                ProtocolError::CorruptedCodeExecution("expected serialized document".to_string()),
117            )?,
118            final_vote_tally: vote_tally.ok_or(ProtocolError::CorruptedCodeExecution(
119                "expected vote tally".to_string(),
120            ))?,
121        })
122    }
123}