-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathproposal.rs
More file actions
312 lines (286 loc) · 7.72 KB
/
proposal.rs
File metadata and controls
312 lines (286 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::collections::{BTreeMap, HashSet};
use std::fmt::Display;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::ledger::governance::cli::onchain::{
PgfAction, PgfContinous, PgfRetro, PgfSteward, StewardsUpdate,
};
use crate::ledger::governance::utils::{ProposalStatus, TallyType};
use crate::ledger::storage_api::token::Amount;
use crate::types::address::Address;
use crate::types::hash::Hash;
use crate::types::storage::Epoch;
#[allow(missing_docs)]
#[derive(Debug, Error)]
pub enum ProposalTypeError {
#[error("Invalid proposal type.")]
InvalidProposalType,
}
/// Storage struture for pgf fundings
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
pub struct StoragePgfFunding {
/// The data about the pgf funding
pub detail: PGFTarget,
/// The id of the proposal that added this funding
pub id: u64,
}
impl StoragePgfFunding {
/// Init a new pgf funding struct
pub fn new(detail: PGFTarget, id: u64) -> Self {
Self { detail, id }
}
}
/// An add or remove action for PGF
#[derive(
Debug,
Clone,
Hash,
PartialEq,
Eq,
PartialOrd,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
pub enum AddRemove<T> {
/// Add
Add(T),
/// Remove
Remove(T),
}
/// The target of a PGF payment
#[derive(
Debug,
Clone,
PartialEq,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
Ord,
Eq,
PartialOrd,
)]
pub struct PGFTarget {
/// The target address
pub target: Address,
/// The amount of token to fund the target address
pub amount: Amount,
}
/// The actions that a PGF Steward can propose to execute
#[derive(
Debug,
Clone,
PartialEq,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
pub enum PGFAction {
/// A continuous payment
Continuous(AddRemove<PGFTarget>),
/// A retro payment
Retro(PGFTarget),
}
/// The type of a Proposal
#[derive(
Debug,
Clone,
PartialEq,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
pub enum ProposalType {
/// Default governance proposal with the optional wasm code
Default(Option<Hash>),
/// PGF stewards proposal
PGFSteward(HashSet<AddRemove<Address>>),
/// PGF funding proposal
PGFPayment(Vec<PGFAction>),
}
impl ProposalType {
/// Check if the proposal type is default
pub fn is_default(&self) -> bool {
matches!(self, ProposalType::Default(_))
}
}
impl Display for ProposalType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProposalType::Default(_) => write!(f, "Default"),
ProposalType::PGFSteward(_) => write!(f, "Pgf steward"),
ProposalType::PGFPayment(_) => write!(f, "Pgf funding"),
}
}
}
impl TryFrom<StewardsUpdate> for HashSet<AddRemove<Address>> {
type Error = ProposalTypeError;
fn try_from(value: StewardsUpdate) -> Result<Self, Self::Error> {
let mut data = HashSet::default();
if value.add.is_some() {
data.insert(AddRemove::Add(value.add.unwrap()));
}
for steward in value.remove {
data.insert(AddRemove::Remove(steward));
}
Ok(data)
}
}
impl TryFrom<PgfSteward> for AddRemove<Address> {
type Error = ProposalTypeError;
fn try_from(value: PgfSteward) -> Result<Self, Self::Error> {
match value.action {
PgfAction::Add => Ok(AddRemove::Add(value.address)),
PgfAction::Remove => Ok(AddRemove::Remove(value.address)),
}
}
}
impl TryFrom<PgfContinous> for PGFAction {
type Error = ProposalTypeError;
fn try_from(value: PgfContinous) -> Result<Self, Self::Error> {
match value.action {
PgfAction::Add => {
Ok(PGFAction::Continuous(AddRemove::Add(PGFTarget {
target: value.target.address,
amount: value.target.amount,
})))
}
PgfAction::Remove => {
Ok(PGFAction::Continuous(AddRemove::Remove(PGFTarget {
target: value.target.address,
amount: value.target.amount,
})))
}
}
}
}
impl TryFrom<PgfRetro> for PGFAction {
type Error = ProposalTypeError;
fn try_from(value: PgfRetro) -> Result<Self, Self::Error> {
Ok(PGFAction::Retro(PGFTarget {
target: value.target.address,
amount: value.target.amount,
}))
}
}
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
/// Proposal rappresentation when fetched from the storage
pub struct StorageProposal {
/// The proposal id
pub id: u64,
/// The proposal content
pub content: BTreeMap<String, String>,
/// The proposal author address
pub author: Address,
/// The proposal type
pub r#type: ProposalType,
/// The epoch from which voting is allowed
pub voting_start_epoch: Epoch,
/// The epoch from which voting is stopped
pub voting_end_epoch: Epoch,
/// The epoch from which this changes are executed
pub grace_epoch: Epoch,
}
impl StorageProposal {
/// Check if the proposal can be voted
pub fn can_be_voted(
&self,
current_epoch: Epoch,
is_validator: bool,
) -> bool {
if is_validator {
self.voting_start_epoch <= current_epoch
&& current_epoch * 3
<= self.voting_start_epoch + self.voting_end_epoch * 2
} else {
let valid_start_epoch = current_epoch >= self.voting_start_epoch;
let valid_end_epoch = current_epoch <= self.voting_end_epoch;
valid_start_epoch && valid_end_epoch
}
}
/// Return the type of tally for the proposal
pub fn get_tally_type(&self, is_steward: bool) -> TallyType {
TallyType::from(self.r#type.clone(), is_steward)
}
/// Return the status of a proposal
pub fn get_status(&self, current_epoch: Epoch) -> ProposalStatus {
if self.voting_start_epoch > current_epoch {
ProposalStatus::Pending
} else if self.voting_start_epoch <= current_epoch
&& current_epoch <= self.voting_end_epoch
{
ProposalStatus::OnGoing
} else {
ProposalStatus::Ended
}
}
/// Serialize a proposal to string
pub fn to_string_with_status(&self, current_epoch: Epoch) -> String {
format!(
"Proposal Id: {}
{:2}Type: {}
{:2}Author: {}
{:2}Content: {:?}
{:2}Start Epoch: {}
{:2}End Epoch: {}
{:2}Grace Epoch: {}
{:2}Status: {}
",
self.id,
"",
self.r#type,
"",
self.author,
"",
self.content,
"",
self.voting_start_epoch,
"",
self.voting_end_epoch,
"",
self.grace_epoch,
"",
self.get_status(current_epoch)
)
}
}
impl Display for StorageProposal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Proposal Id: {}
{:2}Type: {}
{:2}Author: {}
{:2}Start Epoch: {}
{:2}End Epoch: {}
{:2}Grace Epoch: {}
",
self.id,
"",
self.r#type,
"",
self.author,
"",
self.voting_start_epoch,
"",
self.voting_end_epoch,
"",
self.grace_epoch
)
}
}