From 12c4e5b09cbf1c42c130a783e2e3d0402b611b46 Mon Sep 17 00:00:00 2001 From: ovidiu deac Date: Thu, 22 Sep 2022 17:05:31 +0300 Subject: [PATCH 1/3] SCORU: in sc_rollup_node add dependency on scoru_wasm --- manifest/main.ml | 1 + opam/tezos-sc-rollup-node-013-PtJakart.opam | 1 + opam/tezos-sc-rollup-node-014-PtKathma.opam | 1 + opam/tezos-sc-rollup-node-alpha.opam | 1 + src/proto_013_PtJakart/bin_sc_rollup_node/dune | 3 ++- src/proto_014_PtKathma/bin_sc_rollup_node/dune | 3 ++- src/proto_alpha/bin_sc_rollup_node/dune | 3 ++- 7 files changed, 10 insertions(+), 3 deletions(-) diff --git a/manifest/main.ml b/manifest/main.ml index 3ed9815da06a..2a15f8f3e0b7 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -4624,6 +4624,7 @@ module Protocol = Protocol ringo; ringo_lwt; injector |> if_some |> open_; + octez_scoru_wasm; ] in let tx_rollup = diff --git a/opam/tezos-sc-rollup-node-013-PtJakart.opam b/opam/tezos-sc-rollup-node-013-PtJakart.opam index 9cb54d57fa59..4ffc02327e19 100644 --- a/opam/tezos-sc-rollup-node-013-PtJakart.opam +++ b/opam/tezos-sc-rollup-node-013-PtJakart.opam @@ -31,6 +31,7 @@ depends: [ "ringo" { >= "0.9" } "ringo-lwt" { >= "0.9" } "tezos-injector-013-PtJakart" + "tezos-scoru-wasm" ] build: [ ["rm" "-r" "vendors"] diff --git a/opam/tezos-sc-rollup-node-014-PtKathma.opam b/opam/tezos-sc-rollup-node-014-PtKathma.opam index aefc968fd582..c8a33144eb0a 100644 --- a/opam/tezos-sc-rollup-node-014-PtKathma.opam +++ b/opam/tezos-sc-rollup-node-014-PtKathma.opam @@ -31,6 +31,7 @@ depends: [ "ringo" { >= "0.9" } "ringo-lwt" { >= "0.9" } "tezos-injector-014-PtKathma" + "tezos-scoru-wasm" ] build: [ ["rm" "-r" "vendors"] diff --git a/opam/tezos-sc-rollup-node-alpha.opam b/opam/tezos-sc-rollup-node-alpha.opam index 8b6e5315770c..8e620ad04d78 100644 --- a/opam/tezos-sc-rollup-node-alpha.opam +++ b/opam/tezos-sc-rollup-node-alpha.opam @@ -32,6 +32,7 @@ depends: [ "ringo" { >= "0.9" } "ringo-lwt" { >= "0.9" } "tezos-injector-alpha" + "tezos-scoru-wasm" ] build: [ ["rm" "-r" "vendors"] diff --git a/src/proto_013_PtJakart/bin_sc_rollup_node/dune b/src/proto_013_PtJakart/bin_sc_rollup_node/dune index 7f9be3976971..124a12381728 100644 --- a/src/proto_013_PtJakart/bin_sc_rollup_node/dune +++ b/src/proto_013_PtJakart/bin_sc_rollup_node/dune @@ -31,7 +31,8 @@ irmin ringo ringo-lwt - tezos-injector-013-PtJakart) + tezos-injector-013-PtJakart + tezos-scoru-wasm) (link_flags (:standard) (:include %{workspace_root}/static-link-flags.sexp)) diff --git a/src/proto_014_PtKathma/bin_sc_rollup_node/dune b/src/proto_014_PtKathma/bin_sc_rollup_node/dune index 8a29a9f19848..d2e3f873d7ef 100644 --- a/src/proto_014_PtKathma/bin_sc_rollup_node/dune +++ b/src/proto_014_PtKathma/bin_sc_rollup_node/dune @@ -31,7 +31,8 @@ irmin ringo ringo-lwt - tezos-injector-014-PtKathma) + tezos-injector-014-PtKathma + tezos-scoru-wasm) (link_flags (:standard) (:include %{workspace_root}/static-link-flags.sexp)) diff --git a/src/proto_alpha/bin_sc_rollup_node/dune b/src/proto_alpha/bin_sc_rollup_node/dune index 6e27d9926208..72aa7d93598d 100644 --- a/src/proto_alpha/bin_sc_rollup_node/dune +++ b/src/proto_alpha/bin_sc_rollup_node/dune @@ -32,7 +32,8 @@ irmin ringo ringo-lwt - tezos-injector-alpha) + tezos-injector-alpha + tezos-scoru-wasm) (link_flags (:standard) (:include %{workspace_root}/static-link-flags.sexp)) -- GitLab From 6c1e82bcae2ca1089c76a17aafda7b3fc68115e0 Mon Sep 17 00:00:00 2001 From: ovidiu deac Date: Thu, 22 Sep 2022 17:04:03 +0300 Subject: [PATCH 2/3] PROTO: decouple L2 node from protocol PVM signature --- .../bin_sc_rollup_node/wasm_2_0_0_pvm.ml | 92 ++++++++++++++++++- .../lib_client/client_proto_rollups.ml | 4 +- .../lib_protocol/alpha_context.mli | 4 +- .../lib_protocol/sc_rollup_wasm.ml | 71 ++++++++------ .../lib_protocol/sc_rollup_wasm.mli | 4 +- .../test/helpers/sc_rollup_helpers.ml | 2 +- .../test/integration/test_sc_rollup_wasm.ml | 5 +- 7 files changed, 148 insertions(+), 34 deletions(-) diff --git a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml index 5de2060ce12f..1a607bd2d48c 100644 --- a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml +++ b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml @@ -44,8 +44,98 @@ module Wasm_2_0_0_proof_format = .tree_proof_encoding end) +module Conversions = struct + (* BEWARE OF UGLY HACK !!! + Environment.Wasm_2_0_0.info/input/output are not compatible with + Tezos_scoru_wasm.Wasm_pvm_sig.info/input_info/output_info/ + although they should be, by their definitions: + + ``` + type input = Tezos_scoru_wasm.Wasm_pvm_sig.input_info = ... + type output = Tezos_scoru_wasm.Wasm_pvm_sig.input_info = ... + ``` + + Somehow the type equality is not preserved through the protocol environment. + + This is why we need to do the de/reconstruction manualy. + *) + module EInt32 = Environment.Bounded.Non_negative_int32 + module SInt32 = Bounded.Non_negative_int32 + module Opt = Stdlib.Option + module Scoru = Tezos_scoru_wasm.Wasm_pvm_sig + module Env = Environment.Wasm_2_0_0 + + let from_env_int32 (i : EInt32.t) : SInt32.t = + EInt32.to_value i |> SInt32.of_value |> Opt.get + + let to_env_int32 (i : SInt32.t) : EInt32.t = + SInt32.to_value i |> EInt32.of_value |> Opt.get + + let to_env_input (input : Scoru.input_info) : Env.input = + { + inbox_level = to_env_int32 input.inbox_level; + message_counter = input.message_counter; + } + + let to_env_input_request : Scoru.input_request -> Env.input_request = function + | Scoru.No_input_required -> Env.No_input_required + | Input_required -> Input_required + + let of_env_input (input : Env.input) : Scoru.input_info = + { + inbox_level = from_env_int32 input.inbox_level; + message_counter = input.message_counter; + } + + let of_env_output (output : Env.output) : Scoru.output_info = + { + outbox_level = from_env_int32 output.outbox_level; + message_index = output.message_index; + } + + let to_env_info (s_info : Scoru.info) : Env.info = + { + current_tick = s_info.current_tick; + last_input_read = Opt.map to_env_input s_info.last_input_read; + input_request = to_env_input_request s_info.input_request; + } +end + +module type TreeS = + Tezos_context_sigs.Context.TREE + with type key = string list + and type value = bytes + +module Make_backend (Tree : TreeS) = struct + type Lazy_containers.Lazy_map.tree += PVM_tree of Tree.tree + + module Wasm_pvm = Tezos_scoru_wasm.Wasm_pvm.Make (struct + include Tree + + let select = function + | PVM_tree t -> t + | _ -> raise Tree_encoding.Incorrect_tree_type + + let wrap t = PVM_tree t + end) + + let compute_step (tree : Tree.tree) = Wasm_pvm.compute_step tree + + include Conversions + + let set_input_step (input : Environment.Wasm_2_0_0.input) payload + (tree : Tree.tree) = + Wasm_pvm.set_input_step (of_env_input input) payload tree + + let get_output (output : Environment.Wasm_2_0_0.output) (tree : Tree.tree) = + Wasm_pvm.get_output (of_env_output output) tree + + let get_info (tree : Tree.tree) = + Lwt.map to_env_info @@ Wasm_pvm.get_info tree +end + module Impl : Pvm.S = struct - include Sc_rollup.Wasm_2_0_0PVM.Make (Wasm_2_0_0_proof_format) + include Sc_rollup.Wasm_2_0_0PVM.Make (Make_backend) (Wasm_2_0_0_proof_format) module State = Context.PVMState let string_of_status : status -> string = function diff --git a/src/proto_alpha/lib_client/client_proto_rollups.ml b/src/proto_alpha/lib_client/client_proto_rollups.ml index 3d931c35df9a..f41391a9c48c 100644 --- a/src/proto_alpha/lib_client/client_proto_rollups.ml +++ b/src/proto_alpha/lib_client/client_proto_rollups.ml @@ -144,7 +144,9 @@ module ScRollup = struct and type proof = Tezos_context_memory.Context.Proof.tree Tezos_context_memory.Context.Proof.t = - Sc_rollup.Wasm_2_0_0PVM.Make (In_memory_context) + Sc_rollup.Wasm_2_0_0PVM.Make + (Environment.Wasm_2_0_0.Make) + (In_memory_context) let origination_proof_exn ~boot_sector kind = let aux = function diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 0d05acff3f52..f0905280e2c0 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3379,7 +3379,9 @@ module Sc_rollup : sig (proof * 'a) option Lwt.t end - module Make (C : P) : sig + module type Make_wasm = module type of Wasm_2_0_0.Make + + module Make (Wasm_backend : Make_wasm) (C : P) : sig include PVM.S with type context = C.Tree.t diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml index 221d652ea465..acb3d0a5b758 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -55,9 +55,13 @@ module V2_0_0 = struct open Sc_rollup_repr module PS = Sc_rollup_PVM_sig + module type TreeS = + Context.TREE with type key = string list and type value = bytes + + module type Make_wasm = module type of Wasm_2_0_0.Make + module type P = sig - module Tree : - Context.TREE with type key = string list and type value = bytes + module Tree : TreeS type tree = Tree.tree @@ -97,7 +101,12 @@ module V2_0_0 = struct val get_outbox : state -> Sc_rollup_PVM_sig.output list Lwt.t end - module Make (Context : P) : + (* [Make (Make_backend) (Context)] creates a PVM. + + The Make_backend is a functor that creates the backend of the PVM. + The Conext provides the tree and the proof types. + *) + module Make (Make_backend : Make_wasm) (Context : P) : S with type context = Context.Tree.t and type state = Context.tree @@ -169,11 +178,11 @@ module V2_0_0 = struct end end - module WASM_machine = Wasm_2_0_0.Make (Tree) - open State - type state = State.state + module WASM_machine = Make_backend (Tree) + open State + let pp _state = Lwt.return @@ fun fmt () -> Format.pp_print_string fmt "" @@ -426,38 +435,44 @@ module V2_0_0 = struct end end - module Protocol_implementation = Make (struct - module Tree = struct - include Context.Tree + module Protocol_implementation = + Make + (Wasm_2_0_0.Make) + (struct + module Tree = struct + include Context.Tree - type tree = Context.tree + type tree = Context.tree - type t = Context.t + type t = Context.t - type key = string list + type key = string list - type value = bytes - end + type value = bytes + end - type tree = Context.tree + type tree = Context.tree - type proof = Context.Proof.tree Context.Proof.t + type proof = Context.Proof.tree Context.Proof.t - let verify_proof p f = - Lwt.map Result.to_option (Context.verify_tree_proof p f) + let verify_proof p f = + Lwt.map Result.to_option (Context.verify_tree_proof p f) - let produce_proof _context _state _f = - (* Can't produce proof without full context*) - Lwt.return None + let produce_proof _context _state _f = + (* Can't produce proof without full context*) + Lwt.return None - let kinded_hash_to_state_hash = function - | `Value hash | `Node hash -> State_hash.context_hash_to_state_hash hash + let kinded_hash_to_state_hash = function + | `Value hash | `Node hash -> + State_hash.context_hash_to_state_hash hash - let proof_before proof = - kinded_hash_to_state_hash proof.Context.Proof.before + let proof_before proof = + kinded_hash_to_state_hash proof.Context.Proof.before - let proof_after proof = kinded_hash_to_state_hash proof.Context.Proof.after + let proof_after proof = + kinded_hash_to_state_hash proof.Context.Proof.after - let proof_encoding = Context.Proof_encoding.V2.Tree32.tree_proof_encoding - end) + let proof_encoding = + Context.Proof_encoding.V2.Tree32.tree_proof_encoding + end) end diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli b/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli index 280ba1597f64..c33f80431f68 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli @@ -83,8 +83,10 @@ module V2_0_0 : sig Tree.t -> tree -> (tree -> (tree * 'a) Lwt.t) -> (proof * 'a) option Lwt.t end + module type Make_wasm = module type of Wasm_2_0_0.Make + (** Build a WebAssembly PVM using the given proof-supporting context. *) - module Make (Context : P) : + module Make (Lib_scoru_Wasm : Make_wasm) (Context : P) : S with type context = Context.Tree.t and type state = Context.tree diff --git a/src/proto_alpha/lib_protocol/test/helpers/sc_rollup_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/sc_rollup_helpers.ml index 2f3654bb104a..d61dbaf3e935 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/sc_rollup_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/sc_rollup_helpers.ml @@ -98,7 +98,7 @@ module Wasm_pvm : and type proof = Tezos_context_memory.Context.Proof.tree Tezos_context_memory.Context.Proof.t = - Sc_rollup.Wasm_2_0_0PVM.Make (In_memory_context) + Sc_rollup.Wasm_2_0_0PVM.Make (Environment.Wasm_2_0_0.Make) (In_memory_context) let origination_proof ~boot_sector = function | Sc_rollup.Kind.Example_arith -> diff --git a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml index 2ad8c2112d2a..cdf4309b4d08 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml @@ -108,7 +108,10 @@ end module Verifier = Alpha_context.Sc_rollup.Wasm_2_0_0PVM.Protocol_implementation -module Prover = Alpha_context.Sc_rollup.Wasm_2_0_0PVM.Make (WASM_P) +module Prover = + Alpha_context.Sc_rollup.Wasm_2_0_0PVM.Make + (Environment.Wasm_2_0_0.Make) + (WASM_P) (* Helpers *) (* FIXME: https://gitlab.com/tezos/tezos/-/issues/2198 -- GitLab From bef3e327ebb190795889007838b951df8c262a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Thu, 22 Sep 2022 16:35:22 +0100 Subject: [PATCH 3/3] SCORU: WASM: Establish environment equalities to avoid conversion --- .../environment_V7.ml | 7 ++ .../environment_V7.mli | 7 ++ .../bin_sc_rollup_node/wasm_2_0_0_pvm.ml | 73 +------------------ 3 files changed, 15 insertions(+), 72 deletions(-) diff --git a/src/lib_protocol_environment/environment_V7.ml b/src/lib_protocol_environment/environment_V7.ml index d09bc8e95c85..2909cd7a8892 100644 --- a/src/lib_protocol_environment/environment_V7.ml +++ b/src/lib_protocol_environment/environment_V7.ml @@ -102,6 +102,13 @@ module type T = sig * Tezos_protocol_environment_structs.V7.Plonk.transcript and type Dal.parameters = Tezos_crypto_dal.Cryptobox.Verifier.parameters and type Dal.commitment = Tezos_crypto_dal.Cryptobox.Verifier.commitment + and type Bounded.Non_negative_int32.t = + Tezos_base.Bounded.Non_negative_int32.t + and type Wasm_2_0_0.input = Tezos_scoru_wasm.Wasm_pvm_sig.input_info + and type Wasm_2_0_0.output = Tezos_scoru_wasm.Wasm_pvm_sig.output_info + and type Wasm_2_0_0.input_request = + Tezos_scoru_wasm.Wasm_pvm_sig.input_request + and type Wasm_2_0_0.info = Tezos_scoru_wasm.Wasm_pvm_sig.info type error += Ecoproto_error of Error_monad.error diff --git a/src/lib_protocol_environment/environment_V7.mli b/src/lib_protocol_environment/environment_V7.mli index c629499392e2..ae4cbf141664 100644 --- a/src/lib_protocol_environment/environment_V7.mli +++ b/src/lib_protocol_environment/environment_V7.mli @@ -102,6 +102,13 @@ module type T = sig * Tezos_protocol_environment_structs.V7.Plonk.transcript and type Dal.parameters = Tezos_crypto_dal.Cryptobox.Verifier.parameters and type Dal.commitment = Tezos_crypto_dal.Cryptobox.Verifier.commitment + and type Bounded.Non_negative_int32.t = + Tezos_base.Bounded.Non_negative_int32.t + and type Wasm_2_0_0.input = Tezos_scoru_wasm.Wasm_pvm_sig.input_info + and type Wasm_2_0_0.output = Tezos_scoru_wasm.Wasm_pvm_sig.output_info + and type Wasm_2_0_0.input_request = + Tezos_scoru_wasm.Wasm_pvm_sig.input_request + and type Wasm_2_0_0.info = Tezos_scoru_wasm.Wasm_pvm_sig.info (** An [Ecoproto_error e] is a shell error that carry a protocol error. diff --git a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml index 1a607bd2d48c..74f0ac3018ca 100644 --- a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml +++ b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml @@ -44,63 +44,6 @@ module Wasm_2_0_0_proof_format = .tree_proof_encoding end) -module Conversions = struct - (* BEWARE OF UGLY HACK !!! - Environment.Wasm_2_0_0.info/input/output are not compatible with - Tezos_scoru_wasm.Wasm_pvm_sig.info/input_info/output_info/ - although they should be, by their definitions: - - ``` - type input = Tezos_scoru_wasm.Wasm_pvm_sig.input_info = ... - type output = Tezos_scoru_wasm.Wasm_pvm_sig.input_info = ... - ``` - - Somehow the type equality is not preserved through the protocol environment. - - This is why we need to do the de/reconstruction manualy. - *) - module EInt32 = Environment.Bounded.Non_negative_int32 - module SInt32 = Bounded.Non_negative_int32 - module Opt = Stdlib.Option - module Scoru = Tezos_scoru_wasm.Wasm_pvm_sig - module Env = Environment.Wasm_2_0_0 - - let from_env_int32 (i : EInt32.t) : SInt32.t = - EInt32.to_value i |> SInt32.of_value |> Opt.get - - let to_env_int32 (i : SInt32.t) : EInt32.t = - SInt32.to_value i |> EInt32.of_value |> Opt.get - - let to_env_input (input : Scoru.input_info) : Env.input = - { - inbox_level = to_env_int32 input.inbox_level; - message_counter = input.message_counter; - } - - let to_env_input_request : Scoru.input_request -> Env.input_request = function - | Scoru.No_input_required -> Env.No_input_required - | Input_required -> Input_required - - let of_env_input (input : Env.input) : Scoru.input_info = - { - inbox_level = from_env_int32 input.inbox_level; - message_counter = input.message_counter; - } - - let of_env_output (output : Env.output) : Scoru.output_info = - { - outbox_level = from_env_int32 output.outbox_level; - message_index = output.message_index; - } - - let to_env_info (s_info : Scoru.info) : Env.info = - { - current_tick = s_info.current_tick; - last_input_read = Opt.map to_env_input s_info.last_input_read; - input_request = to_env_input_request s_info.input_request; - } -end - module type TreeS = Tezos_context_sigs.Context.TREE with type key = string list @@ -109,7 +52,7 @@ module type TreeS = module Make_backend (Tree : TreeS) = struct type Lazy_containers.Lazy_map.tree += PVM_tree of Tree.tree - module Wasm_pvm = Tezos_scoru_wasm.Wasm_pvm.Make (struct + include Tezos_scoru_wasm.Wasm_pvm.Make (struct include Tree let select = function @@ -118,20 +61,6 @@ module Make_backend (Tree : TreeS) = struct let wrap t = PVM_tree t end) - - let compute_step (tree : Tree.tree) = Wasm_pvm.compute_step tree - - include Conversions - - let set_input_step (input : Environment.Wasm_2_0_0.input) payload - (tree : Tree.tree) = - Wasm_pvm.set_input_step (of_env_input input) payload tree - - let get_output (output : Environment.Wasm_2_0_0.output) (tree : Tree.tree) = - Wasm_pvm.get_output (of_env_output output) tree - - let get_info (tree : Tree.tree) = - Lwt.map to_env_info @@ Wasm_pvm.get_info tree end module Impl : Pvm.S = struct -- GitLab