-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaking_contract.aes
403 lines (333 loc) · 10.9 KB
/
staking_contract.aes
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
@compiler >= 4.3
include "List.aes"
include "Pair.aes"
include "Option.aes"
/**
* Simple implementation of a staking contract for aeternity
* hyperchains. Supports stake aging and randomized elections
* (the entropy should come from external source).
* Delegated voting is not supported.
*/
payable contract SimpleElection =
type delegate = address
type block_height = int
type tokens = int
/**
* Single stake entry
*/
record stake = {
value : tokens,
created : block_height }
/**
* Deferred withdrawal
*/
record withdraw_request = {
value : tokens,
created : block_height }
/**
* General variables and configurations
* - deposit_delay: how much does it take for the staked tokens to
* provide the voting power
* - stake_retraction_delay: how long should the tokens provide voting
* power after the delegate has requested to withdraw them
* - withdraw_delay: after what period of time are the tokens eligible
* to be withdrawn after delegate's request
*
* Should satisfy withdraw_delay >= stake_retraction_delay
*/
record config = {
deposit_delay : block_height,
stake_retraction_delay : block_height,
withdraw_delay : block_height }
/**
* Leader candidate with calculated voting power
*/
record candidate = {
address : delegate,
power : int }
/**
* Description of the election result on a given block height
*/
record election_result = {
leader : delegate,
height : block_height }
/**
* The state of the staking contract
* - stakes: list of all staked entries for each delegate
* - withdraw_requests: list of all withdrawal requests for each delegate'
* - last_election: hypothetical information about the most recent election.
* Should set to None initially and updated with each election
* - config: overall tweaks of the election/staking rules
*/
record state = {
stakes : map(delegate, list(stake)),
withdraw_requests : map(delegate, list(withdraw_request)),
last_election : option(election_result),
config : config }
entrypoint
init : (config, map(address, int)) => state
init(config, initial_stakes) =
require(
config.deposit_delay >= 0,
"NEGATIVE_DEPOSIT_DELAY")
require(
config.stake_retraction_delay >= 0,
"NEGATIVE_STAKE_RETRACTION_DELAY")
require(
config.withdraw_delay >= 0,
"NEGATIVE_WITHDRAW_DELAY")
require(
config.withdraw_delay >= config.stake_retraction_delay,
"STAKE_RETRACTION_AFTER_WITHDRAW"
)
let init_stakes_list = Map.to_list(initial_stakes)
[abort("NEGATIVE_INIT_STAKE") | (_, x) <- init_stakes_list, if(x =< 0)]
require(
List.sum(List.map(Pair.snd, init_stakes_list)) =< Contract.balance,
""
)
{ stakes = Map.from_list([(addr, [{value = x, created = -config.deposit_delay}]) | (addr, x) <- init_stakes_list]),
withdraw_requests = {},
last_election = None,
config = config }
/**
* Default configuration – can be adjusted in the `init` entrypoint
*/
entrypoint
default_config : () => config
default_config() =
{ deposit_delay = 5,
stake_retraction_delay = 5,
withdraw_delay = 10 }
/**
* Constant that defines range of hashes
*/
function
hash_range : () => int
hash_range() =
Bytes.to_int(#ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff : hash)
/**
* Make the function impossible to call by a regular user
*/
function
protocol_restrict : () => unit
protocol_restrict() =
require(Call.origin == Contract.creator, "PROTOCOL_RESTRICTED")
/**
* Calculates the voting power of a stake entry
*/
function
valuate : stake => int
valuate(s) =
let age = Chain.block_height - s.created
if(age >= state.config.deposit_delay) s.value + age^2
else 0
/**
* Get total of staked tokens of a delegate
*/
entrypoint
staked_tokens : delegate => tokens
staked_tokens(address) =
List.sum([s.value | s <- state.stakes[address = []]])
/**
* Get total number of tokens that a delegate requested to thaw
*/
entrypoint
requested_withdrawals : delegate => tokens
requested_withdrawals(address) =
List.sum([w.value | w <- state.withdraw_requests[address = []]])
/**
* Get the amount of the stake that is already retracted by
* withdrawal requests, but not yet withdrawn
*/
entrypoint
retracted_stake : delegate => tokens
retracted_stake(address) =
List.sum([ w.value
| w <- state.withdraw_requests[address = []]
, if(Chain.block_height >=
w.created + state.config.stake_retraction_delay)
])
/**
* Filters out the premature withdrawals and calculates the value
* of the rest
*/
function
extract_ripe_withdrawals : list(withdraw_request) =>
(tokens * list(withdraw_request))
extract_ripe_withdrawals([]) = (0, [])
extract_ripe_withdrawals(w::t) =
let (tokens, rest) = extract_ripe_withdrawals(t)
if(Chain.block_height >= w.created + state.config.withdraw_delay)
// We take it
(tokens + w.value, rest)
else
// We leave it
(tokens, w::rest)
/**
* Drops the least valuable tokens from the stake entries
*/
function
decrease_stake : (list(stake), tokens) => list(stake)
decrease_stake(stakes, value) =
run_decrease_stake(List.sort((s1, s2) => valuate(s1) < valuate(s2), stakes), value)
function
run_decrease_stake : (list(stake), tokens) => list(stake)
run_decrease_stake(l, 0) = l
run_decrease_stake(h::t, amount) =
if(h.value > amount) h{value = h.value - amount}::t
else run_decrease_stake(t, amount - h.value)
/**
* Calculates the voting power of a delegate
*/
function
voting_power : delegate => int
voting_power(address) =
let retracted_stake = retracted_stake(address)
let stakes = decrease_stake(state.stakes[address], retracted_stake)
List.sum([valuate(s) | s <- stakes])
/**
* Extracts list of candidates along with their voting power
* from the state
*/
function
get_candidates : list(delegate) => list(candidate)
get_candidates(delegates) =
let is_considered(d : delegate) =
List.find((x) => x == d, delegates) != None
let stakes = List.filter((p) => is_considered(Pair.fst(p)), Map.to_list(state.stakes))
let make_candidate((addr, stks)) = {
address = addr,
power = List.sum([valuate(st) | st <- stks]) }
List.map(make_candidate, stakes)
/**
* Ordering on candidates
*/
function
candidate_cmp : (candidate, candidate) => bool
candidate_cmp(c1, c2) =
if(c1.power == c2.power) c1.address < c2.address
else c1.power < c2.power
/**
* Extracts the chosen leader from the delegates list
*/
function
choose_by_power : (list(candidate), int) => option(delegate)
choose_by_power(delegates, shot) =
switch(delegates)
[] => None
h::t =>
if(h.power > shot) Some(h.address)
else choose_by_power(t, shot - h.power)
/**
* Performs the election of the leader depending on the random hash
*/
function
elect_candidate : (list(candidate), hash) => option(delegate)
elect_candidate(candidates, rand) =
let total_power = List.sum([c.power | c <- candidates])
let sorted = List.sort(candidate_cmp, candidates)
let shot = total_power * Bytes.to_int(rand) / hash_range()
choose_by_power(sorted, shot)
/**
* Combines all entries of the delegate into a single one with refreshed
* creation date
*/
stateful function
reset_stake_age : delegate => unit
reset_stake_age(addr) =
let total = List.sum([s.value | s <- state.stakes[addr = []]])
put(state{
stakes[addr] = [{value = total, created = Chain.block_height}]})
/**
* Updates the entry of the last election result
*/
stateful function
set_leader : delegate => unit
set_leader(addr) =
put(state{
last_election = Some({leader = addr, height = Chain.block_height})})
/**
* Returns the leader of the upcoming generation without
* performing the election
*/
entrypoint
get_computed_leader : () => option(delegate)
get_computed_leader() =
switch(state.last_election)
None => None
Some(le) =>
if(Chain.block_height != le.height) None
else Some(le.leader)
/*** STAKERS' ACTIONS ***/
/**
* Freezes tokens to be counted as stake
*/
payable stateful entrypoint
deposit_stake : () => unit
deposit_stake() =
let caller = Call.origin
let value = Call.value
require(value > 0, "ZERO_STAKE_DEPOSIT")
let new_stake = {value = value, created = Chain.block_height}
put(state{stakes[caller = []] @ sts = new_stake :: sts})
/**
* Registers withdrawal request
*/
stateful entrypoint
request_withdraw : tokens => unit
request_withdraw(amount) =
let caller = Call.origin
require(
amount > 0,
"NON_POSITIVE_WITHDRAW_REQUEST")
require(
staked_tokens(caller) - requested_withdrawals(caller) >= amount,
"WITHDRAW_TOO_MUCH")
let new_withdrawal =
{value = amount, created = Chain.block_height}
put(state{withdraw_requests[caller = []] @ ws = new_withdrawal :: ws})
/**
* Withdraws tokens from ripe withdraw requests
*/
stateful entrypoint
withdraw : () => tokens
withdraw() =
let caller = Call.origin
let (total_value, new_withdrawals) =
extract_ripe_withdrawals(state.withdraw_requests[caller])
put(state{
stakes[caller] @ stakes = decrease_stake(stakes, total_value),
withdraw_requests[caller] = new_withdrawals})
Chain.spend(caller, total_value)
total_value
/*** RESTRICTED ACTIONS ***/
/**
* Performs the leader election.
*/
stateful entrypoint
get_leader : (list(delegate), hash) => delegate
get_leader(delegates, rand) =
protocol_restrict()
switch(get_computed_leader())
Some(le) => le
None =>
let candidates = get_candidates(delegates)
switch (elect_candidate(candidates, rand))
// if the election fails then pick the previous one
None => switch(state.last_election)
None => abort("NO_CANDIDATE")
Some(le) => le.leader
Some(leader) =>
reset_stake_age(leader)
set_leader(leader)
leader
/**
* Vanishes the staked funds of the user in case of a fraud
*/
stateful entrypoint
punish : delegate => unit
punish(address) =
protocol_restrict()
let stake = staked_tokens(address)
put(state{stakes[address] = [], withdraw_requests[address] = []})