From 9f41ad5b2d7c474505639363567204a5524e5f35 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 24 Nov 2022 10:12:19 +0100 Subject: [PATCH] Proto,WASM: Make the protocol set the max number of ticks of snapshots --- src/lib_protocol_environment/sigs/v8.ml | 3 ++- .../sigs/v8/wasm_2_0_0.mli | 3 ++- src/lib_scoru_wasm/constants.ml | 12 ------------ src/lib_scoru_wasm/test/helpers/wasm_utils.ml | 5 +++-- src/lib_scoru_wasm/wasm_pvm.ml | 14 ++++++-------- src/lib_scoru_wasm/wasm_pvm_sig.ml | 9 +++++---- .../bin_wasm_repl/main_wasm_repl_alpha.ml | 3 ++- src/proto_alpha/lib_protocol/alpha_context.mli | 2 ++ src/proto_alpha/lib_protocol/sc_rollup_wasm.ml | 4 +++- src/proto_alpha/lib_protocol/sc_rollup_wasm.mli | 15 +++++++++++++++ .../lib_protocol/test/unit/test_sc_rollup_wasm.ml | 7 ++++++- 11 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/lib_protocol_environment/sigs/v8.ml b/src/lib_protocol_environment/sigs/v8.ml index 442d10addfb4..3c6ecc5f8178 100644 --- a/src/lib_protocol_environment/sigs/v8.ml +++ b/src/lib_protocol_environment/sigs/v8.ml @@ -12070,7 +12070,8 @@ type info = { module Make (Tree : Context.TREE with type key = string list and type value = bytes) : sig - val install_boot_sector : string -> Tree.tree -> Tree.tree Lwt.t + val install_boot_sector : + ticks_per_snapshot:Z.t -> string -> Tree.tree -> Tree.tree Lwt.t val compute_step : Tree.tree -> Tree.tree Lwt.t 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 1a7dfd7ef23e..c696ead44f0e 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 @@ -46,7 +46,8 @@ type info = { module Make (Tree : Context.TREE with type key = string list and type value = bytes) : sig - val install_boot_sector : string -> Tree.tree -> Tree.tree Lwt.t + val install_boot_sector : + ticks_per_snapshot:Z.t -> string -> Tree.tree -> Tree.tree Lwt.t val compute_step : Tree.tree -> Tree.tree Lwt.t diff --git a/src/lib_scoru_wasm/constants.ml b/src/lib_scoru_wasm/constants.ml index 693e07c48b17..16b0c0991eab 100644 --- a/src/lib_scoru_wasm/constants.ml +++ b/src/lib_scoru_wasm/constants.ml @@ -35,18 +35,6 @@ let wasm_entrypoint = "kernel_run" WASM kernel. *) let wasm_host_funcs_virual_module = "smart_rollup_core" -(* Number of ticks between snapshotable states, - should be chosen low enough to maintain refutability. - - Depends on - - speed (tick/s) of node in slow mode (from benchmark, 6000000 tick/s) - - the number of ticks in a commitment (Int64.max_int, - as per Number_of_ticks.max_value) - - see #3590 for more pointers -*) -let wasm_max_tick = Z.of_int 11_000_000_000 - (* TODO: https://gitlab.com/tezos/tezos/-/issues/3157 Find an appropriate number of reboots per inputs. *) diff --git a/src/lib_scoru_wasm/test/helpers/wasm_utils.ml b/src/lib_scoru_wasm/test/helpers/wasm_utils.ml index 2dee3d875d5e..9f1dffe616a3 100644 --- a/src/lib_scoru_wasm/test/helpers/wasm_utils.ml +++ b/src/lib_scoru_wasm/test/helpers/wasm_utils.ml @@ -49,8 +49,9 @@ let initial_tree ?(max_tick = default_max_tick) let max_tick_Z = Z.of_int64 max_tick in let* tree = empty_tree () in let* boot_sector = if from_binary then Lwt.return code else wat2wasm code in - let* tree = Wasm.install_boot_sector boot_sector tree in - let* tree = Wasm.Internal_for_tests.set_max_nb_ticks max_tick_Z tree in + let* tree = + Wasm.install_boot_sector ~ticks_per_snapshot:max_tick_Z boot_sector tree + in Wasm.Internal_for_tests.set_maximum_reboots_per_input max_reboots tree module Builtins = struct diff --git a/src/lib_scoru_wasm/wasm_pvm.ml b/src/lib_scoru_wasm/wasm_pvm.ml index 3c0d264605d6..d78e6262d37b 100644 --- a/src/lib_scoru_wasm/wasm_pvm.ml +++ b/src/lib_scoru_wasm/wasm_pvm.ml @@ -170,10 +170,7 @@ let pvm_state_encoding = (option durable_buffers_encoding) (scope ["wasm"] tick_state_encoding) (value ~default:Z.zero ["pvm"; "last_top_level_call"] Data_encoding.n) - (value - ~default:Constants.wasm_max_tick - ["pvm"; "max_nb_ticks"] - Data_encoding.n) + (value ["pvm"; "max_nb_ticks"] Data_encoding.n) (value ~default:Constants.maximum_reboots_per_input ["pvm"; "maximum_reboots_per_input"] @@ -207,7 +204,7 @@ module Make_pvm (Wasm_vm : Wasm_vm_sig.S) (T : Tezos_tree_encoding.TREE) : let* tree = T.remove tree ["wasm"] in Tree_encoding_runner.encode pvm_state_encoding pvm_state tree - let install_boot_sector bs tree = + let install_boot_sector ~ticks_per_snapshot bs tree = (* TODO: https://gitlab.com/tezos/tezos/-/issues/4240 We cannot manipulate the durable storage at this point, because the `durable` directory is empty. *) @@ -215,7 +212,7 @@ module Make_pvm (Wasm_vm : Wasm_vm_sig.S) (T : Tezos_tree_encoding.TREE) : let bs = Tezos_lazy_containers.Chunked_byte_vector.of_string bs in Tree_encoding_runner.encode Tezos_tree_encoding.( - tup2 + tup3 ~flatten:true (* We set the reboot flag in the durable storage to allow a reboot to the evaluation phase once the collection of the @@ -226,8 +223,9 @@ module Make_pvm (Wasm_vm : Wasm_vm_sig.S) (T : Tezos_tree_encoding.TREE) : (scope ["durable"; "kernel"; "env"; "reboot"; "_"] chunked_byte_vector) - (scope ["durable"; "kernel"; "boot.wasm"; "_"] chunked_byte_vector)) - (reboot_flag, bs) + (scope ["durable"; "kernel"; "boot.wasm"; "_"] chunked_byte_vector) + (value ["pvm"; "max_nb_ticks"] Data_encoding.n)) + (reboot_flag, bs, ticks_per_snapshot) tree let compute_step_many ?builtins ?stop_at_snapshot ~max_steps tree = diff --git a/src/lib_scoru_wasm/wasm_pvm_sig.ml b/src/lib_scoru_wasm/wasm_pvm_sig.ml index 21623ab7121c..a2fe3a864bae 100644 --- a/src/lib_scoru_wasm/wasm_pvm_sig.ml +++ b/src/lib_scoru_wasm/wasm_pvm_sig.ml @@ -60,10 +60,11 @@ module type S = sig include Wasm_vm_sig.Generic with type state := tree - (** [install_boot_sector payload tree] installs the [payload] passed - as an argument in [tree] so that it is interpreted as the kernel - to be used by the PVM. *) - val install_boot_sector : string -> tree -> tree Lwt.t + (** [install_boot_sector ~ticks_per_snapshot payload tree] installs + the [payload] passed as an argument in [tree] so that it is + interpreted as the kernel to be used by the PVM. *) + val install_boot_sector : + ticks_per_snapshot:Z.t -> string -> tree -> tree Lwt.t (** [get_output output state] returns the payload associated with the given output. The result is meant to be deserialized using diff --git a/src/proto_alpha/bin_wasm_repl/main_wasm_repl_alpha.ml b/src/proto_alpha/bin_wasm_repl/main_wasm_repl_alpha.ml index 9d0b9c72c5d8..9215a094fc59 100644 --- a/src/proto_alpha/bin_wasm_repl/main_wasm_repl_alpha.ml +++ b/src/proto_alpha/bin_wasm_repl/main_wasm_repl_alpha.ml @@ -57,6 +57,7 @@ let link module_ = installation into a tree for the PVM interpreter. *) let handle_module binary name module_ = let open Lwt_result_syntax in + let open Protocol.Alpha_context.Sc_rollup in let* ast = Repl_helpers.trap_exn (fun () -> if binary then parse_binary_module name module_ @@ -67,7 +68,7 @@ let handle_module binary name module_ = let* _ = link ast in let*! tree = initial_tree - ~max_tick:(Z.to_int64 Tezos_scoru_wasm.Constants.wasm_max_tick) + ~max_tick:(Z.to_int64 Wasm_2_0_0PVM.ticks_per_snapshot) ~from_binary:binary module_ in diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 63068a8cc2f6..5a7cb2bc2603 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3632,6 +3632,8 @@ module Sc_rollup : sig end module Wasm_2_0_0PVM : sig + val ticks_per_snapshot : Z.t + module type P = sig module Tree : Context.TREE with type key = string list and type value = bytes diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml index 812bd9a68525..fb5b07f55079 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -66,6 +66,8 @@ let () = (fun () -> WASM_proof_production_failed) module V2_0_0 = struct + let ticks_per_snapshot = Z.of_int64 11_000_000_000L + (* This is the state hash of reference that both the prover of the node and the verifier of the protocol {!Protocol_implementation} @@ -242,7 +244,7 @@ module V2_0_0 = struct Lwt.return state let install_boot_sector state boot_sector = - WASM_machine.install_boot_sector boot_sector state + WASM_machine.install_boot_sector ~ticks_per_snapshot boot_sector state let state_hash state = let context_hash = Tree.hash state in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli b/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli index 3d42188d8863..35f7ef51753e 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.mli @@ -109,4 +109,19 @@ module V2_0_0 : sig have to agree on (if they do, it means they are using the same tree structure). *) val reference_initial_state_hash : Sc_rollup_repr.State_hash.t + + (** Number of ticks between snapshotable states, chosen low enough + to maintain refutability. + + {b Warning:} This value is used to specialize the dissection + predicate of the WASM PVM. Do not change it without a migration + plan for already originated smart rollups. + + Depends on + - speed (tick/s) of node in slow mode (from benchmark, 6000000 tick/s) + - the number of ticks in a commitment ({!Int64.max_int}, + as per Number_of_ticks.max_value) + + see #3590 for more pointers *) + val ticks_per_snapshot : Z.t end diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml index 187b2a935c9b..82a7afc1f409 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml @@ -233,7 +233,12 @@ let test_output () = match parsed.it with Script.Textual m -> m | _ -> assert false in let*! boot_sector = Encode.encode parsed in - let*! tree = Wasm.install_boot_sector boot_sector empty_tree in + let*! tree = + Wasm.install_boot_sector + ~ticks_per_snapshot:Sc_rollup_wasm.V2_0_0.ticks_per_snapshot + boot_sector + empty_tree + in let*! tree = Wasm.Internal_for_tests.set_max_nb_ticks (Z.of_int64 50_000_000L) tree in -- GitLab