diff --git a/src/lib_dal_node/block_handler.ml b/src/lib_dal_node/block_handler.ml index e935a5a246669250785cdf8d40bbf3d4ffc0cf31..5774c872ebb4b2a87faeb1a72cf95c53fe150e0b 100644 --- a/src/lib_dal_node/block_handler.ml +++ b/src/lib_dal_node/block_handler.ml @@ -159,18 +159,17 @@ let may_update_topics ctxt proto_parameters ~block_level = (Node_context.get_gs_worker ctxt) committee -let store_skip_list_cells (type block_info) ctxt cctxt dal_constants - (block_info : block_info) block_level - (module Plugin : Dal_plugin.T with type block_info = block_info) = +let store_skip_list_cells ctxt cctxt dal_constants ~attested_level + (module Plugin : Dal_plugin.T) = let open Lwt_result_syntax in let* cells_of_level = let pred_published_level = Int32.sub - block_level + attested_level (Int32.of_int (1 + dal_constants.Types.attestation_lag)) in Plugin.Skip_list.cells_of_level - block_info + ~attested_level cctxt ~dal_constants ~pred_publication_level_dal_constants: @@ -192,7 +191,7 @@ let store_skip_list_cells (type block_info) ctxt cctxt dal_constants cells_of_level in let store = Node_context.get_store ctxt in - Store.Skip_list_cells.insert store ~attested_level:block_level cells_of_level + Store.Skip_list_cells.insert store ~attested_level cells_of_level (* This functions counts, for each slot, the number of shards attested by the bakers. *) let attested_shards_per_slot attestations committee ~number_of_slots is_attested @@ -364,7 +363,10 @@ let process_block_data ctxt cctxt store proto_parameters block_level (module Plugin : Dal_plugin.T) = let open Lwt_result_syntax in let* block_info = - Plugin.block_info cctxt ~block:(`Level block_level) ~metadata:`Never + Plugin.block_info + cctxt + ~block:(`Level block_level) + ~operations_metadata:`Never in let* () = if Node_context.supports_refutations ctxt then @@ -372,9 +374,8 @@ let process_block_data ctxt cctxt store proto_parameters block_level ctxt cctxt proto_parameters - block_info - block_level - (module Plugin : Dal_plugin.T with type block_info = Plugin.block_info) + ~attested_level:block_level + (module Plugin : Dal_plugin.T) else return_unit in let* slot_headers = Plugin.get_published_slot_headers ~block_level cctxt in diff --git a/src/lib_dal_node/block_handler.mli b/src/lib_dal_node/block_handler.mli index ad25fb66fceac6a8df4f8ef1312fdc9aabb8f319..701cfb46eea63e0badd50d9c74aa59b3b41d69d2 100644 --- a/src/lib_dal_node/block_handler.mli +++ b/src/lib_dal_node/block_handler.mli @@ -48,17 +48,16 @@ val new_finalized_head : launch_time:float -> (unit, tztrace) result Lwt.t -(** [store_skip_list_cells ctxt cctxt proto_parameters block_info block_level - plugin] extracts and stores the skip list cells from [block_info] at - [block_level], using the encoding from the corresponding [plugin]. It is - used to support DAL refutation. *) +(** [store_skip_list_cells ctxt cctxt proto_parameters ~attested_level plugin] + extracts and stores the skip list cells from [block_info] at [block_level], + using the encoding from the corresponding [plugin]. It is used to support + DAL refutation. *) val store_skip_list_cells : Node_context.t -> Rpc_context.t -> Tezos_dal_node_services.Types.proto_parameters -> - 'block_info -> - int32 -> - (module Dal_plugin.T with type block_info = 'block_info) -> + attested_level:int32 -> + (module Dal_plugin.T) -> (unit, tztrace) result Lwt.t (** [remove_old_level_stored_data proto_parameters ctxt current_level] diff --git a/src/lib_dal_node/dal_plugin.ml b/src/lib_dal_node/dal_plugin.ml index 5d1f8447c8c775c60fe56fb238d0b5113db84d79..d94db28525748326e6b812b3ec71542266d8a542 100644 --- a/src/lib_dal_node/dal_plugin.ml +++ b/src/lib_dal_node/dal_plugin.ml @@ -47,7 +47,7 @@ module type T = sig val block_info : ?chain:Tezos_shell_services.Block_services.chain -> ?block:Tezos_shell_services.Block_services.block -> - metadata:[`Always | `Never] -> + operations_metadata:[`Always | `Never] -> Tezos_rpc.Context.generic -> block_info tzresult Lwt.t @@ -121,7 +121,7 @@ module type T = sig val cell_hash : cell -> hash val cells_of_level : - block_info -> + attested_level:int32 -> Tezos_rpc.Context.generic -> dal_constants:Tezos_dal_node_services.Types.proto_parameters -> pred_publication_level_dal_constants: diff --git a/src/lib_dal_node/dal_plugin.mli b/src/lib_dal_node/dal_plugin.mli index dcaf9662aa6f10a9593a55235bdff8754e0c3309..ed2d3a4523d5d34ecd03a4fa859cfd5f9f63d898 100644 --- a/src/lib_dal_node/dal_plugin.mli +++ b/src/lib_dal_node/dal_plugin.mli @@ -53,14 +53,15 @@ module type T = sig type tb_slot - (** [block_info ?chain ?block ~metadata ctxt] returns the information of the - [block] in [ctxt] for the given [chain]. Block's metadata are included or - skipped depending on the value of [metadata]. This is a wrapper on top of + (** [block_info ?chain ?block ~operations_metadata ctxt] returns the + information of the [block] in [ctxt] for the given [chain]. Operations' + metadata are included or skipped depending on the value of + [operations_metadata]. This is a wrapper on top of {!Protocol_client_context.Alpha_block_services.info}. *) val block_info : ?chain:Tezos_shell_services.Block_services.chain -> ?block:Tezos_shell_services.Block_services.block -> - metadata:[`Always | `Never] -> + operations_metadata:[`Always | `Never] -> Tezos_rpc.Context.generic -> block_info tzresult Lwt.t @@ -172,7 +173,7 @@ module type T = sig The ordering of the elements in the returned list is not relevant. *) val cells_of_level : - block_info -> + attested_level:int32 -> Tezos_rpc.Context.generic -> dal_constants:Tezos_dal_node_services.Types.proto_parameters -> pred_publication_level_dal_constants: diff --git a/src/lib_dal_node/dal_store_sqlite3.ml b/src/lib_dal_node/dal_store_sqlite3.ml index 5d526f5127017b03f0d1cff8ea8e09f301e7660b..fbb018da624ea3036c51558a8dee99f14230bdc2 100644 --- a/src/lib_dal_node/dal_store_sqlite3.ml +++ b/src/lib_dal_node/dal_store_sqlite3.ml @@ -163,14 +163,28 @@ module Skip_list_cells = struct open Dal_proto_types open Tezos_dal_node_services.Types - let find : (Skip_list_hash.t, Skip_list_cell.t, [`One]) Caqti_request.t = - (skip_list_hash ->! skip_list_cell) + let find_opt : + (Skip_list_hash.t, Skip_list_cell.t, [`Zero | `One]) Caqti_request.t = + (skip_list_hash ->? skip_list_cell) @@ {sql| SELECT cell FROM skip_list_cells WHERE hash = $1 |sql} + let find_by_slot_id_opt : + (level * slot_index, Skip_list_cell.t, [`One | `Zero]) Caqti_request.t = + let open Caqti_type.Std in + (t2 attested_level dal_slot_index ->? skip_list_cell) + @@ {sql| + SELECT cell + FROM skip_list_cells + WHERE hash = ( + SELECT skip_list_cell_hash + FROM skip_list_slots + WHERE attested_level = $1 AND slot_index = $2 + )|sql} + let insert_skip_list_slot : (level * slot_index * Skip_list_hash.t, unit, [`Zero]) Caqti_request.t = (t3 attested_level dal_slot_index skip_list_hash ->. unit) @@ -209,9 +223,13 @@ module Skip_list_cells = struct |sql} end - let find ?conn store skip_list_hash = + let find_opt ?conn store skip_list_hash = + with_connection store conn @@ fun conn -> + Sqlite.Db.find_opt conn Q.find_opt skip_list_hash + + let find_by_slot_id_opt ?conn store ~attested_level ~slot_index = with_connection store conn @@ fun conn -> - Sqlite.Db.find conn Q.find skip_list_hash + Sqlite.Db.find_opt conn Q.find_by_slot_id_opt (attested_level, slot_index) let remove ?conn store ~attested_level = let open Lwt_result_syntax in diff --git a/src/lib_dal_node/dal_store_sqlite3.mli b/src/lib_dal_node/dal_store_sqlite3.mli index 1eac89215448361ecca5b7b13676e4fe2f5840a4..b24ec12bac895b75eb25b802bc87f3fe78b19c1c 100644 --- a/src/lib_dal_node/dal_store_sqlite3.mli +++ b/src/lib_dal_node/dal_store_sqlite3.mli @@ -43,11 +43,24 @@ module Skip_list_cells : sig (** [use t k] executes [k] with a fresh connection to [t]. *) val use : t -> (conn -> 'a tzresult Lwt.t) -> 'a tzresult Lwt.t - (** [find ?conn store hash] returns the cell associated to [hash] in + (** [find_opt ?conn store hash] returns the cell associated to [hash] in the [store], if any. Uses the [conn] if provided (defaults to [None]). *) - val find : - ?conn:conn -> t -> Skip_list_hash.t -> Skip_list_cell.t tzresult Lwt.t + val find_opt : + ?conn:conn -> + t -> + Skip_list_hash.t -> + Skip_list_cell.t option tzresult Lwt.t + + (** [find_by_slot_id_opt ?conn store ~attested_level ~slot_index] returns the cell + associated to ([attested_level], [slot_index]) in the [store], if + any. Uses the [conn] if provided (defaults to [None]). *) + val find_by_slot_id_opt : + ?conn:conn -> + t -> + attested_level:int32 -> + slot_index:int -> + Skip_list_cell.t option tzresult Lwt.t (** [insert ?conn store ~attested_level values] inserts the given list of [values] associated to the given [attested_level] in the diff --git a/src/lib_dal_node/store.ml b/src/lib_dal_node/store.ml index 7b8663fee64982104805f3e1cf6668d529e1d589..68a82000d6bcfc251bf74c11e26625dc49442ff9 100644 --- a/src/lib_dal_node/store.ml +++ b/src/lib_dal_node/store.ml @@ -663,12 +663,19 @@ let init_sqlite_skip_list_cells_store ?(perm = `Read_write) data_dir = () module Skip_list_cells = struct - let find ?conn t skip_list_hash = - Dal_store_sqlite3.Skip_list_cells.find + let find_opt ?conn t skip_list_hash = + Dal_store_sqlite3.Skip_list_cells.find_opt ?conn t.skip_list_cells_store skip_list_hash + let find_by_slot_id_opt ?conn t ~attested_level ~slot_index = + Dal_store_sqlite3.Skip_list_cells.find_by_slot_id_opt + ?conn + t.skip_list_cells_store + ~attested_level + ~slot_index + let insert ?conn t ~attested_level items = Dal_store_sqlite3.Skip_list_cells.insert ?conn diff --git a/src/lib_dal_node/store.mli b/src/lib_dal_node/store.mli index 3ed576a6c97d9733b7f006544b8178e91fda5b72..9ca17457fd63719370f6b5a81504f8bb2c54982c 100644 --- a/src/lib_dal_node/store.mli +++ b/src/lib_dal_node/store.mli @@ -234,13 +234,23 @@ val add_slot_headers : module Skip_list_cells : sig open Dal_proto_types - (** [find ?conn store hash] returns the cell associated to [hash] in the [store], if - any. *) - val find : + (** [find_opt ?conn store hash] returns the cell associated to [hash] in the + [store], if any. *) + val find_opt : ?conn:Dal_store_sqlite3.conn -> t -> Skip_list_hash.t -> - Skip_list_cell.t tzresult Lwt.t + Skip_list_cell.t option tzresult Lwt.t + + (** [find_by_slot_id_opt ?conn store ~attested_level ~slot_index] returns the + cell associated to ([attested_level], [slot_index]) in the [store], if + any. *) + val find_by_slot_id_opt : + ?conn:Sqlite.conn -> + t -> + attested_level:int32 -> + slot_index:int -> + Dal_proto_types.Skip_list_cell.t option tzresult Lwt.t (** [insert ?conn store ~attested_level values] inserts the given list of [values] associated to the given [attested_level] in the [store]. Any existing value diff --git a/src/lib_dal_node/store_cleanup.ml b/src/lib_dal_node/store_cleanup.ml index a3e3f9b76de52cdb5544b19c38fe6756a24ffee7..2fcd0cc769dfcf3a9a51619c2669cb5dc084f6cb 100644 --- a/src/lib_dal_node/store_cleanup.ml +++ b/src/lib_dal_node/store_cleanup.ml @@ -46,9 +46,6 @@ let clean_up_store_and_catch_up_for_refutation_support ctxt cctxt let*? (module Plugin) = Node_context.get_plugin_for_level ctxt ~level:(Int32.pred level) in - let* block_info = - Plugin.block_info cctxt ~block:(`Level level) ~metadata:`Always - in let*? dal_constants = Node_context.get_proto_parameters ctxt ~level:(`Level level) in @@ -56,9 +53,8 @@ let clean_up_store_and_catch_up_for_refutation_support ctxt cctxt ctxt cctxt dal_constants - block_info - level - (module Plugin : Dal_plugin.T with type block_info = Plugin.block_info) + ~attested_level:level + (module Plugin : Dal_plugin.T) in let store = Node_context.get_store ctxt in let last_processed_level_store = Store.last_processed_level store in diff --git a/src/lib_dal_node/test/test_storage.ml b/src/lib_dal_node/test/test_storage.ml index e645ac342b0c6155b9a735cc51d6dacf2659e4ed..c36f91ad3e8030d23e733bba030537219857240b 100644 --- a/src/lib_dal_node/test/test_storage.ml +++ b/src/lib_dal_node/test/test_storage.ml @@ -312,7 +312,7 @@ let perform_sqlite store action = | Action.Insert {attested_level; payload} -> insert store ~attested_level payload | Find {skip_list_hash} -> - let* _cell = find store skip_list_hash in + let* _cell = find_opt store skip_list_hash in return_unit | Remove {attested_level} -> remove store ~attested_level @@ -337,8 +337,10 @@ let handshake state kvs_store sql_store = (fun hash -> (* The hash must exist in the stores and the associated cell must be the same. *) let* kvs_cell = Kvs_skip_list_cells_store.find kvs_store hash in - let* sql_cell = Dal_store_sqlite3.Skip_list_cells.find sql_store hash in - assert (Skip_list_cell.equal kvs_cell sql_cell) ; + let* sql_cell = + Dal_store_sqlite3.Skip_list_cells.find_opt sql_store hash + in + assert (Option.equal Skip_list_cell.equal (Some kvs_cell) sql_cell) ; return_unit) must_exist_hashes in diff --git a/src/proto_021_PsQuebec/lib_dal/RPC_directory.ml b/src/proto_021_PsQuebec/lib_dal/RPC_directory.ml index 291f6b08e925393933cd9eb0e21ded255f9b9a97..fad35908a16a6d8c813bd50009914ff20ba3f617 100644 --- a/src/proto_021_PsQuebec/lib_dal/RPC_directory.ml +++ b/src/proto_021_PsQuebec/lib_dal/RPC_directory.ml @@ -17,11 +17,13 @@ module Skip_list_handlers = struct Alpha_context.Dal.Slots_history.Pointer_hash.encoding cell_hash in - let* cell = Dal_store_sqlite3.Skip_list_cells.find rpc_context hash in - return - @@ Dal_proto_types.Skip_list_cell.to_proto - Alpha_context.Dal.Slots_history.encoding - cell + let+ cell_opt = + Dal_store_sqlite3.Skip_list_cells.find_opt rpc_context hash + in + Option.map + (Dal_proto_types.Skip_list_cell.to_proto + Alpha_context.Dal.Slots_history.encoding) + cell_opt end let add_service registerer subst service handler directory = @@ -30,7 +32,7 @@ let add_service registerer subst service handler directory = let register_commitments_history ctxt directory = directory |> add_service - Tezos_rpc.Directory.register + Tezos_rpc.Directory.opt_register Tezos_rpc.Service.subst1 Dal_proto_services.Commitments_history.hash_content Skip_list_handlers.cell diff --git a/src/proto_021_PsQuebec/lib_dal/dal_plugin_registration.ml b/src/proto_021_PsQuebec/lib_dal/dal_plugin_registration.ml index 79de163f2bc99b27ea2dc89c988ce264ae46416e..1722b93976413bd723fb278ff92ca4928868427a 100644 --- a/src/proto_021_PsQuebec/lib_dal/dal_plugin_registration.ml +++ b/src/proto_021_PsQuebec/lib_dal/dal_plugin_registration.ml @@ -96,13 +96,13 @@ module Plugin = struct (* This is supposed to be dead code, but we implement a fallback to be defensive. *) fail [DAL_accusation_not_available] - let block_info ?chain ?block ~metadata ctxt = + let block_info ?chain ?block ~operations_metadata ctxt = let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in Protocol_client_context.Alpha_block_services.info cpctxt ?chain ?block - ~metadata + ~metadata:operations_metadata () let block_shell_header (block_info : block_info) = block_info.header.shell @@ -275,11 +275,18 @@ module Plugin = struct The ordering of the elements in the returned list is not relevant. *) - let cells_of_level (block_info : block_info) ctxt ~dal_constants + let cells_of_level ~attested_level ctxt ~dal_constants ~pred_publication_level_dal_constants = let open Lwt_result_syntax in - (* 0. Let's call [attested_level] the block's level. *) - let attested_level = block_info.header.shell.level in + (* 0. For Quebec, block_info is still needed to reconstruct the cells + of the skip list. Now that Rio is activated, we don't expect + [cells_of_level] to be actively called. *) + let* block_info = + block_info + ctxt + ~block:(`Level attested_level) + ~operations_metadata:`Never + in let published_level = Int32.sub attested_level diff --git a/src/proto_022_PsRiotum/lib_dal/RPC_directory.ml b/src/proto_022_PsRiotum/lib_dal/RPC_directory.ml index 291f6b08e925393933cd9eb0e21ded255f9b9a97..fad35908a16a6d8c813bd50009914ff20ba3f617 100644 --- a/src/proto_022_PsRiotum/lib_dal/RPC_directory.ml +++ b/src/proto_022_PsRiotum/lib_dal/RPC_directory.ml @@ -17,11 +17,13 @@ module Skip_list_handlers = struct Alpha_context.Dal.Slots_history.Pointer_hash.encoding cell_hash in - let* cell = Dal_store_sqlite3.Skip_list_cells.find rpc_context hash in - return - @@ Dal_proto_types.Skip_list_cell.to_proto - Alpha_context.Dal.Slots_history.encoding - cell + let+ cell_opt = + Dal_store_sqlite3.Skip_list_cells.find_opt rpc_context hash + in + Option.map + (Dal_proto_types.Skip_list_cell.to_proto + Alpha_context.Dal.Slots_history.encoding) + cell_opt end let add_service registerer subst service handler directory = @@ -30,7 +32,7 @@ let add_service registerer subst service handler directory = let register_commitments_history ctxt directory = directory |> add_service - Tezos_rpc.Directory.register + Tezos_rpc.Directory.opt_register Tezos_rpc.Service.subst1 Dal_proto_services.Commitments_history.hash_content Skip_list_handlers.cell diff --git a/src/proto_022_PsRiotum/lib_dal/dal_plugin_registration.ml b/src/proto_022_PsRiotum/lib_dal/dal_plugin_registration.ml index 818d80be104fe9023f0d2b8f93e3ed1b6ddcae8c..5e54ac3c12d0c73ac6b690a5008985d7990fc49c 100644 --- a/src/proto_022_PsRiotum/lib_dal/dal_plugin_registration.ml +++ b/src/proto_022_PsRiotum/lib_dal/dal_plugin_registration.ml @@ -135,13 +135,13 @@ module Plugin = struct let* _op_hash = Shell_services.Injection.operation cctxt ~chain bytes in return_unit - let block_info ?chain ?block ~metadata ctxt = + let block_info ?chain ?block ~operations_metadata ctxt = let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in Protocol_client_context.Alpha_block_services.info cpctxt ?chain ?block - ~metadata + ~metadata:operations_metadata () let block_shell_header (block_info : block_info) = block_info.header.shell @@ -301,11 +301,10 @@ module Plugin = struct cells of the skip list by calling the appropriate DAL function in the protocol. *) - let cells_of_level (block_info : block_info) ctxt ~dal_constants + let cells_of_level ~attested_level ctxt ~dal_constants ~pred_publication_level_dal_constants:_ = let open Lwt_result_syntax in let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in - let attested_level = block_info.header.shell.level in let published_level = Int32.sub attested_level diff --git a/src/proto_alpha/lib_dal/RPC_directory.ml b/src/proto_alpha/lib_dal/RPC_directory.ml index 291f6b08e925393933cd9eb0e21ded255f9b9a97..fad35908a16a6d8c813bd50009914ff20ba3f617 100644 --- a/src/proto_alpha/lib_dal/RPC_directory.ml +++ b/src/proto_alpha/lib_dal/RPC_directory.ml @@ -17,11 +17,13 @@ module Skip_list_handlers = struct Alpha_context.Dal.Slots_history.Pointer_hash.encoding cell_hash in - let* cell = Dal_store_sqlite3.Skip_list_cells.find rpc_context hash in - return - @@ Dal_proto_types.Skip_list_cell.to_proto - Alpha_context.Dal.Slots_history.encoding - cell + let+ cell_opt = + Dal_store_sqlite3.Skip_list_cells.find_opt rpc_context hash + in + Option.map + (Dal_proto_types.Skip_list_cell.to_proto + Alpha_context.Dal.Slots_history.encoding) + cell_opt end let add_service registerer subst service handler directory = @@ -30,7 +32,7 @@ let add_service registerer subst service handler directory = let register_commitments_history ctxt directory = directory |> add_service - Tezos_rpc.Directory.register + Tezos_rpc.Directory.opt_register Tezos_rpc.Service.subst1 Dal_proto_services.Commitments_history.hash_content Skip_list_handlers.cell diff --git a/src/proto_alpha/lib_dal/dal_plugin_registration.ml b/src/proto_alpha/lib_dal/dal_plugin_registration.ml index f3ad19ecc43b27dbfb052e0dc8e047b8f4ddbfa0..c5c252097b1a412025552d67730d6c47348611cc 100644 --- a/src/proto_alpha/lib_dal/dal_plugin_registration.ml +++ b/src/proto_alpha/lib_dal/dal_plugin_registration.ml @@ -151,13 +151,13 @@ module Plugin = struct let* _op_hash = Shell_services.Injection.operation cctxt ~chain bytes in return_unit - let block_info ?chain ?block ~metadata ctxt = + let block_info ?chain ?block ~operations_metadata ctxt = let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in Protocol_client_context.Alpha_block_services.info cpctxt ?chain ?block - ~metadata + ~metadata:operations_metadata () let block_shell_header (block_info : block_info) = block_info.header.shell @@ -365,11 +365,10 @@ module Plugin = struct cells of the skip list by calling the appropriate DAL function in the protocol. *) - let cells_of_level (block_info : block_info) ctxt ~dal_constants + let cells_of_level ~attested_level ctxt ~dal_constants ~pred_publication_level_dal_constants:_ = let open Lwt_result_syntax in let cpctxt = new Protocol_client_context.wrap_rpc_context ctxt in - let attested_level = block_info.header.shell.level in let published_level = Int32.sub attested_level