-
Notifications
You must be signed in to change notification settings - Fork 9
/
validator_requests.fc
220 lines (208 loc) · 9.16 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
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
() ensure_standart_fee(int msg_value, int id) inline {
int proxy_fee = 1000000000;
int elector_fee = 1000000000;
int our_processing_fee = 50000000;
int receipt_attachment = 10000000;
throw_unless(312 + id, 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 validator,
int total_active,
int total_locked,
int _4, int _5, int _6,
int validator_cover_ability,
cell nominators,
cell requests,
cell transfers,
cell proxies) = storage_untuple(storage);
throw_unless(417, amount <= total_active);
throw_if(473,
max_recommended_punishment_for_validator_misbehaviour(amount) * validator_cover_ability >> 16
>
nominators.get_active_balance_by_address_slice(validator));
raw_reserve(total_locked, 0);
int op = 0x4e73744b;
int elector_query_id = new_query_id();
;; sends message through unused proxy
proxies~send_message_through_unused(
elector_address(),
amount,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.store_slice(in_msg)
.end_cell() );
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(validator, total_active, total_locked, _4, _5, _6, validator_cover_ability, nominators, requests, transfers, proxies);
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;
(slice _1, int _2, int _3, int _4, int _5, int _6, int _7, cell _8, cell requests, cell _10, cell proxies) = storage_untuple(storage);
proxies~send_message_through_matured(elector_address(),
1000000000,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.end_cell()
);
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, _8, requests, _10, proxies);
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, int _7, cell _8, cell requests, cell _10, cell proxies) = storage_untuple(storage);
proxies.send_through_any_proxy(elector_address(),
1000000000,
begin_cell()
.store_uint(op, 32)
.store_uint(elector_query_id, 64)
.store_slice(vote)
.end_cell()
);
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, _8, requests, _10, proxies);
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, int _7, cell _8, cell requests, cell _10, cell _11) = 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, _8, requests, _10, _11);
int receipt_attachment = 10000000;
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, receipt_attachment, 1);
}
;; It is withdrawal of funds which formally is not owned by anybody
() 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, int _7, cell _8, cell _9, cell _10, cell _11) = storage_untuple(storage);
int amount = in_msg~load_grams();
int op = 0x006e7bac;
raw_reserve(total_active + total_locked + undistributed_reward, 0);
raw_reserve(100000000000, 0); ;; Save at least 100 Gr for storage fees
return send_receipt_message(s_addr, op + 0x00001000, query_id, 0, amount, 1);
}
;; returns true if found and processed validator request
(int) process_validator_request (slice in_msg, int msg_value, tuple storage, slice s_addr) impure {
if (in_msg.slice_empty?()) {
return false;
}
int op = in_msg~load_uint(32);
if ( op == 0 ) { ;; op==0 - comment interface is not available for validator
return false;
}
int query_id = in_msg~load_uint(64);
if (op == 0x4e73744b) {
;; new stake message
send_new_stake(msg_value, query_id, storage, s_addr, in_msg);
return true;
}
if (op == 0x47657424) {
;; recover stake request
recover_stake(msg_value, query_id, storage, s_addr); ;;(op, s_addr, in_msg, query_id);
return true;
}
if (op == 0x56744370) {
;; check signature and vote for a complaint
check_and_vote_for_complaint(in_msg, msg_value, query_id, storage, s_addr);
return true;
}
if (op == 0x566f7465) {
vote_for_config(in_msg, msg_value, query_id, storage, s_addr);
return true;
}
if (op == 0x006e7bac) {
validator_withdraw(in_msg, msg_value, query_id, storage, s_addr);
return true;
}
return false;
}