1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-13 03:32:22 +00:00
ton/crypto/smartcont/highload-wallet-v2-code.fc
2024-01-09 15:49:42 +03:00

87 lines
3.4 KiB
Text

;; Heavy-duty wallet for mass transfers (e.g., for cryptocurrency exchanges)
;; accepts orders for up to 254 internal messages (transfers) in one external message
;; this version does not use seqno for replay protection; instead, it remembers all recent query_ids
;; in this way several external messages with different query_id can be sent in parallel
;; Note, when dealing with highload-wallet the following limits need to be checked and taken into account:
;; 1) Storage size limit. Currently, size of contract storage should be less than 65535 cells. If size of
;; old_queries will grow above this limit, exception in ActionPhase will be thrown and transaction will fail.
;; Failed transaction may be replayed.
;; 2) Gas limit. Currently, gas limit is 1'000'000 gas units, that means that there is a limit of how much
;; old queries may be cleaned in one tx. If number of expired queries will be higher, contract will stuck.
;; That means that it is not recommended to set too high expiration date:
;; number of queries during expiration timespan should not exceed 1000.
;; Also, number of expired queries cleaned in one transaction should be below 100.
;; Such precautions are not easy to follow, so it is recommended to use highload contract
;; only when strictly necessary and the developer understands the above details.
() recv_internal(slice in_msg) impure {
;; do nothing for internal messages
}
() recv_external(slice in_msg) impure {
var signature = in_msg~load_bits(512);
var cs = in_msg;
var (subwallet_id, query_id) = (cs~load_uint(32), cs~load_uint(64));
var bound = (now() << 32);
throw_if(35, query_id < bound);
var ds = get_data().begin_parse();
var (stored_subwallet, last_cleaned, public_key, old_queries) = (ds~load_uint(32), ds~load_uint(64), ds~load_uint(256), ds~load_dict());
ds.end_parse();
(_, var found?) = old_queries.udict_get?(64, query_id);
throw_if(32, found?);
throw_unless(34, subwallet_id == stored_subwallet);
throw_unless(35, check_signature(slice_hash(in_msg), signature, public_key));
var dict = cs~load_dict();
cs.end_parse();
accept_message();
int i = -1;
do {
(i, var cs, var f) = dict.idict_get_next?(16, i);
if (f) {
var mode = cs~load_uint(8);
send_raw_message(cs~load_ref(), mode);
}
} until (~ f);
bound -= (64 << 32); ;; clean up records expired more than 64 seconds ago
old_queries~udict_set_builder(64, query_id, begin_cell());
var queries = old_queries;
do {
var (old_queries', i, _, f) = old_queries.udict_delete_get_min(64);
f~touch();
if (f) {
f = (i < bound);
}
if (f) {
old_queries = old_queries';
last_cleaned = i;
}
} until (~ f);
set_data(begin_cell()
.store_uint(stored_subwallet, 32)
.store_uint(last_cleaned, 64)
.store_uint(public_key, 256)
.store_dict(old_queries)
.end_cell());
}
;; Get methods
;; returns -1 for processed queries, 0 for unprocessed, 1 for unknown (forgotten)
int processed?(int query_id) method_id {
var ds = get_data().begin_parse();
var (_, last_cleaned, _, old_queries) = (ds~load_uint(32), ds~load_uint(64), ds~load_uint(256), ds~load_dict());
ds.end_parse();
(_, var found) = old_queries.udict_get?(64, query_id);
return found ? true : - (query_id <= last_cleaned);
}
int get_public_key() method_id {
var cs = get_data().begin_parse();
cs~load_uint(32 + 64);
return cs.preload_uint(256);
}