diff --git a/src/lib_smart_rollup/rollup_constants.ml b/src/lib_smart_rollup/rollup_constants.ml index 72826294237346dba394cbbb91d3384811f67043..3c1c0287b891d73beb7ec55783883b8eda4fabd5 100644 --- a/src/lib_smart_rollup/rollup_constants.ml +++ b/src/lib_smart_rollup/rollup_constants.ml @@ -29,6 +29,7 @@ type dal_constants = { feature_enable : bool; attestation_lag : int; number_of_slots : int; + cryptobox_parameters : Tezos_crypto_dal.Cryptobox.parameters; } type reveal_activation_level = { diff --git a/src/lib_smart_rollup_node/node_context.ml b/src/lib_smart_rollup_node/node_context.ml index 47b6b33de92ba1f702adf92dd8b6a2b8e20cf8b9..306fe102bd3008a36505ad418f1131e06b29aed0 100644 --- a/src/lib_smart_rollup_node/node_context.ml +++ b/src/lib_smart_rollup_node/node_context.ml @@ -773,6 +773,23 @@ let last_seen_protocol node_ctxt = | None | Some [] -> None | Some (p :: _) -> Some p.protocol +let protocol_activation_level node_ctxt protocol_hash = + let open Lwt_result_syntax in + let* protocols = Store.Protocols.read node_ctxt.store.protocols in + match + Option.bind + protocols + (List.find_map (function Store.Protocols.{protocol; level; _} -> + if Protocol_hash.(protocol_hash = protocol) then Some level else None)) + with + | None -> + failwith + "Could not determine the activation level of a previously unseen \ + protocol %a" + Protocol_hash.pp + protocol_hash + | Some l -> return l + let save_protocol_info node_ctxt (block : Layer1.header) ~(predecessor : Layer1.header) = let open Lwt_result_syntax in @@ -943,7 +960,8 @@ let save_confirmed_slots_histories {store; _} block hist = Store.Dal_confirmed_slots_histories.add store.irmin_store block hist module Internal_for_tests = struct - let create_node_context cctxt current_protocol ~data_dir kind = + let create_node_context cctxt (current_protocol : current_protocol) ~data_dir + kind = let open Lwt_result_syntax in let l2_blocks_cache_size = Configuration.default_l2_blocks_cache_size in let index_buffer_size = Configuration.default_index_buffer_size in @@ -966,6 +984,18 @@ module Internal_for_tests = struct let l1_ctxt = Layer1.Internal_for_tests.dummy cctxt in let lcc = Reference.new_ {commitment = Commitment.Hash.zero; level = 0l} in let lpc = Reference.new_ None in + let* () = + Store.Protocols.write + store.protocols + [ + Store.Protocols. + { + level = Activation_level 0l; + proto_level = current_protocol.proto_level; + protocol = current_protocol.hash; + }; + ] + in return { cctxt = (cctxt :> Client_context.full); diff --git a/src/lib_smart_rollup_node/node_context.mli b/src/lib_smart_rollup_node/node_context.mli index e48178608b296803fa86b6b49273ce91bce254a6..8d158599aae60b4703f4bcb40fc671fe9dea0bf4 100644 --- a/src/lib_smart_rollup_node/node_context.mli +++ b/src/lib_smart_rollup_node/node_context.mli @@ -393,6 +393,11 @@ val protocol_of_level : _ t -> int32 -> proto_info tzresult Lwt.t (** Returns the last protocol seen by the rollup node. *) val last_seen_protocol : _ t -> Protocol_hash.t option tzresult Lwt.t +(** Returns the activation level of a protocol or fails if the protocol was + never seen by the rollup node. *) +val protocol_activation_level : + _ t -> Protocol_hash.t -> Store.Protocols.level tzresult Lwt.t + (** [save_protocol_info t block ~predecessor] saves to disk the protocol information associated to the [block], if there is a protocol change between [block] and [predecessor]. *) diff --git a/src/lib_smart_rollup_node/protocol_plugins.ml b/src/lib_smart_rollup_node/protocol_plugins.ml index 5c4919b15a441e6e66f4d7433328d44d11924b13..aa40a351b68cf15f442f5d22f564de94db142c1d 100644 --- a/src/lib_smart_rollup_node/protocol_plugins.ml +++ b/src/lib_smart_rollup_node/protocol_plugins.ml @@ -83,3 +83,40 @@ let last_proto_plugin node_ctxt = | Some protocol -> let*? plugin = proto_plugin_for_protocol protocol in return plugin + +module Constants_cache = + Aches_lwt.Lache.Make_result + (Aches.Rache.Transfer (Aches.Rache.LRU) (Protocol_hash)) + +let constants_cache = + let cache_size = 3 in + Constants_cache.create cache_size + +let get_constants_of_protocol (node_ctxt : _ Node_context.t) protocol_hash = + let open Lwt_result_syntax in + if Protocol_hash.(protocol_hash = node_ctxt.current_protocol.hash) then + return node_ctxt.current_protocol.constants + else + let retrieve protocol_hash = + let*? plugin = proto_plugin_for_protocol protocol_hash in + let module Plugin = (val plugin) in + let* (First_known l | Activation_level l) = + Node_context.protocol_activation_level node_ctxt protocol_hash + in + Plugin.Layer1_helpers.retrieve_constants ~block:(`Level l) node_ctxt.cctxt + in + Constants_cache.bind_or_put + constants_cache + protocol_hash + retrieve + Lwt.return + +let get_constants_of_level node_ctxt level = + let open Lwt_result_syntax in + let* {protocol; _} = Node_context.protocol_of_level node_ctxt level in + get_constants_of_protocol node_ctxt protocol + +let get_constants_of_block_hash node_ctxt block_hash = + let open Lwt_result_syntax in + let* level = Node_context.level_of_hash node_ctxt block_hash in + get_constants_of_level node_ctxt level diff --git a/src/lib_smart_rollup_node/protocol_plugins.mli b/src/lib_smart_rollup_node/protocol_plugins.mli index f846f0415958271f7f81835e2ea6abe6ee812d00..10305f511ef5316a67bcf9322694735b7cee7d55 100644 --- a/src/lib_smart_rollup_node/protocol_plugins.mli +++ b/src/lib_smart_rollup_node/protocol_plugins.mli @@ -23,6 +23,8 @@ (* *) (*****************************************************************************) +(** {2 Protocol registration logic} *) + type proto_plugin = (module Protocol_plugin_sig.S) (** Register a protocol plugin for a specific protocol to be used by the @@ -51,3 +53,27 @@ val proto_plugin_for_block : (** Returns the plugin corresponding to the last protocol seen by the rollup node. *) val last_proto_plugin : _ Node_context.t -> proto_plugin tzresult Lwt.t + +(** {2 Safe protocol specific constants} + + These functions provide a way to retrieve constants in a safe manner, + depending on the context. +*) + +(** Retrieve constants for a given protocol (values are cached). *) +val get_constants_of_protocol : + _ Node_context.t -> + Protocol_hash.t -> + Rollup_constants.protocol_constants tzresult Lwt.t + +(** Retrieve constants for a given level (values are cached). *) +val get_constants_of_level : + _ Node_context.t -> + int32 -> + Rollup_constants.protocol_constants tzresult Lwt.t + +(** Retrieve constants for a given block hash (values are cached). *) +val get_constants_of_block_hash : + _ Node_context.t -> + Block_hash.t -> + Rollup_constants.protocol_constants tzresult Lwt.t diff --git a/src/lib_smart_rollup_node/publisher.ml b/src/lib_smart_rollup_node/publisher.ml index 8298bfb9f3402e6dbd53caed8fe1cf2d90078dcb..f201372c5e491e9158a8ea9211805e46f26383e1 100644 --- a/src/lib_smart_rollup_node/publisher.ml +++ b/src/lib_smart_rollup_node/publisher.ml @@ -75,6 +75,8 @@ let sub_level level decrement = if r < 0l then None else Some r let sc_rollup_commitment_period node_ctxt = + (* Publishing commitments is done w.r.t. the period in the protocol in which + the commitment is published, and not the one for the inbox level. *) node_ctxt.Node_context.current_protocol.constants.sc_rollup .commitment_period_in_blocks diff --git a/src/lib_smart_rollup_node/pvm_plugin_sig.ml b/src/lib_smart_rollup_node/pvm_plugin_sig.ml index fb86b8e1f33edd4b80d7d3ff801dd903dd88db63..df69a54d195ab3db21030b84e37a4742a5154af5 100644 --- a/src/lib_smart_rollup_node/pvm_plugin_sig.ml +++ b/src/lib_smart_rollup_node/pvm_plugin_sig.ml @@ -89,7 +89,7 @@ module type S = sig val install_boot_sector : Kind.t -> Context.tree -> string -> Context.tree Lwt.t - val get_status : _ Node_context.t -> Context.tree -> string Lwt.t + val get_status : _ Node_context.t -> Context.tree -> string tzresult Lwt.t val get_current_level : Kind.t -> Context.tree -> int32 option Lwt.t diff --git a/src/lib_smart_rollup_node/rollup_node_daemon.ml b/src/lib_smart_rollup_node/rollup_node_daemon.ml index 459e0c3f2bfda386b5c8eb50933772d011db5dc1..3d9d7bf5b781cdaab5b7a2628ca65cda5b2cf531 100644 --- a/src/lib_smart_rollup_node/rollup_node_daemon.ml +++ b/src/lib_smart_rollup_node/rollup_node_daemon.ml @@ -32,17 +32,6 @@ type state = { node_ctxt : Node_context.rw; } -let protocol_info cctxt protocol block_hash = - let open Result_syntax in - let+ plugin = Protocol_plugins.proto_plugin_for_protocol protocol in - let module Plugin = (val plugin) in - let constants = - Plugin.Layer1_helpers.retrieve_constants - ~block:(`Hash (block_hash, 0)) - cctxt - in - (constants, plugin) - let is_before_origination (node_ctxt : _ Node_context.t) (header : Layer1.header) = let origination_level = node_ctxt.genesis_info.level in @@ -84,10 +73,10 @@ let handle_protocol_migration ~catching_up state (head : Layer1.header) = state.node_ctxt.current_protocol.proto_level ) (new_protocol, head_proto.proto_level) in - let*? constants, new_plugin = - protocol_info state.node_ctxt.cctxt new_protocol head.hash + let*? new_plugin = Protocol_plugins.proto_plugin_for_protocol new_protocol in + let* constants = + Protocol_plugins.get_constants_of_protocol state.node_ctxt new_protocol in - let* constants in let new_protocol = { Node_context.hash = new_protocol; @@ -517,8 +506,8 @@ let plugin_of_first_block cctxt (block : Layer1.header) = ~block:(`Hash (block.hash, 0)) () in - let*? constants, plugin = protocol_info cctxt current_protocol block.hash in - return (constants, current_protocol, plugin) + let*? plugin = Protocol_plugins.proto_plugin_for_protocol current_protocol in + return (current_protocol, plugin) let run ~data_dir ~irmin_cache_size ~index_buffer_size ?log_kernel_debug_file (configuration : Configuration.t) (cctxt : Client_context.full) = @@ -552,9 +541,10 @@ let run ~data_dir ~irmin_cache_size ~index_buffer_size ?log_kernel_debug_file configuration.sc_rollup_node_operators in - let* constants, protocol, plugin = plugin_of_first_block cctxt head in + let* protocol, plugin = plugin_of_first_block cctxt head in let module Plugin = (val plugin) in - let* constants + let* constants = + Plugin.Layer1_helpers.retrieve_constants ~block:(`Hash (head.hash, 0)) cctxt and* genesis_info = Plugin.Layer1_helpers.retrieve_genesis_info cctxt diff --git a/src/lib_smart_rollup_node/rpc_directory.ml b/src/lib_smart_rollup_node/rpc_directory.ml index 95663b4dcdc756c1d97d5f20e703bed97de9fc55..7f65054f629b6e3efc36453a9d275e18e350f858 100644 --- a/src/lib_smart_rollup_node/rpc_directory.ml +++ b/src/lib_smart_rollup_node/rpc_directory.ml @@ -139,18 +139,25 @@ let () = ((last_commitment - inbox_level) / commitment_period * commitment_period) v} - *) +*) let commitment_level_of_inbox_level (node_ctxt : _ Node_context.t) inbox_level = - let open Option_syntax in - let+ last_published_commitment = Reference.get node_ctxt.lpc in + let open Lwt_result_syntax in + let last_published_commitment = Reference.get node_ctxt.lpc in + let+ constants = + Protocol_plugins.get_constants_of_level node_ctxt inbox_level + in let commitment_period = - Int32.of_int - node_ctxt.current_protocol.constants.sc_rollup.commitment_period_in_blocks + Int32.of_int constants.sc_rollup.commitment_period_in_blocks in - let last_published = last_published_commitment.inbox_level in - let open Int32 in - div (sub last_published inbox_level) commitment_period - |> mul commitment_period |> sub last_published + Option.map + (fun last_published_commitment -> + (* TODO: https://gitlab.com/tezos/tezos/-/issues/6246 + fix and test last_published_inbox_level in RPC dir. *) + let last_published = last_published_commitment.Commitment.inbox_level in + let open Int32 in + div (sub last_published inbox_level) commitment_period + |> mul commitment_period |> sub last_published) + last_published_commitment let inbox_info_of_level (node_ctxt : _ Node_context.t) inbox_level = let open Lwt_result_syntax in @@ -185,7 +192,7 @@ let () = let* finalized, cemented = inbox_info_of_level node_ctxt l1_level in - let commitment_level = + let* commitment_level = commitment_level_of_inbox_level node_ctxt l1_level in match commitment_level with diff --git a/src/proto_017_PtNairob/lib_sc_rollup_node/dal_slots_tracker.ml b/src/proto_017_PtNairob/lib_sc_rollup_node/dal_slots_tracker.ml index 868e7a6692d2dd5a5ef772ba776b18ccd94a99b5..9413217988da641b3d8d9ef51649ef29004d567f 100644 --- a/src/proto_017_PtNairob/lib_sc_rollup_node/dal_slots_tracker.ml +++ b/src/proto_017_PtNairob/lib_sc_rollup_node/dal_slots_tracker.ml @@ -52,12 +52,12 @@ type confirmations_info = { confirmed_slots_indexes : Bitset.t; } -(** [slots_info node_ctxt head] gathers information about the slot confirmations +(** [slots_info constants node_ctxt head] gathers information about the slot confirmations of slot indexes. It reads the slot indexes that have been declared available from [head]'s block receipt. It then returns the hash of the block where the slot headers have been published and the list of slot indexes that have been confirmed for that block. *) -let slots_info node_ctxt (Layer1.{hash; _} as head) = +let slots_info constants node_ctxt (Layer1.{hash; _} as head) = (* DAL/FIXME: https://gitlab.com/tezos/tezos/-/issues/3722 The case for protocol migrations when the lag constant has been changed is tricky, especially if the lag is reduced. @@ -67,9 +67,7 @@ let slots_info node_ctxt (Layer1.{hash; _} as head) = we reduce the lag to 1, then the slots header will never be confirmed. *) let open Lwt_result_syntax in - let lag = - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = constants.Rollup_constants.dal.attestation_lag in (* we are downloading endorsemented for slots at level [level], so we need to download the data at level [level - lag]. *) @@ -127,24 +125,21 @@ let to_slot_index_list (constants : Rollup_constants.protocol_constants) bitset Use a shared storage between dal and rollup node to store slots data. *) -let download_and_save_slots (node_context : _ Node_context.t) +let download_and_save_slots constants (node_context : _ Node_context.t) ~current_block_hash {published_block_hash; confirmed_slots_indexes} = let open Lwt_result_syntax in let*? all_slots = - Bitset.fill - ~length:node_context.current_protocol.constants.dal.number_of_slots + Bitset.fill ~length:constants.Rollup_constants.dal.number_of_slots |> Environment.wrap_tzresult in let*? not_confirmed = Environment.wrap_tzresult - @@ to_slot_index_list node_context.current_protocol.constants + @@ to_slot_index_list constants @@ Bitset.diff all_slots confirmed_slots_indexes in let*? confirmed = Environment.wrap_tzresult - @@ to_slot_index_list - node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in (* The contents of each slot index are written to a different location on disk, therefore calls to store contents for different slot indexes can @@ -181,17 +176,15 @@ let download_and_save_slots (node_context : _ Node_context.t) confirmed module Confirmed_slots_history = struct - (** [confirmed_slots_with_headers node_ctxt confirmations_info] returns the + (** [confirmed_slots_with_headers constants node_ctxt confirmations_info] returns the headers of confirmed slot indexes for the block with hash [confirmations_info.published_block_hash]. *) - let confirmed_slots_with_headers node_ctxt + let confirmed_slots_with_headers constants node_ctxt {published_block_hash; confirmed_slots_indexes; _} = let open Lwt_result_syntax in let*? relevant_slots_indexes = Environment.wrap_tzresult - @@ to_slot_index_list - node_ctxt.Node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in List.map_ep (fun slot_index -> @@ -204,7 +197,7 @@ module Confirmed_slots_history = struct Sc_rollup_proto_types.Dal.Slot_header.of_octez h) relevant_slots_indexes - let read_slots_history_from_l1 {Node_context.cctxt; _} block = + let read_slots_history_from_l1 _constants {Node_context.cctxt; _} block = let open Lwt_result_syntax in (* We return the empty Slots_history if DAL is not enabled. *) let* slots_list_opt = @@ -219,12 +212,9 @@ module Confirmed_slots_history = struct slots_history and slots_history's cache entries in the store after [origination_level + attestation_lag] blocks. This function checks if that level is reached or not. *) - let should_process_dal_slots node_ctxt block_level = + let should_process_dal_slots constants node_ctxt block_level = let open Node_context in - let lag = - Int32.of_int - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = Int32.of_int constants.Rollup_constants.dal.attestation_lag in let block_level = Raw_level.to_int32 block_level in let genesis_level = node_ctxt.genesis_info.level in Int32.(block_level >= add lag genesis_level) @@ -234,13 +224,16 @@ module Confirmed_slots_history = struct = let open Lwt_result_syntax in let* confirmed_slots_history_opt = find node_ctxt block_hash in + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt block_level + in let block_level = Raw_level.of_int32_exn block_level in let should_process_dal_slots = - should_process_dal_slots node_ctxt block_level + should_process_dal_slots constants node_ctxt block_level in match (confirmed_slots_history_opt, should_process_dal_slots) with | Some confirmed_dal_slots, true -> return confirmed_dal_slots - | None, false -> default node_ctxt block_hash + | None, false -> default constants node_ctxt block_hash | Some _confirmed_dal_slots, false -> failwith "The confirmed DAL %S for block hash %a (level = %a) is not expected \ @@ -284,10 +277,8 @@ module Confirmed_slots_history = struct block ~entry_kind:"slots history cache" ~find - ~default:(fun node_ctxt _block -> - let num_slots = - node_ctxt.Node_context.current_protocol.constants.dal.number_of_slots - in + ~default:(fun constants _node_ctxt _block -> + let num_slots = constants.Rollup_constants.dal.number_of_slots in (* FIXME/DAL: https://gitlab.com/tezos/tezos/-/issues/3788 Put an accurate value for capacity. The value `num_slots * 60000` below is chosen based on: @@ -301,11 +292,11 @@ module Confirmed_slots_history = struct @@ Dal.Slots_history.History_cache.empty ~capacity:(Int64.of_int @@ (num_slots * 60000))) - let update node_ctxt Layer1.({hash = head_hash; _} as head) confirmation_info - = + let update constants node_ctxt Layer1.({hash = head_hash; _} as head) + confirmation_info = let open Lwt_result_syntax in let* slots_to_save = - confirmed_slots_with_headers node_ctxt confirmation_info + confirmed_slots_with_headers constants node_ctxt confirmation_info in let slots_to_save = let open Dal in @@ -345,19 +336,21 @@ module Confirmed_slots_history = struct return () end -let process_head node_ctxt (Layer1.{hash = head_hash; _} as head) = +let process_head node_ctxt (Layer1.{hash = head_hash; level} as head) = let open Lwt_result_syntax in - let* confirmation_info = slots_info node_ctxt head in + let* constants = Protocol_plugins.get_constants_of_level node_ctxt level in + let* confirmation_info = slots_info constants node_ctxt head in match confirmation_info with | None -> return_unit | Some confirmation_info -> let* () = download_and_save_slots ~current_block_hash:head_hash + constants node_ctxt confirmation_info in - Confirmed_slots_history.update node_ctxt head confirmation_info + Confirmed_slots_history.update constants node_ctxt head confirmation_info let slots_history_of_hash = Confirmed_slots_history.slots_history_of_hash diff --git a/src/proto_017_PtNairob/lib_sc_rollup_node/fueled_pvm.ml b/src/proto_017_PtNairob/lib_sc_rollup_node/fueled_pvm.ml index e87a2e50e98a3a8a887a160487f7407deeecefc9..41a16a73c474b36f86c42ba99c5635c7e472b879 100644 --- a/src/proto_017_PtNairob/lib_sc_rollup_node/fueled_pvm.ml +++ b/src/proto_017_PtNairob/lib_sc_rollup_node/fueled_pvm.ml @@ -77,11 +77,12 @@ module Make_fueled (F : Fuel.S) : FUELED_PVM with type fuel = F.t = struct message_index ~fuel start_tick failing_ticks state = let open Lwt_result_syntax in let open Delayed_write_monad.Lwt_result_syntax in + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt (Int32.of_int level) + in let module PVM = (val Pvm.of_kind node_ctxt.kind) in let metadata = metadata node_ctxt in - let dal_attestation_lag = - node_ctxt.current_protocol.constants.dal.attestation_lag - in + let dal_attestation_lag = constants.dal.attestation_lag in let decode_reveal (Tezos_scoru_wasm.Wasm_pvm_state.Reveal_raw payload) = match Data_encoding.Binary.of_string_opt diff --git a/src/proto_017_PtNairob/lib_sc_rollup_node/layer1_helpers.ml b/src/proto_017_PtNairob/lib_sc_rollup_node/layer1_helpers.ml index 9d2e83e2b7c365bf0ae14b4b8c857a936a8fbf0d..4b875658949bdc3608d6a45e17322cfe5f6a2038 100644 --- a/src/proto_017_PtNairob/lib_sc_rollup_node/layer1_helpers.ml +++ b/src/proto_017_PtNairob/lib_sc_rollup_node/layer1_helpers.ml @@ -129,7 +129,14 @@ let constants_of_parametric minimal_block_delay; delay_increment_per_round; sc_rollup = {challenge_window_in_blocks; commitment_period_in_blocks; _}; - dal = {feature_enable; attestation_lag; number_of_slots; _}; + dal = + { + feature_enable; + attestation_lag; + number_of_slots; + cryptobox_parameters; + _; + }; _; } = let open Protocol.Alpha_context in @@ -143,7 +150,8 @@ let constants_of_parametric commitment_period_in_blocks; reveal_activation_level = None; }; - dal = {feature_enable; attestation_lag; number_of_slots}; + dal = + {feature_enable; attestation_lag; number_of_slots; cryptobox_parameters}; } (* TODO: https://gitlab.com/tezos/tezos/-/issues/2901 diff --git a/src/proto_017_PtNairob/lib_sc_rollup_node/pvm_plugin.ml b/src/proto_017_PtNairob/lib_sc_rollup_node/pvm_plugin.ml index fbe612a4f49f40fbe377db954ee89e39884a8ec4..4be1b47326abbe80aa3445c02067853696dbdd13 100644 --- a/src/proto_017_PtNairob/lib_sc_rollup_node/pvm_plugin.ml +++ b/src/proto_017_PtNairob/lib_sc_rollup_node/pvm_plugin.ml @@ -51,10 +51,10 @@ let install_boot_sector kind state boot_sector = PVM.install_boot_sector state boot_sector let get_status node_ctxt state = - let open Lwt_syntax in + let open Lwt_result_syntax in let module PVM = (val Pvm.of_kind node_ctxt.Node_context.kind) in - let+ status = PVM.get_status state in - PVM.string_of_status status + let*! status = PVM.get_status state in + return (PVM.string_of_status status) let get_current_level kind state = let open Lwt_option_syntax in diff --git a/src/proto_017_PtNairob/lib_sc_rollup_node/refutation_game_helpers.ml b/src/proto_017_PtNairob/lib_sc_rollup_node/refutation_game_helpers.ml index a0367ac905ccf7319ea08e018521628cc939effc..21c828b627abedc6fccdaa43cdf5d912a5b010b3 100644 --- a/src/proto_017_PtNairob/lib_sc_rollup_node/refutation_game_helpers.ml +++ b/src/proto_017_PtNairob/lib_sc_rollup_node/refutation_game_helpers.ml @@ -143,13 +143,10 @@ let generate_proof (node_ctxt : _ Node_context.t) in (* We fetch the value of protocol constants at block snapshot level where the game started. *) - let* parametric_constants = - let cctxt = node_ctxt.cctxt in - Protocol.Constants_services.parametric - (new Protocol_client_context.wrap_full cctxt) - (cctxt#chain, `Level snapshot_level_int32) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt snapshot_level_int32 in - let dal_l1_parameters = parametric_constants.dal in + let dal_l1_parameters = constants.dal in let dal_parameters = dal_l1_parameters.cryptobox_parameters in let dal_attestation_lag = dal_l1_parameters.attestation_lag in diff --git a/src/proto_018_Proxford/lib_sc_rollup_node/dal_slots_tracker.ml b/src/proto_018_Proxford/lib_sc_rollup_node/dal_slots_tracker.ml index cf0e48520431b00c8fb7f2c205f76c21837d7e87..86fb896379d0bf7300c00c5a55d90f0b2bc48bb0 100644 --- a/src/proto_018_Proxford/lib_sc_rollup_node/dal_slots_tracker.ml +++ b/src/proto_018_Proxford/lib_sc_rollup_node/dal_slots_tracker.ml @@ -52,12 +52,12 @@ type confirmations_info = { confirmed_slots_indexes : Bitset.t; } -(** [slots_info node_ctxt head] gathers information about the slot confirmations +(** [slots_info constants node_ctxt head] gathers information about the slot confirmations of slot indexes. It reads the slot indexes that have been declared available from [head]'s block receipt. It then returns the hash of the block where the slot headers have been published and the list of slot indexes that have been confirmed for that block. *) -let slots_info node_ctxt (Layer1.{hash; _} as head) = +let slots_info constants node_ctxt (Layer1.{hash; _} as head) = (* DAL/FIXME: https://gitlab.com/tezos/tezos/-/issues/3722 The case for protocol migrations when the lag constant has been changed is tricky, especially if the lag is reduced. @@ -67,9 +67,7 @@ let slots_info node_ctxt (Layer1.{hash; _} as head) = we reduce the lag to 1, then the slots header will never be confirmed. *) let open Lwt_result_syntax in - let lag = - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = constants.Rollup_constants.dal.attestation_lag in (* we are downloading attestation for slots at level [level], so we need to download the data at level [level - lag]. *) @@ -103,9 +101,7 @@ let slots_info node_ctxt (Layer1.{hash; _} as head) = node_ctxt ~published_in_block_hash:published_block_hash in - let number_of_slots = - node_ctxt.Node_context.current_protocol.constants.dal.number_of_slots - in + let number_of_slots = constants.dal.number_of_slots in let confirmed_slots_indexes_list = List.filter (Dal.Attestation.is_attested confirmed_slots) @@ -132,24 +128,21 @@ let to_slot_index_list (constants : Rollup_constants.protocol_constants) bitset Use a shared storage between dal and rollup node to store slots data. *) -let download_and_save_slots (node_context : _ Node_context.t) +let download_and_save_slots constants (node_context : _ Node_context.t) ~current_block_hash {published_block_hash; confirmed_slots_indexes} = let open Lwt_result_syntax in let*? all_slots = - Bitset.fill - ~length:node_context.current_protocol.constants.dal.number_of_slots + Bitset.fill ~length:constants.Rollup_constants.dal.number_of_slots |> Environment.wrap_tzresult in let*? not_confirmed = Environment.wrap_tzresult - @@ to_slot_index_list node_context.current_protocol.constants + @@ to_slot_index_list constants @@ Bitset.diff all_slots confirmed_slots_indexes in let*? confirmed = Environment.wrap_tzresult - @@ to_slot_index_list - node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in (* The contents of each slot index are written to a different location on disk, therefore calls to store contents for different slot indexes can @@ -176,8 +169,7 @@ let download_and_save_slots (node_context : _ Node_context.t) let*! () = Dal_slots_tracker_event.slot_has_been_confirmed (Sc_rollup_proto_types.Dal.Slot_index.of_octez - ~number_of_slots: - node_context.current_protocol.constants.dal.number_of_slots + ~number_of_slots:constants.dal.number_of_slots s_slot) published_block_hash current_block_hash @@ -186,17 +178,15 @@ let download_and_save_slots (node_context : _ Node_context.t) confirmed module Confirmed_slots_history = struct - (** [confirmed_slots_with_headers node_ctxt confirmations_info] returns the + (** [confirmed_slots_with_headers constants node_ctxt confirmations_info] returns the headers of confirmed slot indexes for the block with hash [confirmations_info.published_block_hash]. *) - let confirmed_slots_with_headers node_ctxt + let confirmed_slots_with_headers constants node_ctxt {published_block_hash; confirmed_slots_indexes; _} = let open Lwt_result_syntax in let*? relevant_slots_indexes = Environment.wrap_tzresult - @@ to_slot_index_list - node_ctxt.Node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in List.map_ep (fun slot_index -> @@ -207,12 +197,11 @@ module Confirmed_slots_history = struct slot_index in Sc_rollup_proto_types.Dal.Slot_header.of_octez - ~number_of_slots: - node_ctxt.current_protocol.constants.dal.number_of_slots + ~number_of_slots:constants.dal.number_of_slots h) relevant_slots_indexes - let read_slots_history_from_l1 {Node_context.cctxt; _} block = + let read_slots_history_from_l1 _constants {Node_context.cctxt; _} block = let open Lwt_result_syntax in (* We return the empty Slots_history if DAL is not enabled. *) let* slots_list_opt = @@ -227,12 +216,9 @@ module Confirmed_slots_history = struct slots_history and slots_history's cache entries in the store after [origination_level + attestation_lag] blocks. This function checks if that level is reached or not. *) - let should_process_dal_slots node_ctxt block_level = + let should_process_dal_slots constants node_ctxt block_level = let open Node_context in - let lag = - Int32.of_int - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = Int32.of_int constants.Rollup_constants.dal.attestation_lag in let block_level = Raw_level.to_int32 block_level in let genesis_level = node_ctxt.genesis_info.level in Int32.(block_level >= add lag genesis_level) @@ -242,13 +228,16 @@ module Confirmed_slots_history = struct = let open Lwt_result_syntax in let* confirmed_slots_history_opt = find node_ctxt block_hash in + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt block_level + in let block_level = Raw_level.of_int32_exn block_level in let should_process_dal_slots = - should_process_dal_slots node_ctxt block_level + should_process_dal_slots constants node_ctxt block_level in match (confirmed_slots_history_opt, should_process_dal_slots) with | Some confirmed_dal_slots, true -> return confirmed_dal_slots - | None, false -> default node_ctxt block_hash + | None, false -> default constants node_ctxt block_hash | Some _confirmed_dal_slots, false -> failwith "The confirmed DAL %S for block hash %a (level = %a) is not expected \ @@ -292,10 +281,8 @@ module Confirmed_slots_history = struct block ~entry_kind:"slots history cache" ~find - ~default:(fun node_ctxt _block -> - let num_slots = - node_ctxt.Node_context.current_protocol.constants.dal.number_of_slots - in + ~default:(fun constants _node_ctxt _block -> + let num_slots = constants.Rollup_constants.dal.number_of_slots in (* FIXME/DAL: https://gitlab.com/tezos/tezos/-/issues/3788 Put an accurate value for capacity. The value `num_slots * 60000` below is chosen based on: @@ -309,11 +296,11 @@ module Confirmed_slots_history = struct @@ Dal.Slots_history.History_cache.empty ~capacity:(Int64.of_int @@ (num_slots * 60000))) - let update node_ctxt Layer1.({hash = head_hash; _} as head) confirmation_info - = + let update constants node_ctxt Layer1.({hash = head_hash; _} as head) + confirmation_info = let open Lwt_result_syntax in let* slots_to_save = - confirmed_slots_with_headers node_ctxt confirmation_info + confirmed_slots_with_headers constants node_ctxt confirmation_info in let slots_to_save = let open Dal in @@ -353,19 +340,21 @@ module Confirmed_slots_history = struct return () end -let process_head node_ctxt (Layer1.{hash = head_hash; _} as head) = +let process_head node_ctxt (Layer1.{hash = head_hash; level} as head) = let open Lwt_result_syntax in - let* confirmation_info = slots_info node_ctxt head in + let* constants = Protocol_plugins.get_constants_of_level node_ctxt level in + let* confirmation_info = slots_info constants node_ctxt head in match confirmation_info with | None -> return_unit | Some confirmation_info -> let* () = download_and_save_slots ~current_block_hash:head_hash + constants node_ctxt confirmation_info in - Confirmed_slots_history.update node_ctxt head confirmation_info + Confirmed_slots_history.update constants node_ctxt head confirmation_info let slots_history_of_hash = Confirmed_slots_history.slots_history_of_hash diff --git a/src/proto_018_Proxford/lib_sc_rollup_node/fueled_pvm.ml b/src/proto_018_Proxford/lib_sc_rollup_node/fueled_pvm.ml index ee86df71d03b826770ed85dc348afdc52fe82a8b..c2279b71589eaaf67444871b47c79a589c85a937 100644 --- a/src/proto_018_Proxford/lib_sc_rollup_node/fueled_pvm.ml +++ b/src/proto_018_Proxford/lib_sc_rollup_node/fueled_pvm.ml @@ -77,19 +77,20 @@ module Make_fueled (F : Fuel.S) : FUELED_PVM with type fuel = F.t = struct message_index ~fuel start_tick failing_ticks state = let open Lwt_result_syntax in let open Delayed_write_monad.Lwt_result_syntax in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt (Int32.of_int level) + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level - |> WithExceptions.Option.get ~loc:__LOC__ - |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez - |> Sc_rollup.is_reveal_enabled_predicate + match constants.sc_rollup.reveal_activation_level with + | None -> fun ~current_block_level:_ _ -> true + | Some reveal_activation_level -> + reveal_activation_level + |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate in let module PVM = (val Pvm.of_kind node_ctxt.kind) in let metadata = metadata node_ctxt in - let dal_attestation_lag = - node_ctxt.current_protocol.constants.dal.attestation_lag - in + let dal_attestation_lag = constants.dal.attestation_lag in let decode_reveal (Tezos_scoru_wasm.Wasm_pvm_state.Reveal_raw payload) = match Data_encoding.Binary.of_string_opt diff --git a/src/proto_018_Proxford/lib_sc_rollup_node/layer1_helpers.ml b/src/proto_018_Proxford/lib_sc_rollup_node/layer1_helpers.ml index d3adfe0e5eb5e018c86ab9ad2d9a4ff4275e8878..0389dafdc738af0c72315f8d7671cfd718abc7d9 100644 --- a/src/proto_018_Proxford/lib_sc_rollup_node/layer1_helpers.ml +++ b/src/proto_018_Proxford/lib_sc_rollup_node/layer1_helpers.ml @@ -135,7 +135,14 @@ let constants_of_parametric reveal_activation_level; _; }; - dal = {feature_enable; attestation_lag; number_of_slots; _}; + dal = + { + feature_enable; + attestation_lag; + number_of_slots; + cryptobox_parameters; + _; + }; _; } = let open Protocol.Alpha_context in @@ -152,7 +159,8 @@ let constants_of_parametric (Sc_rollup_proto_types.Constants.reveal_activation_level_to_octez reveal_activation_level); }; - dal = {feature_enable; attestation_lag; number_of_slots}; + dal = + {feature_enable; attestation_lag; number_of_slots; cryptobox_parameters}; } (* TODO: https://gitlab.com/tezos/tezos/-/issues/2901 diff --git a/src/proto_018_Proxford/lib_sc_rollup_node/pvm_plugin.ml b/src/proto_018_Proxford/lib_sc_rollup_node/pvm_plugin.ml index 650d061611553abf46e2d887258f0417498e2c90..d479d25a62a457bde240eb7fda095649367e9464 100644 --- a/src/proto_018_Proxford/lib_sc_rollup_node/pvm_plugin.ml +++ b/src/proto_018_Proxford/lib_sc_rollup_node/pvm_plugin.ml @@ -50,25 +50,32 @@ let install_boot_sector kind state boot_sector = let module PVM = (val Pvm.of_kind kind) in PVM.install_boot_sector state boot_sector +let get_current_level kind state = + let open Lwt_option_syntax in + let module PVM = (val Pvm.of_kind kind) in + let+ current_level = PVM.get_current_level state in + Raw_level.to_int32 current_level + let get_status (node_ctxt : _ Node_context.t) state = - let open Lwt_syntax in + let open Lwt_result_syntax in let module PVM = (val Pvm.of_kind node_ctxt.kind) in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let*! current_level = PVM.get_current_level state in + let* constants = + match current_level with + | None -> return node_ctxt.current_protocol.constants + | Some level -> + Protocol_plugins.get_constants_of_level + node_ctxt + (Raw_level.to_int32 level) + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level + constants.sc_rollup.reveal_activation_level |> WithExceptions.Option.get ~loc:__LOC__ |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate in - let+ status = PVM.get_status ~is_reveal_enabled state in - PVM.string_of_status status - -let get_current_level kind state = - let open Lwt_option_syntax in - let module PVM = (val Pvm.of_kind kind) in - let+ current_level = PVM.get_current_level state in - Raw_level.to_int32 current_level + let*! status = PVM.get_status ~is_reveal_enabled state in + return (PVM.string_of_status status) module Fueled = Fueled_pvm diff --git a/src/proto_018_Proxford/lib_sc_rollup_node/refutation_game_helpers.ml b/src/proto_018_Proxford/lib_sc_rollup_node/refutation_game_helpers.ml index 008a5c2505e3321a5b2e61cfdf7be8e5e04764bb..c07a972a3577ca2d1ca59439e0eda6e70e160b13 100644 --- a/src/proto_018_Proxford/lib_sc_rollup_node/refutation_game_helpers.ml +++ b/src/proto_018_Proxford/lib_sc_rollup_node/refutation_game_helpers.ml @@ -66,17 +66,21 @@ let page_membership_proof params page_index slot_data = be unconfirmed on L1, this function returns [None]. If the data of the slot are not saved to the store, the function returns a failure in the error monad. *) -let page_info_from_pvm_state (node_ctxt : _ Node_context.t) ~dal_attestation_lag +let page_info_from_pvm_state constants (node_ctxt : _ Node_context.t) (dal_params : Dal.parameters) start_state = let open Lwt_result_syntax in let module PVM = (val Pvm.of_kind node_ctxt.kind) in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let dal_attestation_lag = constants.Rollup_constants.dal.attestation_lag in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level - |> WithExceptions.Option.get ~loc:__LOC__ - |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez - |> Sc_rollup.is_reveal_enabled_predicate + match constants.sc_rollup.reveal_activation_level with + | Some reveal_activation_level -> + Sc_rollup.is_reveal_enabled_predicate + (Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + reveal_activation_level) + | None -> + (* For older protocol, constants don't have the notion of reveal + activation level. *) + fun ~current_block_level:_ _ -> true in let*! input_request = PVM.is_input_state ~is_reveal_enabled start_state in match input_request with @@ -149,22 +153,15 @@ let generate_proof (node_ctxt : _ Node_context.t) in (* We fetch the value of protocol constants at block snapshot level where the game started. *) - let* parametric_constants = - let cctxt = node_ctxt.cctxt in - Protocol.Constants_services.parametric - (new Protocol_client_context.wrap_full cctxt) - (cctxt#chain, `Level snapshot_level_int32) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt snapshot_level_int32 in - let dal_l1_parameters = parametric_constants.dal in + let dal_l1_parameters = constants.dal in let dal_parameters = dal_l1_parameters.cryptobox_parameters in let dal_attestation_lag = dal_l1_parameters.attestation_lag in let* page_info = - page_info_from_pvm_state - ~dal_attestation_lag - node_ctxt - dal_parameters - start_state + page_info_from_pvm_state constants node_ctxt dal_parameters start_state in let module P = struct include PVM @@ -242,9 +239,17 @@ let generate_proof (node_ctxt : _ Node_context.t) let metadata = metadata node_ctxt in let*! start_tick = PVM.get_tick start_state in let is_reveal_enabled = - Sc_rollup.is_reveal_enabled_predicate - parametric_constants.sc_rollup.reveal_activation_level + match constants.sc_rollup.reveal_activation_level with + | Some reveal_activation_level -> + Sc_rollup.is_reveal_enabled_predicate + (Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + reveal_activation_level) + | None -> + (* For older protocol, constants don't have the notion of reveal + activation level. *) + fun ~current_block_level:_ _ -> true in + let* proof = trace (Sc_rollup_node_errors.Cannot_produce_proof diff --git a/src/proto_alpha/lib_sc_rollup_node/RPC_directory.ml b/src/proto_alpha/lib_sc_rollup_node/RPC_directory.ml index b83bb364a8b197eb90ce706d836c72225612a981..e5c891b8a2920d5213c722de2343225fcc7a5adb 100644 --- a/src/proto_alpha/lib_sc_rollup_node/RPC_directory.ml +++ b/src/proto_alpha/lib_sc_rollup_node/RPC_directory.ml @@ -158,10 +158,11 @@ let simulate_messages (node_ctxt : Node_context.ro) block ~reveal_pages List.filter (fun Sc_rollup.{outbox_level; _} -> outbox_level = level) outbox in let*! state_hash = PVM.state_hash state in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt inbox_level + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level + constants.sc_rollup.reveal_activation_level |> WithExceptions.Option.get ~loc:__LOC__ |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate @@ -218,10 +219,11 @@ let () = let open Lwt_result_syntax in let* state = get_state node_ctxt block in let module PVM = (val Pvm.of_kind node_ctxt.kind) in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let* constants = + Protocol_plugins.get_constants_of_block_hash node_ctxt block + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level + constants.sc_rollup.reveal_activation_level |> WithExceptions.Option.get ~loc:__LOC__ |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate @@ -233,12 +235,15 @@ let () = Block_directory.register0 Sc_rollup_services.Block.dal_slots @@ fun (node_ctxt, block) () () -> let open Lwt_result_syntax in + let* constants = + Protocol_plugins.get_constants_of_block_hash node_ctxt block + in let+ slots = Node_context.get_all_slot_headers node_ctxt ~published_in_block_hash:block in List.rev_map (Sc_rollup_proto_types.Dal.Slot_header.of_octez - ~number_of_slots:node_ctxt.current_protocol.constants.dal.number_of_slots) + ~number_of_slots:constants.dal.number_of_slots) slots |> List.rev diff --git a/src/proto_alpha/lib_sc_rollup_node/dal_slots_tracker.ml b/src/proto_alpha/lib_sc_rollup_node/dal_slots_tracker.ml index cf0e48520431b00c8fb7f2c205f76c21837d7e87..86fb896379d0bf7300c00c5a55d90f0b2bc48bb0 100644 --- a/src/proto_alpha/lib_sc_rollup_node/dal_slots_tracker.ml +++ b/src/proto_alpha/lib_sc_rollup_node/dal_slots_tracker.ml @@ -52,12 +52,12 @@ type confirmations_info = { confirmed_slots_indexes : Bitset.t; } -(** [slots_info node_ctxt head] gathers information about the slot confirmations +(** [slots_info constants node_ctxt head] gathers information about the slot confirmations of slot indexes. It reads the slot indexes that have been declared available from [head]'s block receipt. It then returns the hash of the block where the slot headers have been published and the list of slot indexes that have been confirmed for that block. *) -let slots_info node_ctxt (Layer1.{hash; _} as head) = +let slots_info constants node_ctxt (Layer1.{hash; _} as head) = (* DAL/FIXME: https://gitlab.com/tezos/tezos/-/issues/3722 The case for protocol migrations when the lag constant has been changed is tricky, especially if the lag is reduced. @@ -67,9 +67,7 @@ let slots_info node_ctxt (Layer1.{hash; _} as head) = we reduce the lag to 1, then the slots header will never be confirmed. *) let open Lwt_result_syntax in - let lag = - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = constants.Rollup_constants.dal.attestation_lag in (* we are downloading attestation for slots at level [level], so we need to download the data at level [level - lag]. *) @@ -103,9 +101,7 @@ let slots_info node_ctxt (Layer1.{hash; _} as head) = node_ctxt ~published_in_block_hash:published_block_hash in - let number_of_slots = - node_ctxt.Node_context.current_protocol.constants.dal.number_of_slots - in + let number_of_slots = constants.dal.number_of_slots in let confirmed_slots_indexes_list = List.filter (Dal.Attestation.is_attested confirmed_slots) @@ -132,24 +128,21 @@ let to_slot_index_list (constants : Rollup_constants.protocol_constants) bitset Use a shared storage between dal and rollup node to store slots data. *) -let download_and_save_slots (node_context : _ Node_context.t) +let download_and_save_slots constants (node_context : _ Node_context.t) ~current_block_hash {published_block_hash; confirmed_slots_indexes} = let open Lwt_result_syntax in let*? all_slots = - Bitset.fill - ~length:node_context.current_protocol.constants.dal.number_of_slots + Bitset.fill ~length:constants.Rollup_constants.dal.number_of_slots |> Environment.wrap_tzresult in let*? not_confirmed = Environment.wrap_tzresult - @@ to_slot_index_list node_context.current_protocol.constants + @@ to_slot_index_list constants @@ Bitset.diff all_slots confirmed_slots_indexes in let*? confirmed = Environment.wrap_tzresult - @@ to_slot_index_list - node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in (* The contents of each slot index are written to a different location on disk, therefore calls to store contents for different slot indexes can @@ -176,8 +169,7 @@ let download_and_save_slots (node_context : _ Node_context.t) let*! () = Dal_slots_tracker_event.slot_has_been_confirmed (Sc_rollup_proto_types.Dal.Slot_index.of_octez - ~number_of_slots: - node_context.current_protocol.constants.dal.number_of_slots + ~number_of_slots:constants.dal.number_of_slots s_slot) published_block_hash current_block_hash @@ -186,17 +178,15 @@ let download_and_save_slots (node_context : _ Node_context.t) confirmed module Confirmed_slots_history = struct - (** [confirmed_slots_with_headers node_ctxt confirmations_info] returns the + (** [confirmed_slots_with_headers constants node_ctxt confirmations_info] returns the headers of confirmed slot indexes for the block with hash [confirmations_info.published_block_hash]. *) - let confirmed_slots_with_headers node_ctxt + let confirmed_slots_with_headers constants node_ctxt {published_block_hash; confirmed_slots_indexes; _} = let open Lwt_result_syntax in let*? relevant_slots_indexes = Environment.wrap_tzresult - @@ to_slot_index_list - node_ctxt.Node_context.current_protocol.constants - confirmed_slots_indexes + @@ to_slot_index_list constants confirmed_slots_indexes in List.map_ep (fun slot_index -> @@ -207,12 +197,11 @@ module Confirmed_slots_history = struct slot_index in Sc_rollup_proto_types.Dal.Slot_header.of_octez - ~number_of_slots: - node_ctxt.current_protocol.constants.dal.number_of_slots + ~number_of_slots:constants.dal.number_of_slots h) relevant_slots_indexes - let read_slots_history_from_l1 {Node_context.cctxt; _} block = + let read_slots_history_from_l1 _constants {Node_context.cctxt; _} block = let open Lwt_result_syntax in (* We return the empty Slots_history if DAL is not enabled. *) let* slots_list_opt = @@ -227,12 +216,9 @@ module Confirmed_slots_history = struct slots_history and slots_history's cache entries in the store after [origination_level + attestation_lag] blocks. This function checks if that level is reached or not. *) - let should_process_dal_slots node_ctxt block_level = + let should_process_dal_slots constants node_ctxt block_level = let open Node_context in - let lag = - Int32.of_int - node_ctxt.Node_context.current_protocol.constants.dal.attestation_lag - in + let lag = Int32.of_int constants.Rollup_constants.dal.attestation_lag in let block_level = Raw_level.to_int32 block_level in let genesis_level = node_ctxt.genesis_info.level in Int32.(block_level >= add lag genesis_level) @@ -242,13 +228,16 @@ module Confirmed_slots_history = struct = let open Lwt_result_syntax in let* confirmed_slots_history_opt = find node_ctxt block_hash in + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt block_level + in let block_level = Raw_level.of_int32_exn block_level in let should_process_dal_slots = - should_process_dal_slots node_ctxt block_level + should_process_dal_slots constants node_ctxt block_level in match (confirmed_slots_history_opt, should_process_dal_slots) with | Some confirmed_dal_slots, true -> return confirmed_dal_slots - | None, false -> default node_ctxt block_hash + | None, false -> default constants node_ctxt block_hash | Some _confirmed_dal_slots, false -> failwith "The confirmed DAL %S for block hash %a (level = %a) is not expected \ @@ -292,10 +281,8 @@ module Confirmed_slots_history = struct block ~entry_kind:"slots history cache" ~find - ~default:(fun node_ctxt _block -> - let num_slots = - node_ctxt.Node_context.current_protocol.constants.dal.number_of_slots - in + ~default:(fun constants _node_ctxt _block -> + let num_slots = constants.Rollup_constants.dal.number_of_slots in (* FIXME/DAL: https://gitlab.com/tezos/tezos/-/issues/3788 Put an accurate value for capacity. The value `num_slots * 60000` below is chosen based on: @@ -309,11 +296,11 @@ module Confirmed_slots_history = struct @@ Dal.Slots_history.History_cache.empty ~capacity:(Int64.of_int @@ (num_slots * 60000))) - let update node_ctxt Layer1.({hash = head_hash; _} as head) confirmation_info - = + let update constants node_ctxt Layer1.({hash = head_hash; _} as head) + confirmation_info = let open Lwt_result_syntax in let* slots_to_save = - confirmed_slots_with_headers node_ctxt confirmation_info + confirmed_slots_with_headers constants node_ctxt confirmation_info in let slots_to_save = let open Dal in @@ -353,19 +340,21 @@ module Confirmed_slots_history = struct return () end -let process_head node_ctxt (Layer1.{hash = head_hash; _} as head) = +let process_head node_ctxt (Layer1.{hash = head_hash; level} as head) = let open Lwt_result_syntax in - let* confirmation_info = slots_info node_ctxt head in + let* constants = Protocol_plugins.get_constants_of_level node_ctxt level in + let* confirmation_info = slots_info constants node_ctxt head in match confirmation_info with | None -> return_unit | Some confirmation_info -> let* () = download_and_save_slots ~current_block_hash:head_hash + constants node_ctxt confirmation_info in - Confirmed_slots_history.update node_ctxt head confirmation_info + Confirmed_slots_history.update constants node_ctxt head confirmation_info let slots_history_of_hash = Confirmed_slots_history.slots_history_of_hash diff --git a/src/proto_alpha/lib_sc_rollup_node/fueled_pvm.ml b/src/proto_alpha/lib_sc_rollup_node/fueled_pvm.ml index b0a43e295fb45be910584dba3c2c3f2464503f10..2c1c9c407bfc1408a8a627f93af9b0caf2fd5a24 100644 --- a/src/proto_alpha/lib_sc_rollup_node/fueled_pvm.ml +++ b/src/proto_alpha/lib_sc_rollup_node/fueled_pvm.ml @@ -77,19 +77,20 @@ module Make_fueled (F : Fuel.S) : FUELED_PVM with type fuel = F.t = struct message_index ~fuel start_tick failing_ticks state = let open Lwt_result_syntax in let open Delayed_write_monad.Lwt_result_syntax in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt (Int32.of_int level) + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level - |> WithExceptions.Option.get ~loc:__LOC__ - |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez - |> Sc_rollup.is_reveal_enabled_predicate + match constants.sc_rollup.reveal_activation_level with + | None -> fun ~current_block_level:_ _ -> true + | Some reveal_activation_level -> + reveal_activation_level + |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate in let module PVM = (val Pvm.of_kind node_ctxt.kind) in let metadata = metadata node_ctxt in - let dal_attestation_lag = - node_ctxt.current_protocol.constants.dal.attestation_lag - in + let dal_attestation_lag = constants.dal.attestation_lag in let reveal_builtins request = match Sc_rollup.Wasm_2_0_0PVM.decode_reveal request with | Reveal_raw_data hash -> ( diff --git a/src/proto_alpha/lib_sc_rollup_node/layer1_helpers.ml b/src/proto_alpha/lib_sc_rollup_node/layer1_helpers.ml index 70992d0c444ff4cc6b2916fe4403984dc2b17f4a..f31da2dd9907c0de3ebb51c022526e15c28f4022 100644 --- a/src/proto_alpha/lib_sc_rollup_node/layer1_helpers.ml +++ b/src/proto_alpha/lib_sc_rollup_node/layer1_helpers.ml @@ -135,7 +135,14 @@ let constants_of_parametric reveal_activation_level; _; }; - dal = {feature_enable; attestation_lag; number_of_slots; _}; + dal = + { + feature_enable; + attestation_lag; + number_of_slots; + cryptobox_parameters; + _; + }; _; } = let open Protocol.Alpha_context in @@ -152,7 +159,8 @@ let constants_of_parametric (Sc_rollup_proto_types.Constants.reveal_activation_level_to_octez reveal_activation_level); }; - dal = {feature_enable; attestation_lag; number_of_slots}; + dal = + {feature_enable; attestation_lag; number_of_slots; cryptobox_parameters}; } (* TODO: https://gitlab.com/tezos/tezos/-/issues/2901 diff --git a/src/proto_alpha/lib_sc_rollup_node/pvm_plugin.ml b/src/proto_alpha/lib_sc_rollup_node/pvm_plugin.ml index 650d061611553abf46e2d887258f0417498e2c90..d479d25a62a457bde240eb7fda095649367e9464 100644 --- a/src/proto_alpha/lib_sc_rollup_node/pvm_plugin.ml +++ b/src/proto_alpha/lib_sc_rollup_node/pvm_plugin.ml @@ -50,25 +50,32 @@ let install_boot_sector kind state boot_sector = let module PVM = (val Pvm.of_kind kind) in PVM.install_boot_sector state boot_sector +let get_current_level kind state = + let open Lwt_option_syntax in + let module PVM = (val Pvm.of_kind kind) in + let+ current_level = PVM.get_current_level state in + Raw_level.to_int32 current_level + let get_status (node_ctxt : _ Node_context.t) state = - let open Lwt_syntax in + let open Lwt_result_syntax in let module PVM = (val Pvm.of_kind node_ctxt.kind) in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let*! current_level = PVM.get_current_level state in + let* constants = + match current_level with + | None -> return node_ctxt.current_protocol.constants + | Some level -> + Protocol_plugins.get_constants_of_level + node_ctxt + (Raw_level.to_int32 level) + in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level + constants.sc_rollup.reveal_activation_level |> WithExceptions.Option.get ~loc:__LOC__ |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez |> Protocol.Alpha_context.Sc_rollup.is_reveal_enabled_predicate in - let+ status = PVM.get_status ~is_reveal_enabled state in - PVM.string_of_status status - -let get_current_level kind state = - let open Lwt_option_syntax in - let module PVM = (val Pvm.of_kind kind) in - let+ current_level = PVM.get_current_level state in - Raw_level.to_int32 current_level + let*! status = PVM.get_status ~is_reveal_enabled state in + return (PVM.string_of_status status) module Fueled = Fueled_pvm diff --git a/src/proto_alpha/lib_sc_rollup_node/refutation_game_helpers.ml b/src/proto_alpha/lib_sc_rollup_node/refutation_game_helpers.ml index 66fae2bc805c1c5e82cb529bb8f79a6afe14df3c..366cb5862febe619b602079df2b7ab6de94f5481 100644 --- a/src/proto_alpha/lib_sc_rollup_node/refutation_game_helpers.ml +++ b/src/proto_alpha/lib_sc_rollup_node/refutation_game_helpers.ml @@ -66,17 +66,21 @@ let page_membership_proof params page_index slot_data = be unconfirmed on L1, this function returns [None]. If the data of the slot are not saved to the store, the function returns a failure in the error monad. *) -let page_info_from_pvm_state (node_ctxt : _ Node_context.t) ~dal_attestation_lag +let page_info_from_pvm_state constants (node_ctxt : _ Node_context.t) ~inbox_level (dal_params : Dal.parameters) start_state = let open Lwt_result_syntax in let module PVM = (val Pvm.of_kind node_ctxt.kind) in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/5871 - Use constants for correct protocol. *) + let dal_attestation_lag = constants.Rollup_constants.dal.attestation_lag in let is_reveal_enabled = - node_ctxt.current_protocol.constants.sc_rollup.reveal_activation_level - |> WithExceptions.Option.get ~loc:__LOC__ - |> Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez - |> Sc_rollup.is_reveal_enabled_predicate + match constants.sc_rollup.reveal_activation_level with + | Some reveal_activation_level -> + Sc_rollup.is_reveal_enabled_predicate + (Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + reveal_activation_level) + | None -> + (* For older protocol, constants don't have the notion of reveal + activation level. *) + fun ~current_block_level:_ _ -> true in let*! input_request = PVM.is_input_state ~is_reveal_enabled start_state in match input_request with @@ -153,19 +157,16 @@ let generate_proof (node_ctxt : _ Node_context.t) in (* We fetch the value of protocol constants at block snapshot level where the game started. *) - let* parametric_constants = - let cctxt = node_ctxt.cctxt in - Protocol.Constants_services.parametric - (new Protocol_client_context.wrap_full cctxt) - (cctxt#chain, `Level snapshot_level_int32) + let* constants = + Protocol_plugins.get_constants_of_level node_ctxt snapshot_level_int32 in - let dal_l1_parameters = parametric_constants.dal in + let dal_l1_parameters = constants.dal in let dal_parameters = dal_l1_parameters.cryptobox_parameters in let dal_attestation_lag = dal_l1_parameters.attestation_lag in let* page_info = page_info_from_pvm_state - ~dal_attestation_lag + constants ~inbox_level:game.inbox_level node_ctxt dal_parameters @@ -247,8 +248,18 @@ let generate_proof (node_ctxt : _ Node_context.t) let metadata = metadata node_ctxt in let*! start_tick = PVM.get_tick start_state in let is_reveal_enabled = - Sc_rollup.is_reveal_enabled_predicate - parametric_constants.sc_rollup.reveal_activation_level + match constants.sc_rollup.reveal_activation_level with + | Some reveal_activation_level -> + Sc_rollup.is_reveal_enabled_predicate + (Sc_rollup_proto_types.Constants.reveal_activation_level_of_octez + reveal_activation_level) + | None -> + (* Constants for an older protocol, there is no notion of reveal + activation level for those. *) + (* TODO: https://gitlab.com/tezos/tezos/-/issues/6247 + default value for is_reveal_enabled that returns true for all reveal + supported in protocols <= 18. *) + fun ~current_block_level:_ _ -> true in let* proof = trace