LCOV - code coverage report
Current view: top level - src/governance - vote.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 29 30 96.7 %
Date: 2026-06-25 07:23:43 Functions: 25 27 92.6 %

          Line data    Source code
       1             : // Copyright (c) 2014-2025 The Dash Core developers
       2             : // Distributed under the MIT/X11 software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_GOVERNANCE_VOTE_H
       6             : #define BITCOIN_GOVERNANCE_VOTE_H
       7             : 
       8             : #include <hash.h>
       9             : #include <primitives/transaction.h>
      10             : #include <uint256.h>
      11             : #include <util/string.h>
      12             : 
      13             : class CBLSPublicKey;
      14             : class CDeterministicMNList;
      15             : class CKeyID;
      16             : 
      17             : // INTENTION OF MASTERNODES REGARDING ITEM
      18             : enum vote_outcome_enum_t : int {
      19             :     VOTE_OUTCOME_NONE = 0,
      20             :     VOTE_OUTCOME_YES,
      21             :     VOTE_OUTCOME_NO,
      22             :     VOTE_OUTCOME_ABSTAIN,
      23             :     VOTE_OUTCOME_UNKNOWN
      24             : };
      25             : template<> struct is_serializable_enum<vote_outcome_enum_t> : std::true_type {};
      26             : 
      27             : // SIGNAL VARIOUS THINGS TO HAPPEN:
      28             : enum vote_signal_enum_t : int {
      29             :     VOTE_SIGNAL_NONE = 0,
      30             :     VOTE_SIGNAL_FUNDING,  //   -- fund this object for it's stated amount
      31             :     VOTE_SIGNAL_VALID,    //   -- this object checks out in sentinel engine
      32             :     VOTE_SIGNAL_DELETE,   //   -- this object should be deleted from memory entirely
      33             :     VOTE_SIGNAL_ENDORSED, //   -- officially endorsed by the network somehow (delegation)
      34             :     VOTE_SIGNAL_UNKNOWN
      35             : };
      36             : template<> struct is_serializable_enum<vote_signal_enum_t> : std::true_type {};
      37             : 
      38             : /**
      39             : * Governance Voting
      40             : *
      41             : *   Static class for accessing governance data
      42             : */
      43             : 
      44             : class CGovernanceVoting
      45             : {
      46             : public:
      47             :     static vote_outcome_enum_t ConvertVoteOutcome(const std::string& strVoteOutcome);
      48             :     static vote_signal_enum_t ConvertVoteSignal(const std::string& strVoteSignal);
      49             :     static std::string ConvertOutcomeToString(vote_outcome_enum_t nOutcome);
      50             :     static std::string ConvertSignalToString(vote_signal_enum_t nSignal);
      51             : };
      52             : 
      53             : //
      54             : // CGovernanceVote - Allow a masternode to vote and broadcast throughout the network
      55             : //
      56             : 
      57             : class CGovernanceVote
      58             : {
      59             :     friend bool operator==(const CGovernanceVote& vote1, const CGovernanceVote& vote2);
      60             : 
      61             :     friend bool operator<(const CGovernanceVote& vote1, const CGovernanceVote& vote2);
      62             : 
      63             : private:
      64             :     COutPoint masternodeOutpoint;
      65             :     uint256 nParentHash;
      66        1224 :     vote_outcome_enum_t nVoteOutcome{VOTE_OUTCOME_NONE};
      67        1224 :     vote_signal_enum_t nVoteSignal{VOTE_SIGNAL_NONE};
      68        1224 :     int64_t nTime{0};
      69             :     std::vector<unsigned char> vchSig;
      70             : 
      71             :     /** Memory only. */
      72        1224 :     const uint256 hash{0};
      73             :     void UpdateHash() const;
      74             : 
      75             : public:
      76        3672 :     CGovernanceVote() = default;
      77             :     CGovernanceVote(const COutPoint& outpointMasternodeIn, const uint256& nParentHashIn, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn);
      78             : 
      79        7440 :     int64_t GetTimestamp() const { return nTime; }
      80             : 
      81        7804 :     vote_signal_enum_t GetSignal() const { return nVoteSignal; }
      82             : 
      83        2724 :     vote_outcome_enum_t GetOutcome() const { return nVoteOutcome; }
      84             : 
      85        5940 :     const uint256& GetParentHash() const { return nParentHash; }
      86             : 
      87         156 :     void SetTime(int64_t nTimeIn)
      88             :     {
      89         156 :         nTime = nTimeIn;
      90         156 :         UpdateHash();
      91         156 :     }
      92             : 
      93         276 :     void SetSignature(const std::vector<unsigned char>& vchSigIn) { vchSig = vchSigIn; }
      94             : 
      95             :     bool CheckSignature(const CKeyID& keyID) const;
      96             :     bool CheckSignature(const CBLSPublicKey& pubKey) const;
      97             :     bool IsValid(const CDeterministicMNList& tip_mn_list, bool useVotingKey) const;
      98         800 :     std::string GetSignatureString() const
      99             :     {
     100        2400 :         return masternodeOutpoint.ToStringShort() + "|" + nParentHash.ToString() + "|" +
     101        2400 :                                  ::ToString(nVoteSignal) + "|" +
     102        2400 :                                  ::ToString(nVoteOutcome) + "|" +
     103         800 :                                  ::ToString(nTime);
     104           0 :     }
     105             : 
     106       14482 :     const COutPoint& GetMasternodeOutpoint() const { return masternodeOutpoint; }
     107             : 
     108             :     /**
     109             :     *   GetHash()
     110             :     *
     111             :     *   GET UNIQUE HASH WITH DETERMINISTIC VALUE OF THIS SPECIFIC VOTE
     112             :     */
     113             : 
     114             :     uint256 GetHash() const;
     115        1108 :     uint256 GetSignatureHash() const
     116             :     {
     117        1108 :         return SerializeHash(*this);
     118             :     }
     119             : 
     120             :     std::string ToString(const CDeterministicMNList& tip_mn_list) const;
     121             : 
     122       14268 :     SERIALIZE_METHODS(CGovernanceVote, obj)
     123             :     {
     124        4756 :         READWRITE(obj.masternodeOutpoint, obj.nParentHash, obj.nVoteOutcome, obj.nVoteSignal, obj.nTime);
     125        4756 :         if (!(s.GetType() & SER_GETHASH)) {
     126        3648 :             READWRITE(obj.vchSig);
     127        3648 :         }
     128        5980 :         SER_READ(obj, obj.UpdateHash());
     129        4756 :     }
     130             : };
     131             : 
     132             : /**
     133             :  * Sign a governance vote using wallet signing methods
     134             :  * Handles different signing approaches for different networks
     135             :  */
     136             : 
     137             : #endif // BITCOIN_GOVERNANCE_VOTE_H

Generated by: LCOV version 1.16