From 3a5086930d837658c612b6a680226331f666904c Mon Sep 17 00:00:00 2001 From: Frej Soya Date: Thu, 8 Sep 2022 22:17:59 +0200 Subject: [PATCH 1/5] Proto/Michelson: Deprecate OPEN_CHEST instruction. --- .../lib_protocol/script_ir_translator.ml | 6 ++-- .../integration/michelson/test_timelock.ml | 29 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/script_ir_translator.ml b/src/proto_alpha/lib_protocol/script_ir_translator.ml index efbfb5dc17dc..42db9ea6c7a4 100644 --- a/src/proto_alpha/lib_protocol/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/script_ir_translator.ml @@ -4201,8 +4201,10 @@ and parse_instr : (* Timelocks *) | ( Prim (loc, I_OPEN_CHEST, [], _), Item_t (Chest_key_t, Item_t (Chest_t, Item_t (Nat_t, rest))) ) -> - let instr = {apply = (fun k -> IOpen_chest (loc, k))} in - typed ctxt loc instr (Item_t (union_bytes_bool_t, rest)) + if legacy then + let instr = {apply = (fun k -> IOpen_chest (loc, k))} in + typed ctxt loc instr (Item_t (union_bytes_bool_t, rest)) + else fail (Deprecated_instruction I_OPEN_CHEST) (* Events *) | Prim (loc, I_EMIT, [], annot), Item_t (data, rest) -> check_packable ~legacy loc data >>?= fun () -> diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml index 76f0b047940a..982fc05507df 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml @@ -32,6 +32,7 @@ *) open Protocol +open Lwt_result_syntax let wrap e = Lwt.return (Environment.wrap_tzresult e) @@ -54,7 +55,30 @@ let simple_test () = assert (result = expected_result) ; return_unit -let contract_test () = +let deprecated_chest_open () = + (* Verify contract fails origination legacy *) + let* block, baker, source_contract, _src2 = Contract_helpers.init () in + let storage = "0xdeadbeef" in + let script = Contract_helpers.read_file "./contracts/timelock.tz" in + Contract_helpers.originate_contract_from_string_hash + ~script + ~storage + ~source_contract + ~baker + block + >>= function + | Ok _ -> Alcotest.fail "script originated successfully, expected an error" + | Error lst + when List.mem + ~equal:( = ) + (Environment.Ecoproto_error + (Script_tc_errors.Deprecated_instruction I_OPEN_CHEST)) + lst -> + return () + | Error errs -> + Alcotest.failf "Unexpected error: %a" Error_monad.pp_print_trace errs + +let disabled_contract_test () = (* Parse a Michelson contract from string. *) let originate_contract file storage src b = let load_file f = @@ -163,5 +187,6 @@ let contract_test () = let tests = [ Tztest.tztest "simple test" `Quick simple_test; - Tztest.tztest "contract test" `Quick contract_test; + Tztest.tztest "deprecated chest_open" `Quick deprecated_chest_open + (* Tztest.tztest "contract test" `Quick disabled_contract_test; *); ] -- GitLab From abec369cdd889ff88ab99cf4632bb80dee54bf0e Mon Sep 17 00:00:00 2001 From: Frej Soya Date: Thu, 8 Sep 2022 22:19:06 +0200 Subject: [PATCH 2/5] Proto/Michelson: Safer resource handling. --- .../lib_protocol/test/helpers/contract_helpers.ml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml index ec4d76ef1425..f7d8dd45bc64 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml @@ -38,10 +38,8 @@ let init () = (** Return contents of a given file as string. *) let read_file f = - let ic = open_in f in - let res = really_input_string ic (in_channel_length ic) in - close_in ic ; - res + In_channel.with_open_text f (fun ic -> + really_input_string ic (in_channel_length ic)) (** Loads a script from file. *) let load_script ~storage file = -- GitLab From 1f8513ed96b724ef5729d3efa141e7b6e544549a Mon Sep 17 00:00:00 2001 From: Frej Soya Date: Thu, 8 Sep 2022 22:06:49 +0200 Subject: [PATCH 3/5] Proto/Michelson: Unify around contract test helper - Use Contract_helpers in both cases. - Note that the disabled test wont be run on CI/make test with this approach --- .../integration/michelson/test_timelock.ml | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml index 982fc05507df..f519dd2ad9c6 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml @@ -56,7 +56,7 @@ let simple_test () = return_unit let deprecated_chest_open () = - (* Verify contract fails origination legacy *) + (* Verify contract fails origination as OPEN_CHEST is marked as legacy (deprecated )*) let* block, baker, source_contract, _src2 = Contract_helpers.init () in let storage = "0xdeadbeef" in let script = Contract_helpers.read_file "./contracts/timelock.tz" in @@ -78,29 +78,21 @@ let deprecated_chest_open () = | Error errs -> Alcotest.failf "Unexpected error: %a" Error_monad.pp_print_trace errs +(* Test to verify open_chest correctness + DISABLED as open_chest is deprecated, but is expected to return. +*) let disabled_contract_test () = - (* Parse a Michelson contract from string. *) - let originate_contract file storage src b = - let load_file f = - let ic = open_in f in - let res = really_input_string ic (in_channel_length ic) in - close_in ic ; - res - in - let contract_string = load_file file in - let code = Expr.toplevel_from_string contract_string in - let storage = Expr.from_string storage in - let script = - Alpha_context.Script.{code = lazy_expr code; storage = lazy_expr storage} - in - Op.contract_origination (B b) src ~fee:(Test_tez.of_int 10) ~script - >>=? fun (operation, dst) -> - Incremental.begin_construction b >>=? fun incr -> - Incremental.add_operation incr operation >>=? fun incr -> - Incremental.finalize_block incr >|=? fun b -> (dst, b) + let* block, baker, source_contract, _src2 = Contract_helpers.init () in + let storage = "0xdeadbeef" in + let script = Contract_helpers.read_file "./contracts/timelock.tz" in + let* dst, _script, block = + Contract_helpers.originate_contract_from_string + ~script + ~storage + ~source_contract + ~baker + block in - Context.init3 ~consensus_threshold:0 () >>=? fun (b, (src, _c2, _c3)) -> - originate_contract "contracts/timelock.tz" "0xaa" src b >>=? fun (dst, b) -> let public, secret = Timelock.gen_rsa_keys () in let locked_value = Timelock.gen_locked_value public in let time = 1000 in @@ -133,9 +125,15 @@ let disabled_contract_test () = Alpha_context.Script.(lazy_expr (Expr.from_string michelson_string)) in let fee = Test_tez.of_int 10 in - Op.transaction ~fee (B b) src dst (Test_tez.of_int 3) ~parameters + Op.transaction + ~fee + (B block) + source_contract + dst + (Test_tez.of_int 3) + ~parameters >>=? fun operation -> - Incremental.begin_construction b >>=? fun incr -> + Incremental.begin_construction block >>=? fun incr -> Incremental.add_operation incr operation >>=? fun incr -> Incremental.finalize_block incr >>=? fun block -> Incremental.begin_construction block >>=? fun incr -> @@ -187,6 +185,9 @@ let disabled_contract_test () = let tests = [ Tztest.tztest "simple test" `Quick simple_test; - Tztest.tztest "deprecated chest_open" `Quick deprecated_chest_open + Tztest.tztest + "verify chest_open fails origination" + `Quick + deprecated_chest_open (* Tztest.tztest "contract test" `Quick disabled_contract_test; *); ] -- GitLab From 49eeb2d0fca224649022206fdd9099244c95963f Mon Sep 17 00:00:00 2001 From: Frej Soya Date: Fri, 9 Sep 2022 15:08:28 +0200 Subject: [PATCH 4/5] Proto/Michelin: Crude example for expected fail Wrap old test in expected failure wrapper. This really belongs in the test framework and not here for bettter reporting. --- .../integration/michelson/test_timelock.ml | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml index f519dd2ad9c6..4f0ac0df7bca 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml @@ -81,7 +81,7 @@ let deprecated_chest_open () = (* Test to verify open_chest correctness DISABLED as open_chest is deprecated, but is expected to return. *) -let disabled_contract_test () = +let contract_test () = let* block, baker, source_contract, _src2 = Contract_helpers.init () in let storage = "0xdeadbeef" in let script = Contract_helpers.read_file "./contracts/timelock.tz" in @@ -182,12 +182,29 @@ let disabled_contract_test () = check_storage chest_correct chest_key_incorrect "01" >>=? fun () -> return_unit +(** + Expect fail wrapper for tests that you expect to return Error or throw an exception. + Useful to keep tests enabled even if they fail, but still run them. + @param test_f test function that is expected to fail. + *) +let expect_fail_result_lwt test_f () = + let open Lwt_syntax in + try + let* res = test_f () in + match res with + | Ok _ -> Alcotest.fail "Expect failure" + | Error _ -> return_ok_unit + with _ -> return_ok_unit + let tests = [ Tztest.tztest "simple test" `Quick simple_test; Tztest.tztest "verify chest_open fails origination" `Quick - deprecated_chest_open - (* Tztest.tztest "contract test" `Quick disabled_contract_test; *); + deprecated_chest_open; + Tztest.tztest + "contract test with chest_open (OK when it fails)" + `Quick + (expect_fail_result_lwt contract_test); ] -- GitLab From e6d2e39e1f4aa35f0db744d6b3f582dbceafcab1 Mon Sep 17 00:00:00 2001 From: Frej Soya Date: Tue, 13 Sep 2022 11:43:16 +0200 Subject: [PATCH 5/5] Doc: Add deprecation notice wrt to timelock. --- docs/protocols/alpha.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index b2980ef2b516..529363514f97 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -187,9 +187,14 @@ Data Availability Layer (ongoing) Distribution of rollup operations data off-chain. (MRs :gl:`!5711`, :gl:`!5938`, :gl:`!6024`, :gl:`!5959`) + Breaking Changes ---------------- +- Deprecate timelock functionality, that is the `CHEST_OPEN` + instruction, in michelson to prevent origination of new contracts using. A + safer version of timelock will come in a future procotol. (MR :gl: `!6260`) + - Rename the parameter ``tokens_per_roll`` to ``minimal_stake``. (MR :gl:`!5897`) RPC Changes -- GitLab