Skip to content
This repository was archived by the owner on Jun 12, 2020. It is now read-only.

Commit ecb3e06

Browse files
author
ton
committed
tonlib: big update
1 parent fd7a8de commit ecb3e06

37 files changed

+581
-90
lines changed

catchain/catchain-receiver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ void CatChainReceiverImpl::read_db() {
588588
read_db_ = true;
589589

590590
next_rotate_ = td::Timestamp::in(60 + td::Random::fast(0, 60));
591-
next_sync_ = td::Timestamp::in(0.01 * td::Random::fast(0, 60));
591+
next_sync_ = td::Timestamp::in(0.001 * td::Random::fast(0, 60));
592592
alarm_timestamp().relax(next_rotate_);
593593
alarm_timestamp().relax(next_sync_);
594594

@@ -627,7 +627,7 @@ void CatChainReceiverImpl::receive_query_from_overlay(adnl::AdnlNodeIdShort src,
627627
promise.set_error(td::Status::Error(ErrorCode::notready, "db not read"));
628628
return;
629629
}
630-
td::PerfWarningTimer t{"catchain query process", 0.001};
630+
td::PerfWarningTimer t{"catchain query process", 0.05};
631631
auto F = fetch_tl_object<ton_api::Function>(data.clone(), true);
632632
if (F.is_error()) {
633633
callback_->on_custom_query(get_source_by_adnl_id(src)->get_hash(), std::move(data), std::move(promise));
@@ -909,7 +909,7 @@ void CatChainReceiverImpl::choose_neighbours() {
909909
void CatChainReceiverImpl::alarm() {
910910
alarm_timestamp() = td::Timestamp::never();
911911
if (next_sync_ && next_sync_.is_in_past()) {
912-
next_sync_ = td::Timestamp::in(td::Random::fast(2.0, 3.0));
912+
next_sync_ = td::Timestamp::in(td::Random::fast(0.1, 0.2));
913913
for (auto i = 0; i < 3; i++) {
914914
auto S = get_source(td::Random::fast(0, get_sources_cnt() - 1));
915915
CHECK(S != nullptr);

crypto/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ if (NOT CMAKE_CROSSCOMPILING)
315315
GenFif(DEST smartcont/config-code.fif SOURCE smartcont/config-code.fc)
316316
GenFif(DEST smartcont/wallet-code.fif SOURCE smartcont/wallet-code.fc)
317317
GenFif(DEST smartcont/simple-wallet-code.fif SOURCE smartcont/simple-wallet-code.fc)
318+
GenFif(DEST smartcont/highload-wallet-code.fif SOURCE smartcont/highload-wallet-code.fc)
319+
GenFif(DEST smartcont/highload-wallet-v2-code.fif SOURCE smartcont/highload-wallet-v2-code.fc)
318320
GenFif(DEST smartcont/elector-code.fif SOURCE smartcont/elector-code.fc)
319321
endif()
320322

@@ -333,3 +335,6 @@ target_link_libraries(dump-block PUBLIC ton_crypto fift-lib ton_block)
333335
if (WINGETOPT_FOUND)
334336
target_link_libraries_system(dump-block wingetopt)
335337
endif()
338+
339+
install(TARGETS fift func RUNTIME DESTINATION bin)
340+
install(DIRECTORY fift/lib/ DESTINATION lib/fift)

crypto/parser/srcread.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ bool SourceReader::load_line() {
169169
error("line too long");
170170
return false;
171171
}
172+
if (len && cur_line.back() == '\r') {
173+
// CP/M line breaks support
174+
cur_line.pop_back();
175+
--len;
176+
}
172177
loc.text = cur_line;
173178
cur_line_len = (int)len;
174179
loc.line_pos = 0;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
;; Heavy-duty wallet for mass transfers (e.g., for cryptocurrency exchanges)
2+
;; accepts orders for up to 254 internal messages (transfers) in one external message
3+
4+
() recv_internal(slice in_msg) impure {
5+
;; do nothing for internal messages
6+
}
7+
8+
() recv_external(slice in_msg) impure {
9+
var signature = in_msg~load_bits(512);
10+
var cs = in_msg;
11+
var (subwallet_id, valid_until, msg_seqno) = (cs~load_uint(32), cs~load_uint(32), cs~load_uint(32));
12+
throw_if(35, valid_until <= now());
13+
var ds = get_data().begin_parse();
14+
var (stored_seqno, stored_subwallet, public_key) = (ds~load_uint(32), ds~load_uint(32), ds~load_uint(256));
15+
ds.end_parse();
16+
throw_unless(33, msg_seqno == stored_seqno);
17+
throw_unless(34, subwallet_id == stored_subwallet);
18+
throw_unless(35, check_signature(slice_hash(in_msg), signature, public_key));
19+
var dict = cs~load_dict();
20+
cs.end_parse();
21+
accept_message();
22+
int i = -1;
23+
do {
24+
(i, var cs, var f) = dict.idict_get_next?(16, i);
25+
if (f) {
26+
var mode = cs~load_uint(8);
27+
send_raw_message(cs~load_ref(), mode);
28+
}
29+
} until (~ f);
30+
set_data(begin_cell()
31+
.store_uint(stored_seqno + 1, 32)
32+
.store_uint(stored_subwallet, 32)
33+
.store_uint(public_key, 256)
34+
.end_cell());
35+
}
36+
37+
;; Get methods
38+
39+
int seqno() method_id {
40+
return get_data().begin_parse().preload_uint(32);
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;; Heavy-duty wallet for mass transfers (e.g., for cryptocurrency exchanges)
2+
;; accepts orders for up to 254 internal messages (transfers) in one external message
3+
;; this version does not use seqno for replay protection; instead, it remembers all recent query_ids
4+
;; in this way several external messages with different query_id can be sent in parallel
5+
6+
() recv_internal(slice in_msg) impure {
7+
;; do nothing for internal messages
8+
}
9+
10+
() recv_external(slice in_msg) impure {
11+
var signature = in_msg~load_bits(512);
12+
var cs = in_msg;
13+
var (subwallet_id, query_id) = (cs~load_uint(32), cs~load_uint(64));
14+
var bound = (now() << 32);
15+
throw_if(35, query_id < bound);
16+
var ds = get_data().begin_parse();
17+
var (stored_subwallet, last_cleaned, public_key, old_queries) = (ds~load_uint(32), ds~load_uint(64), ds~load_uint(256), ds~load_dict());
18+
ds.end_parse();
19+
(_, var found?) = old_queries.udict_get?(64, query_id);
20+
throw_if(32, found?);
21+
throw_unless(34, subwallet_id == stored_subwallet);
22+
throw_unless(35, check_signature(slice_hash(in_msg), signature, public_key));
23+
var dict = cs~load_dict();
24+
cs.end_parse();
25+
accept_message();
26+
int i = -1;
27+
do {
28+
(i, var cs, var f) = dict.idict_get_next?(16, i);
29+
if (f) {
30+
var mode = cs~load_uint(8);
31+
send_raw_message(cs~load_ref(), mode);
32+
}
33+
} until (~ f);
34+
bound -= (64 << 32); ;; clean up records expired more than 64 seconds ago
35+
old_queries~udict_set_builder(64, query_id, begin_cell());
36+
var queries = old_queries;
37+
do {
38+
var (old_queries', i, _, f) = old_queries.udict_delete_get_min(64);
39+
f~touch();
40+
if (f) {
41+
f = (i < bound);
42+
}
43+
if (f) {
44+
old_queries = old_queries';
45+
last_cleaned = i;
46+
}
47+
} until (~ f);
48+
set_data(begin_cell()
49+
.store_uint(stored_subwallet, 32)
50+
.store_uint(last_cleaned, 64)
51+
.store_uint(public_key, 256)
52+
.store_dict(old_queries)
53+
.end_cell());
54+
}
55+
56+
;; Get methods
57+
58+
;; returns -1 for processed queries, 0 for unprocessed, 1 for unknown (forgotten)
59+
int processed?(int query_id) method_id {
60+
var ds = get_data().begin_parse();
61+
var (_, last_cleaned, _, old_queries) = (ds~load_uint(32), ds~load_uint(64), ds~load_uint(256), ds~load_dict());
62+
ds.end_parse();
63+
(_, var found) = old_queries.udict_get?(64, query_id);
64+
return found ? true : - (query_id <= last_cleaned);
65+
}

crypto/smartcont/highload-wallet.fif

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env fift -s
2+
"TonUtil.fif" include
3+
4+
{ ."usage: " @' $0 type ." <filename-base> <subwallet-id> <seqno> <order-file> [<savefile>]" cr
5+
."Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-wallet.fif, with private key loaded from file <filename-base>.pk "
6+
."and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" cr
7+
."<order-file> is a text file with lines `SEND <dest-addr> <amount>`" cr 1 halt
8+
} : usage
9+
$# dup 4 < swap 5 > or ' usage if
10+
11+
$1 =: file-base
12+
$2 parse-int dup 32 fits ' usage ifnot =: subwallet-id // parse subwallet-id
13+
{ subwallet-id (.) $+ } : +subwallet
14+
$3 parse-int =: seqno
15+
$4 =: order-file
16+
def? $5 { @' $5 } { "wallet-query" } cond constant savefile
17+
3 constant send-mode // mode for SENDRAWMSG: +1 - sender pays fees, +2 - ignore errors
18+
60 constant timeout // external message expires in 60 seconds
19+
20+
file-base +subwallet +".addr" load-address
21+
2dup 2constant wallet_addr
22+
."Source wallet address = " 2dup .addr cr 6 .Addr cr
23+
file-base +".pk" load-keypair nip constant wallet_pk
24+
25+
variable orders dictnew orders !
26+
variable order# order# 0!
27+
// c --
28+
{ <s order# @ dup 254 >= abort"more than 254 orders"
29+
orders @ 16 udict!+ not abort"cannot add order to dictionary"
30+
orders ! order# 1+!
31+
} : add-order
32+
// b body -- b'
33+
{ tuck <s 2dup s-fits? not rot over 1 i, -rot
34+
{ drop swap ref, } { s, nip } cond
35+
} : append-msg-body
36+
// ng wc addr bounce body -- c
37+
{ <b b{01} s, rot 1 i, b{000100} s, 2swap addr, rot Gram,
38+
0 9 64 32 + + 1+ u, swap append-msg-body b>
39+
} : create-int-msg
40+
// ng wc addr bnc --
41+
{ ."Transferring " 3 roll .GR ."to account "
42+
-rot 2dup 4 pick 7 + .Addr ." = " .addr ." bounce=" . cr
43+
} : .transfer
44+
// addr$ ng -- c
45+
{ swap parse-smc-addr // ng wc addr bnc
46+
2over 2over .transfer
47+
<b 0 32 u, b> create-int-msg
48+
} : create-simple-transfer
49+
// c m -- c'
50+
{ <b swap 8 u, swap ref, b> } : create-order
51+
52+
// addr$ ng --
53+
{ create-simple-transfer send-mode create-order add-order } : send
54+
{ bl word bl word $>GR send } : SEND
55+
56+
// parse order file
57+
order-file include
58+
59+
// create external message
60+
<b subwallet-id 32 i, now timeout + 32 u, seqno 32 u, orders @ dict, b>
61+
dup ."signing message: " <s csr. cr
62+
dup hash wallet_pk ed25519_sign_uint
63+
<b b{1000100} s, wallet_addr addr, 0 Gram, b{00} s,
64+
swap B, swap <s s, b>
65+
dup ."resulting external message: " <s csr. cr
66+
2 boc+>B dup Bx. cr
67+
savefile +".boc" tuck B>file
68+
."(Saved to file " type .")" cr
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env fift -s
2+
"TonUtil.fif" include
3+
"Asm.fif" include
4+
5+
{ ."usage: " @' $0 type ." <workchain-id> <subwallet-id> [<filename-base>]" cr
6+
."Creates a new high-load wallet in the specified workchain, with the controlling private key saved to or loaded from <filename-base>.pk "
7+
."('new-wallet.pk' by default)" cr
8+
."<subwallet-id> is the 32-bit identifier of this subwallet among all controlled by the same private key" cr 1 halt
9+
} : usage
10+
$# 2- -2 and ' usage if
11+
12+
$1 parse-workchain-id =: wc // set workchain id from command line argument
13+
$2 parse-int dup =: subwallet-id // parse subwallet-id
14+
32 fits ' usage ifnot
15+
{ subwallet-id (.) $+ } : +subwallet
16+
def? $3 { @' $3 } { "new-wallet" } cond constant file-base
17+
18+
."Creating new high-load wallet in workchain " wc .
19+
."with subwallet id " subwallet-id . cr
20+
21+
// Create new high-load wallet; source code included from `highload-wallet-code.fif`
22+
"highload-wallet-code.fif" include
23+
// code
24+
<b 0 32 u, subwallet-id 32 i,
25+
file-base +".pk" load-generate-keypair
26+
constant wallet_pk
27+
B,
28+
b> // data
29+
null // no libraries
30+
<b b{0011} s, 3 roll ref, rot ref, swap dict, b> // create StateInit
31+
dup ."StateInit: " <s csr. cr
32+
dup hash wc swap 2dup 2constant wallet_addr
33+
."new wallet address = " 2dup .addr cr
34+
2dup file-base +subwallet +".addr" save-address-verbose
35+
."Non-bounceable address (for init): " 2dup 7 .Addr cr
36+
."Bounceable address (for later access): " 6 .Addr cr
37+
<b subwallet-id 32 i, -1 32 i, 0 32 u, false 1 i, b>
38+
dup ."signing message: " <s csr. cr
39+
dup hash wallet_pk ed25519_sign_uint rot
40+
<b b{1000100} s, wallet_addr addr, b{000010} s, swap <s s, b{0} s, swap B, swap <s s, b>
41+
dup ."External message for initialization is " <s csr. cr
42+
2 boc+>B dup Bx. cr
43+
file-base +subwallet +"-query.boc" tuck B>file
44+
."(Saved wallet creating query to file " type .")" cr

crypto/smartcont/stdlib.fc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ cell dict_set_builder(cell dict, int key_len, slice index, builder value) asm(va
8686
(cell, (slice, slice, int)) ~dict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
8787
(int, slice, int) udict_get_next?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETNEXT" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
8888
(int, slice, int) udict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETPREV" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
89+
(int, slice, int) idict_get_next?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETNEXT" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
90+
(int, slice, int) idict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETPREV" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
8991
cell new_dict() asm "NEWDICT";
9092
int dict_empty?(cell c) asm "DICTEMPTY";
9193

tl/generate/scheme/tonlib_api.tl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ vector {t:Type} # [ t ] = Vector t;
1616
error code:int32 message:string = Error;
1717
ok = Ok;
1818

19+
keyStoreTypeDirectory directory:string = KeyStoreType;
20+
keyStoreTypeInMemory = KeyStoreType;
21+
1922
config config:string blockchain_name:string use_callbacks_for_network:Bool ignore_cache:Bool = Config;
20-
options config:config keystore_directory:string = Options;
23+
24+
options config:config keystore_type:KeyStoreType = Options;
2125

2226
key public_key:string secret:secureBytes = Key;
2327
inputKey key:key local_password:secureBytes = InputKey;
@@ -37,7 +41,7 @@ raw.initialAccountState code:bytes data:bytes = raw.InitialAccountState;
3741
raw.accountState balance:int64 code:bytes data:bytes last_transaction_id:internal.transactionId sync_utime:int53 = raw.AccountState;
3842
raw.message source:string destination:string value:int64 message:bytes = raw.Message;
3943
raw.transaction utime:int53 data:bytes transaction_id:internal.transactionId fee:int64 in_msg:raw.message out_msgs:vector<raw.message> = raw.Transaction;
40-
raw.transactions transactions:vector<raw.Transaction> previous_transaction_id:internal.transactionId = raw.Transactions;
44+
raw.transactions transactions:vector<raw.transaction> previous_transaction_id:internal.transactionId = raw.Transactions;
4145

4246
testWallet.initialAccountState public_key:string = testWallet.InitialAccountState;
4347
testWallet.accountState balance:int64 seqno:int32 last_transaction_id:internal.transactionId sync_utime:int53 = testWallet.AccountState;

tl/generate/scheme/tonlib_api.tlo

204 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)