-
Notifications
You must be signed in to change notification settings - Fork 3
/
counter.fc
77 lines (63 loc) · 2.59 KB
/
counter.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
{-
This is the middle Counter contract in the network stress test system.
It receives reports from Retranslators every N transactions
they (retranslators) make (this parameter is configured in retranslator.fc).
It counts the reported transactions and once in a while sends them
to the Master Counter (master_counter.fc), which is located in workchain -1.
-}
#include "imports/stdlib.fc";
#include "imports/utils.fc";
#include "auto/retranslator-code.fc";
#include "auto/master-counter-address.fc";
{-
_# id:uint16 public_key:uint256 counter:uint32 last_sync:uint48 own_code:^Cell= Storage;
-}
const count_report_as_tx = -1; ;; true
const min_balance = 5000000; ;; 0.005 TON
;; id, public_key, counter, last_sync
(int, int, int, int, cell) load_data () inline {
slice ds = get_data().begin_parse();
return (ds~load_uint(16), ds~load_uint(256),
ds~load_uint(32), ds~load_uint(48),
ds~load_ref());
}
() push_to_master(slice master, int my_id, int counter, int last_sync) impure inline {
var msg = begin_cell()
.store_uint(0x10, 6) ;; non-bounce to prevent extra txs
.store_slice(master)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(my_id, 16)
.store_uint(counter, 32)
.store_uint(last_sync, 48)
.store_uint(now(), 48);
raw_reserve(min_balance, 2);
send_raw_message(msg.end_cell(), 128);
}
{-
report#_ reporter_id:uint16 to_add:uint16 = IntMsgBody;
-}
() main(cell in_msg_full, slice in_msg_body) impure {
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);
(int my_id, int public_key, int counter, int last_sync, cell own_code) = load_data();
slice expected_address = calc_address(retranslator_init(sender_id, public_key, retranslator_code(), own_code));
throw_unless(401, equal_slice_bits(expected_address, sender_address));
int to_add = in_msg_body~load_uint(16);
counter += to_add - count_report_as_tx; ;; if true - adding one (-(-1)), if false - nothing
if (last_sync == 0) {
last_sync = now() - random() % master_report_timestep;
}
if (last_sync + master_report_timestep <= now()) {
push_to_master(master_counter_address, my_id, counter, last_sync);
last_sync = now();
counter = 0;
}
set_data(pack_counter_data(my_id, public_key, counter, last_sync, own_code));
return ();
}