diff --git a/src/bin_dal_node/RPC_server.ml b/src/bin_dal_node/RPC_server.ml index 7010aca12d8b165d858b8c8e69a70b6cf0f2fb3d..685da56c3f7455c3e9d4b446c5171fc98a5ad48a 100644 --- a/src/bin_dal_node/RPC_server.ml +++ b/src/bin_dal_node/RPC_server.ml @@ -38,11 +38,19 @@ module Slots_handlers = struct let patch_slot ctxt commitment () slot_id = call_handler - (fun store cryptobox -> + (fun store _cryptobox -> let open Lwt_result_syntax in - let*! r = Slot_manager.add_slot_id commitment slot_id store cryptobox in + let*! r = Slot_manager.add_slot_id commitment slot_id store in match r with Ok () -> return_some () | Error `Not_found -> return_none) ctxt + + let get_slot_content ctxt commitment () () = + call_handler + (fun store _cryptobox -> + let open Lwt_result_syntax in + let*! r = Slot_manager.get_slot_content commitment store in + match r with Ok s -> return_some s | Error `Not_found -> return_none) + ctxt end let add_service registerer service handler directory = @@ -62,6 +70,10 @@ let register_new : Tezos_rpc.Directory.opt_register1 Services.patch_slot (Slots_handlers.patch_slot ctxt) + |> add_service + Tezos_rpc.Directory.opt_register1 + Services.get_slot + (Slots_handlers.get_slot_content ctxt) let register_legacy ctxt = let open RPC_server_legacy in diff --git a/src/bin_dal_node/slot_manager.ml b/src/bin_dal_node/slot_manager.ml index 073656b677484b183409cdeaf07c7ee92d3dc272..99225e1199185e5081f7473a10bb878baaa5045d 100644 --- a/src/bin_dal_node/slot_manager.ml +++ b/src/bin_dal_node/slot_manager.ml @@ -57,6 +57,11 @@ let polynomial_from_slot cryptobox slot = let {slot_size = expected; _} = parameters cryptobox in tzfail @@ Invalid_slot_size {provided; expected} +let commitment_should_exist node_store commitment = + let open Lwt_result_syntax in + let*! exists = Store.Legacy.exists_slot_by_commitment node_store commitment in + if not exists then fail `Not_found else return_unit + (* Main functions *) let add_slots slot node_store cryptobox = @@ -70,15 +75,17 @@ let add_slots slot node_store cryptobox = in return commitment -let add_slot_id commitment slot_id node_store _cryptobox = +let add_slot_id commitment slot_id node_store = let open Lwt_result_syntax in - let*! exists = Store.Legacy.exists_slot_by_commitment node_store commitment in - if not exists then fail `Not_found - else - let*! () = - Store.Legacy.associate_slot_id_with_commitment - node_store - commitment - slot_id - in - return_unit + let* () = commitment_should_exist node_store commitment in + let*! () = + Store.Legacy.associate_slot_id_with_commitment node_store commitment slot_id + in + return_unit + +let get_slot_content commitment node_store = + let open Lwt_result_syntax in + let*! slot_opt = Store.Legacy.find_slot_by_commitment node_store commitment in + match slot_opt with + | None -> fail `Not_found + | Some slot_content -> return slot_content diff --git a/src/bin_dal_node/slot_manager.mli b/src/bin_dal_node/slot_manager.mli index 71214c23845918195db02ad32ddd44e434789540..f37f11d947f109c07a4581f8dce40f3be3670578 100644 --- a/src/bin_dal_node/slot_manager.mli +++ b/src/bin_dal_node/slot_manager.mli @@ -45,14 +45,22 @@ val add_slots : Cryptobox.t -> Cryptobox.commitment tzresult Lwt.t -(** [add_slot_id commitment slot_id node_Store cryptobox] 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. +(** [add_slot_id commitment slot_id node_Store] 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 : Cryptobox.commitment -> Services.Types.slot_id -> Store.node_store -> - Cryptobox.t -> (unit, [> `Not_found]) result Lwt.t + +(** [get_slot_content commitment node_Store] returns the slot content associated + with the given [commitment] in [node_store]. The function returns [Error + `Not_found] if there is no slot content for [commitment] in [node_store]. +*) +val get_slot_content : + Cryptobox.commitment -> + Store.node_store -> + (slot, [> `Not_found]) result Lwt.t diff --git a/src/bin_dal_node/store.ml b/src/bin_dal_node/store.ml index c43e493f2fe145382221d9734fec328ad864c506..dd6964a665b774bf86ad2fd497fbd9fe50199cdc 100644 --- a/src/bin_dal_node/store.ml +++ b/src/bin_dal_node/store.ml @@ -174,4 +174,11 @@ module Legacy = struct let commitment_b58 = Cryptobox.Commitment.to_b58check commitment in let path = Legacy_paths.slot_by_commitment commitment_b58 in mem node_store.slots_store path + + let find_slot_by_commitment node_store commitment = + let open Lwt_syntax in + let commitment_b58 = Cryptobox.Commitment.to_b58check commitment in + let path = Legacy_paths.slot_by_commitment commitment_b58 in + let* res_opt = find node_store.slots_store path in + Option.map Bytes.of_string res_opt |> Lwt.return end diff --git a/src/lib_dal_node_services/services.ml b/src/lib_dal_node_services/services.ml index 4a32ce7c880c9c1aef644e6bdce71f30bd985d50..ab3cf9e2366c0be41b66eac9100795de2e211893 100644 --- a/src/lib_dal_node_services/services.ml +++ b/src/lib_dal_node_services/services.ml @@ -84,3 +84,18 @@ let patch_slot : ~input:Types.slot_id_encoding ~output:Data_encoding.unit Tezos_rpc.Path.(open_root / "slots" /: Cryptobox.Commitment.rpc_arg) + +let get_slot : + < meth : [`GET] + ; input : unit + ; output : Cryptobox.slot + ; prefix : unit + ; params : unit * Cryptobox.commitment + ; query : unit > + service = + Tezos_rpc.Service.get_service + ~description: + "Retrieve the content of the slot associated with the given commitment." + ~query:Tezos_rpc.Query.empty + ~output:Types.slot_content + Tezos_rpc.Path.(open_root / "slots" /: Cryptobox.Commitment.rpc_arg) diff --git a/src/lib_dal_node_services/services.mli b/src/lib_dal_node_services/services.mli index eea096c3dadc3195f31e1013cd050d1c6f82d6d7..4cb2e299c956089e1afbabff881b35f310c00d46 100644 --- a/src/lib_dal_node_services/services.mli +++ b/src/lib_dal_node_services/services.mli @@ -71,3 +71,13 @@ val patch_slot : ; params : unit * Cryptobox.commitment ; query : unit > service + +(** Retrieve the content of the slot associated with the given commitment. *) +val get_slot : + < meth : [`GET] + ; input : unit + ; output : Cryptobox.slot + ; prefix : unit + ; params : unit * Cryptobox.commitment + ; query : unit > + service diff --git a/tezt/lib_tezos/rollup.ml b/tezt/lib_tezos/rollup.ml index a62599ea0917f0bdd2ebf4b641054f6e073109e5..8b92e11487ae02119a4cf6f6935b4261e391fee1 100644 --- a/tezt/lib_tezos/rollup.ml +++ b/tezt/lib_tezos/rollup.ml @@ -650,6 +650,9 @@ module Dal = struct ] in make ~data PATCH ["slots"; commitment] as_empty_object_or_fail + + let get_slot commitment = + make GET ["slots"; commitment] get_bytes_from_json_string_node end let make diff --git a/tezt/lib_tezos/rollup.mli b/tezt/lib_tezos/rollup.mli index 8a70ad991bfec9440b03cf0b447bb31da94f40fd..04a484c1172d121f9e5eed4aec4f9bdbfcd94471 100644 --- a/tezt/lib_tezos/rollup.mli +++ b/tezt/lib_tezos/rollup.mli @@ -295,6 +295,10 @@ module Dal : sig slot_level:int -> slot_index:int -> (Dal_node.t, unit) RPC_core.t + + (** Call RPC "GET /slots/" to retrieve the slot content associated with the + given commitment. *) + val get_slot : commitment -> (Dal_node.t, slot) RPC_core.t end val make : diff --git a/tezt/tests/dal.ml b/tezt/tests/dal.ml index cb694a31d09ae4c298212e0bcc3e8258aa9c2cfc..262616a7c7a70e9d5d61e963709e712bcdc2ed54 100644 --- a/tezt/tests/dal.ml +++ b/tezt/tests/dal.ml @@ -1001,6 +1001,27 @@ let test_dal_node_test_patch_slots _protocol parameters cryptobox _node client let* () = patch_slot_rpc ~slot_level:0 ~slot_index:1 in patch_slot_rpc ~slot_level:(-4) ~slot_index:3 +let test_dal_node_test_get_slots _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 commit = + Cryptobox.Commitment.to_b58check @@ commitment_of_slot cryptobox slot + in + let* () = + 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 + (* 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) + Check.string + ~error_msg: + "The slot content retrieved from the node is not as expected (expected = \ + %L, got = %R)" ; + unit + let test_dal_node_startup = Protocol.register_test ~__FILE__ @@ -1585,6 +1606,10 @@ let register ~protocols = "dal node PATCH /slots" test_dal_node_test_patch_slots protocols ; + scenario_with_layer1_and_dal_nodes + "dal node GET /slots" + test_dal_node_test_get_slots + protocols ; (* Tests with all nodes *) test_dal_node_dac_threshold_not_reached protocols ;