diff --git a/src/bin_dal_node/RPC_server.ml b/src/bin_dal_node/RPC_server.ml index a6d99d98acc604e0dff996ec7040dd64f5d51d8e..a0f2f9bc144dd1fae4e653a7709653c1d0c776d8 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 b787d87db35956a5e64f626bfe4281e38d11c2fe..521f8606fc0afdf886b2b85f9dbce03c2938d7a8 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 df0422c7f255cb174f7eae8a75d93b11b19befd2..83023e3a1e1a11f02fabb0dfbfa170e13c57379d 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 11f377b4fd688ee3f2e4708d8b72e81c0f32abbd..cdd2c8fddfd5ad903222c783b270ac8d3e84bf57 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 3e5940728daf092afc79ceb7d72256e43ff20e61..aa88084273d5d46e5351ceb86f4b3a248429db93 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 d7f414369271f790f688ea94c98c13e12d3995a0..3c1bc98785a00520ac1ed07046aa0d42a41c99cc 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 48ab4668bc55eed9f4684e3b718e6489e524b8bb..bf74092e8adaba86e9359ed6924c090fe41ad82d 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 96c2cd4ffe428f412839ea5ad06843ebc3d848cd..a2493a7c7e16268b1796156007956ee9616d989e 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 0fae899bf1cab8543721aa5faaa6938a7e0cdd70..23dc6f21c7bc8a25431a9bd9358a0834a54a94db 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 61f8ae00466c1b3d24da4a8cd6513d7777ecc8a5..eecdeb013641594ce7060865f17041de3eacaf41 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_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index ac2f1da05654fdea9f6cc31f00601c72bedb59e2..e8ee0206e1a1329b63ffb56abcf36338c8b1d0bb 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 361147f9d7452962d0fa6af5a160686923b01bbb..0cc25e1e9da5d20a8deee369ffe90ac19ddacddd 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 = Crypto_PH.t * Slot.Header.id + + 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 @@ -2948,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_repr.ml b/src/proto_alpha/lib_protocol/dal_slot_repr.ml index 1062bdade2a2c4898200c2f12cbf81157fcd6857..0d5a505c9dabfb86061e23c3f89bec881742c8c9 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 @@ -288,7 +289,44 @@ 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 * Header.id + + let equal (c1, r1) (c2, 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 + 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 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 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 Header.pp_id r + end module Skip_list_parameters = struct let basis = 4 @@ -524,12 +562,14 @@ 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 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 + ( Data_encoding.Binary.to_bytes_exn Content.encoding current_slot + :: List.map Pointer_hash.to_bytes back_pointers_hashes + |> Pointer_hash.hash_bytes, + 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 970dc4ea9018ed11f56ebe81cf96d01a84885237..0e5cb75af37b90e1bac495840179591437eceb99 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 = Crypto_PH.t * Header.id + + 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 diff --git a/src/proto_alpha/lib_protocol/dal_slot_storage.ml b/src/proto_alpha/lib_protocol/dal_slot_storage.ml index 079a05ba7d5c9dd642f964c0cac01ba75c6978c2..c75b97892f509b892b8ac4f66c44365c7742f79e 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 @@ -65,15 +67,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/dal_slot_storage.mli b/src/proto_alpha/lib_protocol/dal_slot_storage.mli index a7bbd9fb9ccf08382da64e2757ad96fa1e611228..343bc33f325608693eaf49132306f00898b0a90e 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_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 101b8b95ef42a6d130e8eae611620ba4ab65ec83..865158cb1d0316162308e564ca48d353ebb5b84a 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 6e327d7809430c403fd1c047323b4c67995745d1..ed6ea216019b8700fd95f20e4eaa0d3b57e34e3f 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 58d2341083bc7269011498b0d54c4d6eeacb605a..9e5496cf62f5b0b797f5730a56b2ea3bf42f8f1b 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,16 +169,52 @@ 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 = - (* each time a DAL skip list cell is requested by hash, we retrieve it from the DAL node. *) + let get_cell_from_hash ~dal_attestation_lag + ((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 dal_cctxt = node_ctxt.dal_cctxt in - let dal_cctxt = WithExceptions.Option.get ~loc:__LOC__ dal_cctxt in - let+ res = - Dal_proto_client.get_commitments_history_hash_content dal_cctxt hash + let attested_level = + Int32.add + (Int32.of_int dal_attestation_lag) + (Raw_level.to_int32 header_id.Dal.Slot.Header.published_level) in - Result.to_option res + let* cells_of_level = + let cctxt = node_ctxt.cctxt in + Plugin.RPC.Dal.dal_history_cells_of_level + (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 cells_of_level with + | Error _ -> Lwt.return_none + | Ok l -> ( + let l = + 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 (* 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. *) @@ -265,7 +301,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