forked from EmelyanenkoK/nominator_pool
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidator_requests.fc
192 lines (182 loc) · 8.32 KB
/
validator_requests.fc
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
() ensure_standart_fee(int msg_value, int id) inline {
int elector_fee = 1000000000;
int our_processing_fee = 50000000;
int receipt_attachment = 10000000;
throw_unless(312, msg_value >= 1000000000 + 50000000 + 2 * 10000000);
}
() send_new_stake (int msg_value, int query_id, tuple storage, slice s_addr, slice in_msg) impure {
ensure_standart_fee(msg_value, 10000);
int amount = in_msg~load_grams();
slice cs = in_msg;
;; Stake checks borrowed from https://github.com/newton-blockchain/ton/blob/master/crypto/smartcont/elector-code.fc#L208
(int pool_wc, int pool_addr) = parse_std_addr(my_address());
var validator_pubkey = cs~load_uint(256);
var stake_at = cs~load_uint(32);
var max_factor = cs~load_uint(32);
var adnl_addr = cs~load_uint(256);
var signature = cs~load_ref().begin_parse().preload_bits(512);
cs.end_parse();
ifnot (check_data_signature(begin_cell()
.store_uint(0x654c5074, 32)
.store_uint(stake_at, 32)
.store_uint(max_factor, 32)
.store_uint(pool_addr, 256)
.store_uint(adnl_addr, 256)
.end_cell().begin_parse(), signature, validator_pubkey)) {
;; incorrect signature
return send_receipt_message(s_addr, 0xee6f454c, query_id, 1, 0, 64);
}
if (max_factor < 0x10000) {
;; factor must be >= 1. = 65536/65536
return send_receipt_message(s_addr, 0xee6f454c, query_id, 6, 0, 64);
}
;; All checks we can make locally
(slice _1, int total_active, int total_locked, int _4, int _5, int _6, cell _7, cell requests, cell transfers) = storage_untuple(storage);
throw_unless(417, amount <= total_active);
raw_reserve(total_locked, 0);
int op = 0x4e73744b;
int elector_query_id = new_query_id();
send_message(elector_address(),
amount,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.store_slice(in_msg)
.end_cell(),
1 ;; pay fees separately but fail on errors
);
requests~register_request(elector_query_id,
begin_cell()
.store_uint(op,32)
.store_uint(query_id, 64)
.store_uint(now(),32)
.store_slice(s_addr)
.store_grams(amount)
.end_cell());
;; Transfers will be written on response
;;throw_unless( 419, transfers~udict_add_builder?(32, now(), begin_cell().store_grams(amount)));
save_data(_1, total_active, total_locked, _4, _5, _6, _7, requests, transfers);
int receipt_attachment = 10000000;
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, receipt_attachment, 1);
}
() recover_stake (int msg_value, int query_id, tuple storage, slice s_addr) impure {
ensure_standart_fee(msg_value, 20000);
int elector_query_id = new_query_id();
int op = 0x47657424;
send_message(elector_address(),
1000000000,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.end_cell(),
1 ;; pay fees separately but fail on errors
);
(slice _1, int _2, int _3, int _4, int _5, int _6, cell _7, cell requests, cell _9) = storage_untuple(storage);
requests~register_request(elector_query_id,
begin_cell()
.store_uint(op,32)
.store_uint(query_id, 64)
.store_uint(now(), 32)
.store_slice(s_addr)
.end_cell());
save_data(_1, _2, _3, _4, _5, _6, _7, requests, _9);
int receipt_attachment = 10000000;
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, receipt_attachment, 1);
}
() check_and_vote_for_complaint(slice in_msg, int msg_value, int query_id, tuple storage, slice s_addr) impure {
ensure_standart_fee(msg_value, 30000);
slice vote = in_msg;
int op = 0x56744370;
int elector_query_id = new_query_id();
;; Check before sending
;; Borrowed from https://github.com/newton-blockchain/ton/blob/master/crypto/smartcont/elector-code.fc#L652
var signature = in_msg~load_bits(512);
var msg_body = in_msg;
var (sign_tag, idx, elect_id, chash) = (in_msg~load_uint(32), in_msg~load_uint(16), in_msg~load_uint(32), in_msg~load_uint(256));
in_msg.end_parse();
throw_unless(37, sign_tag == 0x56744350);
var (vdescr, total_weight) = get_validator_descr(idx);
var (val_pubkey, weight) = unpack_validator_descr(vdescr);
throw_unless(34, check_data_signature(msg_body, signature, val_pubkey));
;; send 'vote'
(slice _1, int _2, int _3, int _4, int _5, int _6, cell _7, cell requests, cell _9) = storage_untuple(storage);
send_message(elector_address(),
1000000000,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.store_slice(vote)
.end_cell(),
1 ;; pay fees separately but fail on errors
);
requests~register_request(elector_query_id,
begin_cell()
.store_uint(op,32)
.store_uint(query_id, 64)
.store_uint(now(), 32)
.store_slice(s_addr)
.end_cell());
save_data(_1, _2, _3, _4, _5, _6, _7, requests, _9);
int receipt_attachment = 10000000;
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, receipt_attachment, 1);
}
() vote_for_config (slice in_msg, int msg_value, int query_id, tuple storage, slice s_addr) impure {
ensure_standart_fee(msg_value, 40000);
int config_query_id = new_query_id();
int op = 0x566f7465;
;; Should we check signature here?
send_message(config_address(),
1000000000,
begin_cell()
.store_uint(op, 32)
.store_uint(config_query_id, 64)
.store_slice(in_msg)
.end_cell(),
1 ;; pay fees separately but fail on errors
);
(slice _1, int _2, int _3, int _4, int _5, int _6, cell _7, cell requests, cell _9) = storage_untuple(storage);
requests~register_request(config_query_id,
begin_cell()
.store_uint(op,32)
.store_uint(query_id, 64)
.store_uint(now(), 32)
.store_slice(s_addr)
.end_cell());
save_data(_1, _2, _3, _4, _5, _6, _7, requests, _9);
int receipt_attachment = 10000000;
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, receipt_attachment, 1);
}
() validator_withdraw (slice in_msg, int msg_value, int query_id, tuple storage, slice s_addr) impure {
ensure_standart_fee(msg_value, 50000);
(slice _1,
int total_active, int total_locked,
int _4,
int undistributed_reward,
int _6, cell _7, cell _8, cell _9) = storage_untuple(storage);
int amount = in_msg~load_grams();
int op = 0x006e7bac;
raw_reserve(total_active + total_locked + undistributed_reward, 0);
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, amount, 1);
}
() process_validator_request (slice in_msg, int msg_value, tuple storage, slice s_addr) impure {
int op = in_msg~load_uint(32);
int query_id = in_msg~load_uint(64);
if (op == 0x4e73744b) {
;; new stake message
return send_new_stake(msg_value, query_id, storage, s_addr, in_msg);
}
if (op == 0x47657424) {
;; recover stake request
return recover_stake(msg_value, query_id, storage, s_addr); ;;(op, s_addr, in_msg, query_id);
}
if (op == 0x56744370) {
;; check signature and vote for a complaint
return check_and_vote_for_complaint(in_msg, msg_value, query_id, storage, s_addr);
}
if (op == 0x566f7465) {
return vote_for_config(in_msg, msg_value, query_id, storage, s_addr);
}
if (op == 0x006e7bac) {
return validator_withdraw(in_msg, msg_value, query_id, storage, s_addr);
}
}