From c7aeb314b3cee85ffcf1fce8744a4a63be4de5f4 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 30 Nov 2022 14:50:33 +0000 Subject: [PATCH 1/5] Proto/Wasm: Make reveal hash size dynamic --- .../environment_V8.ml | 3 -- src/lib_protocol_environment/sigs/v8.ml | 4 +- .../sigs/v8/wasm_2_0_0.mli | 4 +- src/lib_scoru_wasm/builtins.ml | 6 +-- src/lib_scoru_wasm/fast/funcs.ml | 51 +++++++++++-------- src/lib_scoru_wasm/host_funcs.ml | 30 ++++++++--- src/lib_scoru_wasm/host_funcs.mli | 3 ++ .../test/helpers/ast_generators.ml | 2 +- .../test/helpers/ast_printer.ml | 7 +-- src/lib_scoru_wasm/test/test_fast.ml | 11 ++-- src/lib_scoru_wasm/test/test_reveal.ml | 23 +++++---- src/lib_scoru_wasm/test/test_wasm_pvm.ml | 17 +++---- src/lib_scoru_wasm/wasm_encoding.ml | 10 ++-- src/lib_scoru_wasm/wasm_pvm_state.ml | 9 ++-- src/lib_webassembly/exec/eval.ml | 2 +- src/lib_webassembly/exec/eval.mli | 4 +- src/lib_webassembly/exec/reveal.ml | 10 ---- src/lib_webassembly/exec/reveal.mli | 10 ---- src/lib_webassembly/runtime/host_funcs.ml | 4 +- src/lib_webassembly/runtime/host_funcs.mli | 4 +- .../bin_sc_rollup_node/fueled_pvm.ml | 7 +-- src/proto_alpha/bin_wasm_repl/commands.ml | 5 +- .../lib_protocol/sc_rollup_wasm.ml | 2 +- 23 files changed, 107 insertions(+), 121 deletions(-) delete mode 100644 src/lib_webassembly/exec/reveal.ml delete mode 100644 src/lib_webassembly/exec/reveal.mli diff --git a/src/lib_protocol_environment/environment_V8.ml b/src/lib_protocol_environment/environment_V8.ml index fa3dd867e57b..f1c18d359ba1 100644 --- a/src/lib_protocol_environment/environment_V8.ml +++ b/src/lib_protocol_environment/environment_V8.ml @@ -1109,9 +1109,6 @@ struct type reveal_hash = Tezos_scoru_wasm.Wasm_pvm_state.reveal_hash - let reveal_hash_to_string = - Tezos_scoru_wasm.Wasm_pvm_state.reveal_hash_to_string - type reveal = Tezos_scoru_wasm.Wasm_pvm_state.reveal = | Reveal_raw_data of reveal_hash | Reveal_metadata diff --git a/src/lib_protocol_environment/sigs/v8.ml b/src/lib_protocol_environment/sigs/v8.ml index 681cb2e250a3..3bb0f6ea8103 100644 --- a/src/lib_protocol_environment/sigs/v8.ml +++ b/src/lib_protocol_environment/sigs/v8.ml @@ -12082,9 +12082,7 @@ type input = {inbox_level : Bounded.Non_negative_int32.t; message_counter : Z.t} type output = {outbox_level : Bounded.Non_negative_int32.t; message_index : Z.t} -type reveal_hash - -val reveal_hash_to_string : reveal_hash -> string +type reveal_hash = string type reveal = Reveal_raw_data of reveal_hash | Reveal_metadata diff --git a/src/lib_protocol_environment/sigs/v8/wasm_2_0_0.mli b/src/lib_protocol_environment/sigs/v8/wasm_2_0_0.mli index c696ead44f0e..8b7062f5a221 100644 --- a/src/lib_protocol_environment/sigs/v8/wasm_2_0_0.mli +++ b/src/lib_protocol_environment/sigs/v8/wasm_2_0_0.mli @@ -27,9 +27,7 @@ type input = {inbox_level : Bounded.Non_negative_int32.t; message_counter : Z.t} type output = {outbox_level : Bounded.Non_negative_int32.t; message_index : Z.t} -type reveal_hash - -val reveal_hash_to_string : reveal_hash -> string +type reveal_hash = string type reveal = Reveal_raw_data of reveal_hash | Reveal_metadata diff --git a/src/lib_scoru_wasm/builtins.ml b/src/lib_scoru_wasm/builtins.ml index 5e68b4e41e59..7b396c777c6e 100644 --- a/src/lib_scoru_wasm/builtins.ml +++ b/src/lib_scoru_wasm/builtins.ml @@ -23,11 +23,9 @@ (* *) (*****************************************************************************) -open Tezos_webassembly_interpreter - module type S = sig - (** [reveal_preimage hash] reveals the preimage of the given 32-byte hash. *) - val reveal_preimage : Reveal.reveal_hash -> string Lwt.t + (** [reveal_preimage hash] reveals the preimage of the given hash. *) + val reveal_preimage : string -> string Lwt.t (** [reveal_metadata ()] returns the encoded metadata for the rollup. *) val reveal_metadata : unit -> string Lwt.t diff --git a/src/lib_scoru_wasm/fast/funcs.ml b/src/lib_scoru_wasm/fast/funcs.ml index baa1c3a292ca..8fbe864a5f16 100644 --- a/src/lib_scoru_wasm/fast/funcs.ml +++ b/src/lib_scoru_wasm/fast/funcs.ml @@ -272,34 +272,41 @@ let make (module Builtins : Builtins.S) state = ~key_offset ~key_length) in + (* TODO: https://gitlab.com/tezos/tezos/-/issues/4369 + Align failure mode of reveal_* functions in Fast Execution. *) let reveal_preimage = fn - (i32 @-> i32 @-> i32 @-> returning1 i32) - (fun hash_addr dest max_bytes -> + (i32 @-> i32 @-> i32 @-> i32 @-> returning1 i32) + (fun hash_addr hash_size dest max_bytes -> let mem = state.retrieve_mem () in + let hash_size = Int32.to_int hash_size in let hash_addr = Int32.to_int hash_addr in let dest = Int32.to_int dest in let max_bytes = Int32.to_int max_bytes in - let hash = - String.init 32 (fun i -> - (* XXX: No bounds checks - The "main" reveal function does not deal with out-of-bounds - scenarios. That ultimate means we don't return error codes. - Instead, we just fail with the exception being thrown. - In most cases the Fast Exec mechanism will fall back to another - execution mode to deal with this. *) - Memory.get mem (hash_addr + i) - |> Unsigned.UInt8.to_int |> Char.chr) - |> Tezos_webassembly_interpreter.Reveal.reveal_hash_from_string_exn - in - let+ payload = Builtins.reveal_preimage hash in - let revealed_bytes = min (String.length payload) max_bytes in - let payload = String.sub payload 0 revealed_bytes in - String.iteri - (fun i c -> - Char.code c |> Unsigned.UInt8.of_int |> Memory.set mem (dest + i)) - payload ; - Int32.of_int revealed_bytes) + (* If hash_size is too large we fail and fallback to another execution + mode.*) + if hash_size > Host_funcs.Aux.input_output_max_size then + raise @@ Invalid_argument "Hash size too large" + else + let hash = + String.init hash_size (fun i -> + (* XXX: No bounds checks + The "main" reveal function does not deal with out-of-bounds + scenarios. That ultimate means we don't return error codes. + Instead, we just fail with the exception being thrown. + In most cases the Fast Exec mechanism will fall back to another + execution mode to deal with this. *) + Memory.get mem (hash_addr + i) + |> Unsigned.UInt8.to_int |> Char.chr) + in + let+ payload = Builtins.reveal_preimage hash in + let revealed_bytes = min (String.length payload) max_bytes in + let payload = String.sub payload 0 revealed_bytes in + String.iteri + (fun i c -> + Char.code c |> Unsigned.UInt8.of_int |> Memory.set mem (dest + i)) + payload ; + Int32.of_int revealed_bytes) in let reveal_metadata = fn diff --git a/src/lib_scoru_wasm/host_funcs.ml b/src/lib_scoru_wasm/host_funcs.ml index 6a4761b88fd5..42e597bfd269 100644 --- a/src/lib_scoru_wasm/host_funcs.ml +++ b/src/lib_scoru_wasm/host_funcs.ml @@ -111,6 +111,9 @@ module Aux = struct module type S = sig type memory + (** max size of intputs and outputs. *) + val input_output_max_size : int + (** [write_output ~output_buffer ~memory ~src ~num_bytes] reads num_bytes from the memory of module_inst starting at src and writes this to the output_buffer. It also checks that the input payload is @@ -893,20 +896,32 @@ let reveal_preimage_name = "tezos_reveal_preimage" let reveal_preimage_type = let input_types = - Types.[NumType I32Type; NumType I32Type; NumType I32Type] |> Vector.of_list + Types.[NumType I32Type; NumType I32Type; NumType I32Type; NumType I32Type] + |> Vector.of_list in let output_types = Types.[NumType I32Type] |> Vector.of_list in Types.FuncType (input_types, output_types) let reveal_preimage_parse_args memories args = match args with - | Values.[Num (I32 hash_addr); Num (I32 base); Num (I32 max_bytes)] -> + | Values. + [ + Num (I32 hash_addr); + Num (I32 hash_size); + Num (I32 base); + Num (I32 max_bytes); + ] -> let open Lwt_result_syntax in let*! memory = retrieve_memory memories in - let*! hash = Memory_access_interpreter.load_bytes memory hash_addr 32 in - Lwt_result.return - ( Reveal.(Reveal_raw_data (reveal_hash_from_string_exn hash)), - Host_funcs.{base; max_bytes} ) + let hash_size = Int32.to_int hash_size in + if hash_size > Aux.input_output_max_size then + fail (Error.code Input_output_too_large) + else + let*! hash = + Memory_access_interpreter.load_bytes memory hash_addr hash_size + in + Lwt_result.return + (Host_funcs.(Reveal_raw_data hash), Host_funcs.{base; max_bytes}) | _ -> raise Bad_input let reveal_preimage = Host_funcs.Reveal_func reveal_preimage_parse_args @@ -927,7 +942,8 @@ let reveal_metadata_parse_args _memories args = | Values.[Num (I32 base)] -> Lwt.return (Ok - (Reveal.Reveal_metadata, Host_funcs.{base; max_bytes = metadata_size})) + ( Host_funcs.Reveal_metadata, + Host_funcs.{base; max_bytes = metadata_size} )) | _ -> raise Bad_input let reveal_metadata = Host_funcs.Reveal_func reveal_metadata_parse_args diff --git a/src/lib_scoru_wasm/host_funcs.mli b/src/lib_scoru_wasm/host_funcs.mli index f079f7235caa..0b9736244b23 100644 --- a/src/lib_scoru_wasm/host_funcs.mli +++ b/src/lib_scoru_wasm/host_funcs.mli @@ -103,6 +103,9 @@ module Aux : sig module type S = sig type memory + (** max size of intputs and outputs. *) + val input_output_max_size : int + (** [aux_write_output ~input_buffer ~output_buffer ~module_inst ~src ~num_bytes] reads num_bytes from the memory of module_inst starting at src and writes this to the output_buffer. It also checks that diff --git a/src/lib_scoru_wasm/test/helpers/ast_generators.ml b/src/lib_scoru_wasm/test/helpers/ast_generators.ml index af91a202823f..a98ccad357c6 100644 --- a/src/lib_scoru_wasm/test/helpers/ast_generators.ml +++ b/src/lib_scoru_wasm/test/helpers/ast_generators.ml @@ -758,7 +758,7 @@ let inv_concat_gen ~module_reg = Eval.Inv_concat {arity; vs; instructions; inst; func; concat_kont} let inv_reveal_tick ~module_reg = - let* hash = Reveal.reveal_hash_from_string_exn <$> string_size (pure 32) in + let* hash = string_small in let* base_destination = Int32.of_int <$> small_nat in let* max_bytes = Int32.of_int <$> small_nat in let* vs = small_vector_gen value_gen in diff --git a/src/lib_scoru_wasm/test/helpers/ast_printer.ml b/src/lib_scoru_wasm/test/helpers/ast_printer.ml index e7b5a13bcde5..9f104dfb2d5c 100644 --- a/src/lib_scoru_wasm/test/helpers/ast_printer.ml +++ b/src/lib_scoru_wasm/test/helpers/ast_printer.ml @@ -428,11 +428,8 @@ let pp_concat_kont pp out Eval.{lv; rv; res; offset} = offset let pp_reveal out = function - | Reveal.Reveal_raw_data hash -> - Format.fprintf - out - "Reveal_raw_data (%s)" - (Reveal.reveal_hash_to_string hash) + | Host_funcs.Reveal_raw_data hash -> + Format.fprintf out "Reveal_raw_data (%s)" hash | Reveal_metadata -> Format.fprintf out "Reveal_metadata" let pp_invoke_step_kont out = function diff --git a/src/lib_scoru_wasm/test/test_fast.ml b/src/lib_scoru_wasm/test/test_fast.ml index 7ad62747c2ae..06de0768be22 100644 --- a/src/lib_scoru_wasm/test/test_fast.ml +++ b/src/lib_scoru_wasm/test/test_fast.ml @@ -24,7 +24,6 @@ (*****************************************************************************) open Tztest -open Tezos_webassembly_interpreter module Preimage_map = Map.Make (String) open Wasm_utils @@ -33,7 +32,6 @@ let apply_fast ?(images = Preimage_map.empty) counter tree = let run_counter = ref 0l in let module Builtins = struct let reveal_preimage hash = - let hash = Reveal.reveal_hash_to_string hash in match Preimage_map.find hash images with | None -> Stdlib.failwith "Failed to find preimage" | Some preimage -> Lwt.return preimage @@ -78,7 +76,6 @@ and check_reveal ?(images = Preimage_map.empty) tree = let* info = Wasm_utils.Wasm.get_info tree in match info.input_request with | Reveal_required (Reveal_raw_data hash) -> ( - let hash = Reveal.reveal_hash_to_string hash in match Preimage_map.find hash images with | None -> Lwt.return tree | Some preimage -> @@ -239,7 +236,7 @@ let test_store_read_write = test_against_both ~from_binary:false ~kernel ~messages:(List.repeat 100 "") let test_reveal_preimage = - let example_hash = "this represents the 32-byte hash" in + let example_hash = "this represents the 33-byte hash " in let example_preimage = "This is the expected preimage" in let images = Preimage_map.singleton example_hash example_preimage in @@ -256,12 +253,12 @@ let test_reveal_preimage = (import "smart_rollup_core" "reveal_preimage" - (func $reveal_preimage (param i32 i32 i32) (result i32)) + (func $reveal_preimage (param i32 i32 i32 i32) (result i32)) ) (memory 1) - (export "memory" (memory 0)) + (export "memory" (memory 0)) (data (i32.const 0) "%s") ;; The hash we want to reveal (data (i32.const 100) "/foo") @@ -270,6 +267,8 @@ let test_reveal_preimage = (call $reveal_preimage ;; Address of the hash (i32.const 0) + ;; size of the hash + (i32.const 33) ;; Destination address and length (i32.const 1000) (i32.const 1000) ) diff --git a/src/lib_scoru_wasm/test/test_reveal.ml b/src/lib_scoru_wasm/test/test_reveal.ml index 3e79c9d48388..77304e67f420 100644 --- a/src/lib_scoru_wasm/test/test_reveal.ml +++ b/src/lib_scoru_wasm/test/test_reveal.ml @@ -36,21 +36,22 @@ open Tezos_webassembly_interpreter open Tezos_scoru_wasm open Wasm_utils -let reveal_preimage_module hash_addr preimage_addr max_bytes = +let reveal_preimage_module hash_addr hash_size preimage_addr max_bytes = Format.sprintf {| (module (import "smart_rollup_core" "reveal_preimage" - (func $reveal_preimage (param i32 i32 i32) (result i32)) + (func $reveal_preimage (param i32 i32 i32 i32) (result i32)) ) (memory 1) (export "mem" (memory 0)) (func (export "kernel_run") - (call $reveal_preimage (i32.const %ld) (i32.const %ld) (i32.const %ld)) + (call $reveal_preimage (i32.const %ld) (i32.const %ld) (i32.const %ld) (i32.const %ld)) ) ) |} hash_addr + hash_size preimage_addr max_bytes @@ -95,7 +96,10 @@ let test_reveal_preimage_gen preimage max_bytes = let open Lwt_result_syntax in let hash_addr = 120l in let preimage_addr = 200l in - let modl = reveal_preimage_module hash_addr preimage_addr max_bytes in + let hash_size = 33l in + let modl = + reveal_preimage_module hash_addr hash_size preimage_addr max_bytes + in let*! state = initial_tree modl in let*! state_snapshotted = eval_until_input_requested state in let*! state_with_dummy_input = set_empty_inbox_step 0l state_snapshotted in @@ -109,9 +113,7 @@ let test_reveal_preimage_gen preimage max_bytes = (* The PVM has reached a point where it’s asking for some preimage. Since the memory is left blank, we are looking for the zero hash *) - let zero_hash = - Reveal.reveal_hash_from_string_exn (String.make 32 '\000') - in + let zero_hash = String.make (Int32.to_int hash_size) '\000' in assert (hash = zero_hash) ; return_unit | No_input_required | Input_required | Reveal_required _ -> assert false @@ -218,7 +220,6 @@ let apply_fast ?(images = Preimage_map.empty) tree = let run_counter = ref 0l in let module Builtins = struct let reveal_preimage hash = - let hash = Reveal.reveal_hash_to_string hash in match Preimage_map.find hash images with | None -> Stdlib.failwith "Failed to find preimage" | Some preimage -> Lwt.return preimage @@ -244,7 +245,7 @@ let apply_fast ?(images = Preimage_map.empty) tree = let test_fast_exec_reveal () = let open Lwt.Syntax in - let example_hash = "this represents the 32-byte hash" in + let example_hash = "this represents the 33-byte hash!" in let example_preimage = "This is the expected preimage" in let images = Preimage_map.singleton example_hash example_preimage in @@ -261,7 +262,7 @@ let test_fast_exec_reveal () = (import "smart_rollup_core" "reveal_preimage" - (func $reveal_preimage (param i32 i32 i32) (result i32)) + (func $reveal_preimage (param i32 i32 i32 i32) (result i32)) ) (memory 1) @@ -275,6 +276,8 @@ let test_fast_exec_reveal () = (call $reveal_preimage ;; Address of the hash (i32.const 0) + ;; Size of the hash + (i32.const 33) ;; Destination address and length (i32.const 1000) (i32.const 1000) ) diff --git a/src/lib_scoru_wasm/test/test_wasm_pvm.ml b/src/lib_scoru_wasm/test/test_wasm_pvm.ml index 96ff17748cfc..c00b7816fce8 100644 --- a/src/lib_scoru_wasm/test/test_wasm_pvm.ml +++ b/src/lib_scoru_wasm/test/test_wasm_pvm.ml @@ -912,8 +912,8 @@ let reveal_upgrade_kernel () = let path_start = 23 in let path_len = String.length path in let hash_start = path_start + path_len in - let buffer_start = hash_start + 32 in - (* 32 bytes for reveal hash *) + let buffer_start = hash_start + 33 in + (* 33 bytes for reveal hash *) Format.sprintf {| (module @@ -922,7 +922,7 @@ let reveal_upgrade_kernel () = (import "smart_rollup_core" "store_delete" (func $store_delete (param i32 i32) (result i32))) (import "smart_rollup_core" "reveal_preimage" - (func $reveal_preimage (param i32 i32 i32) (result i32))) + (func $reveal_preimage (param i32 i32 i32 i32) (result i32))) (func (export "kernel_run") (local $preimage_size i32) @@ -938,6 +938,7 @@ let reveal_upgrade_kernel () = (local.set $preimage_size (call $reveal_preimage (local.get $hash_start) + (i32.const 33) (local.get $buffer_start) ;; buffer size (i32.const 4096))) @@ -1016,10 +1017,7 @@ let test_reveal_upgrade_kernel_ok () = (* The PVM has reached a point where it’s asking for some preimage. Since the memory is left blank, we are looking for the zero hash *) - let zero_hash = - Tezos_webassembly_interpreter.Reveal.reveal_hash_from_string_exn - (String.make 32 '\000') - in + let zero_hash = String.make 33 '\000' in assert (hash = zero_hash) ; return_unit | No_input_required | Input_required | Reveal_required _ -> assert false @@ -1095,10 +1093,7 @@ let test_reveal_upgrade_kernel_fallsback_on_error ~binary ~error invalid_kernel (* The PVM has reached a point where it’s asking for some preimage. Since the memory is left blank, we are looking for the zero hash *) - let zero_hash = - Tezos_webassembly_interpreter.Reveal.reveal_hash_from_string_exn - (String.make 32 '\000') - in + let zero_hash = String.make 33 '\000' in assert (hash = zero_hash) ; return_unit | No_input_required | Input_required | Reveal_required _ -> assert false diff --git a/src/lib_scoru_wasm/wasm_encoding.ml b/src/lib_scoru_wasm/wasm_encoding.ml index f03ed383edb9..5a11d17979c9 100644 --- a/src/lib_scoru_wasm/wasm_encoding.ml +++ b/src/lib_scoru_wasm/wasm_encoding.ml @@ -972,11 +972,7 @@ let packed_frame_stack_encoding = (scope ["frame"] frame_encoding) (scope ["label_kont"] packed_label_kont_encoding)) -let reveal_hash_encoding = - conv - Reveal.reveal_hash_from_string_exn - Reveal.reveal_hash_to_string - (value [] (Data_encoding.Fixed.string 32)) +let reveal_hash_encoding = value [] (Data_encoding.string' Hex) let reveal_encoding = tagged_union @@ -985,12 +981,12 @@ let reveal_encoding = case "Reveal_raw_data" reveal_hash_encoding - (function Reveal.Reveal_raw_data hash -> Some hash | _ -> None) + (function Host_funcs.Reveal_raw_data hash -> Some hash | _ -> None) (fun hash -> Reveal_raw_data hash); case "Reveal_metadata" (value [] Data_encoding.unit) - (function Reveal.Reveal_metadata -> Some () | _ -> None) + (function Host_funcs.Reveal_metadata -> Some () | _ -> None) (fun () -> Reveal_metadata); ] diff --git a/src/lib_scoru_wasm/wasm_pvm_state.ml b/src/lib_scoru_wasm/wasm_pvm_state.ml index 06b5b6bcc796..f33167734181 100644 --- a/src/lib_scoru_wasm/wasm_pvm_state.ml +++ b/src/lib_scoru_wasm/wasm_pvm_state.ml @@ -38,13 +38,10 @@ type output_info = { message_index : Z.t; (** The index of the message in the outbox. *) } -type reveal_hash = Tezos_webassembly_interpreter.Reveal.reveal_hash +type reveal_hash = string -let reveal_hash_to_string = - Tezos_webassembly_interpreter.Reveal.reveal_hash_to_string - -type reveal = Tezos_webassembly_interpreter.Reveal.reveal = - | Reveal_raw_data of Tezos_webassembly_interpreter.Reveal.reveal_hash +type reveal = Tezos_webassembly_interpreter.Host_funcs.reveal = + | Reveal_raw_data of reveal_hash | Reveal_metadata (** Represents the state of input requests. *) diff --git a/src/lib_webassembly/exec/eval.ml b/src/lib_webassembly/exec/eval.ml index 2b7f063e4f99..ce12de54ec08 100644 --- a/src/lib_webassembly/exec/eval.ml +++ b/src/lib_webassembly/exec/eval.ml @@ -358,7 +358,7 @@ type invoke_step_kont = concat_kont : value concat_kont; } | Inv_reveal_tick of { - reveal : Reveal.reveal; + reveal : Host_funcs.reveal; base_destination : int32; max_bytes : int32; code : code; diff --git a/src/lib_webassembly/exec/eval.mli b/src/lib_webassembly/exec/eval.mli index f426728dca64..7928fc0c20d3 100644 --- a/src/lib_webassembly/exec/eval.mli +++ b/src/lib_webassembly/exec/eval.mli @@ -118,7 +118,7 @@ type invoke_step_kont = concat_kont : value concat_kont; } | Inv_reveal_tick of { - reveal : Reveal.reveal; + reveal : Host_funcs.reveal; base_destination : int32; max_bytes : int32; code : code; @@ -285,7 +285,7 @@ exception Reveal_error of reveal_error (** [is_reveal_tick config] returns [Some hash] if the evalutation is in a state expecting the payload of a given hash, and returns [None] otherwise. *) -val is_reveal_tick : config -> Reveal.reveal option +val is_reveal_tick : config -> Host_funcs.reveal option (** [reveal_step reveal module_reg payload config] loads [payload] in the memory of the module whose function is being evaluated with diff --git a/src/lib_webassembly/exec/reveal.ml b/src/lib_webassembly/exec/reveal.ml deleted file mode 100644 index 8eb7ba7d9433..000000000000 --- a/src/lib_webassembly/exec/reveal.ml +++ /dev/null @@ -1,10 +0,0 @@ -exception Invalid_reveal_hash - -type reveal_hash = string - -let reveal_hash_from_string_exn str = - if String.length str = 32 then str else raise Invalid_reveal_hash - -let reveal_hash_to_string hash = hash - -type reveal = Reveal_raw_data of reveal_hash | Reveal_metadata diff --git a/src/lib_webassembly/exec/reveal.mli b/src/lib_webassembly/exec/reveal.mli deleted file mode 100644 index 46da2dbea084..000000000000 --- a/src/lib_webassembly/exec/reveal.mli +++ /dev/null @@ -1,10 +0,0 @@ -exception Invalid_reveal_hash - -type reveal_hash - -(** @raise Invalid_reveal_hash *) -val reveal_hash_from_string_exn : string -> reveal_hash - -val reveal_hash_to_string : reveal_hash -> string - -type reveal = Reveal_raw_data of reveal_hash | Reveal_metadata diff --git a/src/lib_webassembly/runtime/host_funcs.ml b/src/lib_webassembly/runtime/host_funcs.ml index 5e2231b72b66..1275513ffe3e 100644 --- a/src/lib_webassembly/runtime/host_funcs.ml +++ b/src/lib_webassembly/runtime/host_funcs.ml @@ -2,12 +2,14 @@ type available_memories = | No_memories_during_init | Available_memories of Instance.memory_inst Instance.Vector.t +type reveal = Reveal_raw_data of string | Reveal_metadata + type reveal_destination = {base : int32; max_bytes : int32} type reveal_func = available_memories -> Values.value list -> - (Reveal.reveal * reveal_destination, int32) result Lwt.t + (reveal * reveal_destination, int32) result Lwt.t type host_func = | Host_func of diff --git a/src/lib_webassembly/runtime/host_funcs.mli b/src/lib_webassembly/runtime/host_funcs.mli index a13263f339c5..296be6205998 100644 --- a/src/lib_webassembly/runtime/host_funcs.mli +++ b/src/lib_webassembly/runtime/host_funcs.mli @@ -5,10 +5,12 @@ type available_memories = type reveal_destination = {base : int32; max_bytes : int32} +type reveal = Reveal_raw_data of string | Reveal_metadata + type reveal_func = available_memories -> Values.value list -> - (Reveal.reveal * reveal_destination, int32) result Lwt.t + (reveal * reveal_destination, int32) result Lwt.t (** The type of a Host function implementation *) type host_func = diff --git a/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml b/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml index 6b417e80e94d..7a83c58f83b0 100644 --- a/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml +++ b/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml @@ -116,10 +116,11 @@ module Make (PVM : Pvm.S) = struct let module Builtins = struct let reveal_preimage hash = let hash = - (* The 32-byte payload represents the encoded [Reveal_hash.t]. We must + (* The payload represents the encoded [Sc_rollup_reveal_hash.t]. We must decode it properly, instead of converting it byte-for-byte. *) - Tezos_webassembly_interpreter.Reveal.reveal_hash_to_string hash - |> Data_encoding.Binary.of_string_exn Sc_rollup_reveal_hash.encoding + Data_encoding.Binary.of_string_exn + Sc_rollup_reveal_hash.encoding + hash in let*! data = get_reveal ~data_dir:node_ctxt.data_dir reveal_map hash diff --git a/src/proto_alpha/bin_wasm_repl/commands.ml b/src/proto_alpha/bin_wasm_repl/commands.ml index 4d030ffe6b55..dedf6fa13844 100644 --- a/src/proto_alpha/bin_wasm_repl/commands.ml +++ b/src/proto_alpha/bin_wasm_repl/commands.ml @@ -213,10 +213,7 @@ let pp_input_request ppf = function | Input_required -> Format.fprintf ppf "Waiting for input" | Reveal_required Reveal_metadata -> Format.fprintf ppf "Waiting for metadata" | Reveal_required (Reveal_raw_data hash) -> - Format.fprintf - ppf - "Waiting for reveal: %s" - (Tezos_webassembly_interpreter.Reveal.reveal_hash_to_string hash) + Format.fprintf ppf "Waiting for reveal: %s" hash (* [show_status tree] show the state of the PVM. *) let show_status tree = diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml index 3bb09f0016f2..c32116b216a3 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -300,7 +300,7 @@ module V2_0_0 = struct match Data_encoding.Binary.of_string_opt Sc_rollup_reveal_hash.encoding - (Wasm_2_0_0.reveal_hash_to_string hash) + hash with | Some hash -> Waiting_for_reveal (Reveal_raw_data hash) | None -> -- GitLab From f0349419e23e68c69c834003bc0d54a469e10321 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 30 Nov 2022 14:51:22 +0000 Subject: [PATCH 2/5] Proto: bump Sc_rollup_reveal_hash.Blake2B.size to 32 bytes --- src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml index fb378ecc5d02..c583ff28e2c1 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml @@ -36,12 +36,12 @@ module Blake2B = struct let title = "A smart contract rollup reveal hash" let b58check_prefix = - "\017\144\121\209\203" (* "scrrh1(54)" decoded from Base58. *) + "\230\206\128\200\196" (* "scrrh1(56)" decoded from Base58. *) - let size = Some 31 + let size = Some 32 end) - let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 54 + let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 56 end type supported_hashes = Blake2B -- GitLab From 74efafd8c13988a5335e9aeb6f5ab353f766afb0 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 30 Nov 2022 14:51:59 +0000 Subject: [PATCH 3/5] Dac/Test: fix tests for merkle tree pagination scheme --- .../lib_dal/test/test_dac_pages_encoding.ml | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml index 9ba72106cd32..0b82b4c87ffa 100644 --- a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml @@ -306,13 +306,13 @@ module Merkle_tree = struct retrieved_payload let multiple_pages_roundtrip_heterogeneous_payload () = - (* Each page in tests contains at most 70 bytes, of which 5 are reserved - for the page prefix. This leaves 65 bytes to store the payload to be + (* Each page in tests contains at most 80 bytes, of which 5 are reserved + for the page prefix. This leaves 75 bytes to store the payload to be serialized in a page. It also means that a `Hashes` page can contain - at most 2 hashes of size 32 bytes each. If we try to serialize a - payload between 131 and 195 bytes (included), then the serialized + at most 2 hashes of size 33 bytes each. If we try to serialize a + payload between 151 and 225 bytes (included), then the serialized payload should be spread among a total of 6 pages. Of these, - 195/65 = 3 pages are used to store the payload, ceil(3/2) = 2 pages + 225/75 = 3 pages are used to store the payload, ceil(3/2) = 2 pages are used for storing the 3 hashes of the 3 payload pages, and ceil(2/2) = 1 page is used for storing the 2 hashes of the previous pages. *) @@ -322,7 +322,7 @@ module Merkle_tree = struct have a fixed size of 32 bytes, and 5 bytes are used for the preamble, we need 32 * 2 + 5 = 69 bytes to store two hashes in a page. We round this value to 70. *) - let max_page_size = 70 in + let max_page_size = 80 in let payload = Bytes.of_string "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \ @@ -349,11 +349,11 @@ module Merkle_tree = struct retrieved_payload let multiple_pages_roundtrip_homogeneous_payload () = - (* Each page in tests contains at most 70 bytes, of which 5 are reserved - for the page prefix. This leaves 65 bytes to store the contents to be + (* Each page in tests contains at most 80 bytes, of which 5 are reserved + for the page prefix. This leaves 75 bytes to store the contents to be serialized in a page. It also means that a `Hashes` page can contain at most 2 hashes of size 32 bytes each. If we try to serialize a - payload of 195 repetitions of the same character, then only one + payload of 225 repetitions of the same character, then only one payload page will be produced. However, the hash of this page will be repeated three times across two pages represent nodes of the Merkle tree. Finally, another page will be used for storing the Merkle tree @@ -365,9 +365,9 @@ module Merkle_tree = struct have a fixed size of 32 bytes, and 5 bytes are used for the preamble, we need 32 * 2 + 5 = 69 bytes to store two hashes in a page. We round this value to 70. *) - let max_page_size = 70 in + let max_page_size = 80 in let payload = - List.repeat 195 (Bytes.of_string "a") |> Bytes.concat Bytes.empty + List.repeat 225 (Bytes.of_string "a") |> Bytes.concat Bytes.empty in let* hash = lift -- GitLab From 658d3a8dc26111113fb5b80a5937821c7eb0a42f Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 30 Nov 2022 14:52:41 +0000 Subject: [PATCH 4/5] Dac/Tezt: fix dac root page hashes --- tezt/tests/dal.ml | 14 ++++++++++---- tezt/tests/sc_rollup.ml | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tezt/tests/dal.ml b/tezt/tests/dal.ml index 0e5583a2dc3e..b217be2d8cfc 100644 --- a/tezt/tests/dal.ml +++ b/tezt/tests/dal.ml @@ -1382,7 +1382,9 @@ let test_dal_node_handles_dac_store_preimage_merkle_V0 _protocol dal_node (* Expected reveal hash equals to the result of [Tezos_dal_alpha.Dac_pages_encoding.Merkle_tree.V0.serialize_payload "test"]. *) - let expected_rh = "scrrh1Y5hijnFNJPb96EFTY9SjZ4epyaYF9xU3Eid9KCj9vda25H8W" in + let expected_rh = + "scrrh13xwafZc5revvm5mittZFsBWb8ynFdH7ZRwpgUzCbtCCZo2orjs" + in check_valid_root_hash expected_rh actual_rh ; let filename = Filename.concat @@ -1419,7 +1421,9 @@ let test_dal_node_handles_dac_store_preimage_hash_chain_V0 _protocol dal_node (* Expected reveal hash equals to the result of [Tezos_dal_alpha.Dac_pages_encoding.Hash_chain.V0.serialize_payload "test"]. *) - let expected_rh = "scrrh1hYNyzXmagRDi22knBt5dhMMi6Sivdo1ztf5wXiectRnzbSyX" in + let expected_rh = + "scrrh13qW2L3g9ubEMjvWj5Rk2eqknXDsvnkZKVErFsavKTaFLgRgsXe" + in check_valid_root_hash expected_rh actual_rh ; let filename = Filename.concat @@ -1468,7 +1472,9 @@ let test_rollup_arith_uses_reveals _protocol dal_node sc_rollup_node ~payload ~pagination_scheme:"Hash_chain_V0") in - let expected_rh = "scrrh1bZAo4kfAohhKcvgKc4yLHYN49EPuHezNXgFraa1mEbKSTCwf" in + let expected_rh = + "scrrh144gSwSgX2cSw3v8p9h5rf6L6Ryvx4ewEhBDhkz7ErY9EChS47a" + in check_valid_root_hash expected_rh actual_rh ; let* () = send_messages @@ -1509,7 +1515,7 @@ let test_reveals_fails_on_wrong_hash _protocol dal_node sc_rollup_node ~pagination_scheme:"Hash_chain_V0") in let errorneous_hash = - "scrrh1kXE3tnCVTJ21aDNVeaV86e8rS6jtiMEDpjZJtDnLXRThQdmy" + "scrrh144gSwSgX2cSw3v8p9h5rf6L6Ryvx4ewEhBDhkz7ErY9EChS47a" in let* genesis_info = RPC.Client.call ~hooks client diff --git a/tezt/tests/sc_rollup.ml b/tezt/tests/sc_rollup.ml index facbfdb55b37..58c42864cc46 100644 --- a/tezt/tests/sc_rollup.ml +++ b/tezt/tests/sc_rollup.ml @@ -2461,8 +2461,8 @@ let test_reveals_fails_on_wrong_hash ~kind = } @@ fun sc_rollup_node _sc_rollup_client sc_rollup _node client -> (* This value has been obtained from the logs of the test - scrrh1kXE3tnCVTJ21aDNVeaV86e8rS6jtiMEDpjZJtDnLXRThQdmy. *) - let hash = "scrrh1kXE3tnCVTJ21aDNVeaV86e8rS6jtiMEDpjZJtDnLXRThQdmy" in + "Alpha: arith - rollup node correctly handles reveals." *) + let hash = "scrrh13sj3sMk3Ne75xd4dnYGH4TF4dHzAcmXpEPkdJrQ3fBEGN4jtoF" in let pvm_dir = Filename.concat (Sc_rollup_node.data_dir sc_rollup_node) "arith" in -- GitLab From b7360680e77ef0362c0e3dc0638bd0228e5d0179 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 30 Nov 2022 14:53:19 +0000 Subject: [PATCH 5/5] Tezt: reset regressions --- ... node with L1 (dac_rollup_arith_uses_reveals).out | 12 ++++++------ ...nd node with L1 (dac_rollup_arith_wrong_hash).out | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_uses_reveals).out b/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_uses_reveals).out index 021bdfbd7b40..57b53f64ff9d 100644 --- a/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_uses_reveals).out +++ b/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_uses_reveals).out @@ -37,9 +37,9 @@ This sequence of operations was run: { "level": 2, "commitment_hash": "[SC_ROLLUP_COMMITMENT_HASH]" } -./octez-client --wait none send sc rollup message 'text:["hash:scrrh1bZAo4kfAohhKcvgKc4yLHYN49EPuHezNXgFraa1mEbKSTCwf"]' from bootstrap2 +./octez-client --wait none send sc rollup message 'text:["hash:scrrh144gSwSgX2cSw3v8p9h5rf6L6Ryvx4ewEhBDhkz7ErY9EChS47a"]' from bootstrap2 Node is bootstrapped. -Estimated gas: 1001.463 units (will add 100 for safety) +Estimated gas: 1001.497 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -50,16 +50,16 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [PUBLIC_KEY_HASH] - Fee to the baker: ꜩ0.000402 + Fee to the baker: ꜩ0.000404 Expected counter: 1 Gas limit: 1102 Storage limit: 0 bytes Balance updates: - [PUBLIC_KEY_HASH] ... -ꜩ0.000402 - payload fees(the block proposer) ....... +ꜩ0.000402 + [PUBLIC_KEY_HASH] ... -ꜩ0.000404 + payload fees(the block proposer) ....... +ꜩ0.000404 Smart contract rollup messages submission This smart contract rollup messages submission was successfully applied - Consumed gas: 1001.463 + Consumed gas: 1001.497 Resulting inbox state: { level = 3 current messages hash = hash: [SC_ROLLUP_INBOX_LEVEL_TREE_HASH] level: 3 nb_messages_in_commitment_period = 9 diff --git a/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_wrong_hash).out b/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_wrong_hash).out index 3c13b62c32e8..fe0be78705b9 100644 --- a/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_wrong_hash).out +++ b/tezt/tests/expected/dal.ml/Alpha- Testing DAL rollup and node with L1 (dac_rollup_arith_wrong_hash).out @@ -37,9 +37,9 @@ This sequence of operations was run: { "level": 2, "commitment_hash": "[SC_ROLLUP_COMMITMENT_HASH]" } -./octez-client --wait none send sc rollup message 'text:["hash:scrrh1kXE3tnCVTJ21aDNVeaV86e8rS6jtiMEDpjZJtDnLXRThQdmy"]' from bootstrap2 +./octez-client --wait none send sc rollup message 'text:["hash:scrrh144gSwSgX2cSw3v8p9h5rf6L6Ryvx4ewEhBDhkz7ErY9EChS47a"]' from bootstrap2 Node is bootstrapped. -Estimated gas: 1001.463 units (will add 100 for safety) +Estimated gas: 1001.497 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -50,16 +50,16 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [PUBLIC_KEY_HASH] - Fee to the baker: ꜩ0.000402 + Fee to the baker: ꜩ0.000404 Expected counter: 1 Gas limit: 1102 Storage limit: 0 bytes Balance updates: - [PUBLIC_KEY_HASH] ... -ꜩ0.000402 - payload fees(the block proposer) ....... +ꜩ0.000402 + [PUBLIC_KEY_HASH] ... -ꜩ0.000404 + payload fees(the block proposer) ....... +ꜩ0.000404 Smart contract rollup messages submission This smart contract rollup messages submission was successfully applied - Consumed gas: 1001.463 + Consumed gas: 1001.497 Resulting inbox state: { level = 3 current messages hash = hash: [SC_ROLLUP_INBOX_LEVEL_TREE_HASH] level: 3 nb_messages_in_commitment_period = 9 -- GitLab