-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.leo
451 lines (392 loc) · 13.9 KB
/
main.leo
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Public state program:
Mappings: custodies, requests, governance...
External usage example:
async function needs_validators() {
let validators: [address; 16]
= dcp_core_protocol.aleo/validator_sets.get(
dcp_core_protocol.aleo/validator_sets_length.get(true) - 1u64
);
...
}
contact@aleo.store - Pierre-André LONG
*/
import dcp_validator_shares.aleo;
import dcp_open_requests.aleo;
import dcp_fee_management.aleo;
program dcp_core_protocol.aleo {
const DEFAULT_AUTHORITY: address = aleo1s3ws5tra87fjycnjrwsjcrnw2qxr8jfqqdugnf0xzqqw29q9m5pqem2u4t;
const ZERO_ADDRESS: address = aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc;
const MAX_VALIDATORS: u8 = 16u8;
struct Proposal {
validators: [address; 16], // MAX_VALIDATORS
threshold: u8,
current_validator_set_index: u64
}
struct CustodyState {
validator_set_index: u64,
custody_trace: field
}
struct Custody {
origin: address,
custody_key: field,
threshold: u8
}
struct Share {
share_val: field,
index: field
}
struct ValidatorShareData {
validator: address,
share: Share
}
record DestinationShare {
owner: address,
share: Share,
custody: Custody
}
mapping validator_sets: u64 => [address; 16]; // MAX_VALIDATORS
// validator set index => validator addresses
mapping validator_sets_length: bool => u64;
// true => validator_sets length
mapping vote_approval_threshold: bool => u8;
// true => required votes for approval
mapping proposals: field => Proposal;
// proposal hash => validator addresses
mapping votes: field => [bool; 16]; // MAX_VALIDATORS
// proposal hash => validator addresses
mapping custodies: field => CustodyState;
// hash(Custody) => CustodyState
inline hash_proposal(proposal: Proposal) -> field {
return BHP256::hash_to_field(proposal);
}
inline hash_tuple(traces: [field; 2]) -> field{
return BHP256::hash_to_field(traces);
}
inline is_address_in_array(
element: address, array: [address; 16] // MAX_VALIDATORS
) -> bool{
let is_in_array: bool = false;
for i: u8 in 0u8..MAX_VALIDATORS {
is_in_array ||= (element == array[i]);
}
return is_in_array;
}
inline set_bool_in_array(
array: [bool; 16], index: u8, value: bool // MAX_VALIDATORS
) -> [bool; 16] { // MAX_VALIDATORS
return [
(index == 0u8) ? value : array[0u8],
(index == 1u8) ? value : array[1u8],
(index == 2u8) ? value : array[2u8],
(index == 3u8) ? value : array[3u8],
(index == 4u8) ? value : array[4u8],
(index == 5u8) ? value : array[5u8],
(index == 6u8) ? value : array[6u8],
(index == 7u8) ? value : array[7u8],
(index == 8u8) ? value : array[8u8],
(index == 9u8) ? value : array[9u8],
(index == 10u8) ? value : array[10u8],
(index == 11u8) ? value : array[11u8],
(index == 12u8) ? value : array[12u8],
(index == 13u8) ? value : array[13u8],
(index == 14u8) ? value : array[14u8],
(index == 15u8) ? value : array[15u8]
];
}
inline get_address_in_array(
array: [address; 16], index: u8 // MAX_VALIDATORS
) -> address {
let ret: address = ZERO_ADDRESS;
for i: u8 in 0u8..MAX_VALIDATORS {
ret = (i == index) ? array[i] : ret;
}
return ret;
}
inline new_bool_array() -> [bool; 16] { // MAX_VALIDATORS
return [
false, false, false, false,
false, false, false, false,
false, false, false, false,
false, false, false, false
];
}
inline are_votes_approved(
vote_values: [bool; 16], threshold: u8 // MAX_VALIDATORS
) -> bool {
let approval_amounts: u8 = 0u8;
for i: u8 in 0u8..MAX_VALIDATORS {
approval_amounts += (vote_values[i] as u8);
}
return (approval_amounts >= threshold);
}
inline hash_custody(custody: Custody) -> field {
return BHP256::hash_to_field(custody);
}
async transition initiate_validators(
public validators: [address; 16], // MAX_VALIDATORS
public threshold: u8
) -> Future {
assert_eq(self.caller, DEFAULT_AUTHORITY);
return finalize_initiate_validators(
validators,
threshold
);
}
async function finalize_initiate_validators(
validators: [address; 16], // MAX_VALIDATORS
threshold: u8
) {
assert(validator_sets_length.contains(true).not());
validator_sets_length.set(true, 1u64);
validator_sets.set(0u64, validators);
vote_approval_threshold.set(true, threshold);
}
async transition initiate_proposal(
public proposal: Proposal
) -> Future {
return finalize_initiate_proposal(
self.caller,
proposal.validators,
proposal.threshold,
proposal.current_validator_set_index
);
}
async function finalize_initiate_proposal(
caller: address,
validators: [address; 16], // MAX_VALIDATORS
threshold: u8,
current_validator_set_index: u64
) {
let proposal: Proposal = Proposal {
validators: validators,
threshold: threshold,
current_validator_set_index: current_validator_set_index
};
let proposal_hash: field = hash_proposal(proposal);
let length: u64 = validator_sets_length.get(true);
assert_eq(
proposal.current_validator_set_index, length - 1u64
);
let current_validators: [address; 16] = validator_sets.get( // MAX_VALIDATORS
proposal.current_validator_set_index
);
is_address_in_array(caller, current_validators);
proposals.set(proposal_hash, proposal);
}
async transition vote(
public proposal: Proposal,
public caller_index: u8,
public value: bool
) -> Future {
return finalize_vote(
self.caller,
proposal.validators,
proposal.threshold,
proposal.current_validator_set_index,
caller_index,
value,
);
}
async function finalize_vote(
caller: address,
validators: [address; 16], // MAX_VALIDATORS
threshold: u8,
current_validator_set_index: u64,
caller_index: u8,
value: bool
) {
let proposal: Proposal = Proposal {
validators: validators,
threshold: threshold,
current_validator_set_index: current_validator_set_index
};
let proposal_hash: field = hash_proposal(proposal);
let length: u64 = validator_sets_length.get(true);
assert_eq(
proposal.current_validator_set_index, length - 1u64
);
let current_validators: [address; 16] = validator_sets.get( // MAX_VALIDATORS
proposal.current_validator_set_index
);
assert_eq(
get_address_in_array(current_validators, caller_index),
caller
);
let current_votes: [bool; 16] = votes.get_or_use( // MAX_VALIDATORS
proposal_hash,
new_bool_array()
);
let new_votes: [bool; 16] = set_bool_in_array( // MAX_VALIDATORS
current_votes,
caller_index,
value
);
votes.set(proposal_hash, new_votes);
}
async transition accept_proposal(
public proposal: Proposal,
public value: bool,
) -> Future {
return finalize_accept_proposal(
self.caller,
proposal.validators,
proposal.threshold,
proposal.current_validator_set_index,
value
);
}
async function finalize_accept_proposal(
caller: address,
validators: [address; 16], // MAX_VALIDATORS
threshold: u8,
current_validator_set_index: u64,
value: bool
) {
let proposal: Proposal = Proposal {
validators: validators,
threshold: threshold,
current_validator_set_index: current_validator_set_index
};
let proposal_hash: field = hash_proposal(proposal);
let length: u64 = validator_sets_length.get(true);
assert_eq(
proposal.current_validator_set_index, length - 1u64
);
let current_validators: [address; 16] = validator_sets.get( // MAX_VALIDATORS
proposal.current_validator_set_index
);
is_address_in_array(caller, current_validators);
let prev_threshold: u8 = vote_approval_threshold.get(true);
let current_votes: [bool; 16] = votes.get(proposal_hash); // MAX_VALIDATORS
assert(
are_votes_approved(current_votes, prev_threshold)
);
validator_sets_length.set(true, length + 1u64);
validator_sets.set(length, proposal.validators);
vote_approval_threshold.set(true, proposal.threshold);
}
async transition custody_data_as_program(
public custody_hash: field,
public validators: [address; 16], // MAX_VALIDATORS
public operation_trace: field
) -> Future {
assert_eq(self.caller, data_custody_protocol.aleo);
return finalize_custody_data_as_program(
custody_hash,
validators,
operation_trace
);
}
async function finalize_custody_data_as_program(
custody_hash: field,
validators: [address; 16], // MAX_VALIDATORS
operation_trace: field
){
let default_custody_state: CustodyState = CustodyState {
validator_set_index: 0u64,
custody_trace: operation_trace
};
let custody_state: CustodyState = custodies.get_or_use(
custody_hash, default_custody_state
);
let validator_sets_len: u64 = validator_sets_length.get(true);
let first_custody: bool = (
custody_state.custody_trace == operation_trace
);
let validator_set_index: u64 = first_custody ?
(validator_sets_len - 1u64) :
custody_state.validator_set_index;
let validator_set: [address; 16] = validator_sets.get(
validator_set_index
); // MAX_VALIDATORS
assert_eq(validators, validator_set);
let new_custody_trace: field = first_custody ?
operation_trace :
hash_tuple([
custody_state.custody_trace,
operation_trace
]);
let new_custody_state: CustodyState = CustodyState {
validator_set_index: validator_set_index,
custody_trace: new_custody_trace
};
custodies.set(custody_hash, new_custody_state);
}
async transition request_open(
public custody_hash: field,
public validators: [address; 16], // MAX_VALIDATORS
public custody_trace: field
) -> Future {
assert_eq(self.caller, data_custody_protocol.aleo);
return finalize_request_open(
custody_hash,
validators,
custody_trace
);
}
async function finalize_request_open(
custody_hash: field,
validators: [address; 16], // MAX_VALIDATORS
custody_trace: field
){
let custody_state: CustodyState = custodies.get(custody_hash);
assert_eq(custody_trace, custody_state.custody_trace);
let validator_set: [address; 16] = validator_sets.get(
custody_state.validator_set_index
); // MAX_VALIDATORS
assert_eq(validators, validator_set);
}
transition process_request_as_validator(
validator_share: dcp_validator_shares.aleo/ValidatorShare,
open_request: dcp_open_requests.aleo/OpenRequest,
) -> (DestinationShare, dcp_fee_management.aleo/Fee) {
let custody_hash: field = hash_custody(validator_share.custody);
assert_eq(open_request.custody_hash, custody_hash);
let destination_share: DestinationShare = DestinationShare {
owner: open_request.to,
share: validator_share.share,
custody: validator_share.custody
};
dcp_open_requests.aleo/spend_open_request(open_request);
let fee: dcp_fee_management.aleo/Fee
= dcp_fee_management.aleo/mint_fee(
self.caller,
open_request.fee_amount,
open_request.expire
);
assert_eq(open_request.custody_trace, validator_share.custody_trace);
return (destination_share, fee);
}
transition spend_destination_shares(
destination_share_0: DestinationShare,
destination_share_1: DestinationShare,
destination_share_2: DestinationShare,
destination_share_3: DestinationShare,
destination_share_4: DestinationShare,
destination_share_5: DestinationShare,
destination_share_6: DestinationShare,
destination_share_7: DestinationShare,
destination_share_8: DestinationShare,
destination_share_9: DestinationShare,
destination_share_10: DestinationShare,
destination_share_11: DestinationShare,
destination_share_12: DestinationShare,
destination_share_13: DestinationShare,
destination_share_14: DestinationShare
){
assert_eq(self.caller, dcp_reconstruct_secret.aleo);
}
transition mint_null_destination_share(
custody: Custody,
) -> DestinationShare{
return DestinationShare{
owner: self.signer,
share: Share{
share_val: 0field,
index: 0field,
},
custody: custody
};
}
}