forked from ton-blockchain/benchmark-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retranslator.fc
274 lines (237 loc) · 8.26 KB
/
retranslator.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
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
{-
This is the Retranslator contract in the network stress test system.
It is launched from an external message, very similar in structure
to a message to a wallet. Until the started chain ends, it forwards
almost identical messages, which are called hops, to its own copies.
There are usually several hundred thousand of these messages in one test.
Every few hops (see txs_per_report in imports/utils.fc), it (i.e. its copy)
sends a report to the Сounter contract (counter.fc).
-}
#include "imports/stdlib.fc";
#include "imports/utils.fc";
{-
_# id:uint16 seqno:uint32 public_key:uint256 found_counter:uint32 next_counter_to_calc:int32 own_code:^Cell counter_code:^Cell = Storage;
-}
const monkey_mode = 0;
const max_retranslators = 4096;
const counter_calc_tries_per_hop = 100;
const retranslator_calc_tries_limit = 2;
const chance_base = 65535;
;; This amount should be enough for counter to init
;; and send a report to master:
const report_amount = 90000000; ;; 0.09 TON
;; storage variables and arguments for retranslate()
global int same_shard_chance;
global int remaining_hops;
global int split_hops;
global int id;
global int txs_per_report;
global int stored_seqno;
global int public_key;
global cell own_code;
global int found_counter;
global int next_counter_to_calc;
global cell counter_code;
global slice payload;
global int need_save;
() load_data() impure inline {
slice ds = get_data().begin_parse();
id = ds~load_uint(16);
stored_seqno = ds~load_uint(32);
public_key = ds~load_uint(256);
found_counter = ds~load_uint(32);
next_counter_to_calc = ds~load_int(32);
own_code = ds~load_ref();
counter_code = ds.preload_ref();
}
;; variables to set in loops & ifs
global cell next_hop_initstate;
global slice next_hop_address;
global int my_shard;
global int max_shard_depth;
global cell counter_initstate;
global int counter_addr_hash;
global cell counter_initstate;
global int counter_addr_hash;
() calc_counter(int id) impure inline {
counter_initstate = counter_init(id, public_key, counter_code);
counter_addr_hash = cell_hash(counter_initstate);
}
() send_report(int any_shard?) impure inline {
if (any_shard?) {
calc_counter(random() % max_shard_depth);
}
else {
if (next_counter_to_calc != -1) { ;; if counter is not found yet - try to calc it
int tries = 0;
do {
calc_counter(next_counter_to_calc);
int counter_shard = counter_addr_hash >> (256 - max_shard_depth);
if (counter_shard == my_shard) {
found_counter = next_counter_to_calc;
next_counter_to_calc = -1;
tries = counter_calc_tries_per_hop; ;; break
} else {
next_counter_to_calc += 1;
tries += 1;
}
} until (tries >= counter_calc_tries_per_hop);
need_save = true;
} else { ;; means we've found the counter
calc_counter(found_counter);
}
}
var report_msg = begin_cell()
.store_uint(0x10, 6)
.store_uint(0x400, 11) ;; address serialization and wc
.store_uint(counter_addr_hash, 256)
.store_coins(report_amount)
.store_uint(4 + 2, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1) ;; stateinit presents | body in slice
.store_ref(counter_initstate)
.store_uint(id, 16)
.store_uint(txs_per_report, 16);
send_raw_message(report_msg.end_cell(), 3);
}
() retranslate(int amount) impure {
randomize_lt();
var need_report? = remaining_hops % txs_per_report == 0;
var any_shard_hop? = amount | ((random() % chance_base) >= same_shard_chance);
max_shard_depth = max_split_from_config();
(_, int my_addr_hash) = parse_std_addr(my_address());
;; first `max_shard_depth` bits of an address are the shard id
my_shard = my_addr_hash >> (256 - max_shard_depth);
int found? = false;
int next_hop = random() % max_retranslators;
int tries = 0;
do {
;; if any shard hop - then just select the random hop
;; if the same needed - then check random retranslators until found
next_hop_initstate = retranslator_init(next_hop, public_key, own_code, counter_code);
next_hop_address = calc_address(next_hop_initstate);
(_, int hop_addr_hash) = parse_std_addr(next_hop_address);
int next_hop_shard = hop_addr_hash >> (256 - max_shard_depth);
found? = any_shard_hop? ? true : (next_hop_shard == my_shard);
next_hop += 1;
if (next_hop >= max_retranslators) {
next_hop = 0;
}
tries += 1;
if ((tries >= retranslator_calc_tries_limit) & (~ found?)) {
next_hop = id;
next_hop_initstate = retranslator_init(next_hop, public_key, own_code, counter_code);
next_hop_address = my_address();
found? = true;
}
} until found?;
if (need_report?) {
send_report(any_shard_hop?);
if (amount > 0) {
amount -= report_amount;
}
}
var hop_msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(next_hop_address)
.store_coins(amount > 0 ? amount : 0)
.store_uint(4 + 2 + 1, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1)
.store_ref(next_hop_initstate);
var hop_msg_body = begin_cell()
.store_uint(id, 16)
.store_uint(remaining_hops, 32)
.store_uint(split_hops, 8)
.store_uint(txs_per_report, 16)
.store_uint(same_shard_chance, 16)
.store_slice(payload)
.end_cell();
hop_msg = hop_msg.store_ref(hop_msg_body);
int send_mode = 128 + 2;
if (amount) { send_mode = 3; }
if (monkey_mode) { send_mode += 32; } ;; self-destroy if zero balance
send_raw_message(hop_msg.end_cell(), send_mode);
}
{-
retransalate#_ id:uint16 remaining_hops:uint32 split_hops:uint8 txs_per_report:uint16 same_shard_chance:uint16 payload:Cell = IntMsgBody;
-}
() main(int msg_value, cell in_msg_full, slice in_msg_body) {
if (in_msg_body.slice_empty?()) { ;; ignore empty messages
return ();
}
check_spam_config();
slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
if (flags & 1) {
return ();
}
slice sender_address = cs~load_msg_addr();
int sender_id = in_msg_body~load_uint(16);
load_data();
need_save = false;
slice expected_address = calc_address(retranslator_init(sender_id, public_key, own_code, counter_code));
throw_unless(401, equal_slice_bits(expected_address, sender_address));
remaining_hops = in_msg_body~load_uint(32);
split_hops = in_msg_body~load_uint(8);
txs_per_report = in_msg_body~load_uint(16);
same_shard_chance = in_msg_body~load_uint(16);
ifnot(remaining_hops) {
return ();
}
remaining_hops -= 1;
ifnot (monkey_mode) {
;; do not decrease the balance
raw_reserve(pair_first(get_balance()) - msg_value, 2);
}
payload = in_msg_body; ;; to send with hop. giving an option to set msg size
if(split_hops) {
split_hops -= 1;
msg_value -= 27372000; ;; gas + fwd, it will not make division by 2 perfect, but good enough
retranslate(msg_value / 2);
retranslate(0); ;; send the rest
} else {
retranslate(0); ;; send all incoming value
}
if (need_save) {
set_data(pack_retranslator_data(id, stored_seqno, public_key, found_counter, next_counter_to_calc, own_code, counter_code));
}
}
() recv_external(slice in_msg) impure {
var signature = in_msg~load_bits(512);
var cs = in_msg;
var (subwallet_id, valid_until, msg_seqno) = (cs~load_uint(32), cs~load_uint(32), cs~load_uint(32));
throw_if(35, valid_until <= now());
load_data();
throw_unless(33, msg_seqno == stored_seqno);
throw_unless(34, subwallet_id == id);
throw_unless(35, check_signature(slice_hash(in_msg), signature, public_key));
throw_unless(36, (subwallet_id == 0) & (stored_seqno == 0)); ;; Allow starting spam only once
accept_message();
var mode = cs~load_uint(8);
throw_unless(37, mode == 255);
(int threads, ;; a number of tx-chains to start
remaining_hops,
split_hops,
txs_per_report,
same_shard_chance,
int amount) = (cs~load_uint(8),
cs~load_uint(32),
cs~load_uint(8),
cs~load_uint(16),
cs~load_uint(16),
cs~load_coins());
payload = cs;
while (threads > 0) {
retranslate(amount);
threads -= 1;
}
set_data(pack_retranslator_data(id, stored_seqno + 1, public_key, found_counter, next_counter_to_calc, own_code, counter_code));
}
;;
;; Get methods:
;;
int seqno() method_id {
load_data();
return stored_seqno;
}
int get_public_key() method_id {
load_data();
return public_key;
}