PIVX Core  5.6.99
P2P Digital Currency
governancemodel.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef PIVX_QT_GOVERNANCEMODEL_H
6 #define PIVX_QT_GOVERNANCEMODEL_H
7 
8 #include "clientmodel.h"
9 #include "operationresult.h"
10 #include "uint256.h"
11 
12 #include <atomic>
13 #include <string>
14 #include <list>
15 #include <utility>
16 
17 #include <QObject>
18 
19 struct ProposalInfo {
20 public:
21  enum Status {
26  FINISHED
27  };
28 
31  std::string name;
32  std::string url;
33  int votesYes{0};
34  int votesNo{0};
36  std::string recipientAdd;
40  int totalPayments{0};
46  int startBlock{0};
48  int endBlock{0};
49 
51  explicit ProposalInfo(const uint256& _id, std::string _name, std::string _url,
52  int _votesYes, int _votesNo, std::string _recipientAdd,
53  CAmount _amount, int _totalPayments, int _remainingPayments,
54  Status _status, int _startBlock, int _endBlock) :
55  id(_id), name(std::move(_name)), url(std::move(_url)), votesYes(_votesYes), votesNo(_votesNo),
56  recipientAdd(std::move(_recipientAdd)), amount(_amount), totalPayments(_totalPayments),
57  remainingPayments(_remainingPayments), status(_status), startBlock(_startBlock),
58  endBlock(_endBlock) {}
59 
60  bool operator==(const ProposalInfo& prop2) const { return id == prop2.id; }
61  bool isFinished() const { return status == Status::FINISHED; }
62  std::string statusToStr() const;
63 };
64 
65 struct VoteInfo {
67  ABSTAIN = 0,
68  YES = 1,
69  NO = 2
70  };
71 
72  explicit VoteInfo(const COutPoint _mnId, VoteDirection _vote, std::string _mnAlias, int64_t _time) :
73  mnVoter(_mnId), vote(_vote), mnAlias(std::move(_mnAlias)), time(_time) {}
76  std::string mnAlias;
77  int64_t time;
78 };
79 
80 class CBudgetProposal;
81 class TransactionRecord;
82 class MNModel;
83 class WalletModel;
84 
85 QT_BEGIN_NAMESPACE
86 class QTimer;
87 QT_END_NAMESPACE
88 
89 class GovernanceModel : public QObject
90 {
91 
92 public:
93  explicit GovernanceModel(ClientModel* _clientModel, MNModel* _mnModel);
94  ~GovernanceModel() override;
95  void setWalletModel(WalletModel* _walletModel);
96 
97  // Return proposals ordered by net votes.
98  // By default, do not return zombie finished proposals that haven't been cleared yet (backend removal sources need a cleanup).
99  std::list<ProposalInfo> getProposals(const ProposalInfo::Status* filterByStatus = nullptr, bool filterFinished = true);
100  // Returns true if there is at least one proposal cached
101  bool hasProposals();
102  // Whether a visual refresh is needed
103  bool isRefreshNeeded() { return refreshNeeded; }
104  // Return the number of blocks per budget cycle
105  int getNumBlocksPerBudgetCycle() const;
106  // Return the minimum time when an MN can update a vote for a proposal
107  int getProposalVoteUpdateMinTime() const;
108  // Return the budget maximum available amount for the running chain
110  // Return the proposal maximum payments count for the running chain
111  int getPropMaxPaymentsCount() const;
112  // Return the required fee for proposals
114  int getNextSuperblockHeight() const;
115  // Returns the sum of all of the passing proposals
118  // Return the votes that the local masternodes did for the inputted proposal
119  std::vector<VoteInfo> getLocalMNsVotesForProposal(const ProposalInfo& propInfo);
120  // Check if the URL is valid.
121  OperationResult validatePropURL(const QString& url) const;
122  OperationResult validatePropName(const QString& name) const;
124  OperationResult validatePropPaymentCount(int paymentCount) const;
125  // Whether the tier two network synchronization has finished or not
126  bool isTierTwoSync();
127 
128  // Creates a proposal, crafting and broadcasting the fee transaction,
129  // storing it locally to be broadcasted when the fee tx proposal depth
130  // fulfills the minimum depth requirements
131  OperationResult createProposal(const std::string& strProposalName,
132  const std::string& strURL,
133  int nPaymentCount,
134  CAmount nAmount,
135  const std::string& strPaymentAddr);
136 
138  bool isVotePositive,
139  const std::vector<std::string>& mnVotingAlias);
140 
141  // Stop internal timers
142  void stop();
143 
144 public Q_SLOTS:
145  void pollGovernanceChanged();
146  void txLoaded(const QString& hash, const int txType, const int txStatus);
147 
148 private:
151  MNModel* mnModel{nullptr};
152  std::atomic<bool> refreshNeeded{false};
153 
154  // Cached amount
156 
157  QTimer* pollTimer{nullptr};
158  // Cached proposals waiting for the minimum required confirmations
159  // to be broadcasted to the network.
160  std::vector<CBudgetProposal> waitingPropsForConfirmations;
161 
162  void scheduleBroadcast(const CBudgetProposal& proposal);
163 
164  // Util function to create a ProposalInfo object
165  ProposalInfo buildProposalInfo(const CBudgetProposal* prop, bool isPassing, bool isPending);
166 };
167 
168 #endif // PIVX_QT_GOVERNANCEMODEL_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
Model for PIVX network client.
Definition: clientmodel.h:50
int getPropMaxPaymentsCount() const
~GovernanceModel() override
WalletModel * walletModel
int getNextSuperblockHeight() const
std::atomic< bool > refreshNeeded
GovernanceModel(ClientModel *_clientModel, MNModel *_mnModel)
OperationResult validatePropPaymentCount(int paymentCount) const
int getProposalVoteUpdateMinTime() const
std::vector< CBudgetProposal > waitingPropsForConfirmations
OperationResult validatePropURL(const QString &url) const
OperationResult validatePropName(const QString &name) const
ProposalInfo buildProposalInfo(const CBudgetProposal *prop, bool isPassing, bool isPending)
CAmount getBudgetAvailableAmount() const
void scheduleBroadcast(const CBudgetProposal &proposal)
ClientModel * clientModel
void txLoaded(const QString &hash, const int txType, const int txStatus)
OperationResult voteForProposal(const ProposalInfo &prop, bool isVotePositive, const std::vector< std::string > &mnVotingAlias)
CAmount getProposalFeeAmount() const
OperationResult validatePropAmount(CAmount amount) const
CAmount getBudgetAllocatedAmount() const
int getNumBlocksPerBudgetCycle() const
void setWalletModel(WalletModel *_walletModel)
OperationResult createProposal(const std::string &strProposalName, const std::string &strURL, int nPaymentCount, CAmount nAmount, const std::string &strPaymentAddr)
std::list< ProposalInfo > getProposals(const ProposalInfo::Status *filterByStatus=nullptr, bool filterFinished=true)
CAmount getMaxAvailableBudgetAmount() const
std::vector< VoteInfo > getLocalMNsVotesForProposal(const ProposalInfo &propInfo)
UI model for a transaction.
Interface to PIVX wallet from Qt view code.
Definition: walletmodel.h:109
256-bit opaque blob.
Definition: uint256.h:138
Definition: uint256.h:212
const char * name
Definition: rest.cpp:37
const char * url
Definition: rpcconsole.cpp:51
std::string recipientAdd
Payment script destination.
bool operator==(const ProposalInfo &prop2) const
ProposalInfo(const uint256 &_id, std::string _name, std::string _url, int _votesYes, int _votesNo, std::string _recipientAdd, CAmount _amount, int _totalPayments, int _remainingPayments, Status _status, int _startBlock, int _endBlock)
std::string url
int remainingPayments
Amount of times that the proposal was paid already.
int endBlock
End superblock height.
int totalPayments
Amount of times that the proposal will be paid.
CAmount amount
Amount of PIV paid per month.
bool isFinished() const
uint256 id
Proposal hash.
std::string statusToStr() const
std::string name
Status status
Proposal state.
int startBlock
Start superblock height.
VoteDirection vote
std::string mnAlias
VoteInfo(const COutPoint _mnId, VoteDirection _vote, std::string _mnAlias, int64_t _time)
int64_t time
COutPoint mnVoter