diff --git a/src/bin_dal_node/RPC_server.ml b/src/bin_dal_node/RPC_server.ml index 6988c994b760bc872c8ba3fd99f470cf4864c201..f494d41cb8fef7cf329f3fd659d784742131b55a 100644 --- a/src/bin_dal_node/RPC_server.ml +++ b/src/bin_dal_node/RPC_server.ml @@ -34,14 +34,20 @@ module Slots_handlers = struct let store = Node_context.get_store ctxt in handler store cryptobox - let post_slots ctxt () slot = - call_handler (fun store -> Slot_manager.add_slots store slot) ctxt + let post_commitment ctxt () slot = + call_handler (fun store -> Slot_manager.add_commitment store slot) ctxt - let patch_slot ctxt commitment () slot_id = + let patch_commitment ctxt commitment () slot_id = call_handler (fun store cryptobox -> let open Lwt_result_syntax in - let*! r = Slot_manager.add_slot_id store cryptobox commitment slot_id in + let*! r = + Slot_manager.associate_slot_id_with_commitment + store + cryptobox + commitment + slot_id + in match r with Ok () -> return_some () | Error `Not_found -> return_none) ctxt @@ -120,12 +126,12 @@ let register_new : directory |> add_service Tezos_rpc.Directory.register0 - Services.post_slots - (Slots_handlers.post_slots ctxt) + Services.post_commitment + (Slots_handlers.post_commitment ctxt) |> add_service Tezos_rpc.Directory.opt_register1 - Services.patch_slot - (Slots_handlers.patch_slot ctxt) + Services.patch_commitment + (Slots_handlers.patch_commitment ctxt) |> add_service Tezos_rpc.Directory.opt_register1 Services.get_slot diff --git a/src/bin_dal_node/slot_manager.ml b/src/bin_dal_node/slot_manager.ml index 3c677c05b29d8118c4a654ad60d427047bec65a0..95f557fe58cb3697d152121e5021ab5fb44758db 100644 --- a/src/bin_dal_node/slot_manager.ml +++ b/src/bin_dal_node/slot_manager.ml @@ -66,7 +66,7 @@ let commitment_should_exist node_store cryptobox commitment = (* Main functions *) -let add_slots node_store slot cryptobox = +let add_commitment node_store slot cryptobox = let open Lwt_result_syntax in let*? polynomial = polynomial_from_slot cryptobox slot in let commitment = Cryptobox.commit cryptobox polynomial in @@ -80,7 +80,7 @@ let add_slots node_store slot cryptobox = in return commitment -let add_slot_id node_store cryptobox commitment slot_id = +let associate_slot_id_with_commitment node_store cryptobox commitment slot_id = let open Lwt_result_syntax in let* () = commitment_should_exist node_store cryptobox commitment in let*! () = diff --git a/src/bin_dal_node/slot_manager.mli b/src/bin_dal_node/slot_manager.mli index 2e2015fb9b0e9ce04a462f6b020cfd9d799d3600..3fb454f536c9e44c0f920fa1595a174b47d47984 100644 --- a/src/bin_dal_node/slot_manager.mli +++ b/src/bin_dal_node/slot_manager.mli @@ -31,26 +31,26 @@ include module type of Slot_manager_legacy type error += Invalid_slot_size of {provided : int; expected : int} -(** [add_slots node_store slot cryptobox] computes the given [slot]'s commitment - and adds the association "commitment -> slot" in the DAL's [node_store] if - the commitment is not already bound to some data. +(** [add_commitment node_store slot cryptobox] computes the given [slot]'s + commitment and adds the association "commitment -> slot" in the DAL's + [node_store] if the commitment is not already bound to some data. The function returns an error {!ref:Invalid_slot_size} if the [slot]'s size doesn't match the expected slots' size given in [cryptobox], or the [slot]'s commitment otherwise. *) -val add_slots : +val add_commitment : Store.node_store -> Cryptobox.slot -> Cryptobox.t -> Cryptobox.commitment tzresult Lwt.t -(** [add_slot_id node_tore cryptobox commitment slot_id] associates a [slot_id] to a - [commitment] in [node_store]. The function returns [Error `Not_found] if - there is no entry for [commitment] in [node_store]. Otherwise, [Ok ()] is - returned. +(** [associate_slot_id_with_commitment node_store cryptobox commitment slot_id] + associates a [slot_id] to a [commitment] in [node_store]. The function + returns [Error `Not_found] if there is no entry for [commitment] in + [node_store]. Otherwise, [Ok ()] is returned. *) -val add_slot_id : +val associate_slot_id_with_commitment : Store.node_store -> Cryptobox.t -> Cryptobox.commitment -> diff --git a/src/bin_dal_node/store.ml b/src/bin_dal_node/store.ml index 10e4ad99accde1e043929bc3c05820ce751b115e..fa586b7ce30fc669ad1cb3fbbf3a6e2e172a50ae 100644 --- a/src/bin_dal_node/store.ml +++ b/src/bin_dal_node/store.ml @@ -199,6 +199,19 @@ module Legacy = struct let decode encoding string = Data_encoding.Binary.of_string_opt encoding string + let encode_commitment = Cryptobox.Commitment.to_b58check + + let decode_commitment = Cryptobox.Commitment.of_b58check_opt + + let encode_header_status = + Data_encoding.Binary.to_string_exn Services.Types.header_status_encoding + + let decode_header_status = + Data_encoding.Binary.of_string_opt Services.Types.header_status_encoding + + let decode_slot_id = + Data_encoding.Binary.of_string_exn Services.Types.slot_id_encoding + let add_slot_by_commitment node_store cryptobox slot commitment = let open Lwt_syntax in let Cryptobox.{slot_size; _} = Cryptobox.parameters cryptobox in @@ -210,11 +223,38 @@ module Legacy = struct return_unit let associate_slot_id_with_commitment node_store commitment slot_id = + (* TODO: https://gitlab.com/tezos/tezos/-/issues/4528 + Improve the implementation of this handler. + *) let open Lwt_syntax in - let path = Path.Commitment.header commitment slot_id in - (* The path allows to reconstruct the data. *) - let* () = set ~msg:"Slot id stored" node_store.store path "" in - return_unit + let store = node_store.store in + let header_path = Path.Commitment.header commitment slot_id in + let levels_path = Path.Level.other_header_status slot_id commitment in + let* known_levels = mem store levels_path in + let* known_header = mem store header_path in + (* An invariant that should hold for the storage. *) + assert (known_levels = known_header) ; + if known_levels then return_unit + else + (* The path allows to reconstruct the data. *) + let* () = + set + ~msg: + (Path.to_string + ~prefix:"associate_slot_id_with_commitment:" + header_path) + store + header_path + "" + in + set + ~msg: + (Path.to_string + ~prefix:"associate_slot_id_with_commitment:" + levels_path) + store + levels_path + (encode_header_status `Unseen) let exists_slot_by_commitment node_store cryptobox commitment = let Cryptobox.{slot_size; _} = Cryptobox.parameters cryptobox in @@ -250,19 +290,6 @@ module Legacy = struct Lwt.return_unit) slot_headers - let encode_commitment = Cryptobox.Commitment.to_b58check - - let decode_commitment = Cryptobox.Commitment.of_b58check_opt - - let encode_header_status = - Data_encoding.Binary.to_string_exn Services.Types.header_status_encoding - - let decode_header_status = - Data_encoding.Binary.of_string_opt Services.Types.header_status_encoding - - let decode_slot_id = - Data_encoding.Binary.of_string_exn Services.Types.slot_id_encoding - let add_slot_headers ~block_level ~block_hash slot_headers node_store = let open Lwt_syntax in let* () = legacy_add_slot_headers ~block_hash slot_headers node_store in @@ -270,6 +297,7 @@ module Legacy = struct (* TODO: https://gitlab.com/tezos/tezos/-/issues/4388 Handle reorgs. *) (* TODO: https://gitlab.com/tezos/tezos/-/issues/4389 + https://gitlab.com/tezos/tezos/-/issues/4528 Handle statuses evolution. *) List.iter_s (fun (slot_header, status) -> @@ -442,6 +470,9 @@ module Legacy = struct indices let get_commitment_headers commitment ?slot_level ?slot_index node_store = + (* TODO: https://gitlab.com/tezos/tezos/-/issues/4528 + Improve the implementation of this handler. + *) let open Lwt_result_syntax in let store = node_store.store in (* Get the list of known slot identifiers for [commitment]. *) diff --git a/src/lib_dal_node_services/services.ml b/src/lib_dal_node_services/services.ml index 477f2c75c80572a5ecc0464950d604df157b461e..17cbe1f2ae98081d25be639edc5ad1e2d07e9369 100644 --- a/src/lib_dal_node_services/services.ml +++ b/src/lib_dal_node_services/services.ml @@ -150,7 +150,7 @@ module Types = struct |> seal end -let post_slots : +let post_commitment : < meth : [`POST] ; input : Cryptobox.slot ; output : Cryptobox.commitment @@ -165,9 +165,9 @@ let post_slots : ~query:Tezos_rpc.Query.empty ~input:Types.slot_encoding ~output:Cryptobox.Commitment.encoding - Tezos_rpc.Path.(open_root / "slots") + Tezos_rpc.Path.(open_root / "commitments") -let patch_slot : +let patch_commitment : < meth : [`PATCH] ; input : Types.slot_id ; output : unit @@ -180,7 +180,7 @@ let patch_slot : ~query:Tezos_rpc.Query.empty ~input:Types.slot_id_encoding ~output:Data_encoding.unit - Tezos_rpc.Path.(open_root / "slots" /: Cryptobox.Commitment.rpc_arg) + Tezos_rpc.Path.(open_root / "commitments" /: Cryptobox.Commitment.rpc_arg) let get_slot : < meth : [`GET] diff --git a/src/lib_dal_node_services/services.mli b/src/lib_dal_node_services/services.mli index 0d066c8a1c0e617ecf3e5fd12692c280688937c6..29ecbb0c2457206822efa55ba5011333121f7f3b 100644 --- a/src/lib_dal_node_services/services.mli +++ b/src/lib_dal_node_services/services.mli @@ -92,8 +92,8 @@ end (** Add the given slot in the node if not already present. The corresponding commitment is returned. See {!val: - Slot_manager.add_slot} for more details. *) -val post_slots : + Slot_manager.add_commitment} for more details. *) +val post_commitment : < meth : [`POST] ; input : Cryptobox.slot ; output : Cryptobox.commitment @@ -103,8 +103,8 @@ val post_slots : service (** Associate a commitment to a level and a slot index. See {!val: - Slot_manager.add_slot_id} for more details. *) -val patch_slot : + Slot_manager.associate_slot_id_with_commitment} for more details. *) +val patch_commitment : < meth : [`PATCH] ; input : Types.slot_id ; output : unit diff --git a/tezt/lib_tezos/rollup.ml b/tezt/lib_tezos/rollup.ml index 82b7857df072ff3e175914915e2794027b38f11f..e3761ce83a5b9bd793f98d8a25e68ecd96fe3a88 100644 --- a/tezt/lib_tezos/rollup.ml +++ b/tezt/lib_tezos/rollup.ml @@ -707,16 +707,16 @@ module Dal = struct | [] -> () | _ -> JSON.error t "Not an empty object" - let post_slot slot = + let post_commitment slot = let slot = JSON.parse - ~origin:"Rollup.RPC.post_slots" + ~origin:"Rollup.RPC.post_commitments" (encode_bytes_to_hex_string slot) in let data : RPC_core.data = Data (JSON.unannotate slot) in - make ~data POST ["slots"] JSON.as_string + make ~data POST ["commitments"] JSON.as_string - let patch_slot commitment ~slot_level ~slot_index = + let patch_commitment commitment ~slot_level ~slot_index = let data : RPC_core.data = Data (`O @@ -725,7 +725,7 @@ module Dal = struct ("slot_index", `Float (float_of_int slot_index)); ]) in - make ~data PATCH ["slots"; commitment] as_empty_object_or_fail + make ~data PATCH ["commitments"; commitment] as_empty_object_or_fail let get_slot commitment = make GET ["slots"; commitment] get_bytes_from_json_string_node diff --git a/tezt/lib_tezos/rollup.mli b/tezt/lib_tezos/rollup.mli index 245d443499f99517622c9f8f3d33ad17b9628172..b14b4d7ee4c271ac2fffc692d2dc4460f453100e 100644 --- a/tezt/lib_tezos/rollup.mli +++ b/tezt/lib_tezos/rollup.mli @@ -313,13 +313,13 @@ module Dal : sig the input (and output) is expected to be a list. *) val slot_headers_of_json : JSON.t -> slot_header list - (** Call RPC "POST /slots" to store a slot and retrun the commitment in case - of success. *) - val post_slot : slot -> (Dal_node.t, commitment) RPC_core.t + (** Call RPC "POST /commitments" to store a slot and retrun the commitment + in case of success. *) + val post_commitment : slot -> (Dal_node.t, commitment) RPC_core.t - (** Call RPC "PATCH /slots" to associate the given level and index to the slot + (** Call RPC "PATCH /commitments" to associate the given level and index to the slot whose commitment is given. *) - val patch_slot : + val patch_commitment : commitment -> slot_level:int -> slot_index:int -> diff --git a/tezt/tests/dal.ml b/tezt/tests/dal.ml index 851e6d897c0ac7808556b08948599899f9c5cff2..9a14d18be63a998e3f931f744da8e44d01543376 100644 --- a/tezt/tests/dal.ml +++ b/tezt/tests/dal.ml @@ -1151,13 +1151,15 @@ let commitment_of_slot cryptobox slot = in Cryptobox.commit cryptobox polynomial -let test_dal_node_test_post_slots _protocol parameters cryptobox _node client - dal_node = +let test_dal_node_test_post_commitments _protocol parameters cryptobox _node + client dal_node = let mk_slot size = Rollup.Dal.make_slot ~padding:false (generate_dummy_slot size) client in let failing_post_slot_rpc slot = - let* response = RPC.call_raw dal_node @@ Rollup.Dal.RPC.post_slot slot in + let* response = + RPC.call_raw dal_node @@ Rollup.Dal.RPC.post_commitment slot + in return @@ RPC.check_string_response ~body_rex:"dal.node.invalid_slot_size" @@ -1170,8 +1172,12 @@ let test_dal_node_test_post_slots _protocol parameters cryptobox _node client let* slot_ok = mk_slot size in let* () = failing_post_slot_rpc slot_big in let* () = failing_post_slot_rpc slot_small in - let* commitment1 = RPC.call dal_node (Rollup.Dal.RPC.post_slot slot_ok) in - let* commitment2 = RPC.call dal_node (Rollup.Dal.RPC.post_slot slot_ok) in + let* commitment1 = + RPC.call dal_node (Rollup.Dal.RPC.post_commitment slot_ok) + in + let* commitment2 = + RPC.call dal_node (Rollup.Dal.RPC.post_commitment slot_ok) + in (* TODO/DAL: https://gitlab.com/tezos/tezos/-/issues/4250 The second RPC call above succeeeds, but the (untested) returned HTTP status should likely be 200 and 201 in the first similar RPC call. @@ -1191,12 +1197,12 @@ let test_dal_node_test_post_slots _protocol parameters cryptobox _node client locally (current = %L, expected = %R)" ; unit -let test_dal_node_test_patch_slots _protocol parameters cryptobox _node client - dal_node = +let test_dal_node_test_patch_commitments _protocol parameters cryptobox _node + client dal_node = let failing_patch_slot_rpc slot ~slot_level ~slot_index = let* response = RPC.call_raw dal_node - @@ Rollup.Dal.RPC.patch_slot slot ~slot_level ~slot_index + @@ Rollup.Dal.RPC.patch_commitment slot ~slot_level ~slot_index in return @@ RPC.check_string_response ~code:404 response in @@ -1206,7 +1212,7 @@ let test_dal_node_test_patch_slots _protocol parameters cryptobox _node client Cryptobox.Commitment.to_b58check @@ commitment_of_slot cryptobox slot in let* () = failing_patch_slot_rpc commitment ~slot_level:0 ~slot_index:0 in - let* commitment' = RPC.call dal_node (Rollup.Dal.RPC.post_slot slot) in + let* commitment' = RPC.call dal_node (Rollup.Dal.RPC.post_commitment slot) in Check.(commitment' = commitment) Check.string ~error_msg: @@ -1215,7 +1221,7 @@ let test_dal_node_test_patch_slots _protocol parameters cryptobox _node client let patch_slot_rpc ~slot_level ~slot_index = RPC.call dal_node - (Rollup.Dal.RPC.patch_slot commitment ~slot_level ~slot_index) + (Rollup.Dal.RPC.patch_commitment commitment ~slot_level ~slot_index) in let* () = patch_slot_rpc ~slot_level:0 ~slot_index:0 in let* () = patch_slot_rpc ~slot_level:0 ~slot_index:0 in @@ -1233,7 +1239,7 @@ let test_dal_node_test_get_slots _protocol parameters cryptobox _node client let* response = RPC.call_raw dal_node @@ Rollup.Dal.RPC.get_slot commit in return @@ RPC.check_string_response ~code:404 response in - let* _commitment = RPC.call dal_node (Rollup.Dal.RPC.post_slot slot) in + let* _commitment = RPC.call dal_node (Rollup.Dal.RPC.post_commitment slot) in (* commit = _commitment already test in /POST test. *) let* got_slot = RPC.call dal_node (Rollup.Dal.RPC.get_slot commit) in Check.(Rollup.Dal.content_of_slot slot = Rollup.Dal.content_of_slot got_slot) @@ -1247,7 +1253,7 @@ let test_dal_node_test_get_slot_proof _protocol parameters cryptobox _node client dal_node = let size = parameters.Rollup.Dal.Parameters.cryptobox.slot_size in let* slot = Rollup.Dal.make_slot (generate_dummy_slot size) client in - let* commitment = RPC.call dal_node (Rollup.Dal.RPC.post_slot slot) in + let* commitment = RPC.call dal_node (Rollup.Dal.RPC.post_commitment slot) in let* proof = RPC.call dal_node (Rollup.Dal.RPC.get_slot_proof commitment) in let _, expected_proof = Rollup.Dal.Commitment.dummy_commitment cryptobox (generate_dummy_slot size) @@ -1904,12 +1910,12 @@ let register ~protocols = test_dal_node_test_slots_propagation protocols ; scenario_with_layer1_and_dal_nodes - "dal node POST /slots" - test_dal_node_test_post_slots + "dal node POST /commitments" + test_dal_node_test_post_commitments protocols ; scenario_with_layer1_and_dal_nodes - "dal node PATCH /slots" - test_dal_node_test_patch_slots + "dal node PATCH /commitments" + test_dal_node_test_patch_commitments protocols ; scenario_with_layer1_and_dal_nodes "dal node GET /slots"