From 8e30fdf7d96aab76724a7fe03c3ab38e291c7294 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Wed, 1 Mar 2023 11:01:32 +0100 Subject: [PATCH] SCORU,Node: Handle the Protocol_migration internal message Gitlab-Issue: https://gitlab.com/tezos/tezos/-/issues/4918 --- src/proto_alpha/lib_sc_rollup_node/inbox.ml | 38 ++++++++++++------- src/proto_alpha/lib_sc_rollup_node/inbox.mli | 14 ++++--- .../lib_sc_rollup_node/interpreter.ml | 16 ++++---- .../lib_sc_rollup_node/node_context.ml | 28 ++++++++++---- .../lib_sc_rollup_node/node_context.mli | 1 + .../lib_sc_rollup_node/refutation_game.ml | 8 +++- src/proto_alpha/lib_sc_rollup_node/store.ml | 5 ++- src/proto_alpha/lib_sc_rollup_node/store.mli | 2 +- 8 files changed, 75 insertions(+), 37 deletions(-) diff --git a/src/proto_alpha/lib_sc_rollup_node/inbox.ml b/src/proto_alpha/lib_sc_rollup_node/inbox.ml index 9eb3213911b3..8482218af91e 100644 --- a/src/proto_alpha/lib_sc_rollup_node/inbox.ml +++ b/src/proto_alpha/lib_sc_rollup_node/inbox.ml @@ -79,10 +79,21 @@ let get_messages Node_context.{l1_ctxt; _} head = {apply; apply_internal}) in let ({predecessor; _} : Block_header.shell_header) = block.header.shell in - let* {timestamp = predecessor_timestamp; _} = + let* { + timestamp = predecessor_timestamp; + proto_level = predecessor_proto_level; + _; + } = Layer1.fetch_tezos_shell_header l1_ctxt predecessor in - return (List.rev rev_messages, predecessor_timestamp, predecessor) + let is_migration_block = + block.header.shell.proto_level <> predecessor_proto_level + in + return + ( is_migration_block, + List.rev rev_messages, + predecessor_timestamp, + predecessor ) let same_inbox_as_layer_1 node_ctxt head_hash inbox = let open Lwt_result_syntax in @@ -95,7 +106,8 @@ let same_inbox_as_layer_1 node_ctxt head_hash inbox = (Sc_rollup.Inbox.equal layer1_inbox inbox) (Sc_rollup_node_errors.Inconsistent_inbox {layer1_inbox; inbox}) -let add_messages ~predecessor_timestamp ~predecessor inbox messages = +let add_messages ~is_migration_block ~predecessor_timestamp ~predecessor inbox + messages = let open Lwt_result_syntax in let no_history = Sc_rollup.Inbox.History.empty ~capacity:0L in lift @@ -104,11 +116,8 @@ let add_messages ~predecessor_timestamp ~predecessor inbox messages = inbox, witness, messages_with_protocol_internal_messages ) = - (* TODO: https://gitlab.com/tezos/tezos/-/issues/4918 Inject - [Protocol_migration (Proto_017)] when migrating to proto_alpha - (N after next snapshot). *) Sc_rollup.Inbox.add_all_messages - ~first_block:false + ~first_block:is_migration_block ~predecessor_timestamp ~predecessor no_history @@ -150,7 +159,10 @@ let process_head (node_ctxt : _ Node_context.t) return (Context.empty node_ctxt.context) else Node_context.checkout_context node_ctxt predecessor.hash in - let* collected_messages, predecessor_timestamp, predecessor_hash = + let* ( is_migration_block, + collected_messages, + predecessor_timestamp, + predecessor_hash ) = get_messages node_ctxt head_hash in let*! () = @@ -164,6 +176,7 @@ let process_head (node_ctxt : _ Node_context.t) inbox, messages_with_protocol_internal_messages ) = add_messages + ~is_migration_block ~predecessor_timestamp ~predecessor:predecessor_hash inbox @@ -176,6 +189,7 @@ let process_head (node_ctxt : _ Node_context.t) node_ctxt witness_hash { + is_migration_block; predecessor = predecessor_hash; predecessor_timestamp; messages = collected_messages; @@ -200,7 +214,8 @@ let process_head (node_ctxt : _ Node_context.t) let start () = Inbox_event.starting () -let payloads_history_of_messages ~predecessor ~predecessor_timestamp messages = +let payloads_history_of_messages ~is_migration_block ~predecessor + ~predecessor_timestamp messages = let open Result_syntax in Environment.wrap_tzresult @@ let* dummy_inbox = @@ -215,11 +230,8 @@ let payloads_history_of_messages ~predecessor ~predecessor_timestamp messages = _inbox, _witness, _messages_with_protocol_internal_messages ) = - (* TODO: https://gitlab.com/tezos/tezos/-/issues/4918 Inject - [Protocol_migration (Proto_017)] when migrating to proto_alpha - (N after next snapshot). *) Sc_rollup.Inbox.add_all_messages - ~first_block:false + ~first_block:is_migration_block ~predecessor_timestamp ~predecessor (Sc_rollup.Inbox.History.empty ~capacity:0L) diff --git a/src/proto_alpha/lib_sc_rollup_node/inbox.mli b/src/proto_alpha/lib_sc_rollup_node/inbox.mli index f1b33285c076..ecf8e3f83406 100644 --- a/src/proto_alpha/lib_sc_rollup_node/inbox.mli +++ b/src/proto_alpha/lib_sc_rollup_node/inbox.mli @@ -53,9 +53,11 @@ val process_head : (** [start ()] initializes the inbox to track the messages being published. *) val start : unit -> unit Lwt.t -(** [add_messages ~predecessor_timestamp ~predecessor inbox messages] adds - [messages] to the [inbox] using {!Inbox.add_all_messages}. *) +(** [add_messages ~is_migration_block ~predecessor_timestamp + ~predecessor inbox messages] adds [messages] to the [inbox] using + {!Inbox.add_all_messages}. *) val add_messages : + is_migration_block:bool -> predecessor_timestamp:Timestamp.time -> predecessor:Block_hash.t -> Inbox.t -> @@ -67,11 +69,13 @@ val add_messages : tzresult Lwt.t -(** [payloads_history_of_messages ~predecessor ~predecessor_timestamp messages] - builds the payloads history for the list of [messages]. This allows to not - store payloads histories (which contain merkelized skip lists) but simply +(** [payloads_history_of_messages ~is_migration_block ~predecessor + ~predecessor_timestamp messages] builds the payloads history for + the list of [messages]. This allows to not store payloads + histories (which contain merkelized skip lists) but simply messages. *) val payloads_history_of_messages : + is_migration_block:bool -> predecessor:Block_hash.t -> predecessor_timestamp:Timestamp.time -> Sc_rollup.Inbox_message.t list -> diff --git a/src/proto_alpha/lib_sc_rollup_node/interpreter.ml b/src/proto_alpha/lib_sc_rollup_node/interpreter.ml index 9442e3bd1fef..52fc91285706 100644 --- a/src/proto_alpha/lib_sc_rollup_node/interpreter.ml +++ b/src/proto_alpha/lib_sc_rollup_node/interpreter.ml @@ -200,16 +200,18 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct Layer1.{hash = block.header.predecessor; level = pred_level} in let* inbox = Node_context.get_inbox node_ctxt block.header.inbox_hash in - let* {predecessor; predecessor_timestamp; messages} = + let* {is_migration_block; predecessor; predecessor_timestamp; messages} = Node_context.get_messages node_ctxt block.header.inbox_witness in - (* TODO: https://gitlab.com/tezos/tezos/-/issues/4918 Inject - [Protocol_migration (Proto_017)] when migrating to proto_alpha - (N after next snapshot). *) let messages = - Sc_rollup.Inbox_message.Internal Start_of_level - :: Internal (Info_per_level {predecessor; predecessor_timestamp}) - :: messages + let open Sc_rollup.Inbox_message in + Internal Start_of_level + :: + (if is_migration_block then + [Internal Sc_rollup.Inbox_message.protocol_migration_internal_message] + else []) + @ Internal (Info_per_level {predecessor; predecessor_timestamp}) + :: messages @ [Internal End_of_level] in let>* state, _counter, _level, _fuel = diff --git a/src/proto_alpha/lib_sc_rollup_node/node_context.ml b/src/proto_alpha/lib_sc_rollup_node/node_context.ml index 0009bf09b4b1..828a1d2ec6b3 100644 --- a/src/proto_alpha/lib_sc_rollup_node/node_context.ml +++ b/src/proto_alpha/lib_sc_rollup_node/node_context.ml @@ -514,6 +514,7 @@ let get_inbox_by_block_hash node_ctxt hash = inbox_of_head node_ctxt {hash; level} type messages_info = { + is_migration_block : bool; predecessor : Block_hash.t; predecessor_timestamp : Timestamp.t; messages : Sc_rollup.Inbox_message.t list; @@ -528,15 +529,22 @@ let get_messages {store; _} messages_hash = "Could not retrieve messages with payloads merkelized hash %a" Sc_rollup.Inbox_merkelized_payload_hashes.Hash.pp messages_hash - | Some (messages, (predecessor, predecessor_timestamp, _num_messages)) -> - return {predecessor; predecessor_timestamp; messages} + | Some + ( messages, + (is_migration_block, predecessor, predecessor_timestamp, _num_messages) + ) -> + return {is_migration_block; predecessor; predecessor_timestamp; messages} let find_messages {store; _} hash = let open Lwt_result_syntax in let+ msgs = Store.Messages.read store.messages hash in Option.map - (fun (messages, (predecessor, predecessor_timestamp, _num_messages)) -> - {predecessor; predecessor_timestamp; messages}) + (fun ( messages, + ( is_migration_block, + predecessor, + predecessor_timestamp, + _num_messages ) ) -> + {is_migration_block; predecessor; predecessor_timestamp; messages}) msgs let get_num_messages {store; _} hash = @@ -548,15 +556,19 @@ let get_num_messages {store; _} hash = "Could not retrieve number of messages for inbox witness %a" Sc_rollup.Inbox_merkelized_payload_hashes.Hash.pp hash - | Some (_predecessor, _predecessor_timestamp, num_messages) -> + | Some (_first_block, _predecessor, _predecessor_timestamp, num_messages) -> return num_messages -let save_messages {store; _} key {predecessor; predecessor_timestamp; messages} - = +let save_messages {store; _} key + {is_migration_block; predecessor; predecessor_timestamp; messages} = Store.Messages.append store.messages ~key - ~header:(predecessor, predecessor_timestamp, List.length messages) + ~header: + ( is_migration_block, + predecessor, + predecessor_timestamp, + List.length messages ) ~value:messages let get_full_l2_block node_ctxt block_hash = diff --git a/src/proto_alpha/lib_sc_rollup_node/node_context.mli b/src/proto_alpha/lib_sc_rollup_node/node_context.mli index 82fc45f6a887..b62ddddfe2b5 100644 --- a/src/proto_alpha/lib_sc_rollup_node/node_context.mli +++ b/src/proto_alpha/lib_sc_rollup_node/node_context.mli @@ -261,6 +261,7 @@ val commitment_was_published : (** {3 Inboxes} *) type messages_info = { + is_migration_block : bool; predecessor : Block_hash.t; predecessor_timestamp : Timestamp.t; messages : Sc_rollup.Inbox_message.t list; diff --git a/src/proto_alpha/lib_sc_rollup_node/refutation_game.ml b/src/proto_alpha/lib_sc_rollup_node/refutation_game.ml index ed54943089a6..db3bf161b0b5 100644 --- a/src/proto_alpha/lib_sc_rollup_node/refutation_game.ml +++ b/src/proto_alpha/lib_sc_rollup_node/refutation_game.ml @@ -249,11 +249,17 @@ module Make (Interpreter : Interpreter.S) : ~error:(Format.kasprintf Stdlib.failwith "%a" pp_print_trace)) @@ let open Lwt_result_syntax in - let* {predecessor; predecessor_timestamp; messages} = + let* { + is_migration_block; + predecessor; + predecessor_timestamp; + messages; + } = Node_context.get_messages node_ctxt witness in let*? hist = Inbox.payloads_history_of_messages + ~is_migration_block ~predecessor ~predecessor_timestamp messages diff --git a/src/proto_alpha/lib_sc_rollup_node/store.ml b/src/proto_alpha/lib_sc_rollup_node/store.ml index ad771eb068d7..dd1a6b70ae1c 100644 --- a/src/proto_alpha/lib_sc_rollup_node/store.ml +++ b/src/proto_alpha/lib_sc_rollup_node/store.ml @@ -104,13 +104,14 @@ module Messages = Data_encoding.(list @@ dynamic_size Sc_rollup.Inbox_message.encoding) module Header = struct - type t = Block_hash.t * Timestamp.t * int + type t = bool * Block_hash.t * Timestamp.t * int let name = "messages_inbox_info" let encoding = let open Data_encoding in - obj3 + obj4 + (req "is_migration_block" bool) (req "predecessor" Block_hash.encoding) (req "predecessor_timestamp" Timestamp.encoding) (req "num_messages" int31) diff --git a/src/proto_alpha/lib_sc_rollup_node/store.mli b/src/proto_alpha/lib_sc_rollup_node/store.mli index fdcd57b6f2b7..974f5241c44c 100644 --- a/src/proto_alpha/lib_sc_rollup_node/store.mli +++ b/src/proto_alpha/lib_sc_rollup_node/store.mli @@ -40,7 +40,7 @@ module Messages : INDEXED_FILE with type key := Sc_rollup.Inbox_merkelized_payload_hashes.Hash.t and type value := Sc_rollup.Inbox_message.t list - and type header := Block_hash.t * Timestamp.t * int + and type header := bool * Block_hash.t * Timestamp.t * int (** Aggregated collection of messages from the L1 inbox *) module Inboxes : -- GitLab