From 1b1d4527e05ce4ef1c16bcbc438638ff31acd7a9 Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Wed, 9 Oct 2024 13:28:20 +0200 Subject: [PATCH 1/5] ADAL/Proto: Add the level of cells in backpointers --- .../lib_protocol/alpha_context.mli | 18 +++++++- src/proto_alpha/lib_protocol/dal_slot_repr.ml | 44 ++++++++++++++++--- .../lib_protocol/dal_slot_repr.mli | 20 ++++++++- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 361147f9d745..78f157cb59c6 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2908,7 +2908,23 @@ module Dal : sig module Slots_history : sig type t - module Pointer_hash : S.HASH + module Pointer_hash : sig + module Crypto_PH : S.HASH + + type t = private Crypto_PH.t * Raw_level_repr.t + + val equal : t -> t -> bool + + val compare : t -> t -> int + + val encoding : t Data_encoding.t + + val hash_bytes : ?key:bytes -> bytes list -> Crypto_PH.t + + val to_bytes : t -> bytes + + val pp : Format.formatter -> t -> unit + end type hash = Pointer_hash.t diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.ml b/src/proto_alpha/lib_protocol/dal_slot_repr.ml index 1062bdade2a2..3d656a3baf4c 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.ml +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.ml @@ -288,7 +288,37 @@ module History = struct let title = "A hash that represents the skip list pointers" end - module Pointer_hash = Blake2B.Make (Base58) (Pointer_prefix) + module Pointer_hash = struct + module Crypto_PH = Blake2B.Make (Base58) (Pointer_prefix) + + type t = Crypto_PH.t * Raw_level_repr.t + + let equal (c1, r1) (c2, r2) = + Crypto_PH.equal c1 c2 && Raw_level_repr.equal r1 r2 + + let compare (c1, r1) (c2, r2) = + let c = Crypto_PH.compare c1 c2 in + if Compare.Int.(c <> 0) then c else Raw_level_repr.compare r1 r2 + + let encoding = Data_encoding.tup2 Crypto_PH.encoding Raw_level_repr.encoding + + let hash_bytes = Crypto_PH.hash_bytes + + let to_bytes (c, r) = + Bytes.concat + Bytes.empty + [ + Crypto_PH.to_bytes c; + Raw_level_repr.to_int32 r |> Int32.to_string |> Bytes.of_string; + ] + + let pp fmt (c, r) = + Format.fprintf fmt "(%a|%a)" Crypto_PH.pp c Raw_level_repr.pp r + + (* + let pp = Crypto_PH.pp + *) + end module Skip_list_parameters = struct let basis = 4 @@ -524,12 +554,16 @@ module History = struct let equal : t -> t -> bool = equal_history - let hash cell = + let hash cell : Pointer_hash.t = let current_slot = Skip_list.content cell in + let published_level = + (Content.content_id current_slot).Header.published_level + in let back_pointers_hashes = Skip_list.back_pointers cell in - Data_encoding.Binary.to_bytes_exn Content.encoding current_slot - :: List.map Pointer_hash.to_bytes back_pointers_hashes - |> Pointer_hash.hash_bytes + ( Data_encoding.Binary.to_bytes_exn Content.encoding current_slot + :: List.map Pointer_hash.to_bytes back_pointers_hashes + |> Pointer_hash.hash_bytes, + published_level ) let pp_history fmt (history : history) = let history_hash = hash history in diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.mli b/src/proto_alpha/lib_protocol/dal_slot_repr.mli index 970dc4ea9018..b0347828a9eb 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.mli +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.mli @@ -217,7 +217,25 @@ module History : sig confirmed slot headers. *) type t - module Pointer_hash : S.HASH + module Pointer_hash : sig + module Crypto_PH : S.HASH + + type t = private Crypto_PH.t * Raw_level_repr.t + + val equal : t -> t -> bool + + val compare : t -> t -> int + + val encoding : t Data_encoding.t + + val hash_bytes : ?key:bytes -> bytes list -> Crypto_PH.t + + val to_bytes : t -> bytes + + val pp : Format.formatter -> t -> unit + end + + (*S.HASH*) (** Type of hashes of history. *) type hash = Pointer_hash.t -- GitLab From 9563b64370c6081172281103b2a3fd7c32c21bcf Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Wed, 9 Oct 2024 15:31:56 +0200 Subject: [PATCH 2/5] ADAL/Proto: Add Header.id of cells in backpointers, not just level --- .../lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/dal_slot_repr.ml | 33 +++++++++++-------- .../lib_protocol/dal_slot_repr.mli | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 78f157cb59c6..11845c63db31 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2911,7 +2911,7 @@ module Dal : sig module Pointer_hash : sig module Crypto_PH : S.HASH - type t = private Crypto_PH.t * Raw_level_repr.t + type t = private Crypto_PH.t * Slot.Header.id val equal : t -> t -> bool diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.ml b/src/proto_alpha/lib_protocol/dal_slot_repr.ml index 3d656a3baf4c..7484378861ba 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.ml +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.ml @@ -291,33 +291,40 @@ module History = struct module Pointer_hash = struct module Crypto_PH = Blake2B.Make (Base58) (Pointer_prefix) - type t = Crypto_PH.t * Raw_level_repr.t + type t = Crypto_PH.t * Header.id let equal (c1, r1) (c2, r2) = - Crypto_PH.equal c1 c2 && Raw_level_repr.equal r1 r2 + Crypto_PH.equal c1 c2 && Header.slot_id_equal r1 r2 let compare (c1, r1) (c2, r2) = let c = Crypto_PH.compare c1 c2 in - if Compare.Int.(c <> 0) then c else Raw_level_repr.compare r1 r2 + if Compare.Int.(c <> 0) then c + else + let Header.{published_level; index} = r1 in + let c = + Raw_level_repr.compare published_level r2.Header.published_level + in + if Compare.Int.(c <> 0) then c + else Slot_index.compare index r2.Header.index - let encoding = Data_encoding.tup2 Crypto_PH.encoding Raw_level_repr.encoding + let encoding = Data_encoding.tup2 Crypto_PH.encoding Header.id_encoding let hash_bytes = Crypto_PH.hash_bytes let to_bytes (c, r) = + let Header.{published_level; index} = r in Bytes.concat Bytes.empty [ Crypto_PH.to_bytes c; - Raw_level_repr.to_int32 r |> Int32.to_string |> Bytes.of_string; + Raw_level_repr.to_int32 published_level + |> Int32.to_string |> Bytes.of_string; + Slot_index.to_int index |> Int32.of_int |> Int32.to_string + |> Bytes.of_string; ] let pp fmt (c, r) = - Format.fprintf fmt "(%a|%a)" Crypto_PH.pp c Raw_level_repr.pp r - - (* - let pp = Crypto_PH.pp - *) + Format.fprintf fmt "(%a|%a)" Crypto_PH.pp c Header.pp_id r end module Skip_list_parameters = struct @@ -556,14 +563,12 @@ module History = struct let hash cell : Pointer_hash.t = let current_slot = Skip_list.content cell in - let published_level = - (Content.content_id current_slot).Header.published_level - in + let slot_id = Content.content_id current_slot in let back_pointers_hashes = Skip_list.back_pointers cell in ( Data_encoding.Binary.to_bytes_exn Content.encoding current_slot :: List.map Pointer_hash.to_bytes back_pointers_hashes |> Pointer_hash.hash_bytes, - published_level ) + slot_id ) let pp_history fmt (history : history) = let history_hash = hash history in diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.mli b/src/proto_alpha/lib_protocol/dal_slot_repr.mli index b0347828a9eb..e5adb937f868 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.mli +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.mli @@ -220,7 +220,7 @@ module History : sig module Pointer_hash : sig module Crypto_PH : S.HASH - type t = private Crypto_PH.t * Raw_level_repr.t + type t = private Crypto_PH.t * Header.id val equal : t -> t -> bool -- GitLab From 785e53aee9a40cccfc43e3c06a4cd809ff6dec9c Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Wed, 9 Oct 2024 15:32:26 +0200 Subject: [PATCH 3/5] ADAL: disable old way of accessing skip list cells when doing refutations --- src/bin_dal_node/RPC_server.ml | 4 +++- src/lib_dal_node/dal_plugin.ml | 2 ++ src/lib_dal_node/dal_plugin.mli | 2 ++ src/proto_alpha/lib_dal/RPC_directory.ml | 2 ++ src/proto_alpha/lib_dal/RPC_directory.mli | 2 ++ src/proto_alpha/lib_dal/dal_plugin_registration.ml | 6 +++++- src/proto_alpha/lib_dal/dal_proto_client.ml | 2 ++ src/proto_alpha/lib_dal/dal_proto_client.mli | 2 ++ src/proto_alpha/lib_dal/dal_services.ml | 3 +++ src/proto_alpha/lib_dal/dal_services.mli | 2 ++ .../lib_sc_rollup_node/refutation_game_helpers.ml | 7 +++++-- 11 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/bin_dal_node/RPC_server.ml b/src/bin_dal_node/RPC_server.ml index a6d99d98acc6..a0f2f9bc144d 100644 --- a/src/bin_dal_node/RPC_server.ml +++ b/src/bin_dal_node/RPC_server.ml @@ -542,7 +542,8 @@ let register : Services.health (Health.get_health ctxt) -let register_plugin node_ctxt = +let register_plugin _node_ctxt = Tezos_rpc.Directory.empty +(* let open Lwt_syntax in Tezos_rpc.Directory.register_dynamic_directory Tezos_rpc.Directory.empty @@ -567,6 +568,7 @@ let register_plugin node_ctxt = Tezos_rpc.Directory.empty (Node_context.get_all_plugins node_ctxt) |> return) +*) let start configuration ctxt = let open Lwt_syntax in diff --git a/src/lib_dal_node/dal_plugin.ml b/src/lib_dal_node/dal_plugin.ml index b787d87db359..521f8606fc0a 100644 --- a/src/lib_dal_node/dal_plugin.ml +++ b/src/lib_dal_node/dal_plugin.ml @@ -170,9 +170,11 @@ module type T = sig (hash * cell) list tzresult Lwt.t end + (* module RPC : sig val directory : Skip_list_cells_store.t -> unit Tezos_rpc.Directory.t end + *) end let table : (module T) Protocol_hash.Table.t = Protocol_hash.Table.create 5 diff --git a/src/lib_dal_node/dal_plugin.mli b/src/lib_dal_node/dal_plugin.mli index df0422c7f255..83023e3a1e1a 100644 --- a/src/lib_dal_node/dal_plugin.mli +++ b/src/lib_dal_node/dal_plugin.mli @@ -155,10 +155,12 @@ module type T = sig (hash * cell) list tzresult Lwt.t end + (* module RPC : sig (** RPCs directory of the protocol-related part of the DAL node. *) val directory : Skip_list_cells_store.t -> unit Tezos_rpc.Directory.t end + *) end val register : (module T) -> unit diff --git a/src/proto_alpha/lib_dal/RPC_directory.ml b/src/proto_alpha/lib_dal/RPC_directory.ml index 11f377b4fd68..cdd2c8fddfd5 100644 --- a/src/proto_alpha/lib_dal/RPC_directory.ml +++ b/src/proto_alpha/lib_dal/RPC_directory.ml @@ -6,6 +6,7 @@ (* *) (*****************************************************************************) +(* module Dal_proto_services = Dal_services open Protocol @@ -38,3 +39,4 @@ let register_commitments_history ctxt directory = let directory (rpc_ctxt : Skip_list_cells_store.t) = register_commitments_history rpc_ctxt Tezos_rpc.Directory.empty +*) diff --git a/src/proto_alpha/lib_dal/RPC_directory.mli b/src/proto_alpha/lib_dal/RPC_directory.mli index 3e5940728daf..aa88084273d5 100644 --- a/src/proto_alpha/lib_dal/RPC_directory.mli +++ b/src/proto_alpha/lib_dal/RPC_directory.mli @@ -6,5 +6,7 @@ (* *) (*****************************************************************************) +(* (** The RPCs directory of the protocol part of DAL nodes. *) val directory : Skip_list_cells_store.t -> unit Environment.RPC_directory.t +*) diff --git a/src/proto_alpha/lib_dal/dal_plugin_registration.ml b/src/proto_alpha/lib_dal/dal_plugin_registration.ml index d7f414369271..3c1bc98785a0 100644 --- a/src/proto_alpha/lib_dal/dal_plugin_registration.ml +++ b/src/proto_alpha/lib_dal/dal_plugin_registration.ml @@ -299,13 +299,17 @@ module Plugin = struct let open Dal.Slots_history.History_cache in view cache |> Map.bindings in - return last_cells + (* ADAL/FIXME: don't index skip list cells anymore *) + ignore last_cells ; + return [] (* last_cells *) end + (* module RPC = struct let directory skip_list_cells_store = RPC_directory.directory skip_list_cells_store end + *) end let () = Dal_plugin.register (module Plugin) diff --git a/src/proto_alpha/lib_dal/dal_proto_client.ml b/src/proto_alpha/lib_dal/dal_proto_client.ml index 48ab4668bc55..bf74092e8ada 100644 --- a/src/proto_alpha/lib_dal/dal_proto_client.ml +++ b/src/proto_alpha/lib_dal/dal_proto_client.ml @@ -24,6 +24,7 @@ (* *) (*****************************************************************************) +(* type cctxt = Dal_node_client.cctxt let get_commitments_history_hash_content (cctxt : cctxt) hash = @@ -35,3 +36,4 @@ let get_commitments_history_hash_content (cctxt : cctxt) hash = ((), hash) () () +*) diff --git a/src/proto_alpha/lib_dal/dal_proto_client.mli b/src/proto_alpha/lib_dal/dal_proto_client.mli index 96c2cd4ffe42..a2493a7c7e16 100644 --- a/src/proto_alpha/lib_dal/dal_proto_client.mli +++ b/src/proto_alpha/lib_dal/dal_proto_client.mli @@ -24,7 +24,9 @@ (* *) (*****************************************************************************) +(* val get_commitments_history_hash_content : Dal_node_client.cctxt -> Dal_plugin_registration.Plugin.Skip_list.hash -> Dal_plugin_registration.Plugin.Skip_list.cell tzresult Lwt.t +*) diff --git a/src/proto_alpha/lib_dal/dal_services.ml b/src/proto_alpha/lib_dal/dal_services.ml index 0fae899bf1ca..23dc6f21c7bc 100644 --- a/src/proto_alpha/lib_dal/dal_services.ml +++ b/src/proto_alpha/lib_dal/dal_services.ml @@ -6,6 +6,7 @@ (* *) (*****************************************************************************) +(* open Protocol.Alpha_context open Tezos_rpc @@ -21,6 +22,7 @@ type 'rpc service = ; output : 'output > module Commitments_history = struct + let cell_hash_arg : Dal.Slots_history.Pointer_hash.t Arg.t = Arg.make ~descr:"The hash of a DAL skip list cell" @@ -49,3 +51,4 @@ module Commitments_history = struct / Protocol_hash.to_b58check Protocol.hash / "commitments_history" / "hash" /: cell_hash_arg) end + *) diff --git a/src/proto_alpha/lib_dal/dal_services.mli b/src/proto_alpha/lib_dal/dal_services.mli index 61f8ae00466c..eecdeb013641 100644 --- a/src/proto_alpha/lib_dal/dal_services.mli +++ b/src/proto_alpha/lib_dal/dal_services.mli @@ -6,6 +6,7 @@ (* *) (*****************************************************************************) +(* open Protocol.Alpha_context type 'rpc service = @@ -30,3 +31,4 @@ module Commitments_history : sig ; query : unit > service end +*) 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 58d2341083bc..87d664cc79a7 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 @@ -169,14 +169,17 @@ let generate_proof (node_ctxt : _ Node_context.t) (* Similarly to what's done for inbox snapshot above. *) Sc_rollup_proto_types.Dal.Slot_history.of_octez game.dal_snapshot in - let get_cell_from_hash hash = + let get_cell_from_hash _hash = (* each time a DAL skip list cell is requested by hash, we retrieve it from the DAL node. *) (* TODO: We may want to have a local cache here. *) let open Lwt_syntax in let dal_cctxt = node_ctxt.dal_cctxt in - let dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in + let _dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in let+ res = + assert false + (* Dal_proto_client.get_commitments_history_hash_content dal_cctxt hash + *) in Result.to_option res in -- GitLab From 8900fd8b9293600faf95817f80e5343d66767cdc Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Wed, 9 Oct 2024 16:53:23 +0200 Subject: [PATCH 4/5] ADAL/Proto: remember skip list cells instead of (just) seen slot headers --- .../lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/dal_slot_repr.ml | 1 + .../lib_protocol/dal_slot_repr.mli | 2 +- .../lib_protocol/dal_slot_storage.ml | 11 ++- src/proto_alpha/lib_protocol/storage.ml | 18 +++++ src/proto_alpha/lib_protocol/storage.mli | 7 ++ .../refutation_game_helpers.ml | 75 ++++++++++++++++--- 7 files changed, 103 insertions(+), 13 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 11845c63db31..9c04d8d00d7d 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2911,7 +2911,7 @@ module Dal : sig module Pointer_hash : sig module Crypto_PH : S.HASH - type t = private Crypto_PH.t * Slot.Header.id + type t = Crypto_PH.t * Slot.Header.id val equal : t -> t -> bool diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.ml b/src/proto_alpha/lib_protocol/dal_slot_repr.ml index 7484378861ba..0d5a505c9dab 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.ml +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.ml @@ -63,6 +63,7 @@ module Header = struct type t = {id : id; commitment : Commitment.t} + (* We should probably export it. Maybe we should have a module Header_id. *) let slot_id_equal {published_level; index} s2 = Raw_level_repr.equal published_level s2.published_level && Dal_slot_index_repr.equal index s2.index diff --git a/src/proto_alpha/lib_protocol/dal_slot_repr.mli b/src/proto_alpha/lib_protocol/dal_slot_repr.mli index e5adb937f868..0e5cb75af37b 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_repr.mli +++ b/src/proto_alpha/lib_protocol/dal_slot_repr.mli @@ -220,7 +220,7 @@ module History : sig module Pointer_hash : sig module Crypto_PH : S.HASH - type t = private Crypto_PH.t * Header.id + type t = Crypto_PH.t * Header.id val equal : t -> t -> bool diff --git a/src/proto_alpha/lib_protocol/dal_slot_storage.ml b/src/proto_alpha/lib_protocol/dal_slot_storage.ml index 079a05ba7d5c..99970dcf0643 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_storage.ml +++ b/src/proto_alpha/lib_protocol/dal_slot_storage.ml @@ -65,15 +65,22 @@ let get_slot_headers_history ctxt = let update_skip_list ctxt ~seen_slot_headers ~level_attested ~number_of_slots = let open Lwt_result_syntax in let* slots_history = get_slot_headers_history ctxt in - let*? slots_history = - Dal_slot_repr.History.add_confirmed_slot_headers_no_cache + let*? slots_history, cache = + Dal_slot_repr.History.add_confirmed_slot_headers ~number_of_slots slots_history + (Dal_slot_repr.History.History_cache.empty ~capacity:Int64.max_int) level_attested ~seen_slot_headers in + let last_cells = + let open Dal_slot_repr.History.History_cache in + view cache |> Map.bindings + in let*! ctxt = Storage.Dal.Slot.History.add ctxt slots_history in let*! ctxt = Storage.Dal.Slot.Headers_status.add ctxt seen_slot_headers in + (* With this addition of LevelsHistories, Headers_status becomes useless. *) + let*! ctxt = Storage.Dal.Slot.LevelHistories.add ctxt last_cells in return ctxt let finalize_pending_slot_headers ctxt ~number_of_slots = diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 101b8b95ef42..865158cb1d03 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -2210,6 +2210,24 @@ module Dal = struct let encoding = Dal_slot_repr.History.encoding end) + + module LevelHistories = + Make_single_data_storage (Registered) (Raw_context) + (struct + let name = ["level_slot_headers_histories"] + end) + (struct + type t = + (Dal_slot_repr.History.Pointer_hash.t * Dal_slot_repr.History.t) + list + + let encoding = + let open Data_encoding in + list + (tup2 + Dal_slot_repr.History.Pointer_hash.encoding + Dal_slot_repr.History.encoding) + end) end end diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 6e327d780943..ed6ea216019b 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -1027,6 +1027,13 @@ module Dal : sig Single_data_storage with type t := Raw_context.t and type value = Dal_slot_repr.History.t + + (** This is a permanent storage for of the "current" level. *) + module LevelHistories : + Single_data_storage + with type t := Raw_context.t + and type value = + (Dal_slot_repr.History.Pointer_hash.t * Dal_slot_repr.History.t) list end end 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 87d664cc79a7..e884915811c5 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 @@ -169,19 +169,75 @@ let generate_proof (node_ctxt : _ Node_context.t) (* Similarly to what's done for inbox snapshot above. *) Sc_rollup_proto_types.Dal.Slot_history.of_octez game.dal_snapshot in - let get_cell_from_hash _hash = + let get_cell_from_hash ~dal_attestation_lag + ((_hash, (header_id : Dal.Slot.Header.id)) : Dal.Slots_history.hash) = (* each time a DAL skip list cell is requested by hash, we retrieve it from the DAL node. *) (* TODO: We may want to have a local cache here. *) let open Lwt_syntax in - let dal_cctxt = node_ctxt.dal_cctxt in - let _dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in - let+ res = - assert false - (* + (* + let attested_level = block_info.header.shell.level in + let published_level = + Int32.sub + attested_level + (Int32.of_int dal_constants.Dal_plugin.attestation_lag) + in + (* 1. There are no cells for [published_level = 0]. *) + if published_level <= 0l then return [] + else + (* ADAL/TODO: Remove th dead code related to _feature_enable. *) + let* _feature_enable, prev_number_of_slots = + if published_level = 1l then + (* For this level, cannot retrieve the constants (as [pred + publication_level = 0]), but dummy values will suffice. *) + return (false, 0) + else + let* prev_constants = + Lazy.force pred_publication_level_dal_constants + in + return + ( prev_constants.Dal_plugin.feature_enable, + prev_constants.number_of_slots ) + in + let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in + + *) + let attested_level = + Int32.add + (Int32.of_int dal_attestation_lag) + (Raw_level.to_int32 header_id.Dal.Slot.Header.published_level) + in + let* slot_headers_status = + let cctxt = node_ctxt.cctxt in + Plugin.RPC.Dal.dal_published_slot_headers_status + (new Protocol_client_context.wrap_full cctxt) + (cctxt#chain, `Level attested_level) + () + in + let slot_id_equal Dal.Slot.Header.{published_level; index} s2 = + Raw_level.equal published_level s2.Dal.Slot.Header.published_level + && Dal.Slot_index.equal index s2.Dal.Slot.Header.index + in + match slot_headers_status with + | Error _ -> Lwt.return_none + | Ok l -> ( + let l = + List.filter + (fun (header, _) -> + slot_id_equal header.Dal.Slot.Header.id header_id) + l + in + match l with + | [] | _ :: _ :: _ -> assert false (* Not reachable *) + | [(_header, _header_info)] -> + let dal_cctxt = node_ctxt.dal_cctxt in + let _dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in + let+ res = + assert false + (* Dal_proto_client.get_commitments_history_hash_content dal_cctxt hash *) - in - Result.to_option res + in + Result.to_option res) in (* We fetch the value of protocol constants at block snapshot level where the game started. *) @@ -268,7 +324,8 @@ let generate_proof (node_ctxt : _ Node_context.t) module Dal_with_history = struct let confirmed_slots_history = dal_slots_history - let get_history = get_cell_from_hash + let get_history x : Dal.Slots_history.t option Lwt.t = + get_cell_from_hash ~dal_attestation_lag x let dal_attestation_lag = dal_attestation_lag -- GitLab From fe9c28d6c928eb1fcefe94a9d1303e0b0fdc1997 Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Wed, 9 Oct 2024 19:38:59 +0200 Subject: [PATCH 5/5] ADAL: Get and use DAL skip list cells from L1 contextes to perform refutations --- src/proto_alpha/lib_plugin/RPC.ml | 27 +++++++- .../lib_protocol/alpha_context.mli | 5 ++ .../lib_protocol/dal_slot_storage.ml | 2 + .../lib_protocol/dal_slot_storage.mli | 3 + .../refutation_game_helpers.ml | 69 +++++++------------ 5 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/proto_alpha/lib_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index ac2f1da05654..e8ee0206e1a1 100644 --- a/src/proto_alpha/lib_plugin/RPC.ml +++ b/src/proto_alpha/lib_plugin/RPC.ml @@ -3044,6 +3044,20 @@ module Dal = struct ~query:RPC_query.empty ~output RPC_path.(path / "published_slot_headers_status") + + let history_cells_of_level = + let output = + Data_encoding.( + list + (tup2 + Dal.Slots_history.Pointer_hash.encoding + Dal.Slots_history.encoding)) + in + RPC_service.get_service + ~description:"Get cells of the skip list of this block" + ~query:RPC_query.empty + ~output + RPC_path.(path / "history_cells_of_level") end let register_dal_commitments_history () = @@ -3092,6 +3106,9 @@ module Dal = struct let dal_published_slot_headers_status ctxt block () = RPC_context.make_call0 S.published_slot_headers_status ctxt block () () + let dal_history_cells_of_level ctxt block () = + RPC_context.make_call0 S.history_cells_of_level ctxt block () () + let register_published_slot_headers () = let open Lwt_result_syntax in Registration.register0 ~chunked:true S.published_slot_headers @@ -3111,11 +3128,19 @@ module Dal = struct let* result = Dal.Slots_storage.find_slot_headers_status ctxt in match result with Some l -> return l | None -> return [] + let register_history_cells_of_level () = + let open Lwt_result_syntax in + Registration.register0 ~chunked:true S.history_cells_of_level + @@ fun ctxt () () -> + let* result = Dal.Slots_storage.find_history_cells ctxt in + match result with Some l -> return l | None -> return [] + let register () = register_dal_commitments_history () ; register_shards () ; register_published_slot_headers () ; - register_published_slot_headers_status () + register_published_slot_headers_status () ; + register_history_cells_of_level () end module Forge = struct diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 9c04d8d00d7d..0cc25e1e9da5 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2964,6 +2964,11 @@ module Dal : sig val find_slot_headers_status : t -> (Slot.Header.t * (bool * int * int)) list option tzresult Lwt.t + + val find_history_cells : + context -> + (Slots_history.Pointer_hash.t * Slots_history.t) list option tzresult + Lwt.t end end diff --git a/src/proto_alpha/lib_protocol/dal_slot_storage.ml b/src/proto_alpha/lib_protocol/dal_slot_storage.ml index 99970dcf0643..c75b97892f50 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_storage.ml +++ b/src/proto_alpha/lib_protocol/dal_slot_storage.ml @@ -27,6 +27,8 @@ let find_slot_headers ctxt level = Storage.Dal.Slot.Headers.find ctxt level let find_slot_headers_status ctxt = Storage.Dal.Slot.Headers_status.find ctxt +let find_history_cells ctxt = Storage.Dal.Slot.LevelHistories.find ctxt + let finalize_current_slot_headers ctxt = Storage.Dal.Slot.Headers.add ctxt diff --git a/src/proto_alpha/lib_protocol/dal_slot_storage.mli b/src/proto_alpha/lib_protocol/dal_slot_storage.mli index a7bbd9fb9ccf..343bc33f3256 100644 --- a/src/proto_alpha/lib_protocol/dal_slot_storage.mli +++ b/src/proto_alpha/lib_protocol/dal_slot_storage.mli @@ -57,6 +57,9 @@ val find_slot_headers_status : Raw_context.t -> (Dal_slot_repr.Header.t * (bool * int * int)) list option tzresult Lwt.t +val find_history_cells : + Raw_context.t -> Storage.Dal.Slot.LevelHistories.value option tzresult Lwt.t + (** [finalize_current_slot_headers ctxt] finalizes the current slot headers posted on this block and marks them as pending into the context. *) 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 e884915811c5..9e5496cf62f5 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 @@ -170,45 +170,19 @@ let generate_proof (node_ctxt : _ Node_context.t) Sc_rollup_proto_types.Dal.Slot_history.of_octez game.dal_snapshot in let get_cell_from_hash ~dal_attestation_lag - ((_hash, (header_id : Dal.Slot.Header.id)) : Dal.Slots_history.hash) = - (* each time a DAL skip list cell is requested by hash, we retrieve it from the DAL node. *) + ((hash, (header_id : Dal.Slot.Header.id)) : Dal.Slots_history.hash) = + (* TODO: update this comment -> each time a DAL skip list cell is requested + by hash, we retrieve it from the DAL node. *) (* TODO: We may want to have a local cache here. *) let open Lwt_syntax in - (* - let attested_level = block_info.header.shell.level in - let published_level = - Int32.sub - attested_level - (Int32.of_int dal_constants.Dal_plugin.attestation_lag) - in - (* 1. There are no cells for [published_level = 0]. *) - if published_level <= 0l then return [] - else - (* ADAL/TODO: Remove th dead code related to _feature_enable. *) - let* _feature_enable, prev_number_of_slots = - if published_level = 1l then - (* For this level, cannot retrieve the constants (as [pred - publication_level = 0]), but dummy values will suffice. *) - return (false, 0) - else - let* prev_constants = - Lazy.force pred_publication_level_dal_constants - in - return - ( prev_constants.Dal_plugin.feature_enable, - prev_constants.number_of_slots ) - in - let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in - - *) let attested_level = Int32.add (Int32.of_int dal_attestation_lag) (Raw_level.to_int32 header_id.Dal.Slot.Header.published_level) in - let* slot_headers_status = + let* cells_of_level = let cctxt = node_ctxt.cctxt in - Plugin.RPC.Dal.dal_published_slot_headers_status + Plugin.RPC.Dal.dal_history_cells_of_level (new Protocol_client_context.wrap_full cctxt) (cctxt#chain, `Level attested_level) () @@ -217,27 +191,30 @@ let generate_proof (node_ctxt : _ Node_context.t) Raw_level.equal published_level s2.Dal.Slot.Header.published_level && Dal.Slot_index.equal index s2.Dal.Slot.Header.index in - match slot_headers_status with + match cells_of_level with | Error _ -> Lwt.return_none | Ok l -> ( let l = - List.filter - (fun (header, _) -> - slot_id_equal header.Dal.Slot.Header.id header_id) + List.filter_map + (fun ((cell_hash, cell_slot_id), cell) -> + if + slot_id_equal cell_slot_id header_id + && Dal.Slots_history.Pointer_hash.Crypto_PH.equal cell_hash hash + then Some cell + else None) l in match l with - | [] | _ :: _ :: _ -> assert false (* Not reachable *) - | [(_header, _header_info)] -> - let dal_cctxt = node_ctxt.dal_cctxt in - let _dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in - let+ res = - assert false - (* - Dal_proto_client.get_commitments_history_hash_content dal_cctxt hash - *) - in - Result.to_option res) + | [] | _ :: _ :: _ -> assert false (* Should not be reachable *) + | [cell] -> + let Dal.Slot.Header.{published_level; index} = header_id in + Format.eprintf + "#### Got cell of slot (%a, %a) from L1 node ...@." + Raw_level.pp + published_level + Dal.Slot_index.pp + index ; + return_some cell) in (* We fetch the value of protocol constants at block snapshot level where the game started. *) -- GitLab