From d41ce55305718d207867f0dac60319ec4b6846f2 Mon Sep 17 00:00:00 2001 From: ton Date: Thu, 12 Dec 2019 19:16:25 +0400 Subject: [PATCH] updated fift/func --- crypto/block/block.tlb | 4 +- crypto/block/dump-block.cpp | 14 +- crypto/fift/lib/Asm.fif | 17 +- crypto/fift/lib/GetOpt.fif | 19 +- crypto/fift/words.cpp | 21 +++ crypto/func/analyzer.cpp | 17 +- crypto/func/test/a10.fc | 9 + crypto/func/test/a6_4.fc | 20 ++ crypto/func/test/w8.fc | 22 +++ crypto/func/test/w9.fc | 8 + crypto/smartcont/highload-wallet-v2.fif | 2 +- crypto/smartcont/highload-wallet.fif | 2 +- crypto/smartcont/simple-wallet-code.fc | 2 +- crypto/smartcont/simple-wallet-ext-code.fc | 6 +- crypto/smartcont/stdlib.fc | 10 +- crypto/smartcont/wallet-v2.fif | 2 +- crypto/smartcont/wallet-v3.fif | 2 +- crypto/smartcont/wallet.fif | 2 +- crypto/tl/tlbc.cpp | 9 +- crypto/tl/tlblib.cpp | 6 + crypto/tl/tlblib.hpp | 1 + crypto/vm/continuation.cpp | 208 ++++++++++++++++++++- crypto/vm/continuation.h | 39 +++- crypto/vm/stack.cpp | 149 ++++++++++++++- crypto/vm/stack.hpp | 12 ++ crypto/vm/tonops.cpp | 120 +++++++++++- doc/tvm.tex | 20 +- tddb/td/db/RocksDb.cpp | 12 ++ tddb/td/db/RocksDb.h | 4 + tddb/test/binlog.cpp | 2 +- tddb/test/key_value.cpp | 22 ++- 31 files changed, 717 insertions(+), 66 deletions(-) create mode 100644 crypto/func/test/a10.fc create mode 100644 crypto/func/test/a6_4.fc create mode 100644 crypto/func/test/w8.fc create mode 100644 crypto/func/test/w9.fc diff --git a/crypto/block/block.tlb b/crypto/block/block.tlb index 65537a1a..882e0d7b 100644 --- a/crypto/block/block.tlb +++ b/crypto/block/block.tlb @@ -713,8 +713,8 @@ gas_limits#_ remaining:int64 _:^[ max_limit:int64 cur_limit:int64 credit:int64 ] = VmGasLimits; _ libraries:(HashmapE 256 ^Cell) = VmLibraries; -vm_ctl_data$_ nargs:int14 stack:(Maybe VmStack) save:VmSaveList -cp:int16 = VmControlData; +vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList +cp:(Maybe int16) = VmControlData; vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont; vmc_quit$1000 exit_code:int32 = VmCont; diff --git a/crypto/block/dump-block.cpp b/crypto/block/dump-block.cpp index a29d5c0a..86465bf6 100644 --- a/crypto/block/dump-block.cpp +++ b/crypto/block/dump-block.cpp @@ -199,10 +199,13 @@ void usage() { int main(int argc, char* const argv[]) { int i; int new_verbosity_level = VERBOSITY_NAME(INFO); - bool dump_state = false; + bool dump_state = false, dump_vmcont = false; auto zerostate = std::make_unique(); - while ((i = getopt(argc, argv, "Shv:")) != -1) { + while ((i = getopt(argc, argv, "CShv:")) != -1) { switch (i) { + case 'C': + dump_vmcont = true; + break; case 'S': dump_state = true; break; @@ -230,12 +233,13 @@ int main(int argc, char* const argv[]) { vm::CellSlice cs{vm::NoVm(), boc}; cs.print_rec(std::cout); std::cout << std::endl; - auto& type = dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block; - std::string type_name = dump_state ? "ShardState" : "Block"; + auto& type = !dump_vmcont + ? (dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block) + : block::gen::t_VmCont; type.print_ref(std::cout, boc); std::cout << std::endl; bool ok = type.validate_ref(boc); - std::cout << "(" << (ok ? "" : "in") << "valid " << type_name << ")" << std::endl; + std::cout << "(" << (ok ? "" : "in") << "valid " << type << ")" << std::endl; } } if (!done) { diff --git a/crypto/fift/lib/Asm.fif b/crypto/fift/lib/Asm.fif index 730ef78a..997e06a3 100644 --- a/crypto/fift/lib/Asm.fif +++ b/crypto/fift/lib/Asm.fif @@ -310,14 +310,14 @@ x{A938} @Defop(8u+1) MODPOW2# x{A984} @Defop MULDIV x{A985} @Defop MULDIVR x{A98C} @Defop MULDIVMOD +x{A9A4} @Defop MULRSHIFT +x{A9A5} @Defop MULRSHIFTR +x{A9B4} @Defop(8u+1) MULRSHIFT# +x{A9B5} @Defop(8u+1) MULRSHIFTR# x{A9C4} @Defop LSHIFTDIV x{A9C5} @Defop LSHIFTDIVR x{A9D4} @Defop(8u+1) LSHIFT#DIV x{A9D5} @Defop(8u+1) LSHIFT#DIVR -x{A9E4} @Defop MULRSHIFT -x{A9E5} @Defop MULRSHIFTR -x{A9F4} @Defop(8u+1) MULRSHIFT# -x{A9F5} @Defop(8u+1) MULRSHIFTR# x{AA} @Defop(8u+1) LSHIFT# x{AB} @Defop(8u+1) RSHIFT# x{AC} @Defop LSHIFT @@ -972,12 +972,17 @@ x{F800} @Defop ACCEPT x{F801} @Defop SETGASLIMIT x{F80F} @Defop COMMIT +x{F810} @Defop RANDU256 +x{F811} @Defop RAND +x{F814} @Defop SETRAND +x{F815} dup @Defop ADDRAND @Defop RANDOMIZE + x{F82} @Defop(4u) GETPARAM x{F823} @Defop NOW x{F824} @Defop BLOCKLT x{F825} @Defop LTIME -x{F826} @Defop BALANCE -x{F827} @Defop RANDSEED +x{F826} @Defop RANDSEED +x{F827} @Defop BALANCE x{F828} @Defop MYADDR x{F829} @Defop CONFIGROOT x{F830} @Defop CONFIGDICT diff --git a/crypto/fift/lib/GetOpt.fif b/crypto/fift/lib/GetOpt.fif index d354dd3f..442552b6 100644 --- a/crypto/fift/lib/GetOpt.fif +++ b/crypto/fift/lib/GetOpt.fif @@ -26,9 +26,14 @@ recursive list-delete-range { } : $pfx? // ( s -- ? ) checks whether s is an option (a string beginning with '-') { dup $len 1 > { "-" $pfx? } { drop false } cond } : is-opt? +// ( s -- ? ) checks whether s is a digit option +{ 2 $| drop 1 $| nip $>B 8 B>u@ dup 57 <= swap 48 >= and } : is-digit-opt? +0 box constant disable-digit-opts // ( l -- s i or 0 ) finds first string in l beginning with '-' { 0 { 1+ over null? { 2drop 0 true } { - swap uncons over is-opt? { drop swap true } { nip swap false } cond + swap uncons over is-opt? + { disable-digit-opts @ { over is-digit-opt? not } { true } cond } { false } cond + { drop swap true } { nip swap false } cond } cond } until } : list-find-opt // ( -- s i or 0 ) finds first option in cmdline args @@ -51,11 +56,15 @@ recursive list-delete-range { // ( s -- s' null or s' s'' ) Splits long option --opt=arg at '=' { dup "=" $pos 1+ ?dup { tuck $| swap rot 1- $| drop swap } { null } cond } : split-longopt +// ( l -- f or 0 ) Extracts global option flags from first entry of l +{ dup null? { drop 0 } { car get-opt-flags -256 and } cond +} : get-global-option-flags variable options-list // ( l -- i or 0 ) // parses command line arguments according to option description list l // and returns index i of first incorrect option -{ options-list ! +{ dup options-list ! get-global-option-flags + 256 and disable-digit-opts ! { first-opt dup 0= { true } { swap dup "--" $pfx? { // i s dup $len 2 = { drop dup 1 $*del.. 0 true } { @@ -116,5 +125,7 @@ anon constant opt-list-marker { 6 2swap 4 tuple } : short-long-option-?arg // ( o s -- s' ) Adds help message to option ' , : option-help -// ( s -- o ) Creates a generic help message -{ 'nop 8 "" 3 roll 4 tuple } : generic-help +// ( s f -- o ) Creates a generic help message +{ swap 'nop rot "" 3 roll 4 tuple } : generic-help-setopt +{ 0 generic-help-setopt } : generic-help +256 constant disable-digit-options diff --git a/crypto/fift/words.cpp b/crypto/fift/words.cpp index 7459656b..043fc17f 100644 --- a/crypto/fift/words.cpp +++ b/crypto/fift/words.cpp @@ -2356,6 +2356,25 @@ void interpret_db_run_vm_parallel(IntCtx& ctx) { do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, threads_n, tasks_n); } +void interpret_store_vm_cont(vm::Stack& stack) { + auto vmcont = stack.pop_cont(); + auto cb = stack.pop_builder(); + if (!vmcont->serialize(cb.write())) { + throw IntError{"cannot serialize vm continuation"}; + } + stack.push_builder(std::move(cb)); +} + +void interpret_fetch_vm_cont(vm::Stack& stack) { + auto cs = stack.pop_cellslice(); + auto vmcont = vm::Continuation::deserialize(cs.write()); + if (vmcont.is_null()) { + throw IntError{"cannot deserialize vm continuation"}; + } + stack.push_cellslice(std::move(cs)); + stack.push_cont(std::move(vmcont)); +} + Ref cmdline_args{true}; void interpret_get_fixed_cmdline_arg(vm::Stack& stack, int n) { @@ -2858,6 +2877,8 @@ void init_words_vm(Dictionary& d) { d.def_ctx_word("gasrunvmctx ", std::bind(interpret_run_vm_c7, _1, true)); d.def_ctx_word("dbrunvm ", interpret_db_run_vm); d.def_ctx_word("dbrunvm-parallel ", interpret_db_run_vm_parallel); + d.def_stack_word("vmcont, ", interpret_store_vm_cont); + d.def_stack_word("vmcont@ ", interpret_fetch_vm_cont); } void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]) { diff --git a/crypto/func/analyzer.cpp b/crypto/func/analyzer.cpp index a80bc74e..354486cf 100644 --- a/crypto/func/analyzer.cpp +++ b/crypto/func/analyzer.cpp @@ -431,26 +431,25 @@ bool Op::compute_used_vars(const CodeBlob& code, bool edit) { } case _While: { // while (block0 || left) block1; - // ... { block0 left block1 } block0 left next - VarDescrList after_cond_first{next_var_info}; - after_cond_first += left; - code.compute_used_code_vars(block0, after_cond_first, false); - VarDescrList new_var_info{block0->var_info}; + // ... block0 left { block1 block0 left } next + VarDescrList new_var_info{next_var_info}; bool changes = false; do { - code.compute_used_code_vars(block1, block0->var_info, changes); - VarDescrList after_cond{block1->var_info}; + VarDescrList after_cond{new_var_info}; after_cond += left; code.compute_used_code_vars(block0, after_cond, changes); + code.compute_used_code_vars(block1, block0->var_info, changes); std::size_t n = new_var_info.size(); - new_var_info += block0->var_info; + new_var_info += block1->var_info; new_var_info.clear_last(); if (changes) { break; } changes = (new_var_info.size() == n); } while (changes <= edit); - return set_var_info(std::move(new_var_info)); + new_var_info += left; + code.compute_used_code_vars(block0, new_var_info, edit); + return set_var_info(block0->var_info); } case _Until: { // until (block0 || left); diff --git a/crypto/func/test/a10.fc b/crypto/func/test/a10.fc new file mode 100644 index 00000000..70c7871d --- /dev/null +++ b/crypto/func/test/a10.fc @@ -0,0 +1,9 @@ +_ f(int a, int x) { + int y = 0; + int z = 0; + while ((y = x * x) > a) { + x -= 1; + z = 1; + } + return (y, z); +} diff --git a/crypto/func/test/a6_4.fc b/crypto/func/test/a6_4.fc new file mode 100644 index 00000000..d7e6596a --- /dev/null +++ b/crypto/func/test/a6_4.fc @@ -0,0 +1,20 @@ +var calc_root(m) { + int base = 1; + repeat(70) { base *= 10; } + var (a, b, c) = (1, 0, - m); + var (p1, q1, p2, q2) = (1, 0, 0, 1); + do { + int k = -1; + var (a1, b1, c1) = (0, 0, 0); + do { + k += 1; + (a1, b1, c1) = (a, b, c); + c += b; + c += b += a; + } until (c > 0); + (a, b, c) = (- c1, - b1, - a1); + (p1, q1) = (k * p1 + q1, p1); + (p2, q2) = (k * p2 + q2, p2); + } until (p1 > base); + return (p1, q1, p2, q2); +} diff --git a/crypto/func/test/w8.fc b/crypto/func/test/w8.fc new file mode 100644 index 00000000..bd0696e7 --- /dev/null +++ b/crypto/func/test/w8.fc @@ -0,0 +1,22 @@ +int check_signatures(msg_hash, signatures, signers, bitmask_size) impure { + var bitmask = 0; + var id = -1; + do { + (id, var signature, var f) = signatures.udict_get_next?(32, id); + if (f){ + var sig = signature.preload_bits(512); + var public_key = -1; + do { + (public_key, var cs, var _found) = signers.udict_get_next?(256, public_key); + if (_found){ + if (check_signature(msg_hash, sig, public_key)){ + var signer_index = cs~load_uint(bitmask_size); + bitmask = bitmask | (1 << (signer_index - 1)); + } + } + } until (~ _found); + ;; signature~touch(); + } + } until (~ f); + return bitmask; +} diff --git a/crypto/func/test/w9.fc b/crypto/func/test/w9.fc new file mode 100644 index 00000000..d9435542 --- /dev/null +++ b/crypto/func/test/w9.fc @@ -0,0 +1,8 @@ +_ g(s) { + var (z, t) = (17, s); + while (z > 0) { + t = s; + z -= 1; + } + return ~ t; +} diff --git a/crypto/smartcont/highload-wallet-v2.fif b/crypto/smartcont/highload-wallet-v2.fif index 9651aa53..ba1fc801 100755 --- a/crypto/smartcont/highload-wallet-v2.fif +++ b/crypto/smartcont/highload-wallet-v2.fif @@ -14,7 +14,7 @@ begin-options +"Creates a request with up to 254 orders loaded from to high-load (sub)wallet created by new-highload-v2-wallet.fif, with private key loaded from file .pk " +"and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" +cr +" is a text file with lines `SEND `" - generic-help + disable-digit-options generic-help-setopt "n" "--no-bounce" { false =: allow-bounce } short-long-option "Clears bounce flag" option-help "b" "--force-bounce" { true =: force-bounce } short-long-option diff --git a/crypto/smartcont/highload-wallet.fif b/crypto/smartcont/highload-wallet.fif index f0c0c411..2ab8521e 100755 --- a/crypto/smartcont/highload-wallet.fif +++ b/crypto/smartcont/highload-wallet.fif @@ -14,7 +14,7 @@ begin-options +"Creates a request with up to 254 orders loaded from to high-load (sub)wallet created by new-highload-wallet.fif, with private key loaded from file .pk " +"and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" +cr +" is a text file with lines `SEND `" - generic-help + disable-digit-options generic-help-setopt "n" "--no-bounce" { false =: allow-bounce } short-long-option "Clears bounce flag" option-help "b" "--force-bounce" { true =: force-bounce } short-long-option diff --git a/crypto/smartcont/simple-wallet-code.fc b/crypto/smartcont/simple-wallet-code.fc index 266bc9ac..a43b8b92 100644 --- a/crypto/smartcont/simple-wallet-code.fc +++ b/crypto/smartcont/simple-wallet-code.fc @@ -15,7 +15,7 @@ throw_unless(33, msg_seqno == stored_seqno); throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); accept_message(); - cs~touch_slice(); + cs~touch(); if (cs.slice_refs()) { var mode = cs~load_uint(8); send_raw_message(cs~load_ref(), mode); diff --git a/crypto/smartcont/simple-wallet-ext-code.fc b/crypto/smartcont/simple-wallet-ext-code.fc index a20b96a8..52c4619f 100644 --- a/crypto/smartcont/simple-wallet-ext-code.fc +++ b/crypto/smartcont/simple-wallet-ext-code.fc @@ -30,7 +30,7 @@ slice do_verify_message(slice in_msg, int seqno, int public_key) { (int stored_seqno, int public_key) = load_state(); var cs = do_verify_message(in_msg, stored_seqno, public_key); accept_message(); - cs~touch_slice(); + cs~touch(); if (cs.slice_refs()) { var mode = cs~load_uint(8); send_raw_message(cs~load_ref(), mode); @@ -57,11 +57,7 @@ cell prepare_send_message(int mode, cell msg) method_id { return prepare_send_message_with_seqno(mode, msg, seqno()); } - slice verify_message(slice msg) method_id { var (stored_seqno, public_key) = load_state(); return do_verify_message(msg, stored_seqno, public_key); } - - - diff --git a/crypto/smartcont/stdlib.fc b/crypto/smartcont/stdlib.fc index 12889a13..9041bafe 100644 --- a/crypto/smartcont/stdlib.fc +++ b/crypto/smartcont/stdlib.fc @@ -21,6 +21,8 @@ forall X -> X null() asm "PUSHNULL"; int now() asm "NOW"; slice my_address() asm "MYADDR"; tuple get_balance() asm "BALANCE"; +int cur_lt() asm "LTIME"; +int block_lt() asm "BLOCKLT"; int cell_hash(cell c) asm "HASHCU"; int slice_hash(slice s) asm "HASHSU"; @@ -154,5 +156,9 @@ int cell_null?(cell c) asm "ISNULL"; () send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG"; () set_code(cell new_code) impure asm "SETCODE"; -slice touch_slice(slice s) asm "NOP"; -(slice,()) ~touch_slice(slice s) asm "NOP"; +int random() impure asm "RANDU256"; +int rand(int range) impure asm "RAND"; +int get_seed() impure asm "RANDSEED"; +int set_seed() impure asm "SETRAND"; +() randomize(int x) impure asm "ADDRAND"; +() randomize_lt() impure asm "LTIME" "ADDRAND"; diff --git a/crypto/smartcont/wallet-v2.fif b/crypto/smartcont/wallet-v2.fif index a131d8c5..6bf12103 100755 --- a/crypto/smartcont/wallet-v2.fif +++ b/crypto/smartcont/wallet-v2.fif @@ -14,7 +14,7 @@ begin-options " [-n|-b] [-t] [-B ] [-C ] []" +cr +tab +"Creates a request to advanced wallet created by new-wallet-v2.fif, with private key loaded from file .pk " +"and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" - generic-help + disable-digit-options generic-help-setopt "n" "--no-bounce" { false =: allow-bounce } short-long-option "Clears bounce flag" option-help "b" "--force-bounce" { true =: force-bounce } short-long-option diff --git a/crypto/smartcont/wallet-v3.fif b/crypto/smartcont/wallet-v3.fif index 1568e198..8804acbb 100644 --- a/crypto/smartcont/wallet-v3.fif +++ b/crypto/smartcont/wallet-v3.fif @@ -14,7 +14,7 @@ begin-options " [-n|-b] [-t] [-B ] [-C ] []" +cr +tab +"Creates a request to advanced wallet created by new-wallet-v3.fif, with private key loaded from file .pk " +"and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" - generic-help + disable-digit-options generic-help-setopt "n" "--no-bounce" { false =: allow-bounce } short-long-option "Clears bounce flag" option-help "b" "--force-bounce" { true =: force-bounce } short-long-option diff --git a/crypto/smartcont/wallet.fif b/crypto/smartcont/wallet.fif index c3d628d4..e382b645 100755 --- a/crypto/smartcont/wallet.fif +++ b/crypto/smartcont/wallet.fif @@ -13,7 +13,7 @@ begin-options " [-n|-b] [-B ] [-C ] []" +cr +tab +"Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file .pk " +"and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" - generic-help + disable-digit-options generic-help-setopt "n" "--no-bounce" { false =: allow-bounce } short-long-option "Clears bounce flag" option-help "b" "--force-bounce" { true =: force-bounce } short-long-option diff --git a/crypto/tl/tlbc.cpp b/crypto/tl/tlbc.cpp index ff2e19e6..0364e5db 100644 --- a/crypto/tl/tlbc.cpp +++ b/crypto/tl/tlbc.cpp @@ -102,13 +102,15 @@ unsigned long long get_special_value(std::string str) { unsigned long long val = 0; int bits = 0; if (str[0] == '#') { - while (i < n) { - int c = str[i++]; + for (; i < n; i++) { + int c = str[i]; if (c == '_') { break; } if (c >= '0' && c <= '9') { c -= '0'; + } else if (c >= 'A' && c <= 'F') { + c -= 'A' - 10; } else if (c >= 'a' && c <= 'f') { c -= 'a' - 10; } else { @@ -146,6 +148,9 @@ unsigned long long get_special_value(std::string str) { while (bits && !((val >> (64 - bits)) & 1)) { --bits; } + if (bits) { + --bits; + } } if (bits == 64) { return 0; diff --git a/crypto/tl/tlblib.cpp b/crypto/tl/tlblib.cpp index 40637c57..ae759de5 100644 --- a/crypto/tl/tlblib.cpp +++ b/crypto/tl/tlblib.cpp @@ -33,6 +33,12 @@ const NatWidth t_Nat{32}; const Anything t_Anything; const RefAnything t_RefCell; +std::string TLB::get_type_name() const { + std::ostringstream os; + print_type(os); + return os.str(); +} + bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { int t = get_tag(cs); return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false"); diff --git a/crypto/tl/tlblib.hpp b/crypto/tl/tlblib.hpp index ba9c783f..d90bfcc4 100644 --- a/crypto/tl/tlblib.hpp +++ b/crypto/tl/tlblib.hpp @@ -196,6 +196,7 @@ class TLB { virtual std::ostream& print_type(std::ostream& os) const { return os << ""; } + std::string get_type_name() const; virtual bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const; virtual bool print(PrettyPrinter& pp, const vm::CellSlice& cs) const { vm::CellSlice cs_copy{cs}; diff --git a/crypto/vm/continuation.cpp b/crypto/vm/continuation.cpp index 3946f10d..09d2be89 100644 --- a/crypto/vm/continuation.cpp +++ b/crypto/vm/continuation.cpp @@ -32,6 +32,17 @@ bool Continuation::has_c0() const { return cont_data && cont_data->save.c[0].not_null(); } +bool ControlRegs::clear() { + for (unsigned i = 0; i < creg_num; i++) { + c[i].clear(); + } + for (unsigned i = 0; i < dreg_num; i++) { + d[i].clear(); + } + c7.clear(); + return true; +} + StackEntry ControlRegs::get(unsigned idx) const { if (idx < creg_num) { return get_c(idx); @@ -108,6 +119,7 @@ ControlRegs& ControlRegs::operator&=(const ControlRegs& save) { } bool ControlRegs::serialize(CellBuilder& cb) const { + // _ cregs:(HashmapE 4 VmStackValue) = VmSaveList; Dictionary dict{4}; CellBuilder cb2; for (int i = 0; i < creg_num; i++) { @@ -126,8 +138,34 @@ bool ControlRegs::serialize(CellBuilder& cb) const { std::move(dict).append_dict_to_bool(cb); } +bool ControlRegs::deserialize(CellSlice& cs, int mode) { + // _ cregs:(HashmapE 4 VmStackValue) = VmSaveList; + Ref root; + return cs.fetch_maybe_ref(root) && deserialize(std::move(root), mode); +} + +bool ControlRegs::deserialize(Ref root, int mode) { + try { + clear(); + Dictionary dict{std::move(root), 4}; + return dict.check_for_each([this, mode](Ref val, td::ConstBitPtr key, int n) { + StackEntry value; + return value.deserialize(val.write(), mode) && val->empty_ext() && set((int)key.get_uint(4), std::move(value)); + }); + } catch (VmError&) { + return false; + } +} + +bool ControlData::clear() { + stack.clear(); + save.clear(); + nargs = cp = -1; + return true; +} + bool ControlData::serialize(CellBuilder& cb) const { - // vm_ctl_data$_ nargs:(Maybe int13) stack:(Maybe VmStack) save:VmSaveList + // vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList // cp:(Maybe int16) = VmControlData; return cb.store_bool_bool(nargs >= 0) // vm_ctl_data$_ nargs:(Maybe ... && (nargs < 0 || cb.store_long_bool(nargs, 13)) // ... int13) @@ -138,16 +176,88 @@ bool ControlData::serialize(CellBuilder& cb) const { && (cp == -1 || cb.store_long_bool(cp, 16)); // ... int16) } +bool ControlData::deserialize(CellSlice& cs, int mode) { + // vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList + // cp:(Maybe int16) = VmControlData; + nargs = cp = -1; + stack.clear(); + bool f; + return cs.fetch_bool_to(f) && (!f || cs.fetch_uint_to(13, nargs)) // nargs:(Maybe uint13) + && cs.fetch_bool_to(f) && (!f || Stack::deserialize_to(cs, stack, mode)) // stack:(Maybe VmStack) + && save.deserialize(cs, mode) // save:VmSaveList + && cs.fetch_bool_to(f) && (!f || (cs.fetch_int_to(16, cp) && cp != -1)); // cp:(Maybe int16) +} + bool Continuation::serialize_ref(CellBuilder& cb) const { vm::CellBuilder cb2; return serialize(cb2) && cb.store_ref_bool(cb2.finalize()); } +Ref Continuation::deserialize(CellSlice& cs, int mode) { + if (mode & 0x1002) { + return {}; + } + mode |= 0x1000; + switch (cs.bselect_ext(6, 0x100f011100010001ULL)) { + case 0: + // vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; + return OrdCont::deserialize(cs, mode); + case 1: + // vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont; + return ArgContExt::deserialize(cs, mode); + case 2: + // vmc_quit$1000 exit_code:int32 = VmCont; + return QuitCont::deserialize(cs, mode); + case 3: + // vmc_quit_exc$1001 = VmCont; + return ExcQuitCont::deserialize(cs, mode); + case 4: + // vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont; + return RepeatCont::deserialize(cs, mode); + case 5: + // vmc_until$110000 body:^VmCont after:^VmCont = VmCont; + return UntilCont::deserialize(cs, mode); + case 6: + // vmc_again$110001 body:^VmCont = VmCont; + return AgainCont::deserialize(cs, mode); + case 7: + // vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont; + return WhileCont::deserialize(cs, mode | 0x2000); + case 8: + // vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont; + return WhileCont::deserialize(cs, mode & ~0x2000); + case 9: + // vmc_pushint$1111 value:int32 next:^VmCont = VmCont; + return PushIntCont::deserialize(cs, mode); + default: + return {}; + } +} + +bool Continuation::deserialize_to(Ref cell, Ref& cont, int mode) { + if (cell.is_null()) { + cont.clear(); + return false; + } + CellSlice cs = load_cell_slice(std::move(cell)); + return deserialize_to(cs, cont, mode & ~0x1000) && cs.empty_ext(); +} + bool QuitCont::serialize(CellBuilder& cb) const { // vmc_quit$1000 exit_code:int32 = VmCont; return cb.store_long_bool(8, 4) && cb.store_long_bool(exit_code, 32); } +Ref QuitCont::deserialize(CellSlice& cs, int mode) { + // vmc_quit$1000 exit_code:int32 = VmCont; + int exit_code; + if (cs.fetch_ulong(4) == 8 && cs.fetch_int_to(32, exit_code)) { + return Ref{true, exit_code}; + } else { + return {}; + } +} + int ExcQuitCont::jump(VmState* st) const & { int n = 0; try { @@ -164,6 +274,11 @@ bool ExcQuitCont::serialize(CellBuilder& cb) const { return cb.store_long_bool(9, 4); } +Ref ExcQuitCont::deserialize(CellSlice& cs, int mode) { + // vmc_quit_exc$1001 = VmCont; + return cs.fetch_ulong(4) == 9 ? Ref{true} : Ref{}; +} + int PushIntCont::jump(VmState* st) const & { VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)"; st->get_stack().push_smallint(push_val); @@ -181,6 +296,19 @@ bool PushIntCont::serialize(CellBuilder& cb) const { return cb.store_long_bool(15, 4) && cb.store_long_bool(push_val, 32) && next->serialize_ref(cb); } +Ref PushIntCont::deserialize(CellSlice& cs, int mode) { + // vmc_pushint$1111 value:int32 next:^VmCont = VmCont; + int value; + Ref ref; + Ref next; + if (cs.fetch_ulong(4) == 15 && cs.fetch_int_to(32, value) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), next, mode)) { + return Ref{true, value, std::move(next)}; + } else { + return {}; + } +} + int ArgContExt::jump(VmState* st) const & { st->adjust_cr(data.save); if (data.cp != -1) { @@ -202,6 +330,18 @@ bool ArgContExt::serialize(CellBuilder& cb) const { return cb.store_long_bool(1, 2) && data.serialize(cb) && ext->serialize_ref(cb); } +Ref ArgContExt::deserialize(CellSlice& cs, int mode) { + // vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont; + ControlData cdata; + Ref ref; + Ref next; + mode &= ~0x1000; + return cs.fetch_ulong(2) == 1 && cdata.deserialize(cs, mode) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), next, mode) + ? Ref{true, std::move(next), std::move(cdata)} + : Ref{}; +} + int RepeatCont::jump(VmState* st) const & { VM_LOG(st) << "repeat " << count << " more times (slow)\n"; if (count <= 0) { @@ -236,6 +376,20 @@ bool RepeatCont::serialize(CellBuilder& cb) const { after->serialize_ref(cb); } +Ref RepeatCont::deserialize(CellSlice& cs, int mode) { + // vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont; + long long count; + Ref ref; + Ref body, after; + if (cs.fetch_ulong(5) == 0x14 && cs.fetch_uint_to(63, count) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), after, mode)) { + return Ref{true, std::move(body), std::move(after), count}; + } else { + return {}; + } +} + int VmState::repeat(Ref body, Ref after, long long count) { if (count <= 0) { body.clear(); @@ -268,6 +422,17 @@ bool AgainCont::serialize(CellBuilder& cb) const { return cb.store_long_bool(0x31, 6) && body->serialize_ref(cb); } +Ref AgainCont::deserialize(CellSlice& cs, int mode) { + // vmc_again$110001 body:^VmCont = VmCont; + Ref ref; + Ref body; + if (cs.fetch_ulong(6) == 0x31 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode)) { + return Ref{true, std::move(body)}; + } else { + return {}; + } +} + int VmState::again(Ref body) { return jump(Ref{true, std::move(body)}); } @@ -305,6 +470,18 @@ bool UntilCont::serialize(CellBuilder& cb) const { return cb.store_long_bool(0x30, 6) && body->serialize_ref(cb) && after->serialize_ref(cb); } +Ref UntilCont::deserialize(CellSlice& cs, int mode) { + // vmc_until$110000 body:^VmCont after:^VmCont = VmCont; + Ref ref; + Ref body, after; + if (cs.fetch_ulong(6) == 0x30 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode) && + cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), after, mode)) { + return Ref{true, std::move(body), std::move(after)}; + } else { + return {}; + } +} + int VmState::until(Ref body, Ref after) { if (!body->has_c0()) { set_c0(Ref{true, body, std::move(after)}); @@ -371,6 +548,22 @@ bool WhileCont::serialize(CellBuilder& cb) const { body->serialize_ref(cb) && after->serialize_ref(cb); } +Ref WhileCont::deserialize(CellSlice& cs, int mode) { + // vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont; + // vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont; + bool at_body; + Ref ref; + Ref cond, body, after; + if (cs.fetch_ulong(5) == 0x19 && cs.fetch_bool_to(at_body) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), cond, mode) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) && + deserialize_to(std::move(ref), after, mode)) { + return Ref{true, std::move(cond), std::move(body), std::move(after), !at_body}; + } else { + return {}; + } +} + int VmState::loop_while(Ref cond, Ref body, Ref after) { if (!cond->has_c0()) { set_c0(Ref{true, cond, std::move(body), std::move(after), true}); @@ -392,7 +585,18 @@ int OrdCont::jump_w(VmState* st) & { bool OrdCont::serialize(CellBuilder& cb) const { // vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; - return cb.store_long_bool(1, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb); + return cb.store_long_bool(0, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb, 0x1000); +} + +Ref OrdCont::deserialize(CellSlice& cs, int mode) { + // vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; + ControlData cdata; + StackEntry val; + mode &= ~0x1000; + return cs.fetch_ulong(2) == 0 && cdata.deserialize(cs, mode) && val.deserialize(cs, 0x4000) && + val.is(StackEntry::t_slice) + ? Ref{true, std::move(val).as_slice(), std::move(cdata)} + : Ref{}; } void VmState::init_cregs(bool same_c3, bool push_0) { diff --git a/crypto/vm/continuation.h b/crypto/vm/continuation.h index a5ff599b..61cbc2d6 100644 --- a/crypto/vm/continuation.h +++ b/crypto/vm/continuation.h @@ -37,6 +37,7 @@ struct ControlRegs { Ref c[creg_num]; // c0..c3 Ref d[dreg_num]; // c4..c5 Ref c7; // c7 + bool clear(); Ref get_c(unsigned idx) const { return idx < creg_num ? c[idx] : Ref{}; } @@ -136,6 +137,8 @@ struct ControlRegs { ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0 ControlRegs& operator^=(ControlRegs&& save); bool serialize(CellBuilder& cb) const; + bool deserialize(CellSlice& cs, int mode = 0); + bool deserialize(Ref root, int mode = 0); }; struct ControlData { @@ -151,7 +154,9 @@ struct ControlData { } ControlData(int _cp, Ref _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) { } + bool clear(); bool serialize(CellBuilder& cb) const; + bool deserialize(CellSlice& cs, int mode = 0); }; class Continuation : public td::CntObject { @@ -164,10 +169,6 @@ class Continuation : public td::CntObject { virtual const ControlData* get_cdata() const { return 0; } - virtual bool serialize(CellBuilder& cb) const { - return false; - } - bool serialize_ref(CellBuilder& cb) const; bool has_c0() const; Continuation() { } @@ -181,6 +182,15 @@ class Continuation : public td::CntObject { return *this; } ~Continuation() override = default; + virtual bool serialize(CellBuilder& cb) const { + return false; + } + bool serialize_ref(CellBuilder& cb) const; + static Ref deserialize(CellSlice& cs, int mode = 0); + static bool deserialize_to(CellSlice& cs, Ref& cont, int mode = 0) { + return (cont = deserialize(cs, mode)).not_null(); + } + static bool deserialize_to(Ref cell, Ref& cont, int mode = 0); }; class QuitCont : public Continuation { @@ -194,6 +204,7 @@ class QuitCont : public Continuation { return ~exit_code; } bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class ExcQuitCont : public Continuation { @@ -202,6 +213,7 @@ class ExcQuitCont : public Continuation { ~ExcQuitCont() override = default; int jump(VmState* st) const & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class PushIntCont : public Continuation { @@ -215,6 +227,7 @@ class PushIntCont : public Continuation { int jump(VmState* st) const & override; int jump_w(VmState* st) & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class RepeatCont : public Continuation { @@ -229,6 +242,7 @@ class RepeatCont : public Continuation { int jump(VmState* st) const & override; int jump_w(VmState* st) & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class AgainCont : public Continuation { @@ -241,6 +255,7 @@ class AgainCont : public Continuation { int jump(VmState* st) const & override; int jump_w(VmState* st) & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class UntilCont : public Continuation { @@ -253,6 +268,7 @@ class UntilCont : public Continuation { int jump(VmState* st) const & override; int jump_w(VmState* st) & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class WhileCont : public Continuation { @@ -267,6 +283,7 @@ class WhileCont : public Continuation { int jump(VmState* st) const & override; int jump_w(VmState* st) & override; bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class ArgContExt : public Continuation { @@ -274,9 +291,13 @@ class ArgContExt : public Continuation { Ref ext; public: - ArgContExt(Ref _ext) : data(), ext(_ext) { + ArgContExt(Ref _ext) : data(), ext(std::move(_ext)) { } - ArgContExt(Ref _ext, Ref _stack) : data(_stack), ext(_ext) { + ArgContExt(Ref _ext, Ref _stack) : data(std::move(_stack)), ext(std::move(_ext)) { + } + ArgContExt(Ref _ext, const ControlData& _cdata) : data(_cdata), ext(std::move(_ext)) { + } + ArgContExt(Ref _ext, ControlData&& _cdata) : data(std::move(_cdata)), ext(std::move(_ext)) { } ArgContExt(const ArgContExt&) = default; ArgContExt(ArgContExt&&) = default; @@ -293,6 +314,7 @@ class ArgContExt : public Continuation { return new ArgContExt{*this}; } bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; class OrdCont : public Continuation { @@ -310,6 +332,10 @@ class OrdCont : public Continuation { OrdCont(Ref _code, int _cp, Ref _stack, int nargs = -1) : data(_cp, std::move(_stack), nargs), code(std::move(_code)) { } + OrdCont(Ref _code, const ControlData& _cdata) : data(_cdata), code(std::move(_code)) { + } + OrdCont(Ref _code, ControlData&& _cdata) : data(std::move(_cdata)), code(std::move(_code)) { + } OrdCont(const OrdCont&) = default; OrdCont(OrdCont&&) = default; ~OrdCont() override = default; @@ -342,6 +368,7 @@ class OrdCont : public Continuation { return Ref{true, *this}; } bool serialize(CellBuilder& cb) const override; + static Ref deserialize(CellSlice& cs, int mode = 0); }; struct GasLimits { diff --git a/crypto/vm/stack.cpp b/crypto/vm/stack.cpp index f3c80504..7327df60 100644 --- a/crypto/vm/stack.cpp +++ b/crypto/vm/stack.cpp @@ -688,7 +688,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const { return cb.store_long_bool(0x02ff, 16); } else if (!(mode & 1) && val->signed_fits_bits(64)) { // vm_stk_tinyint#01 value:int64 = VmStackValue; - return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 256); + return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 64); } else { // vm_stk_int#0201_ value:int257 = VmStackValue; return cb.store_long_bool(0x0200 / 2, 15) && cb.store_int256_bool(std::move(val), 257); @@ -701,7 +701,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const { // _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits } // st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice; const auto& cs = *static_cast>(ref); - return cb.store_long_bool(4, 8) // vm_stk_slice#04 _:VmCellSlice = VmStackValue; + return ((mode & 0x1000) || cb.store_long_bool(4, 8)) // vm_stk_slice#04 _:VmCellSlice = VmStackValue; && cb.store_ref_bool(cs.get_base_cell()) // _ cell:^Cell && cb.store_long_bool(cs.cur_pos(), 10) // st_bits:(## 10) && cb.store_long_bool(cs.cur_pos() + cs.size(), 10) // end_bits:(## 10) @@ -738,6 +738,110 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const { } } +bool StackEntry::deserialize(CellSlice& cs, int mode) { + clear(); + int t = (mode & 0xf000) ? ((mode >> 12) & 15) : (int)cs.prefetch_ulong(8); + switch (t) { + case 0: + // vm_stk_null#00 = VmStackValue; + return cs.advance(8); + case 1: { + // vm_stk_tinyint#01 value:int64 = VmStackValue; + td::RefInt256 val; + return !(mode & 1) && cs.advance(8) && cs.fetch_int256_to(64, val) && set_int(std::move(val)); + } + case 2: { + t = (int)cs.prefetch_ulong(16) & 0x1ff; + if (t == 0xff) { + // vm_stk_nan#02ff = VmStackValue; + return cs.advance(16) && set_int(td::RefInt256{true}); + } else { + // vm_stk_int#0201_ value:int257 = VmStackValue; + td::RefInt256 val; + return cs.fetch_ulong(15) == 0x0200 / 2 && cs.fetch_int256_to(257, val) && set_int(std::move(val)); + } + } + case 3: { + // vm_stk_cell#03 cell:^Cell = VmStackValue; + return cs.have_refs() && cs.advance(8) && set(t_cell, cs.fetch_ref()); + } + case 4: { + // _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits } + // st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice; + // vm_stk_slice#04 _:VmCellSlice = VmStackValue; + unsigned st_bits, end_bits, st_ref, end_ref; + Ref cell; + Ref csr; + return ((mode & 0xf000) || cs.advance(8)) // vm_stk_slice#04 + && cs.fetch_ref_to(cell) // cell:^Cell + && cs.fetch_uint_to(10, st_bits) // st_bits:(## 10) + && cs.fetch_uint_to(10, end_bits) // end_bits:(## 10) + && st_bits <= end_bits // { st_bits <= end_bits } + && cs.fetch_uint_to(3, st_ref) // st_ref:(#<= 4) + && cs.fetch_uint_to(3, end_ref) // end_ref:(#<= 4) + && st_ref <= end_ref && end_ref <= 4 // { st_ref <= end_ref } + && (csr = load_cell_slice_ref(std::move(cell))).not_null() // load cell slice + && csr->have(end_bits, end_ref) && + csr.write().skip_last(csr->size() - end_bits, csr->size_refs() - end_ref) && + csr.write().skip_first(st_bits, st_ref) && set(t_slice, std::move(csr)); + } + case 5: { + // vm_stk_builder#05 cell:^Cell = VmStackValue; + Ref cell; + Ref csr; + Ref cb{true}; + return cs.advance(8) && cs.fetch_ref_to(cell) && (csr = load_cell_slice_ref(std::move(cell))).not_null() && + cb.write().append_cellslice_bool(std::move(csr)) && set(t_builder, std::move(cb)); + } + case 6: { + // vm_stk_cont#06 cont:VmCont = VmStackValue; + Ref cont; + return !(mode & 2) && cs.advance(8) && Continuation::deserialize_to(cs, cont, mode) && + set(t_vmcont, std::move(cont)); + } + case 7: { + // vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue; + int n; + if (!(cs.advance(8) && cs.fetch_uint_to(16, n))) { + return false; + } + Ref tuple{true, n}; + auto& t = tuple.write(); + if (n > 1) { + Ref head, tail; + n--; + if (!(cs.fetch_ref_to(head) && cs.fetch_ref_to(tail) && t[n].deserialize(std::move(tail), mode))) { + return false; + } + vm::CellSlice cs2; + while (--n > 0) { + if (!(cs2.load(std::move(head)) && cs2.fetch_ref_to(head) && cs2.fetch_ref_to(tail) && cs2.empty_ext() && + t[n].deserialize(std::move(tail), mode))) { + return false; + } + } + if (!t[0].deserialize(std::move(head), mode)) { + return false; + } + } else if (n == 1) { + return cs.have_refs() && t[0].deserialize(cs.fetch_ref(), mode); + } + return set(t_tuple, std::move(tuple)); + } + default: + return false; + } +} + +bool StackEntry::deserialize(Ref cell, int mode) { + if (cell.is_null()) { + clear(); + return false; + } + CellSlice cs = load_cell_slice(std::move(cell)); + return deserialize(cs, mode) && cs.empty_ext(); +} + bool Stack::serialize(vm::CellBuilder& cb, int mode) const { // vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack; unsigned n = depth(); @@ -758,4 +862,45 @@ bool Stack::serialize(vm::CellBuilder& cb, int mode) const { return cb.store_ref_bool(std::move(rest)) && stack[n - 1].serialize(cb, mode); } +bool Stack::deserialize(vm::CellSlice& cs, int mode) { + clear(); + // vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack; + int n; + if (!cs.fetch_uint_to(24, n)) { + return false; + } + if (!n) { + return true; + } + stack.resize(n); + Ref rest; + if (!(cs.fetch_ref_to(rest) && stack[n - 1].deserialize(cs, mode))) { + clear(); + return false; + } + for (int i = n - 2; i >= 0; --i) { + // vm_stk_cons#_ {n:#} rest:^(VmStackList n) tos:VmStackValue = VmStackList (n + 1); + vm::CellSlice cs2 = load_cell_slice(std::move(rest)); + if (!(cs2.fetch_ref_to(rest) && stack[i].deserialize(cs2, mode) && cs2.empty_ext())) { + clear(); + return false; + } + } + if (!load_cell_slice(std::move(rest)).empty_ext()) { + clear(); + return false; + } + return true; +} + +bool Stack::deserialize_to(vm::CellSlice& cs, Ref& stack, int mode) { + stack = Ref{true}; + if (stack.unique_write().deserialize(cs, mode)) { + return true; + } else { + stack.clear(); + return false; + } +} + } // namespace vm diff --git a/crypto/vm/stack.hpp b/crypto/vm/stack.hpp index 1fce43f2..830f4340 100644 --- a/crypto/vm/stack.hpp +++ b/crypto/vm/stack.hpp @@ -135,6 +135,9 @@ class StackEntry { tp = t_null; return *this; } + bool set_int(td::RefInt256 value) { + return set(t_int, std::move(value)); + } bool empty() const { return tp == t_null; } @@ -168,6 +171,8 @@ class StackEntry { } // mode: +1 = disable short ints, +2 = disable continuations bool serialize(vm::CellBuilder& cb, int mode = 0) const; + bool deserialize(vm::CellSlice& cs, int mode = 0); + bool deserialize(Ref cell, int mode = 0); private: static bool is_list(const StackEntry* se); @@ -195,6 +200,11 @@ class StackEntry { Ref move_as() & { return tp == tag ? Ref{td::static_cast_ref(), std::move(ref)} : td::Ref{}; } + bool set(Type _tp, RefAny _ref) { + tp = _tp; + ref = std::move(_ref); + return ref.not_null() || tp == t_null; + } public: static StackEntry make_list(std::vector&& elems); @@ -511,6 +521,8 @@ class Stack : public td::CntObject { // mode: +1 = add eoln, +2 = Lisp-style lists void dump(std::ostream& os, int mode = 1) const; bool serialize(vm::CellBuilder& cb, int mode = 0) const; + bool deserialize(vm::CellSlice& cs, int mode = 0); + static bool deserialize_to(vm::CellSlice& cs, Ref& stack, int mode = 0); }; } // namespace vm diff --git a/crypto/vm/tonops.cpp b/crypto/vm/tonops.cpp index 8697ddcc..5dcd0d54 100644 --- a/crypto/vm/tonops.cpp +++ b/crypto/vm/tonops.cpp @@ -165,6 +165,8 @@ int exec_set_global_common(VmState* st, unsigned idx) { if (idx >= 255) { throw VmError{Excno::range_chk, "tuple index out of range"}; } + static auto empty_tuple = Ref{true}; + st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7() auto tpay = tuple_extend_set_index(tuple, idx, std::move(x)); if (tpay > 0) { st->consume_tuple_gas(tpay); @@ -193,7 +195,8 @@ void register_ton_config_ops(OpcodeTable& cp0) { .insert(OpcodeInstr::mksimple(0xf823, 16, "NOW", std::bind(exec_get_param, _1, 3, "NOW"))) .insert(OpcodeInstr::mksimple(0xf824, 16, "BLOCKLT", std::bind(exec_get_param, _1, 4, "BLOCKLT"))) .insert(OpcodeInstr::mksimple(0xf825, 16, "LTIME", std::bind(exec_get_param, _1, 5, "LTIME"))) - .insert(OpcodeInstr::mkfixedrange(0xf826, 0xf828, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) + .insert(OpcodeInstr::mksimple(0xf826, 16, "RANDSEED", std::bind(exec_get_param, _1, 6, "RANDSEED"))) + .insert(OpcodeInstr::mksimple(0xf827, 16, "BALANCE", std::bind(exec_get_param, _1, 7, "BALANCE"))) .insert(OpcodeInstr::mksimple(0xf828, 16, "MYADDR", std::bind(exec_get_param, _1, 8, "MYADDR"))) .insert(OpcodeInstr::mksimple(0xf829, 16, "CONFIGROOT", std::bind(exec_get_param, _1, 9, "CONFIGROOT"))) .insert(OpcodeInstr::mkfixedrange(0xf82a, 0xf830, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) @@ -206,6 +209,112 @@ void register_ton_config_ops(OpcodeTable& cp0) { .insert(OpcodeInstr::mkfixedrange(0xf861, 0xf880, 16, 5, instr::dump_1c_and(31, "SETGLOB "), exec_set_global)); } +static constexpr int randseed_idx = 6; + +td::RefInt256 generate_randu256(VmState* st) { + auto tuple = st->get_c7(); + auto t1 = tuple_index(*tuple, 0).as_tuple_range(255); + if (t1.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + auto seedv = tuple_index(*t1, randseed_idx).as_int(); + if (seedv.is_null()) { + throw VmError{Excno::type_chk, "random seed is not an integer"}; + } + unsigned char seed[32]; + if (!seedv->export_bytes(seed, 32, false)) { + throw VmError{Excno::range_chk, "random seed out of range"}; + } + unsigned char hash[64]; + digest::hash_str(hash, seed, 32); + if (!seedv.write().import_bytes(hash, 32, false)) { + throw VmError{Excno::range_chk, "cannot store new random seed"}; + } + td::RefInt256 res{true}; + if (!res.write().import_bytes(hash + 32, 32, false)) { + throw VmError{Excno::range_chk, "cannot store new random number"}; + } + static auto empty_tuple = Ref{true}; + st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7() + tuple.write()[0].clear(); + t1.write().at(randseed_idx) = std::move(seedv); + st->consume_tuple_gas(t1); + tuple.write().at(0) = std::move(t1); + st->consume_tuple_gas(tuple); + st->set_c7(std::move(tuple)); + return res; +} + +int exec_randu256(VmState* st) { + VM_LOG(st) << "execute RANDU256"; + st->get_stack().push_int(generate_randu256(st)); + return 0; +} + +int exec_rand_int(VmState* st) { + VM_LOG(st) << "execute RAND"; + auto& stack = st->get_stack(); + stack.check_underflow(1); + auto x = stack.pop_int_finite(); + auto y = generate_randu256(st); + typename td::BigInt256::DoubleInt tmp{0}; + tmp.add_mul(*x, *y); + tmp.rshift(256, -1).normalize(); + stack.push_int(td::RefInt256{true, tmp}); + return 0; +} + +int exec_set_rand(VmState* st, bool mix) { + VM_LOG(st) << "execute " << (mix ? "ADDRAND" : "SETRAND"); + auto& stack = st->get_stack(); + stack.check_underflow(1); + auto x = stack.pop_int_finite(); + if (!x->unsigned_fits_bits(256)) { + throw VmError{Excno::range_chk, "new random seed out of range"}; + } + auto tuple = st->get_c7(); + auto t1 = tuple_index(*tuple, 0).as_tuple_range(255); + if (t1.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + if (mix) { + auto seedv = tuple_index(*t1, randseed_idx).as_int(); + if (seedv.is_null()) { + throw VmError{Excno::type_chk, "random seed is not an integer"}; + } + unsigned char buffer[64], hash[32]; + if (!std::move(seedv)->export_bytes(buffer, 32, false)) { + throw VmError{Excno::range_chk, "random seed out of range"}; + } + if (!x->export_bytes(buffer + 32, 32, false)) { + throw VmError{Excno::range_chk, "mixed seed value out of range"}; + } + digest::hash_str(hash, buffer, 64); + if (!x.write().import_bytes(hash, 32, false)) { + throw VmError{Excno::range_chk, "new random seed value out of range"}; + } + } + static auto empty_tuple = Ref{true}; + st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7() + tuple.write()[0].clear(); + auto tpay = tuple_extend_set_index(t1, randseed_idx, std::move(x)); + if (tpay > 0) { + st->consume_tuple_gas(tpay); + } + tuple.unique_write()[0] = std::move(t1); + st->consume_tuple_gas(tuple); + st->set_c7(std::move(tuple)); + return 0; +} + +void register_prng_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xf810, 16, "RANDU256", exec_randu256)) + .insert(OpcodeInstr::mksimple(0xf811, 16, "RAND", exec_rand_int)) + .insert(OpcodeInstr::mksimple(0xf814, 16, "SETRAND", std::bind(exec_set_rand, _1, false))) + .insert(OpcodeInstr::mksimple(0xf815, 16, "ADDRAND", std::bind(exec_set_rand, _1, true))); +} + int exec_compute_hash(VmState* st, int mode) { VM_LOG(st) << "execute HASH" << (mode & 1 ? 'S' : 'C') << 'U'; Stack& stack = st->get_stack(); @@ -677,7 +786,7 @@ int exec_set_lib_code(VmState* st) { } return install_output_action(st, cb.finalize()); } - + int exec_change_lib(VmState* st) { VM_LOG(st) << "execute CHANGELIB"; Stack& stack = st->get_stack(); @@ -688,9 +797,9 @@ int exec_change_lib(VmState* st) { throw VmError{Excno::range_chk, "library hash must be non-negative"}; } CellBuilder cb; - if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) - && cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4 - && cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 } + if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) + && cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4 + && cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 } && cb.store_int256_bool(hash, 256, false))) { // libref:LibRef = OutAction; throw VmError{Excno::cell_ov, "cannot serialize library hash into an output action cell"}; } @@ -710,6 +819,7 @@ void register_ton_message_ops(OpcodeTable& cp0) { void register_ton_ops(OpcodeTable& cp0) { register_basic_gas_ops(cp0); register_ton_gas_ops(cp0); + register_prng_ops(cp0); register_ton_config_ops(cp0); register_ton_crypto_ops(cp0); register_ton_currency_address_ops(cp0); diff --git a/doc/tvm.tex b/doc/tvm.tex index 73a2145b..cf6027e8 100644 --- a/doc/tvm.tex +++ b/doc/tvm.tex @@ -1505,7 +1505,7 @@ The general encoding of a {\tt DIV}, {\tt DIVMOD}, or {\tt MOD} operation is {\t \begin{itemize} \item $0\leq m\leq1$ --- Indicates whether there is pre-multiplication ({\tt MULDIV} operation and its variants), possibly replaced by a left shift. \item $0\leq s\leq2$ --- Indicates whether either the multiplication or the division have been replaced by shifts: $s=0$---no replacement, $s=1$---division replaced by a right shift, $s=2$---multiplication replaced by a left shift (possible only for $m=1$). -\item $0\leq c\leq1$ --- Indicates whether there is a constant one-byte argument $tt$ for the shift operator (if $s\neq0$). For $s=0$, $c=0$. If $c=1$, then $0\leq tt\leq 255$, and the shift is performed by $tt+1$ bits. +\item $0\leq c\leq1$ --- Indicates whether there is a constant one-byte argument $tt$ for the shift operator (if $s\neq0$). For $s=0$, $c=0$. If $c=1$, then $0\leq tt\leq 255$, and the shift is performed by $tt+1$ bits. If $s\neq0$ and $c=0$, then the shift amount is provided to the instruction as a top-of-stack {\em Integer\/} in range $0\ldots256$. \item $1\leq d\leq3$ --- Indicates which results of division are required: $1$---only the quotient, $2$---only the remainder, $3$---both. \item $0\leq f\leq2$ --- Rounding mode: $0$---floor, $1$---nearest integer, $2$---ceiling (cf.~\ptref{sp:div.round}). \end{itemize} @@ -1524,6 +1524,14 @@ Examples: \item {\tt A938$tt$} --- {\tt MODPOW2 $tt+1$}: ($x$ -- $x\bmod 2^{tt+1}$). \item {\tt A985} --- {\tt MULDIVR} ($x$ $y$ $z$ -- $q'$), where $q'=\lfloor xy/z+1/2\rfloor$. \item {\tt A98C} --- {\tt MULDIVMOD} ($x$ $y$ $z$ -- $q$ $r$), where $q:=\lfloor x\cdot y/z\rfloor$, $r:=x\cdot y\bmod z$ (same as {\tt */MOD} in Forth). +\item {\tt A9A4} --- {\tt MULRSHIFT} ($x$ $y$ $z$ -- $\lfloor xy\cdot2^{-z}\rfloor$) for $0\leq z\leq 256$. +\item {\tt A9A5} --- {\tt MULRSHIFTR} ($x$ $y$ $z$ -- $\lfloor xy\cdot2^{-z}+1/2\rfloor$) for $0\leq z\leq 256$. +\item {\tt A9B4$tt$} --- {\tt MULRSHIFT $tt+1$} ($x$ $y$ -- $\lfloor xy\cdot 2^{-tt-1}\rfloor$). +\item {\tt A9B5$tt$} --- {\tt MULRSHIFTR $tt+1$} ($x$ $y$ -- $\lfloor xy\cdot 2^{-tt-1}+1/2\rfloor$). +\item {\tt A9C4} --- {\tt LSHIFTDIV} ($x$ $y$ $z$ -- $\lfloor 2^zx/y\rfloor$) for $0\leq z\leq 256$. +\item {\tt A9C5} --- {\tt LSHIFTDIVR} ($x$ $y$ $z$ -- $\lfloor 2^zx/y+1/2\rfloor$) for $0\leq z\leq 256$. +\item {\tt A9D4$tt$} --- {\tt LSHIFTDIV $tt+1$} ($x$ $y$ -- $\lfloor 2^{tt+1}x/y\rfloor$). +\item {\tt A9D5$tt$} --- {\tt LSHIFTDIVR $tt+1$} ($x$ $y$ -- $\lfloor 2^{tt+1}x/y+1/2\rfloor$). \end{itemize} The most useful of these operations are {\tt DIV}, {\tt DIVMOD}, {\tt MOD}, {\tt DIVR}, {\tt DIVC}, {\tt MODPOW2 $t$}, and {\tt RSHIFTR $t$} (for integer arithmetic); and {\tt MULDIVMOD}, {\tt MULDIV}, {\tt MULDIVR}, {\tt LSHIFTDIVR $t$}, and {\tt MULRSHIFTR $t$} (for fixed-point arithmetic). @@ -2165,8 +2173,12 @@ Of the following primitives, only the first two are ``pure'' in the sense that t \end{itemize} \nxsubpoint\emb{Pseudo-random number generator primitives} -The pseudo-random number generator uses the random seed and (sometimes) other data kept in {\tt c7}. +The pseudo-random number generator uses the random seed (parameter \#6, cf.~\ptref{sp:prim.conf.param}), an unsigned 256-bit {\em Integer}, and (sometimes) other data kept in {\tt c7}. The initial value of the random seed before a smart contract is executed in TON Blockchain is a hash of the smart contract address and the global block random seed. If there are several runs of the same smart contract inside a block, then all of these runs will have the same random seed. This can be fixed, for example, by running {\tt LTIME; ADDRAND} before using the pseudo-random number generator for the first time. \begin{itemize} +\item {\tt F810} --- {\tt RANDU256} ( -- $x$), generates a new pseudo-random unsigned 256-bit {\em Integer}~$x$. The algorithm is as follows: if $r$ is the old value of the random seed, considered as a 32-byte array (by constructing the big-endian representation of an unsigned 256-bit integer), then its $\opsc{sha512}(r)$ is computed; the first 32 bytes of this hash are stored as the new value $r'$ of the random seed, and the remaining 32 bytes are returned as the next random value~$x$. +\item {\tt F811} --- {\tt RAND} ($y$ -- $z$), generates a new pseudo-random integer $z$ in the range $0\ldots y-1$ (or $y\ldots-1$, if $y<0$). More precisely, an unsigned random value $x$ is generated as in {\tt RAND256U}; then $z:=\lfloor xy/2^{256}\rfloor$ is computed. Equivalent to {\tt RANDU256; MULRSHIFT 256}. +\item {\tt F814} --- {\tt SETRAND} ($x$ -- ), sets the random seed to unsigned 256-bit {\em Integer\/}~$x$. +\item {\tt F815} --- {\tt ADDRAND} ($x$ -- ), mixes unsigned 256-bit {\em Integer\/}~$x$ into the random seed $r$ by setting the random seed to $\Sha$ of the concatenation of two 32-byte strings: the first with the big-endian representation of the old seed $r$, and the second with the big-endian representation of $x$. \item {\tt F810}--{\tt F81F} --- Reserved for pseudo-random number generator primitives. \end{itemize} @@ -2177,8 +2189,8 @@ The following primitives read configuration data provided in the {\em Tuple\/} s \item {\tt F823} --- {\tt NOW} ( -- $x$), returns the current Unix time as an {\em Integer}. If it is impossible to recover the requested value starting from {\tt c7}, throws a type checking or range checking exception as appropriate. Equivalent to {\tt GETPARAM 3}. \item {\tt F824} --- {\tt BLOCKLT} ( -- $x$), returns the starting logical time of the current block. Equivalent to {\tt GETPARAM 4}. \item {\tt F825} --- {\tt LTIME} ( -- $x$), returns the logical time of the current transaction. Equivalent to {\tt GETPARAM 5}. -\item {\tt F826} --- {\tt BALANCE} ( -- $t$), returns the remaining balance of the smart contract as a {\em Tuple\/} consisting of an {\em Integer} (the remaining Gram balance in nanograms) and a {\em Maybe Cell} (a dictionary with 32-bit keys representing the balance of ``extra currencies''). Equivalent to {\tt GETPARAM 6}. Note that {\tt RAW} primitives such as {\tt SENDRAWMSG} do not update this field. -\item {\tt F827} --- {\tt RANDSEED} ( -- $x$), returns the current random seed as an unsigned 256-bit {\em Integer}. Equivalent to {\tt GETPARAM 7}. +\item {\tt F826} --- {\tt RANDSEED} ( -- $x$), returns the current random seed as an unsigned 256-bit {\em Integer}. Equivalent to {\tt GETPARAM 6}. +\item {\tt F827} --- {\tt BALANCE} ( -- $t$), returns the remaining balance of the smart contract as a {\em Tuple\/} consisting of an {\em Integer} (the remaining Gram balance in nanograms) and a {\em Maybe Cell} (a dictionary with 32-bit keys representing the balance of ``extra currencies''). Equivalent to {\tt GETPARAM 7}. Note that {\tt RAW} primitives such as {\tt SENDRAWMSG} do not update this field. \item {\tt F828} --- {\tt MYADDR} ( -- $s$), returns the internal address of the current smart contract as a {\em Slice\/} with a {\tt MsgAddressInt}. If necessary, it can be parsed further using primitives such as {\tt PARSESTDADDR} or {\tt REWRITESTDADDR}. Equivalent to {\tt GETPARAM 8}. \item {\tt F829} --- {\tt CONFIGROOT} ( -- $D$), returns the {\em Maybe Cell\/}~$D$ with the current global configuration dictionary. Equivalent to {\tt GETPARAM 9}. \item {\tt F830} --- {\tt CONFIGDICT} ( -- $D$ $32$), returns the global configuration dictionary along with its key length (32). Equivalent to {\tt CONFIGROOT}; {\tt PUSHINT 32}. diff --git a/tddb/td/db/RocksDb.cpp b/tddb/td/db/RocksDb.cpp index 16f4b4eb..47fdf4f5 100644 --- a/tddb/td/db/RocksDb.cpp +++ b/tddb/td/db/RocksDb.cpp @@ -19,6 +19,7 @@ #include "td/db/RocksDb.h" #include "rocksdb/db.h" +#include "rocksdb/table.h" #include "rocksdb/statistics.h" #include "rocksdb/write_batch.h" #include "rocksdb/utilities/optimistic_transaction_db.h" @@ -63,6 +64,13 @@ Result RocksDb::open(std::string path) { auto statistics = rocksdb::CreateDBStatistics(); { rocksdb::Options options; + + static auto cache = rocksdb::NewLRUCache(1 << 30); + + rocksdb::BlockBasedTableOptions table_options; + table_options.block_cache = cache; + options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options)); + options.manual_wal_flush = true; options.create_if_missing = true; options.max_background_compactions = 4; @@ -82,6 +90,10 @@ std::unique_ptr RocksDb::snapshot() { } std::string RocksDb::stats() const { + std::string out; + db_->GetProperty("rocksdb.stats", &out); + //db_->GetProperty("rocksdb.cur-size-all-mem-tables", &out); + return out; return statistics_->ToString(); } diff --git a/tddb/td/db/RocksDb.h b/tddb/td/db/RocksDb.h index 04abba77..77ed46b3 100644 --- a/tddb/td/db/RocksDb.h +++ b/tddb/td/db/RocksDb.h @@ -60,6 +60,10 @@ class RocksDb : public KeyValue { RocksDb &operator=(RocksDb &&); ~RocksDb(); + std::shared_ptr raw_db() const { + return db_; + }; + private: std::shared_ptr db_; std::shared_ptr statistics_; diff --git a/tddb/test/binlog.cpp b/tddb/test/binlog.cpp index fc46157c..d2cd3522 100644 --- a/tddb/test/binlog.cpp +++ b/tddb/test/binlog.cpp @@ -679,7 +679,7 @@ TEST(Buffers, CyclicBufferSimple) { auto data = td::rand_string('a', 'z', 100001); td::Slice write_slice = data; td::Slice read_slice = data; - for (size_t i = 1; (int)i < options.count; i++) { + for (size_t i = 1; i < options.count; i++) { ASSERT_EQ((i - 1) * options.chunk_size, reader.reader_size()); ASSERT_EQ((i - 1) * options.chunk_size, writer.writer_size()); auto slice = writer.prepare_write(); diff --git a/tddb/test/key_value.cpp b/tddb/test/key_value.cpp index 550ccd2f..cf147e57 100644 --- a/tddb/test/key_value.cpp +++ b/tddb/test/key_value.cpp @@ -170,7 +170,11 @@ TEST(KeyValue, Bench) { TEST(KeyValue, Stress) { return; td::Slice db_name = "testdb"; - td::RocksDb::destroy(db_name).ignore(); + size_t N = 20; + auto db_name_i = [&](size_t i) { return PSTRING() << db_name << i; }; + for (size_t i = 0; i < N; i++) { + td::RocksDb::destroy(db_name_i(i)).ignore(); + } td::actor::Scheduler scheduler({6}); auto watcher = td::create_shared_destructor([] { td::actor::SchedulerContext::get()->stop(); }); @@ -186,9 +190,13 @@ TEST(KeyValue, Stress) { void tear_down() override { } void loop() override { + if (stat_at_.is_in_past()) { + stat_at_ = td::Timestamp::in(10); + LOG(ERROR) << db_->stats(); + } if (!kv_) { - kv_ = td::KeyValueAsync( - std::make_unique(td::RocksDb::open(db_name_).move_as_ok())); + db_ = std::make_shared(td::RocksDb::open(db_name_).move_as_ok()); + kv_ = td::KeyValueAsync(db_); set_start_at_ = td::Timestamp::now(); } if (next_set_ && next_set_.is_in_past()) { @@ -207,6 +215,7 @@ TEST(KeyValue, Stress) { private: std::shared_ptr watcher_; + std::shared_ptr db_; td::optional> kv_; std::string db_name_; int left_cnt_ = 1000000000; @@ -214,6 +223,7 @@ TEST(KeyValue, Stress) { td::Timestamp next_set_ = td::Timestamp::now(); td::Timestamp set_start_at_; td::Timestamp set_finish_at_; + td::Timestamp stat_at_ = td::Timestamp::in(10); void do_set() { td::UInt128 key = td::UInt128::zero(); @@ -236,8 +246,10 @@ TEST(KeyValue, Stress) { } } }; - scheduler.run_in_context([watcher = std::move(watcher), &db_name]() mutable { - td::actor::create_actor("Worker", watcher, db_name.str()).release(); + scheduler.run_in_context([watcher = std::move(watcher), &db_name_i, &N]() mutable { + for (size_t i = 0; i < N; i++) { + td::actor::create_actor("Worker", watcher, db_name_i(i)).release(); + } watcher.reset(); });