From c7170eafbf931663a10b30e84c6f673359284a51 Mon Sep 17 00:00:00 2001 From: Joel Bjornson Date: Thu, 5 May 2022 14:19:50 +0100 Subject: [PATCH 1/2] Proto: introduce rollup management protocol module --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/dune.inc | 5 + .../sc_rollup_management_protocol.ml | 108 ++++++++++++++++++ .../sc_rollup_management_protocol.mli | 63 ++++++++++ 4 files changed, 177 insertions(+) create mode 100644 src/proto_alpha/lib_protocol/sc_rollup_management_protocol.ml create mode 100644 src/proto_alpha/lib_protocol/sc_rollup_management_protocol.mli diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 5fcda2eccf0e..6904742f91e8 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -180,6 +180,7 @@ "Script_interpreter_defs", "Script_interpreter", + "Sc_rollup_management_protocol", "Sc_rollup_operations", "Sc_rollup_PVM_sem", "Sc_rollup_game", diff --git a/src/proto_alpha/lib_protocol/dune.inc b/src/proto_alpha/lib_protocol/dune.inc index 7998717557e4..4c9b30ca4b51 100644 --- a/src/proto_alpha/lib_protocol/dune.inc +++ b/src/proto_alpha/lib_protocol/dune.inc @@ -192,6 +192,7 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end tx_rollup_ticket.mli tx_rollup_ticket.ml script_interpreter_defs.ml script_interpreter.mli script_interpreter.ml + sc_rollup_management_protocol.mli sc_rollup_management_protocol.ml sc_rollup_operations.mli sc_rollup_operations.ml sc_rollup_PVM_sem.ml sc_rollup_game.mli sc_rollup_game.ml @@ -381,6 +382,7 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end tx_rollup_ticket.mli tx_rollup_ticket.ml script_interpreter_defs.ml script_interpreter.mli script_interpreter.ml + sc_rollup_management_protocol.mli sc_rollup_management_protocol.ml sc_rollup_operations.mli sc_rollup_operations.ml sc_rollup_PVM_sem.ml sc_rollup_game.mli sc_rollup_game.ml @@ -570,6 +572,7 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end tx_rollup_ticket.mli tx_rollup_ticket.ml script_interpreter_defs.ml script_interpreter.mli script_interpreter.ml + sc_rollup_management_protocol.mli sc_rollup_management_protocol.ml sc_rollup_operations.mli sc_rollup_operations.ml sc_rollup_PVM_sem.ml sc_rollup_game.mli sc_rollup_game.ml @@ -781,6 +784,7 @@ include Tezos_raw_protocol_alpha.Main Tx_rollup_ticket Script_interpreter_defs Script_interpreter + Sc_rollup_management_protocol Sc_rollup_operations Sc_rollup_PVM_sem Sc_rollup_game @@ -1011,6 +1015,7 @@ include Tezos_raw_protocol_alpha.Main tx_rollup_ticket.mli tx_rollup_ticket.ml script_interpreter_defs.ml script_interpreter.mli script_interpreter.ml + sc_rollup_management_protocol.mli sc_rollup_management_protocol.ml sc_rollup_operations.mli sc_rollup_operations.ml sc_rollup_PVM_sem.ml sc_rollup_game.mli sc_rollup_game.ml diff --git a/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.ml b/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.ml new file mode 100644 index 000000000000..fdb202b73a23 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.ml @@ -0,0 +1,108 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +open Alpha_context + +type error += + | (* `Permanent *) Error_encode_inbox_message + | (* `Permanent *) Error_decode_inbox_message + +let () = + let open Data_encoding in + let msg = + "Failed to encode a rollup management protocol inbox message value" + in + register_error_kind + `Permanent + ~id:"rollup_management_protocol.error_encoding_inbox_message" + ~title:msg + ~pp:(fun fmt () -> Format.fprintf fmt "%s" msg) + ~description:msg + unit + (function Error_encode_inbox_message -> Some () | _ -> None) + (fun () -> Error_encode_inbox_message) ; + let msg = + "Failed to decode a rollup management protocol inbox message value" + in + register_error_kind + `Permanent + ~id:"rollup_management_protocol.error_decoding_inbox_message" + ~title:msg + ~pp:(fun fmt () -> Format.fprintf fmt "%s" msg) + ~description:msg + unit + (function Error_decode_inbox_message -> Some () | _ -> None) + (fun () -> Error_decode_inbox_message) + +type sc_message = { + payload : Script_repr.expr; + (** A Micheline value containing the parameters passed to the rollup. *) + sender : Alpha_context.Contract.t; (** The L1 caller contract. *) + source : Signature.public_key_hash; + (** The implicit account that originated the transaction. *) +} + +type inbox_message = Sc_message of sc_message + +let make_inbox_message ctxt ty ~payload ~sender ~source = + let open Lwt_tzresult_syntax in + let+ (payload, ctxt) = + Script_ir_translator.unparse_data + ctxt + Script_ir_translator.Optimized + ty + payload + in + let payload = Micheline.strip_locations payload in + (Sc_message {payload; sender; source}, ctxt) + +let sc_inbox_message_encoding = + let open Data_encoding in + conv + (fun {payload; sender; source} -> (payload, sender, source)) + (fun (payload, sender, source) -> {payload; sender; source}) + @@ obj3 + (req "payload" Script_repr.expr_encoding) + (req "sender" Contract.encoding) + (req "source" Signature.Public_key_hash.encoding) + +let inbox_message_encoding = + let open Data_encoding in + conv + (fun (Sc_message m) -> m) + (fun m -> Sc_message m) + sc_inbox_message_encoding + +let bytes_of_inbox_message msg = + let open Tzresult_syntax in + match Data_encoding.Binary.to_bytes_opt inbox_message_encoding msg with + | None -> fail Error_encode_inbox_message + | Some bs -> return bs + +let inbox_message_of_bytes bytes = + let open Tzresult_syntax in + match Data_encoding.Binary.of_bytes_opt inbox_message_encoding bytes with + | None -> fail Error_decode_inbox_message + | Some deposit -> return deposit diff --git a/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.mli b/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.mli new file mode 100644 index 000000000000..dba8bba87333 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_management_protocol.mli @@ -0,0 +1,63 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** The Rollup Management Protocol defines the communication protocol for + exchanging messages between Layer 1 and Layer 2 for a SCORU. + + This module exposes a type {!inbox_message}. Inbox messages produced by the + Layer 1 protocol are encoded using the {!bytes_of_inbox} function, before + added to a SCORU's inbox. + + An {!inbox_message} consists of: + - [payload] the parameters passed to the SCORU. + - [sender] the Layer 1 contract caller. + - [source] the public key hash used for originating the transaction. + + The Layer 2 node is responsible for decoding and interpreting the messages. + *) + +open Alpha_context + +(** A type representing messages from Layer 1 to Layer 2. *) +type inbox_message + +(** [make_inbox_message ctxt ty ~payload ~sender ~source] constructs a SCORU + [inbox message] (an L1 to L2 message) with the given [payload], [sender], + and [source]. *) +val make_inbox_message : + context -> + ('a, _) Script_typed_ir.ty -> + payload:'a -> + sender:Contract.t -> + source:public_key_hash -> + (inbox_message * context) tzresult Lwt.t + +(** [bytes_of_inbox_message msg] encodes an inbox message [msg] in binary + format. *) +val bytes_of_inbox_message : inbox_message -> bytes tzresult + +(** [inbox_message_of_bytes bs] decodes an inbox message from the given bytes + [bs]. *) +val inbox_message_of_bytes : bytes -> inbox_message tzresult -- GitLab From 98c9c6eff9618ba6ef301d197518c9d7dcbd457f Mon Sep 17 00:00:00 2001 From: Joel Bjornson Date: Thu, 5 May 2022 14:20:07 +0100 Subject: [PATCH 2/2] Test: rollup management protocol --- .../lib_protocol/test/unit/main.ml | 3 + .../test_sc_rollup_management_protocol.ml | 110 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_management_protocol.ml diff --git a/src/proto_alpha/lib_protocol/test/unit/main.ml b/src/proto_alpha/lib_protocol/test/unit/main.ml index 4862b4dafa5d..8e1f03660c12 100644 --- a/src/proto_alpha/lib_protocol/test/unit/main.ml +++ b/src/proto_alpha/lib_protocol/test/unit/main.ml @@ -76,5 +76,8 @@ let () = Unit_test.spec "merkle list" Test_merkle_list.tests; Unit_test.spec "sc rollup inbox" Test_sc_rollup_inbox.tests; Unit_test.spec "skip list" Test_skip_list_repr.tests; + Unit_test.spec + "sc rollup management protocol" + Test_sc_rollup_management_protocol.tests; ] |> Lwt_main.run diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_management_protocol.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_management_protocol.ml new file mode 100644 index 000000000000..41684fc5dc17 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_management_protocol.ml @@ -0,0 +1,110 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** Testing + ------- + Component: Protocol (Rollup Management Protocol) + Invocation: dune exec src/proto_alpha/lib_protocol/test/unit/main.exe \ + -- test "^\[Unit\] sc rollup management protocol$" + Subject: Sanity checks for the Rollup Management Protocol module. +*) + +open Protocol +open Alpha_context + +let wrap m = m >|= Environment.wrap_tzresult + +let check_encode_decode_inbox_message effect = + let open Lwt_result_syntax in + let*? bytes = + Environment.wrap_tzresult + @@ Sc_rollup_management_protocol.bytes_of_inbox_message effect + in + let*? message' = + Environment.wrap_tzresult + @@ Sc_rollup_management_protocol.inbox_message_of_bytes bytes + in + let*? bytes' = + Environment.wrap_tzresult + @@ Sc_rollup_management_protocol.bytes_of_inbox_message message' + in + Assert.equal_string + ~loc:__LOC__ + (Bytes.to_string bytes) + (Bytes.to_string bytes') + +let string_ticket ticketer contents amount = + let open WithExceptions in + let amount = Script_int.abs @@ Script_int.of_int amount in + let ticketer = Result.get_ok ~loc:__LOC__ (Contract.of_b58check ticketer) in + let contents = + Result.get_ok ~loc:__LOC__ (Script_string.of_string contents) + in + Script_typed_ir.{ticketer; contents; amount} + +let init_ctxt () = + let open Lwt_result_syntax in + let* (block, _baker, _contract, _src2) = Contract_helpers.init () in + let+ incr = Incremental.begin_construction block in + Incremental.alpha_ctxt incr + +let test_encode_decode_deposit () = + let open WithExceptions in + let open Lwt_result_syntax in + let* ctxt = init_ctxt () in + let*? sender = + Environment.wrap_tzresult + (Contract.of_b58check "KT1BuEZtb68c1Q4yjtckcNjGELqWt56Xyesc") + in + let source = + Result.get_ok + ~loc:__LOC__ + (Signature.Public_key_hash.of_b58check + "tz1RjtZUVeLhADFHDL8UwDZA6vjWWhojpu5w") + in + let*? (Script_typed_ir.Ty_ex_c pair_nat_ticket_string_ty) = + Environment.wrap_tzresult + (let open Result_syntax in + let open Script_typed_ir in + let* ticket_t = ticket_t (-1) string_t in + pair_t (-1) nat_t ticket_t) + in + let payload = + ( Script_int.(abs @@ of_int 42), + string_ticket "KT1ThEdxfUcWUwqsdergy3QnbCWGHSUHeHJq" "red" 1 ) + in + let* (deposit, _ctxt) = + wrap + @@ Sc_rollup_management_protocol.make_inbox_message + ctxt + pair_nat_ticket_string_ty + ~payload + ~sender + ~source + in + check_encode_decode_inbox_message deposit + +let tests = + [Tztest.tztest "Encode/decode deposit" `Quick test_encode_decode_deposit] -- GitLab