-
Notifications
You must be signed in to change notification settings - Fork 369
/
Proposals.sol
386 lines (354 loc) · 12.7 KB
/
Proposals.sol
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
pragma solidity ^0.5.8;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/utils/Address.sol";
import "solidity-bytes-utils/contracts/BytesLib.sol";
import "../common/FixidityLib.sol";
/**
* @title A library operating on Celo Governance proposals.
*/
library Proposals {
using FixidityLib for FixidityLib.Fraction;
using SafeMath for uint256;
using BytesLib for bytes;
enum Stage { None, Queued, Approval, Referendum, Execution, Expiration }
enum VoteValue { None, Abstain, No, Yes }
struct StageDurations {
uint256 approval;
uint256 referendum;
uint256 execution;
}
// TODO(asa): Reduce storage usage here.
struct VoteTotals {
uint256 yes;
uint256 no;
uint256 abstain;
}
struct Transaction {
uint256 value;
address destination;
bytes data;
}
struct Proposal {
address proposer;
uint256 deposit;
uint256 timestamp;
VoteTotals votes;
Transaction[] transactions;
bool approved;
uint256 networkWeight;
string descriptionUrl;
}
/**
* @notice Constructs a proposal.
* @param proposal The proposal struct to be constructed.
* @param values The values of Celo Gold to be sent in the proposed transactions.
* @param destinations The destination addresses of the proposed transactions.
* @param data The concatenated data to be included in the proposed transactions.
* @param dataLengths The lengths of each transaction's data.
* @param proposer The proposer.
* @param deposit The proposal deposit.
*/
function make(
Proposal storage proposal,
uint256[] memory values,
address[] memory destinations,
bytes memory data,
uint256[] memory dataLengths,
address proposer,
uint256 deposit
) public {
require(
values.length == destinations.length && destinations.length == dataLengths.length,
"Array length mismatch"
);
uint256 transactionCount = values.length;
proposal.proposer = proposer;
proposal.deposit = deposit;
// solhint-disable-next-line not-rely-on-time
proposal.timestamp = now;
uint256 dataPosition = 0;
delete proposal.transactions;
for (uint256 i = 0; i < transactionCount; i = i.add(1)) {
proposal.transactions.push(
Transaction(values[i], destinations[i], data.slice(dataPosition, dataLengths[i]))
);
dataPosition = dataPosition.add(dataLengths[i]);
}
}
function setDescriptionUrl(Proposal storage proposal, string memory descriptionUrl) internal {
require(bytes(descriptionUrl).length != 0, "Description url must have non-zero length");
proposal.descriptionUrl = descriptionUrl;
}
/**
* @notice Constructs a proposal for use in memory.
* @param values The values of Celo Gold to be sent in the proposed transactions.
* @param destinations The destination addresses of the proposed transactions.
* @param data The concatenated data to be included in the proposed transactions.
* @param dataLengths The lengths of each transaction's data.
* @param proposer The proposer.
* @param deposit The proposal deposit.
*/
function makeMem(
uint256[] memory values,
address[] memory destinations,
bytes memory data,
uint256[] memory dataLengths,
address proposer,
uint256 deposit
) internal view returns (Proposal memory) {
require(
values.length == destinations.length && destinations.length == dataLengths.length,
"Array length mismatch"
);
uint256 transactionCount = values.length;
Proposal memory proposal;
proposal.proposer = proposer;
proposal.deposit = deposit;
// solhint-disable-next-line not-rely-on-time
proposal.timestamp = now;
uint256 dataPosition = 0;
proposal.transactions = new Transaction[](transactionCount);
for (uint256 i = 0; i < transactionCount; i = i.add(1)) {
proposal.transactions[i] = Transaction(
values[i],
destinations[i],
data.slice(dataPosition, dataLengths[i])
);
dataPosition = dataPosition.add(dataLengths[i]);
}
return proposal;
}
/**
* @notice Adds or changes a vote on a proposal.
* @param proposal The proposal struct.
* @param previousWeight The previous weight of the vote.
* @param currentWeight The current weight of the vote.
* @param previousVote The vote to be removed, or None for a new vote.
* @param currentVote The vote to be set.
*/
function updateVote(
Proposal storage proposal,
uint256 previousWeight,
uint256 currentWeight,
VoteValue previousVote,
VoteValue currentVote
) public {
// Subtract previous vote.
if (previousVote == VoteValue.Abstain) {
proposal.votes.abstain = proposal.votes.abstain.sub(previousWeight);
} else if (previousVote == VoteValue.Yes) {
proposal.votes.yes = proposal.votes.yes.sub(previousWeight);
} else if (previousVote == VoteValue.No) {
proposal.votes.no = proposal.votes.no.sub(previousWeight);
}
// Add new vote.
if (currentVote == VoteValue.Abstain) {
proposal.votes.abstain = proposal.votes.abstain.add(currentWeight);
} else if (currentVote == VoteValue.Yes) {
proposal.votes.yes = proposal.votes.yes.add(currentWeight);
} else if (currentVote == VoteValue.No) {
proposal.votes.no = proposal.votes.no.add(currentWeight);
}
}
/**
* @notice Executes the proposal.
* @param proposal The proposal struct.
* @dev Reverts if any transaction fails.
*/
function execute(Proposal storage proposal) public {
executeTransactions(proposal.transactions);
}
/**
* @notice Executes the proposal.
* @param proposal The proposal struct.
* @dev Reverts if any transaction fails.
*/
function executeMem(Proposal memory proposal) internal {
executeTransactions(proposal.transactions);
}
function executeTransactions(Transaction[] memory transactions) internal {
for (uint256 i = 0; i < transactions.length; i = i.add(1)) {
require(
externalCall(
transactions[i].destination,
transactions[i].value,
transactions[i].data.length,
transactions[i].data
),
"Proposal execution failed"
);
}
}
/**
* @notice Computes the support ratio for a proposal with the quorum condition:
* If the total number of votes (yes + no + abstain) is less than the required number of votes,
* "no" votes are added to increase particiption to this level. The ratio of yes / (yes + no)
* votes is returned.
* @param proposal The proposal struct.
* @param quorum The minimum participation at which "no" votes are not added.
* @return The support ratio with the quorum condition.
*/
function getSupportWithQuorumPadding(
Proposal storage proposal,
FixidityLib.Fraction memory quorum
) internal view returns (FixidityLib.Fraction memory) {
uint256 yesVotes = proposal.votes.yes;
if (yesVotes == 0) {
return FixidityLib.newFixed(0);
}
uint256 noVotes = proposal.votes.no;
uint256 totalVotes = yesVotes.add(noVotes).add(proposal.votes.abstain);
uint256 requiredVotes = quorum
.multiply(FixidityLib.newFixed(proposal.networkWeight))
.fromFixed();
if (requiredVotes > totalVotes) {
noVotes = noVotes.add(requiredVotes.sub(totalVotes));
}
return FixidityLib.newFixedFraction(yesVotes, yesVotes.add(noVotes));
}
/**
* @notice Returns the stage of a dequeued proposal.
* @param proposal The proposal struct.
* @param stageDurations The durations of the dequeued proposal stages.
* @return The stage of the dequeued proposal.
* @dev Must be called on a dequeued proposal.
*/
function getDequeuedStage(Proposal storage proposal, StageDurations storage stageDurations)
internal
view
returns (Stage)
{
uint256 stageStartTime = proposal
.timestamp
.add(stageDurations.approval)
.add(stageDurations.referendum)
.add(stageDurations.execution);
// solhint-disable-next-line not-rely-on-time
if (now >= stageStartTime) {
return Stage.Expiration;
}
stageStartTime = stageStartTime.sub(stageDurations.execution);
// solhint-disable-next-line not-rely-on-time
if (now >= stageStartTime) {
return Stage.Execution;
}
stageStartTime = stageStartTime.sub(stageDurations.referendum);
// solhint-disable-next-line not-rely-on-time
if (now >= stageStartTime) {
return Stage.Referendum;
}
return Stage.Approval;
}
/**
* @notice Returns the number of votes cast on the proposal over the total number
* of votes in the network as a fraction.
* @param proposal The proposal struct.
* @return The participation of the proposal.
*/
function getParticipation(Proposal storage proposal)
internal
view
returns (FixidityLib.Fraction memory)
{
uint256 totalVotes = proposal.votes.yes.add(proposal.votes.no).add(proposal.votes.abstain);
return FixidityLib.newFixedFraction(totalVotes, proposal.networkWeight);
}
/**
* @notice Returns a specified transaction in a proposal.
* @param proposal The proposal struct.
* @param index The index of the specified transaction in the proposal's transaction list.
* @return The specified transaction.
*/
function getTransaction(Proposal storage proposal, uint256 index)
public
view
returns (uint256, address, bytes memory)
{
require(index < proposal.transactions.length, "getTransaction: bad index");
Transaction storage transaction = proposal.transactions[index];
return (transaction.value, transaction.destination, transaction.data);
}
/**
* @notice Returns an unpacked proposal struct with its transaction count.
* @param proposal The proposal struct.
* @return The unpacked proposal with its transaction count.
*/
function unpack(Proposal storage proposal)
internal
view
returns (address, uint256, uint256, uint256, string storage)
{
return (
proposal.proposer,
proposal.deposit,
proposal.timestamp,
proposal.transactions.length,
proposal.descriptionUrl
);
}
/**
* @notice Returns the referendum vote totals for a proposal.
* @param proposal The proposal struct.
* @return The yes, no, and abstain vote totals.
*/
function getVoteTotals(Proposal storage proposal)
internal
view
returns (uint256, uint256, uint256)
{
return (proposal.votes.yes, proposal.votes.no, proposal.votes.abstain);
}
/**
* @notice Returns whether or not a proposal has been approved.
* @param proposal The proposal struct.
* @return Whether or not the proposal has been approved.
*/
function isApproved(Proposal storage proposal) internal view returns (bool) {
return proposal.approved;
}
/**
* @notice Returns whether or not a proposal exists.
* @param proposal The proposal struct.
* @return Whether or not the proposal exists.
*/
function exists(Proposal storage proposal) internal view returns (bool) {
return proposal.timestamp > 0;
}
// call has been separated into its own function in order to take advantage
// of the Solidity's code generator to produce a loop that copies tx.data into memory.
// TODO: Move to Transaction.execute whenever the next change to Governance is made.
/**
* @notice Executes a function call.
* @param value The value of Celo Gold to be sent with the function call.
* @param destination The destination address of the function call.
* @param dataLength The length of the data to be included in the function call.
* @param data The data to be included in the function call.
*/
function externalCall(address destination, uint256 value, uint256 dataLength, bytes memory data)
private
returns (bool)
{
bool result;
if (dataLength > 0) require(Address.isContract(destination), "Invalid contract address");
/* solhint-disable no-inline-assembly */
assembly {
/* solhint-disable max-line-length */
let x := mload(0x40) // "Allocate" memory for output (0x40 is where "free memory" pointer is stored by convention)
let d := add(data, 32) // First 32 bytes are the padded length of data, so exclude that
result := call(
sub(gas, 34710), // 34710 is the value that solidity is currently emitting
// It includes callGas (700) + callVeryLow (3, to pay for SUB) + callValueTransferGas (9000) +
// callNewAccountGas (25000, in case the destination address does not exist and needs creating)
destination,
value,
d,
dataLength, // Size of the input (in bytes) - this is what fixes the padding problem
x,
0 // Output is ignored, therefore the output size is zero
)
/* solhint-enable max-line-length */
}
/* solhint-enable no-inline-assembly */
return result;
}
}