drive/state_transition_action/identity/masternode_vote/
transformer.rs

1use crate::drive::Drive;
2use crate::error::Error;
3use crate::state_transition_action::identity::masternode_vote::v0::{
4    MasternodeVoteTransitionActionV0, PreviousVoteCount,
5};
6use crate::state_transition_action::identity::masternode_vote::MasternodeVoteTransitionAction;
7use dpp::state_transition::masternode_vote_transition::MasternodeVoteTransition;
8use dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice;
9use grovedb::TransactionArg;
10use platform_version::version::PlatformVersion;
11
12impl MasternodeVoteTransitionAction {
13    /// Transforms an owned `MasternodeVoteTransition` into a `MasternodeVoteTransitionAction`.
14    ///
15    /// # Parameters
16    ///
17    /// - `value`: The owned `MasternodeVoteTransition` to transform.
18    /// - `voter_identity_id`: The pre-calculated voter identity id, if it isn't given we will calculate it
19    /// - `masternode_strength`: The strength of the masternode, normal ones have 1, evonodes have 4
20    /// - `drive`: A reference to the `Drive` instance.
21    /// - `transaction`: The transaction argument.
22    /// - `platform_version`: A reference to the platform version.
23    ///
24    /// # Returns
25    ///
26    /// A `Result` containing the transformed `MasternodeVoteTransitionAction`, or an `Error` if the transformation fails.
27    pub fn transform_from_owned_transition(
28        value: MasternodeVoteTransition,
29        voting_address: [u8; 20],
30        masternode_strength: u8,
31        previous_resource_vote_choice_to_remove: Option<(ResourceVoteChoice, PreviousVoteCount)>,
32        drive: &Drive,
33        transaction: TransactionArg,
34        platform_version: &PlatformVersion,
35    ) -> Result<Self, Error> {
36        match value {
37            MasternodeVoteTransition::V0(v0) => Ok(
38                MasternodeVoteTransitionActionV0::transform_from_owned_transition(
39                    v0,
40                    voting_address,
41                    masternode_strength,
42                    previous_resource_vote_choice_to_remove,
43                    drive,
44                    transaction,
45                    platform_version,
46                )?
47                .into(),
48            ),
49        }
50    }
51
52    /// Transforms a borrowed `MasternodeVoteTransition` into a `MasternodeVoteTransitionAction`.
53    ///
54    /// # Parameters
55    ///
56    /// - `value`: A reference to the `MasternodeVoteTransition` to transform.
57    /// - `voter_identity_id`: The pre-calculated voter identity id, if it isn't given we will calculate it
58    /// - `masternode_strength`: The strength of the masternode, normal ones have 1, evonodes have 4
59    /// - `drive`: A reference to the `Drive` instance.
60    /// - `transaction`: The transaction argument.
61    /// - `platform_version`: A reference to the platform version.
62    ///
63    /// # Returns
64    ///
65    /// A `Result` containing the transformed `MasternodeVoteTransitionAction`, or an `Error` if the transformation fails.
66    pub fn transform_from_transition(
67        value: &MasternodeVoteTransition,
68        voting_address: [u8; 20],
69        masternode_strength: u8,
70        previous_resource_vote_choice_to_remove: Option<(ResourceVoteChoice, PreviousVoteCount)>,
71        drive: &Drive,
72        transaction: TransactionArg,
73        platform_version: &PlatformVersion,
74    ) -> Result<Self, Error> {
75        match value {
76            MasternodeVoteTransition::V0(v0) => {
77                Ok(MasternodeVoteTransitionActionV0::transform_from_transition(
78                    v0,
79                    voting_address,
80                    masternode_strength,
81                    previous_resource_vote_choice_to_remove,
82                    drive,
83                    transaction,
84                    platform_version,
85                )?
86                .into())
87            }
88        }
89    }
90}