diff --git a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml index f0aa3b828ddcd82bed7459fe47edb136c151cf72..2947d3bf3cf677612194a0a80891ade37cfb4fd5 100644 --- a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml +++ b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml @@ -207,12 +207,13 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct (module Store.Last_published_commitment_level) store in - (* If the commitment has been published then the corresponding - level in Store.Commitments.published_at_level is available. *) + (* The corresponding level in Store.Commitments.published_at_level is + available only when the commitment has been published and included + in a block. *) let*! published_at_level = - Store.Commitments_published_at_level.get store hash + Store.Commitments_published_at_level.find store hash in - return (commitment, hash, Some published_at_level) + return (commitment, hash, published_at_level) in return result) diff --git a/src/proto_alpha/bin_sc_rollup_node/commitment.ml b/src/proto_alpha/bin_sc_rollup_node/commitment.ml index c265e771cef08781809daaacc6031d2034fe9036..8612b08be0ddc17cf5dce525fa57cefc13071621 100644 --- a/src/proto_alpha/bin_sc_rollup_node/commitment.ml +++ b/src/proto_alpha/bin_sc_rollup_node/commitment.ml @@ -276,7 +276,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct Store.Commitments.mem store next_level_to_publish in if is_commitment_available then - let*! commitment, commitment_hash = + let*! commitment, _commitment_hash = Store.Commitments.get store next_level_to_publish in let* () = @@ -303,7 +303,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct (* Configured to not publish commitments *) return_unit | Some (source, src_pk, src_sk) -> - let* _, _, Manager_operation_result {operation_result; _} = + let* _oph, _op, _results = Client_proto_context.sc_rollup_publish cctxt ~chain:cctxt#chain @@ -316,30 +316,12 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct ~fee_parameter:Configuration.default_fee_parameter () in - let open Apply_results in let*! () = - match operation_result with - | Applied (Sc_rollup_publish_result {published_at_level; _}) -> - let open Lwt_syntax in - let* () = - Store.Last_published_commitment_level.set - store - commitment.inbox_level - in - let* () = - Store.Commitments_published_at_level.add - store - commitment_hash - published_at_level - in - Commitment_event.publish_commitment_injected commitment - | Failed (Sc_rollup_publish_manager_kind, _errors) -> - Commitment_event.publish_commitment_failed commitment - | Backtracked (Sc_rollup_publish_result _, _errors) -> - Commitment_event.publish_commitment_backtracked commitment - | Skipped Sc_rollup_publish_manager_kind -> - Commitment_event.publish_commitment_skipped commitment + Store.Last_published_commitment_level.set + store + commitment.inbox_level in + let*! () = Commitment_event.publish_commitment_injected commitment in return_unit else return_unit @@ -408,8 +390,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct else return_false let cement_commitment ({Node_context.cctxt; rollup_address; _} as node_ctxt) - ({Sc_rollup.Commitment.inbox_level; _} as commitment) commitment_hash - store = + commitment commitment_hash = let open Lwt_result_syntax in let* operator = Node_context.get_operator_keys node_ctxt Cement in match operator with @@ -417,7 +398,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct (* Configured to not cement commitments *) return_unit | Some (source, src_pk, src_sk) -> - let* _, _, Manager_operation_result {operation_result; _} = + let* _oph, _op, _results = Client_proto_context.sc_rollup_cement cctxt ~chain:cctxt#chain @@ -430,25 +411,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct ~fee_parameter:Configuration.default_fee_parameter () in - let open Apply_results in - let*! () = - match operation_result with - | Applied (Sc_rollup_cement_result _) -> - let open Lwt_syntax in - let* () = - Store.Last_cemented_commitment_level.set store inbox_level - in - let* () = - Store.Last_cemented_commitment_hash.set store commitment_hash - in - Commitment_event.cement_commitment_injected commitment - | Failed (Sc_rollup_cement_manager_kind, _errors) -> - Commitment_event.cement_commitment_failed commitment - | Backtracked (Sc_rollup_cement_result _, _errors) -> - Commitment_event.cement_commitment_backtracked commitment - | Skipped Sc_rollup_cement_manager_kind -> - Commitment_event.cement_commitment_skipped commitment - in + let*! () = Commitment_event.cement_commitment_injected commitment in return_unit (* TODO: https://gitlab.com/tezos/tezos/-/issues/3008 @@ -486,7 +449,7 @@ module Make (PVM : Pvm.S) : Commitment_sig.S with module PVM = PVM = struct commitment_hash in if green_flag then - cement_commitment node_ctxt commitment commitment_hash store + cement_commitment node_ctxt commitment commitment_hash else return () | None -> return ()) | None -> return () diff --git a/src/proto_alpha/bin_sc_rollup_node/commitment_event.ml b/src/proto_alpha/bin_sc_rollup_node/commitment_event.ml index ee58fe7b61204eb0c53e61105a743aec8d7aaad0..183032af2b9f36fea6b60044460be4915297997b 100644 --- a/src/proto_alpha/bin_sc_rollup_node/commitment_event.ml +++ b/src/proto_alpha/bin_sc_rollup_node/commitment_event.ml @@ -31,64 +31,6 @@ module Simple = struct let section = ["sc_rollup_node"; "commitment"] - let event_name ?op_result op_type = - let op_type_string = - match op_type with - | `Cement -> "cement" - | `Publish -> "publish" - | `Store -> "stored" - in - let op_result_string_opt = - op_result - |> Option.map (function - | `Injected -> "injected" - | `Backtracked -> "backtracked" - | `Skipped -> "skipped" - | `Failed -> "failed") - in - match op_result_string_opt with - | None -> "sc_rollup_node_commitment_" ^ op_type_string - | Some op_result_string -> - "sc_rollup_" ^ op_type_string ^ "_commitment_" ^ op_result_string - - let event_msg_prefix ?op_result op_type = - let op_type_string = - match op_type with - | `Cement -> "Cementing" - | `Publish -> "Publishing" - | `Store -> "was stored" - in - let op_result_string_opt = - op_result - |> Option.map (function - | `Injected -> "was injected" - | `Backtracked -> "was backtracked" - | `Skipped -> "was skipped" - | `Failed -> "has failed") - in - match op_result_string_opt with - | None -> "Commitment " ^ op_type_string - | Some op_result_string -> - op_type_string ^ " Commitment " ^ op_result_string - - let commitment_event ?op_result op_type = - let name = event_name ?op_result op_type in - let msg = - event_msg_prefix ?op_result op_type - ^ " - predecessor: {predecessor}, inbox_level: {inbox_level}, \ - compressed_state: {compressed_state}, number_of_ticks: \ - {number_of_ticks}" - in - declare_4 - ~section - ~name - ~msg - ~level:Notice - ("predecessor", Sc_rollup.Commitment.Hash.encoding) - ("inbox_level", Raw_level.encoding) - ("compressed_state", Sc_rollup.State_hash.encoding) - ("number_of_ticks", Sc_rollup.Number_of_ticks.encoding) - let starting = declare_0 ~section @@ -157,26 +99,34 @@ module Simple = struct ("predecessor_hash", Sc_rollup.Commitment.Hash.encoding) ("lcc_hash", Sc_rollup.Commitment.Hash.encoding) - let commitment_stored = commitment_event `Store - - let publish_commitment_injected = - commitment_event `Publish ~op_result:`Injected - - let publish_commitment_backtracked = - commitment_event `Publish ~op_result:`Backtracked - - let publish_commitment_skipped = commitment_event `Publish ~op_result:`Skipped - - let publish_commitment_failed = commitment_event `Publish ~op_result:`Failed - - let cement_commitment_injected = commitment_event `Cement ~op_result:`Injected - - let cement_commitment_backtracked = - commitment_event `Cement ~op_result:`Backtracked - - let cement_commitment_skipped = commitment_event `Cement ~op_result:`Skipped + let commitment_stored = + declare_4 + ~section + ~name:"sc_rollup_node_commitment_stored" + ~msg: + "Commitment was stored - predecessor: {predecessor}, inbox_level: \ + {inbox_level}, compressed_state: {compressed_state}, number_of_ticks: \ + {number_of_ticks}" + ~level:Notice + ("predecessor", Sc_rollup.Commitment.Hash.encoding) + ("inbox_level", Raw_level.encoding) + ("compressed_state", Sc_rollup.State_hash.encoding) + ("number_of_ticks", Sc_rollup.Number_of_ticks.encoding) - let cement_commitment_failed = commitment_event `Cement ~op_result:`Failed + let commitment_injected kind = + declare_4 + ~section + ~name:(Printf.sprintf "sc_rollup_%s_commitment_injected" kind) + ~msg: + (kind + ^ " commitment was injected - predecessor: {predecessor}, inbox_level: \ + {inbox_level}, compressed_state: {compressed_state}, \ + number_of_ticks: {number_of_ticks}") + ~level:Notice + ("predecessor", Sc_rollup.Commitment.Hash.encoding) + ("inbox_level", Raw_level.encoding) + ("compressed_state", Sc_rollup.State_hash.encoding) + ("number_of_ticks", Sc_rollup.Number_of_ticks.encoding) end let starting = Simple.(emit starting) @@ -199,28 +149,10 @@ let commitment_will_not_be_published lcc_level let commitment_stored = emit_commitment_event Simple.commitment_stored let publish_commitment_injected = - emit_commitment_event Simple.publish_commitment_injected - -let publish_commitment_skipped = - emit_commitment_event Simple.publish_commitment_skipped - -let publish_commitment_backtracked = - emit_commitment_event Simple.publish_commitment_backtracked - -let publish_commitment_failed = - emit_commitment_event Simple.publish_commitment_failed + emit_commitment_event (Simple.commitment_injected "publish") let cement_commitment_injected = - emit_commitment_event Simple.cement_commitment_injected - -let cement_commitment_skipped = - emit_commitment_event Simple.cement_commitment_skipped - -let cement_commitment_backtracked = - emit_commitment_event Simple.cement_commitment_backtracked - -let cement_commitment_failed = - emit_commitment_event Simple.cement_commitment_failed + emit_commitment_event (Simple.commitment_injected "cement") let last_cemented_commitment_updated head level = Simple.(emit last_cemented_commitment_updated (head, level)) diff --git a/src/proto_alpha/bin_sc_rollup_node/commitment_event.mli b/src/proto_alpha/bin_sc_rollup_node/commitment_event.mli index 0b94e86ab11369c5fe5227de0184dcbb78fb345c..93c445c43228d19dd4909d484e59e47bd2085e82 100644 --- a/src/proto_alpha/bin_sc_rollup_node/commitment_event.mli +++ b/src/proto_alpha/bin_sc_rollup_node/commitment_event.mli @@ -32,27 +32,11 @@ val starting : unit -> unit Lwt.t val stopping : unit -> unit Lwt.t -(** The commitment functions below emit events depending on whether they are - being published or cemented, along with their status: - * backtracked; - * skipped; - * injected; - or if it has failed. *) - -val publish_commitment_failed : Sc_rollup.Commitment.t -> unit Lwt.t - -val publish_commitment_backtracked : Sc_rollup.Commitment.t -> unit Lwt.t - -val publish_commitment_skipped : Sc_rollup.Commitment.t -> unit Lwt.t +(** The functions below emit commitment events depending on whether they are + being published or cemented. *) val publish_commitment_injected : Sc_rollup.Commitment.t -> unit Lwt.t -val cement_commitment_failed : Sc_rollup.Commitment.t -> unit Lwt.t - -val cement_commitment_backtracked : Sc_rollup.Commitment.t -> unit Lwt.t - -val cement_commitment_skipped : Sc_rollup.Commitment.t -> unit Lwt.t - val cement_commitment_injected : Sc_rollup.Commitment.t -> unit Lwt.t (** [commitment_stored commitment] emits the event that the [commitment] was diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon.ml b/src/proto_alpha/bin_sc_rollup_node/daemon.ml index caa3c559622d96405ff4b5259ff51239ab61ab46..8af89e30275e0e2f5aeb55c33069989211e71abe 100644 --- a/src/proto_alpha/bin_sc_rollup_node/daemon.ml +++ b/src/proto_alpha/bin_sc_rollup_node/daemon.ml @@ -62,6 +62,104 @@ let categorise_heads (node_ctxt : Node_context.t) old_heads new_heads = module Make (PVM : Pvm.S) = struct module Components = Components.Make (PVM) + open Protocol + open Alpha_context + open Apply_results + + (** Process an L1 SCORU operation (for the node's rollup) which is included + for the first time. {b Note}: this function does not process inboxes for + the rollup, which is done instead by {!Inbox.process_head}. *) + let process_included_l1_operation (type kind) _node_ctxt store ~source:_ + (operation : kind manager_operation) + (result : kind successful_manager_operation_result) = + let open Lwt_syntax in + match (operation, result) with + | ( Sc_rollup_publish {commitment; _}, + Sc_rollup_publish_result {published_at_level; _} ) -> + (* Published commitment --------------------------------------------- *) + let commitment_hash = + Sc_rollup.Commitment.hash_uncarbonated commitment + in + Store.Commitments_published_at_level.add + store + commitment_hash + published_at_level + | Sc_rollup_cement {commitment; _}, Sc_rollup_cement_result {inbox_level; _} + -> + (* Cemented commitment ---------------------------------------------- *) + let* () = Store.Last_cemented_commitment_level.set store inbox_level in + Store.Last_cemented_commitment_hash.set store commitment + | _, _ -> + (* Other manager operations *) + return_unit + + (** Process an L1 SCORU operation (for the node's rollup) which is finalized + for the first time. *) + let process_finalized_l1_operation (type kind) _node_ctxt _store ~source:_ + (_operation : kind manager_operation) + (_result : kind successful_manager_operation_result) = + Lwt.return_unit + + let process_l1_operation (type kind) ~finalized node_ctxt store ~source + (operation : kind manager_operation) + (result : kind Apply_results.manager_operation_result) = + let open Lwt_syntax in + let is_for_my_rollup : type kind. kind manager_operation -> bool = function + | Sc_rollup_add_messages {rollup; _} + | Sc_rollup_cement {rollup; _} + | Sc_rollup_publish {rollup; _} + | Sc_rollup_refute {rollup; _} + | Sc_rollup_timeout {rollup; _} + | Sc_rollup_execute_outbox_message {rollup; _} + | Sc_rollup_recover_bond {sc_rollup = rollup} + | Sc_rollup_dal_slot_subscribe {rollup; _} -> + Sc_rollup.Address.(rollup = node_ctxt.Node_context.rollup_address) + | Reveal _ | Transaction _ | Origination _ | Delegation _ + | Register_global_constant _ | Set_deposits_limit _ + | Increase_paid_storage _ | Tx_rollup_origination + | Tx_rollup_submit_batch _ | Tx_rollup_commit _ | Tx_rollup_return_bond _ + | Tx_rollup_finalize_commitment _ | Tx_rollup_remove_commitment _ + | Tx_rollup_rejection _ | Tx_rollup_dispatch_tickets _ | Transfer_ticket _ + | Dal_publish_slot_header _ | Sc_rollup_originate _ -> + false + in + if not (is_for_my_rollup operation) then return_unit + else + (* Only look at operations that are for the node's rollup *) + let* () = Daemon_event.included_operation ~finalized operation result in + match result with + | Applied success_result -> + let process = + if finalized then process_finalized_l1_operation + else process_included_l1_operation + in + process node_ctxt store ~source operation success_result + | _ -> + (* No action for non successful operations *) + return_unit + + let process_l1_block_operations ~finalized node_ctxt store + (Layer1.Head {hash; _}) = + let open Lwt_result_syntax in + let* block = Layer1.fetch_tezos_block node_ctxt.Node_context.l1_ctxt hash in + let apply (type kind) accu ~source (operation : kind manager_operation) + result = + let open Lwt_syntax in + let* () = accu in + process_l1_operation ~finalized node_ctxt store ~source operation result + in + let apply_internal (type kind) accu ~source:_ + (_operation : kind Apply_internal_results.internal_operation) + (_result : kind Apply_internal_results.internal_operation_result) = + accu + in + let*! () = + Layer1_services.process_manager_operations + Lwt.return_unit + block.operations + {apply; apply_internal} + in + return_unit let process_head node_ctxt store head_state = let open Lwt_result_syntax in @@ -69,9 +167,13 @@ module Make (PVM : Pvm.S) = struct let* () = let*! () = emit_head_processing_event head_state in (* Avoid processing inbox again if it has been processed before for this head *) - if seen_before then return_unit + if seen_before then + if finalized then + process_l1_block_operations ~finalized node_ctxt store head + else return_unit else let* () = Inbox.process_head node_ctxt store head in + let* () = process_l1_block_operations ~finalized node_ctxt store head in (* Avoid storing and publishing commitments if the head is not final *) (* Avoid triggering the pvm execution if this has been done before for this head *) let* () = Components.Interpreter.process_head node_ctxt store head in diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon_event.ml b/src/proto_alpha/bin_sc_rollup_node/daemon_event.ml index 76304ab7172dea9b2846894446b91fcd26171648..4019b4244c8638158e405dbe24978dee70739125 100644 --- a/src/proto_alpha/bin_sc_rollup_node/daemon_event.ml +++ b/src/proto_alpha/bin_sc_rollup_node/daemon_event.ml @@ -62,6 +62,46 @@ module Simple = struct ~level:Notice ("from", Data_encoding.int32) ("to", Data_encoding.int32) + + let included_successful_operation = + declare_1 + ~section + ~name:"sc_rollup_daemon_included_successful_operation" + ~msg:"Operation {operation} was included as successful" + ~level:Debug + ("operation", L1_operation.encoding) + ~pp1:L1_operation.pp + + let included_failed_operation = + declare_3 + ~section + ~name:"sc_rollup_daemon_included_failed_operation" + ~msg:"Operation {operation} was included as {status} with error {error}" + ~level:Warning + ("operation", L1_operation.encoding) + ( "status", + Data_encoding.( + string_enum + [ + ("failed", `Failed); + ("backtracked", `Backtracked); + ("skipped", `Skipped); + ]) ) + ("error", Data_encoding.option Environment.Error_monad.trace_encoding) + ~pp1:L1_operation.pp + ~pp3: + (fun ppf -> function + | None -> Format.pp_print_string ppf "none" + | Some e -> Environment.Error_monad.pp_trace ppf e) + + let finalized_successful_operation = + declare_1 + ~section + ~name:"sc_rollup_daemon_finalized_successful_operation" + ~msg:"Operation {operation} was finalized" + ~level:Debug + ("operation", L1_operation.encoding) + ~pp1:L1_operation.pp end let head_processing hash level finalized seen_before = @@ -86,3 +126,24 @@ let processing_heads_iteration old_heads new_heads = | Some from_level, Some to_level -> Simple.(emit processing_heads_iteration (from_level, to_level)) | _ -> Lwt.return_unit + +let included_operation (type kind) ~finalized + (operation : kind Protocol.Alpha_context.manager_operation) + (result : kind Protocol.Apply_results.manager_operation_result) = + let operation = L1_operation.make operation in + match result with + | Applied _ when finalized -> + Simple.(emit finalized_successful_operation) operation + | _ when finalized -> + (* No events for finalized non successful operations *) + Lwt.return_unit + | Applied _ -> Simple.(emit included_successful_operation) operation + | result -> + let status, errors = + match result with + | Applied _ -> assert false + | Failed (_, e) -> (`Failed, Some e) + | Backtracked (_, e) -> (`Backtracked, e) + | Skipped _ -> (`Skipped, None) + in + Simple.(emit included_failed_operation) (operation, status, errors) diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon_event.mli b/src/proto_alpha/bin_sc_rollup_node/daemon_event.mli index d0927a06d11f6d00a34bd45b2a9a8ebdde9abed0..0ce9f4777a3b14c7d9f1bdc4c305c19ebb5df2d3 100644 --- a/src/proto_alpha/bin_sc_rollup_node/daemon_event.mli +++ b/src/proto_alpha/bin_sc_rollup_node/daemon_event.mli @@ -42,3 +42,11 @@ val not_finalized_head : Block_hash.t -> int32 -> unit Lwt.t the [new_heads]. *) val processing_heads_iteration : Layer1.head list -> Layer1.head list -> unit Lwt.t + +(** [included_operation ~finalized op result] emits an event that an operation + for the rollup was included in a block (or finalized). *) +val included_operation : + finalized:bool -> + 'kind Protocol.Alpha_context.manager_operation -> + 'kind Protocol.Apply_results.manager_operation_result -> + unit Lwt.t diff --git a/src/proto_alpha/bin_sc_rollup_node/layer1_services.ml b/src/proto_alpha/bin_sc_rollup_node/layer1_services.ml index 922c75ab8afe2fa7c645e133f587f132e6e78d14..22773e17db82ac5acb19a5b6545bcce5d4534c08 100644 --- a/src/proto_alpha/bin_sc_rollup_node/layer1_services.ml +++ b/src/proto_alpha/bin_sc_rollup_node/layer1_services.ml @@ -28,7 +28,7 @@ open Alpha_context open Apply_results open Protocol_client_context.Alpha_block_services -type 'accu operation_processor = { +type 'accu successful_operation_processor = { apply : 'kind. 'accu -> @@ -45,43 +45,49 @@ type 'accu operation_processor = { 'accu; } -let process_applied_manager_operations operations accu f = - let rec on_applied_operation_and_result : +type 'accu operation_processor = { + apply : + 'kind. + 'accu -> + source:public_key_hash -> + 'kind manager_operation -> + 'kind Apply_results.manager_operation_result -> + 'accu; + apply_internal : + 'kind. + 'accu -> + source:public_key_hash -> + 'kind Apply_internal_results.internal_operation -> + 'kind Apply_internal_results.internal_operation_result -> + 'accu; +} + +let process_manager_operations operations accu f = + let rec on_operation_and_result : type kind. _ -> kind Apply_results.contents_and_result_list -> _ = fun accu -> function | Single_and_result ( Manager_operation {operation; source; _}, Manager_operation_result - { - operation_result = Applied operation_result; - internal_operation_results; - _; - } ) -> + {operation_result; internal_operation_results; _} ) -> let accu = f.apply accu ~source operation operation_result in - on_applied_internal_operations accu source internal_operation_results + on_internal_operations accu source internal_operation_results | Single_and_result (_, _) -> accu | Cons_and_result ( Manager_operation {operation; source; _}, Manager_operation_result - { - operation_result = Applied operation_result; - internal_operation_results; - _; - }, + {operation_result; internal_operation_results; _}, rest ) -> let accu = f.apply accu ~source operation operation_result in let accu = - on_applied_internal_operations accu source internal_operation_results + on_internal_operations accu source internal_operation_results in - on_applied_operation_and_result accu rest - | Cons_and_result (_, _, rest) -> on_applied_operation_and_result accu rest - and on_applied_internal_operations accu source internal_operation_results = + on_operation_and_result accu rest + and on_internal_operations accu source internal_operation_results = let open Apply_internal_results in List.fold_left (fun accu (Internal_operation_result (operation, result)) -> - match result with - | Applied result -> f.apply_internal accu ~source operation result - | _ -> accu) + f.apply_internal accu ~source operation result) accu internal_operation_results in @@ -96,7 +102,7 @@ let process_applied_manager_operations operations accu f = | Receipt (Operation_metadata {contents = results; _}) -> ( match Apply_results.kind_equal_list contents results with | Some Eq -> - on_applied_operation_and_result accu + on_operation_and_result accu @@ Apply_results.pack_contents_list contents results | None -> (* Should not happen *) @@ -104,3 +110,20 @@ let process_applied_manager_operations operations accu f = in let process_operations = List.fold_left process_contents in List.fold_left process_operations operations accu + +let process_applied_manager_operations operations accu + (f : _ successful_operation_processor) = + let apply (type kind) accu ~source (operation : kind manager_operation) + (result : kind Apply_results.manager_operation_result) = + match result with + | Applied result -> f.apply accu ~source operation result + | _ -> accu + in + let apply_internal (type kind) accu ~source + (operation : kind Apply_internal_results.internal_operation) + (result : kind Apply_internal_results.internal_operation_result) = + match result with + | Applied result -> f.apply_internal accu ~source operation result + | _ -> accu + in + process_manager_operations operations accu {apply; apply_internal} diff --git a/src/proto_alpha/bin_sc_rollup_node/layer1_services.mli b/src/proto_alpha/bin_sc_rollup_node/layer1_services.mli index 669cacbe849526c151a95863d803106f72cd3d38..bbcf372491af5e1f66c5f9e0e951ef2dbb69575a 100644 --- a/src/proto_alpha/bin_sc_rollup_node/layer1_services.mli +++ b/src/proto_alpha/bin_sc_rollup_node/layer1_services.mli @@ -27,7 +27,7 @@ open Protocol open Alpha_context open Protocol_client_context.Alpha_block_services -type 'accu operation_processor = { +type 'accu successful_operation_processor = { apply : 'kind. 'accu -> @@ -44,8 +44,31 @@ type 'accu operation_processor = { 'accu; } -(** [process_applied_manager_operations accu operations operator] - folds over the list of [operations] applying [operator] to +type 'accu operation_processor = { + apply : + 'kind. + 'accu -> + source:public_key_hash -> + 'kind manager_operation -> + 'kind Apply_results.manager_operation_result -> + 'accu; + apply_internal : + 'kind. + 'accu -> + source:public_key_hash -> + 'kind Apply_internal_results.internal_operation -> + 'kind Apply_internal_results.internal_operation_result -> + 'accu; +} + +(** [process_manager_operations accu operations operator] folds over the list of + manager operations in [operations] applying [operator] to transform [accu] + along the way. *) +val process_manager_operations : + 'a -> operation list list -> 'a operation_processor -> 'a + +(** [process_applied_manager_operations accu operations operator] folds over the + list of applied manager operations in [operations] applying [operator] to transform [accu] along the way. *) val process_applied_manager_operations : - 'a -> operation list list -> 'a operation_processor -> 'a + 'a -> operation list list -> 'a successful_operation_processor -> 'a diff --git a/src/proto_alpha/bin_sc_rollup_node/refutation_game.ml b/src/proto_alpha/bin_sc_rollup_node/refutation_game.ml index 380db2d237b3af80a5cc68d06c17488f506e1d01..a837a2186b561f84caf03d740cd8a67a6888db64 100644 --- a/src/proto_alpha/bin_sc_rollup_node/refutation_game.ml +++ b/src/proto_alpha/bin_sc_rollup_node/refutation_game.ml @@ -82,7 +82,7 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct let open Node_context in let open Lwt_result_syntax in let {rollup_address; cctxt; _} = node_ctxt in - let* _, _, Manager_operation_result {operation_result; _} = + let* _oph, _op, _results = Client_proto_context.sc_rollup_refute cctxt ~chain:cctxt#chain @@ -96,18 +96,6 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct ~fee_parameter:Configuration.default_fee_parameter () in - let open Apply_results in - let*! () = - match operation_result with - | Applied (Sc_rollup_refute_result _) -> - Refutation_game_event.refutation_published opponent refutation - | Failed (Sc_rollup_refute_manager_kind, _errors) -> - Refutation_game_event.refutation_failed opponent refutation - | Backtracked (Sc_rollup_refute_result _, _errors) -> - Refutation_game_event.refutation_backtracked opponent refutation - | Skipped Sc_rollup_refute_manager_kind -> - Refutation_game_event.refutation_skipped opponent refutation - in return_unit let generate_proof node_ctxt store game start_state = @@ -285,7 +273,7 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct let open Node_context in let open Lwt_result_syntax in let {rollup_address; cctxt; _} = node_ctxt in - let* _, _, Manager_operation_result {operation_result; _} = + let* _oph, _op, _results = Client_proto_context.sc_rollup_timeout cctxt ~chain:cctxt#chain @@ -299,18 +287,6 @@ module Make (PVM : Pvm.S) : S with module PVM = PVM = struct ~fee_parameter:Configuration.default_fee_parameter () in - let open Apply_results in - let*! () = - match operation_result with - | Applied (Sc_rollup_timeout_result _) -> - Refutation_game_event.timeout_published players - | Failed (Sc_rollup_timeout_manager_kind, _errors) -> - Refutation_game_event.timeout_failed players - | Backtracked (Sc_rollup_timeout_result _, _errors) -> - Refutation_game_event.timeout_backtracked players - | Skipped Sc_rollup_timeout_manager_kind -> - Refutation_game_event.timeout_skipped players - in return_unit let timeout_reached ~self head_block node_ctxt players = diff --git a/src/proto_alpha/bin_sc_rollup_node/refutation_game_event.ml b/src/proto_alpha/bin_sc_rollup_node/refutation_game_event.ml index 1fbe9d13e79bb18671ba9f37d80b0b50342a9727..b7b96030d0135b8978f8582e8be6bdf75ec08648 100644 --- a/src/proto_alpha/bin_sc_rollup_node/refutation_game_event.ml +++ b/src/proto_alpha/bin_sc_rollup_node/refutation_game_event.ml @@ -68,67 +68,12 @@ module Simple = struct ("other", Sc_rollup.Staker.encoding) ("their_commitment_hash", Sc_rollup.Commitment.Hash.encoding) ("parent_commitment_hash", Sc_rollup.Commitment.Hash.encoding) - - let refutation_event state = - declare_2 - ~section - ~name:("sc_rollup_node_refutation_" ^ state) - ~msg: - ("Refutation was " ^ state - ^ " - opponent: {opponent}, refutation: {refutation}") - ~level:Notice - ("opponent", Sc_rollup.Staker.encoding) - ("refutation", Data_encoding.option Sc_rollup.Game.refutation_encoding) - - let refutation_published = refutation_event "published" - - let refutation_failed = refutation_event "failed" - - let refutation_backtracked = refutation_event "backtracked" - - let refutation_skipped = refutation_event "skipped" - - let timeout_event state = - declare_1 - ~section - ~name:("sc_rollup_node_timeout_" ^ state) - ~msg:("Timeout was " ^ state ^ " - players: {players}") - ~level:Notice - ("players", Sc_rollup.Game.Index.encoding) - - let timeout_published = timeout_event "published" - - let timeout_failed = timeout_event "failed" - - let timeout_backtracked = timeout_event "backtracked" - - let timeout_skipped = timeout_event "skipped" end let timeout address = Simple.(emit timeout address) let invalid_move () = Simple.(emit invalid_move ()) -let refutation_published opponent refutation = - Simple.(emit refutation_published (opponent, refutation)) - -let refutation_failed opponent refutation = - Simple.(emit refutation_failed (opponent, refutation)) - -let refutation_backtracked opponent refutation = - Simple.(emit refutation_backtracked (opponent, refutation)) - -let refutation_skipped opponent refutation = - Simple.(emit refutation_skipped (opponent, refutation)) - -let timeout_published players = Simple.(emit timeout_published players) - -let timeout_failed players = Simple.(emit timeout_failed players) - -let timeout_backtracked players = Simple.(emit timeout_backtracked players) - -let timeout_skipped players = Simple.(emit timeout_skipped players) - let conflict_detected (conflict : Sc_rollup.Refutation_storage.conflict) = let our_commitment_hash = Sc_rollup.Commitment.hash_uncarbonated conflict.our_commitment diff --git a/src/proto_alpha/bin_sc_rollup_node/store.ml b/src/proto_alpha/bin_sc_rollup_node/store.ml index 144775f33a416e998d928be3f85e05f977194a77..02553212507c05d94d6a7d886ebb0095999f69ca 100644 --- a/src/proto_alpha/bin_sc_rollup_node/store.ml +++ b/src/proto_alpha/bin_sc_rollup_node/store.ml @@ -108,16 +108,13 @@ module Make_map (P : KeyValue) = struct let find store key = let open Lwt_syntax in - let* exists = mem store key in - if exists then - let+ value = get store key in - Some value - else return_none + let* value = IStore.find store (make_key key) in + Option.map_s decode_value value let find_with_default store key ~on_default = let open Lwt_syntax in - let* exists = mem store key in - if exists then get store key else return (on_default ()) + let+ value = find store key in + Option.value_f value ~default:on_default end module Make_updatable_map (P : KeyValue) = struct @@ -501,7 +498,7 @@ module Last_cemented_commitment_hash = Make_mutable_value (struct let value_encoding = Sc_rollup.Commitment.Hash.encoding end) -module Commitments_published_at_level = Make_append_only_map (struct +module Commitments_published_at_level = Make_updatable_map (struct let path = ["commitments"; "published_at_level"] let keep_last_n_entries_in_memory = 10 diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 837b411554386653823c8413af77058573f091b0..1a96ee9c1437269bd61dd30f0600e65f19c1425e 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -723,8 +723,10 @@ let pp_manager_operation_contents_result ppf op_result = pp_consumed_gas ppf consumed_gas ; pp_inbox_after ppf inbox_after in - let pp_sc_rollup_cement_result (Sc_rollup_cement_result {consumed_gas}) = - pp_consumed_gas ppf consumed_gas + let pp_sc_rollup_cement_result + (Sc_rollup_cement_result {consumed_gas; inbox_level}) = + pp_consumed_gas ppf consumed_gas ; + Format.fprintf ppf "@,Inbox level: %a" Raw_level.pp inbox_level in let pp_sc_rollup_publish_result (Sc_rollup_publish_result diff --git a/src/proto_alpha/lib_injector/l1_operation.ml b/src/proto_alpha/lib_injector/l1_operation.ml index 2aa9b014171e7079ad09524cb2e51f12eaf4a354..e801c9a852b16ee35dec9e335d70c3797a4f99f0 100644 --- a/src/proto_alpha/lib_injector/l1_operation.ml +++ b/src/proto_alpha/lib_injector/l1_operation.ml @@ -68,6 +68,8 @@ module Manager_operation = struct make sc_rollup_refute_case; make sc_rollup_timeout_case; make sc_rollup_execute_outbox_message_case; + make sc_rollup_recover_bond_case; + make sc_rollup_dal_slot_subscribe_case; ] let get_case : diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index e450eca62dc62031e44dd1828195c4f28b92b79b..070c86660305ac9c52c91e70484e97585d8fa4e9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3445,7 +3445,10 @@ module Sc_rollup : sig Lwt.t val cement_commitment : - context -> t -> Commitment.Hash.t -> context tzresult Lwt.t + context -> + t -> + Commitment.Hash.t -> + (context * Commitment.t) tzresult Lwt.t val withdraw_stake : context -> diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index e491c03c362a463499837a7210ac1bf199d9b35c..7f9f086db701361869ca6010e4abcc6bcdf0e4ce 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1810,9 +1810,12 @@ let apply_manager_operation : return (ctxt, result, []) | Sc_rollup_cement {rollup; commitment} -> Sc_rollup.Stake_storage.cement_commitment ctxt rollup commitment - >>=? fun ctxt -> + >>=? fun (ctxt, commitment) -> let consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt in - let result = Sc_rollup_cement_result {consumed_gas} in + let result = + Sc_rollup_cement_result + {consumed_gas; inbox_level = commitment.inbox_level} + in return (ctxt, result, []) | Sc_rollup_publish {rollup; commitment} -> Sc_rollup.Stake_storage.publish_commitment ctxt rollup source commitment diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index c533455dadc246bc5c6471b7b1ed4363941906e1..153bcaf99e9dc33934b356c52837b769eef88137 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -136,6 +136,7 @@ type _ successful_manager_operation_result = -> Kind.sc_rollup_add_messages successful_manager_operation_result | Sc_rollup_cement_result : { consumed_gas : Gas.Arith.fp; + inbox_level : Raw_level.t; } -> Kind.sc_rollup_cement successful_manager_operation_result | Sc_rollup_publish_result : { @@ -806,13 +807,18 @@ module Manager_result = struct make ~op_case:Operation.Encoding.Manager_operations.sc_rollup_cement_case ~encoding: - (obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)) + (obj2 + (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero) + (req "inbox_level" Raw_level.encoding)) ~select:(function | Successful_manager_result (Sc_rollup_cement_result _ as op) -> Some op | _ -> None) - ~proj:(function Sc_rollup_cement_result {consumed_gas} -> consumed_gas) + ~proj:(function + | Sc_rollup_cement_result {consumed_gas; inbox_level} -> + (consumed_gas, inbox_level)) ~kind:Kind.Sc_rollup_cement_manager_kind - ~inj:(fun consumed_gas -> Sc_rollup_cement_result {consumed_gas}) + ~inj:(fun (consumed_gas, inbox_level) -> + Sc_rollup_cement_result {consumed_gas; inbox_level}) let sc_rollup_publish_case = make diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index 164505df7e1c066e93ca7a3399df19c0dd539d29..737894532feffc53ea6d65be0c902aed6e92422a 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -232,6 +232,7 @@ and _ successful_manager_operation_result = -> Kind.sc_rollup_add_messages successful_manager_operation_result | Sc_rollup_cement_result : { consumed_gas : Gas.Arith.fp; + inbox_level : Raw_level.t; } -> Kind.sc_rollup_cement successful_manager_operation_result | Sc_rollup_publish_result : { diff --git a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml index 84c9cbfe586f4ca9aa67c6fc96c494fb6b11362e..b0e6f0ac6263baa9698aff574af66a2f26c2d4be 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml @@ -372,7 +372,8 @@ let cement_commitment ctxt rollup new_lcc = on the new LCC, and no one is directly staked on the old LCC. We can safely deallocate the old LCC. *) - deallocate ctxt rollup old_lcc + let+ ctxt = deallocate ctxt rollup old_lcc in + (ctxt, new_lcc_commitment) let remove_staker ctxt rollup staker = let open Lwt_tzresult_syntax in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli index 172e1d9155b9aa943c194f94b868207902139360..346da8650a1b8b1ac41d2277cfe14c69824f814d 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli @@ -81,7 +81,7 @@ val publish_commitment : Lwt.t (** [cement_commitment context rollup commitment] cements the given - commitment. + commitment whose hash is given (and returns the corresponding commitment). Subsequent calls to [refine_stake] and [cement_commitment] must use a [context] with greater level, or behavior is undefined. @@ -111,7 +111,7 @@ val cement_commitment : Raw_context.t -> Sc_rollup_repr.t -> Sc_rollup_commitment_repr.Hash.t -> - Raw_context.t tzresult Lwt.t + (Raw_context.t * Sc_rollup_commitment_repr.t) tzresult Lwt.t (** [find_staker_unsafe ctxt rollup staker] returns the branch on which the stake is deposited for the [rollup]'s [staker]. diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml index d1cd421fbfb69f13688a28ae5399e9881fc1e2fe..fce3d4bf825b3bbca1257be8488ccc945310c759 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml @@ -240,6 +240,32 @@ let assert_commitment_hash_equal ~loc _ctxt x y = x y +let assert_commitment_equal ~loc x y = + Assert.equal + ~loc + (fun Commitment_repr. + { + compressed_state = c1; + inbox_level = l1; + predecessor = p1; + number_of_ticks = n1; + } + Commitment_repr. + { + compressed_state = c2; + inbox_level = l2; + predecessor = p2; + number_of_ticks = n2; + } -> + Sc_rollup_repr.State_hash.equal c1 c2 + && Raw_level_repr.equal l1 l2 + && Commitment_repr.Hash.equal p1 p2 + && Sc_rollup_repr.Number_of_ticks.equal n1 n2) + "Compare commitment" + Commitment_repr.pp + x + y + let assert_kinds_are_equal ~loc x y = Assert.equal ~loc @@ -765,9 +791,7 @@ let test_cement_three_commitments () = let challenge_window = Constants_storage.sc_rollup_challenge_window_in_blocks ctxt in - lift - @@ - let commitment = + let commitment1 = Commitment_repr. { predecessor = genesis_hash; @@ -777,13 +801,14 @@ let test_cement_three_commitments () = } in let* c1, _level, ctxt = - Sc_rollup_stake_storage.Internal_for_tests.refine_stake - ctxt - rollup - staker - commitment + lift + @@ Sc_rollup_stake_storage.Internal_for_tests.refine_stake + ctxt + rollup + staker + commitment1 in - let commitment = + let commitment2 = Commitment_repr. { predecessor = c1; @@ -793,13 +818,14 @@ let test_cement_three_commitments () = } in let* c2, _level, ctxt = - Sc_rollup_stake_storage.Internal_for_tests.refine_stake - ctxt - rollup - staker - commitment + lift + @@ Sc_rollup_stake_storage.Internal_for_tests.refine_stake + ctxt + rollup + staker + commitment2 in - let commitment = + let commitment3 = Commitment_repr. { predecessor = c2; @@ -809,16 +835,26 @@ let test_cement_three_commitments () = } in let* c3, _level, ctxt = - Sc_rollup_stake_storage.Internal_for_tests.refine_stake - ctxt - rollup - staker - commitment + lift + @@ Sc_rollup_stake_storage.Internal_for_tests.refine_stake + ctxt + rollup + staker + commitment3 in let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in - let* ctxt = Sc_rollup_stake_storage.cement_commitment ctxt rollup c2 in - let* ctxt = Sc_rollup_stake_storage.cement_commitment ctxt rollup c3 in + let* ctxt, commitment1' = + lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 + in + let* ctxt, commitment2' = + lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c2 + in + let* ctxt, commitment3' = + lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c3 + in + let* () = assert_commitment_equal ~loc:__LOC__ commitment1' commitment1 in + let* () = assert_commitment_equal ~loc:__LOC__ commitment2' commitment2 in + let* () = assert_commitment_equal ~loc:__LOC__ commitment3' commitment3 in assert_true ctxt let test_cement_then_remove () = @@ -849,7 +885,7 @@ let test_cement_then_remove () = commitment in let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = + let* ctxt, _ = lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in assert_fails_with @@ -1027,7 +1063,7 @@ let test_last_cemented_commitment_hash_with_level () = commitment in let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = + let* ctxt, _ = lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in let* c1', inbox_level', ctxt = @@ -1817,7 +1853,7 @@ let test_no_conflict_point_one_staker_at_lcc () = Constants_storage.sc_rollup_challenge_window_in_blocks ctxt in let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = + let* ctxt, _ = lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in assert_fails_with @@ -1862,7 +1898,7 @@ let test_no_conflict_point_both_stakers_at_lcc () = Constants_storage.sc_rollup_challenge_window_in_blocks ctxt in let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = + let* ctxt, _ = lift @@ Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in assert_fails_with @@ -2194,7 +2230,9 @@ let test_concurrent_refinement_cement () = let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 in + let* ctxt, _ = + Sc_rollup_stake_storage.cement_commitment ctxt rollup c1 + in Sc_rollup_commitment_storage.last_cemented_commitment ctxt rollup in let* c2, ctxt = @@ -2219,7 +2257,9 @@ let test_concurrent_refinement_cement () = let ctxt = Raw_context.Internal_for_tests.add_level ctxt challenge_window in - let* ctxt = Sc_rollup_stake_storage.cement_commitment ctxt rollup c2 in + let* ctxt, _ = + Sc_rollup_stake_storage.cement_commitment ctxt rollup c2 + in Sc_rollup_commitment_storage.last_cemented_commitment ctxt rollup in assert_commitment_hash_equal ~loc:__LOC__ ctxt c1 c2 diff --git a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (first_p.out b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (first_p.out index 2964701ea7015e6fe0fd8156d0ef75066b365d91..85f02a11358797d45bae62f79680aee9644d6a65 100644 --- a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (first_p.out +++ b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (first_p.out @@ -37,6 +37,15 @@ This sequence of operations was run: { "level": 2, "commitment_hash": "[SC_ROLLUP_COMMITMENT_HASH]" } +./tezos-sc-rollup-client-alpha rpc get /local/last_published_commitment +{ "commitment": + { "compressed_state": + "scs139rUcWoTB9MbNUEaWYn5RswKF7G2uENnRPq7Q9ByQRMahtB2NA", + "inbox_level": 32, + "predecessor": "[SC_ROLLUP_COMMITMENT_HASH]", + "number_of_ticks": 0 }, + "hash": "[SC_ROLLUP_COMMITMENT_HASH]" } + ./tezos-sc-rollup-client-alpha rpc get /local/last_published_commitment { "commitment": { "compressed_state": diff --git a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (handles.out b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (handles.out index 35d72b6aa0668546118804504beade30651d7014..30fac1bd1803beed2af41fe59a5820e51f68416e 100644 --- a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (handles.out +++ b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (handles.out @@ -92,5 +92,4 @@ This sequence of operations was run: "inbox_level": 32, "predecessor": "[SC_ROLLUP_COMMITMENT_HASH]", "number_of_ticks": 0 }, - "hash": "[SC_ROLLUP_COMMITMENT_HASH]", - "published_at_level": 35 } + "hash": "[SC_ROLLUP_COMMITMENT_HASH]" } diff --git a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (message.out b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (message.out index 89de5aa9a387adeb57deb22ef6b3bee0b041f8b8..3f3297f09f7b561561154f2d1760f51ce5826923 100644 --- a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (message.out +++ b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (message.out @@ -1317,5 +1317,4 @@ This sequence of operations was run: "inbox_level": 62, "predecessor": "[SC_ROLLUP_COMMITMENT_HASH]", "number_of_ticks": 0 }, - "hash": "[SC_ROLLUP_COMMITMENT_HASH]", - "published_at_level": 65 } + "hash": "[SC_ROLLUP_COMMITMENT_HASH]" } diff --git a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out index 3af1dce9770863ffd7ad78b153570781363dcf55..47f4ce5a062b4b236eea04ae42a155e88ab0c0dc 100644 --- a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out +++ b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out @@ -53,8 +53,7 @@ This sequence of operations was run: "inbox_level": 32, "predecessor": "[SC_ROLLUP_COMMITMENT_HASH]", "number_of_ticks": 0 }, - "hash": "[SC_ROLLUP_COMMITMENT_HASH]", - "published_at_level": 35 } + "hash": "[SC_ROLLUP_COMMITMENT_HASH]" } ./tezos-client rpc get /chains/main/blocks/head/context/constants { "proof_of_work_nonce_size": 8, "nonce_length": 32, @@ -160,6 +159,7 @@ This sequence of operations was run: Commitment: [SC_ROLLUP_COMMITMENT_HASH] This smart contract rollup commitment cementing was successfully applied Consumed gas: 3625.362 + Inbox level: 32 ./tezos-client rpc get /chains/main/blocks/head/context/constants diff --git a/tezt/tests/sc_rollup.ml b/tezt/tests/sc_rollup.ml index a7bf8ce7bebf1a18ce552264b03219346c3984c6..5a59c3ea12dc6bb39e7cbd61706d02a9513d0e55 100644 --- a/tezt/tests/sc_rollup.ml +++ b/tezt/tests/sc_rollup.ml @@ -1051,20 +1051,30 @@ let bake_levels ?hook n client = let* () = match hook with None -> return () | Some hook -> hook i in Client.bake_for_and_wait client -let check_eq_commitment (c1 : Sc_rollup_client.commitment) - (c2 : Sc_rollup_client.commitment) = - Check.(c1.predecessor = c2.predecessor) - Check.string - ~error_msg:"Commitments differ in inbox_level (%L = %R)" ; - Check.(c1.compressed_state = c2.compressed_state) - Check.string - ~error_msg:"Commitments differ in inbox_level (%L = %R)" ; - Check.(c1.inbox_level = c2.inbox_level) - Check.int - ~error_msg:"Commitments differ in inbox_level (%L = %R)" ; - Check.(c1.number_of_ticks = c2.number_of_ticks) - Check.int - ~error_msg:"Commitments differ in inbox_level (%L = %R)" +let eq_commitment_typ = + Check.equalable + (fun ppf (c : Sc_rollup_client.commitment) -> + Format.fprintf + ppf + "@[{ predecessor: %s,@,\ + state: %s,@,\ + inbox level: %d,@,\ + ticks: %d }@]" + c.predecessor + c.compressed_state + c.inbox_level + c.number_of_ticks) + ( = ) + +let check_commitment_eq (commitment, name) (expected_commitment, exp_name) = + Check.((commitment = expected_commitment) (option eq_commitment_typ)) + ~error_msg: + (sf + "Commitment %s differs from the one %s.\n%s: %%L\n%s: %%R" + name + exp_name + (String.capitalize_ascii name) + (String.capitalize_ascii exp_name)) let tezos_client_get_commitment client sc_rollup commitment_hash = let* output = @@ -1085,8 +1095,8 @@ let tezos_client_get_commitment client sc_rollup commitment_hash = in Lwt.return @@ Sc_rollup_client.commitment_from_json output -let check_published_commitment_in_l1 ?(force_new_level = true) sc_rollup client - published_commitment = +let check_published_commitment_in_l1 ?(allow_non_published = false) + ?(force_new_level = true) sc_rollup client published_commitment = let* () = if force_new_level then (* Triggers injection into the L1 context *) @@ -1095,13 +1105,19 @@ let check_published_commitment_in_l1 ?(force_new_level = true) sc_rollup client in let* commitment_in_l1 = match published_commitment with - | None -> Lwt.return_none + | None -> + if not allow_non_published then + Test.fail "No commitment has been published" ; + Lwt.return_none | Some (hash, _commitment, _level) -> tezos_client_get_commitment client sc_rollup hash in - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind published_commitment (fun (_, c1, _level) -> - Option.map (fun c2 -> (c1, c2)) commitment_in_l1) ; + let published_commitment = + Option.map (fun (_, c, _) -> c) published_commitment + in + check_commitment_eq + (commitment_in_l1, "in L1") + (published_commitment, "published") ; Lwt.return_unit let test_commitment_scenario ?commitment_period ?challenge_window @@ -1171,12 +1187,14 @@ let commitment_stored _protocol sc_rollup_node sc_rollup _node client = (Check.option Check.int) ~error_msg: "Commitment has been stored at a level different than expected (%L = %R)" ; + (* Bake one level for commitment to be included *) + let* () = Client.bake_for_and_wait client in let* published_commitment = Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client in - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind published_commitment (fun (_hash, c1, _level) -> - Option.map (fun (_, c2, _level) -> (c1, c2)) stored_commitment) ; + check_commitment_eq + (Option.map (fun (_, c, _) -> c) stored_commitment, "stored") + (Option.map (fun (_, c, _) -> c) published_commitment, "published") ; check_published_commitment_in_l1 sc_rollup client published_commitment let mode_publish mode publishes protocol sc_rollup_node sc_rollup node client = @@ -1367,9 +1385,9 @@ let commitments_messages_reset _protocol sc_rollup_node sc_rollup _node client = let* published_commitment = Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client in - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind published_commitment (fun (_hash, c1, _level) -> - Option.map (fun (_hash, c2, _level) -> (c1, c2)) stored_commitment) ; + check_commitment_eq + (Option.map (fun (_, c, _) -> c) stored_commitment, "stored") + (Option.map (fun (_, c, _) -> c) published_commitment, "published") ; check_published_commitment_in_l1 sc_rollup client published_commitment let commitment_stored_robust_to_failures _protocol sc_rollup_node sc_rollup node @@ -1445,9 +1463,9 @@ let commitment_stored_robust_to_failures _protocol sc_rollup_node sc_rollup node let* stored_commitment' = Sc_rollup_client.last_stored_commitment ~hooks sc_rollup_client' in - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind stored_commitment' (fun (_hash, c1, _level) -> - Option.map (fun (_, c2, _level) -> (c1, c2)) stored_commitment) ; + check_commitment_eq + (Option.map (fun (_, c, _) -> c) stored_commitment, "stored in first node") + (Option.map (fun (_, c, _) -> c) stored_commitment', "stored in second node") ; return () let commitments_reorgs protocol sc_rollup_node sc_rollup node client = @@ -1561,9 +1579,9 @@ let commitments_reorgs protocol sc_rollup_node sc_rollup node client = let* published_commitment = Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client in - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind published_commitment (fun (_hash, c1, _level) -> - Option.map (fun (_hash, c2, _level) -> (c1, c2)) stored_commitment) ; + check_commitment_eq + (Option.map (fun (_, c, _) -> c) stored_commitment, "stored") + (Option.map (fun (_, c, _) -> c) published_commitment, "published") ; check_published_commitment_in_l1 sc_rollup client published_commitment type balances = {liquid : int; frozen : int} @@ -1819,21 +1837,28 @@ let first_published_level_is_global _protocol sc_rollup_node sc_rollup node let* rollup_node1_published_commitment = Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client in - let () = - Check.( - Option.map inbox_level rollup_node1_published_commitment - = Some commitment_inbox_level) - (Check.option Check.int) - ~error_msg: - "Commitment has been published at a level different than expected (%L \ - = %R)" ; - Check.( - Option.bind rollup_node1_published_commitment first_published_at_level - <> None) - (Check.option Check.int) - ~error_msg: - "Level at which commitment has first been published is undefined" + Check.( + Option.map inbox_level rollup_node1_published_commitment + = Some commitment_inbox_level) + (Check.option Check.int) + ~error_msg: + "Commitment has been published at a level different than expected (%L = \ + %R)" ; + (* Bake an additional block for the commitment to be included. *) + let* () = Client.bake_for_and_wait client in + let* commitment_publish_level = + Sc_rollup_node.wait_for_level sc_rollup_node (commitment_finalized_level + 1) + in + let* rollup_node1_published_commitment = + Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client in + Check.( + Option.bind rollup_node1_published_commitment first_published_at_level + = Some commitment_publish_level) + (Check.option Check.int) + ~error_msg: + "Level at which commitment has first been published (%L) is wrong. \ + Expected %R." ; let* () = Sc_rollup_node.terminate sc_rollup_node in (* Rollup node 2 starts and processes enough levels to publish a commitment.*) let bootstrap2_key = Constant.bootstrap2.public_key_hash in @@ -1857,13 +1882,11 @@ let first_published_level_is_global _protocol sc_rollup_node sc_rollup node let* rollup_node2_published_commitment = Sc_rollup_client.last_published_commitment ~hooks sc_rollup_client' in - let* () = - Option.iter (fun (c1, c2) -> check_eq_commitment c1 c2) - @@ Option.bind rollup_node1_published_commitment (fun (_, c1, _level) -> - rollup_node2_published_commitment - |> Option.map (fun (_, c2, _level) -> (c1, c2))) ; - Lwt.return_unit - in + check_commitment_eq + ( Option.map (fun (_, c, _) -> c) rollup_node1_published_commitment, + "published by rollup node 1" ) + ( Option.map (fun (_, c, _) -> c) rollup_node2_published_commitment, + "published by rollup node 2" ) ; let () = Check.( Option.bind rollup_node1_published_commitment first_published_at_level