diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index d9b9d94b154ea995baa0f878e69a5871bd4c2a30..fe26d8b662419b38c292a7a815745e1618427804 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -150,6 +150,7 @@ "Sc_rollup_errors", "Sc_rollup_commitment_storage", "Sc_rollup_inbox_storage", + "Sc_rollup_outbox_storage", "Sc_rollup_stake_storage", "Sc_rollup_refutation_storage", "Sc_rollup_storage", diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index ab60b8afa8ee8095329a9b9403c6969a96f396f1..6408a2c8deb158758b6dbc57ef570b5e8b3179d8 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -80,7 +80,7 @@ module Sc_rollup = struct include Sc_rollups module Outbox = struct - include Sc_rollup_storage.Outbox + include Sc_rollup_outbox_storage module Message = Sc_rollup_outbox_message_repr end diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index 67d4c1f46bf54d465b3d4b2088c3bd249ae184f2..ac2fa1f075a53797ce16bb31fbbce4d0156e8d65 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -170,6 +170,7 @@ Sc_rollup_errors Sc_rollup_commitment_storage Sc_rollup_inbox_storage + Sc_rollup_outbox_storage Sc_rollup_stake_storage Sc_rollup_refutation_storage Sc_rollup_storage @@ -398,6 +399,7 @@ sc_rollup_errors.ml sc_rollup_commitment_storage.ml sc_rollup_commitment_storage.mli sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli + sc_rollup_outbox_storage.ml sc_rollup_outbox_storage.mli sc_rollup_stake_storage.ml sc_rollup_stake_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli sc_rollup_storage.ml sc_rollup_storage.mli @@ -612,6 +614,7 @@ sc_rollup_errors.ml sc_rollup_commitment_storage.ml sc_rollup_commitment_storage.mli sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli + sc_rollup_outbox_storage.ml sc_rollup_outbox_storage.mli sc_rollup_stake_storage.ml sc_rollup_stake_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli sc_rollup_storage.ml sc_rollup_storage.mli @@ -835,6 +838,7 @@ sc_rollup_errors.ml sc_rollup_commitment_storage.ml sc_rollup_commitment_storage.mli sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli + sc_rollup_outbox_storage.ml sc_rollup_outbox_storage.mli sc_rollup_stake_storage.ml sc_rollup_stake_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli sc_rollup_storage.ml sc_rollup_storage.mli @@ -1062,6 +1066,7 @@ sc_rollup_errors.ml sc_rollup_commitment_storage.ml sc_rollup_commitment_storage.mli sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli + sc_rollup_outbox_storage.ml sc_rollup_outbox_storage.mli sc_rollup_stake_storage.ml sc_rollup_stake_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli sc_rollup_storage.ml sc_rollup_storage.mli diff --git a/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.ml new file mode 100644 index 0000000000000000000000000000000000000000..f1c5858cc271b44aa2a0b370eae6317a88fe75e9 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.ml @@ -0,0 +1,82 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2021 Nomadic Labs *) +(* Copyright (c) 2022 TriliTech *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +let level_index ctxt level = + let max_active_levels = + Constants_storage.sc_rollup_max_active_outbox_levels ctxt + in + Int32.rem (Raw_level_repr.to_int32 level) max_active_levels + +let record_applied_message ctxt rollup level ~message_index = + let open Lwt_tzresult_syntax in + (* Check that the 0 <= message index < maximum number of outbox messages per + level. *) + let*? () = + let max_outbox_messages_per_level = + Constants_storage.sc_rollup_max_outbox_messages_per_level ctxt + in + error_unless + Compare.Int.( + 0 <= message_index && message_index < max_outbox_messages_per_level) + Sc_rollup_errors.Sc_rollup_invalid_outbox_message_index + in + let level_index = level_index ctxt level in + let* ctxt, level_and_bitset_opt = + Storage.Sc_rollup.Applied_outbox_messages.find (ctxt, rollup) level_index + in + let*? bitset, ctxt = + let open Tzresult_syntax in + let* bitset, ctxt = + match level_and_bitset_opt with + | Some (existing_level, bitset) + when Raw_level_repr.(existing_level = level) -> + (* The level at the index is the same as requested. Fail if the + message has been applied already. *) + let* already_applied = Bitset.mem bitset message_index in + let* () = + error_when + already_applied + Sc_rollup_errors.Sc_rollup_outbox_message_already_applied + in + return (bitset, ctxt) + | Some (existing_level, _bitset) + when Raw_level_repr.(level < existing_level) -> + fail Sc_rollup_errors.Sc_rollup_outbox_level_expired + | Some _ | None -> + (* The old level is outdated or there is no previous bitset at + this index. *) + return (Bitset.empty, ctxt) + in + let* bitset = Bitset.add bitset message_index in + return (bitset, ctxt) + in + let+ ctxt, size_diff, _is_new = + Storage.Sc_rollup.Applied_outbox_messages.add + (ctxt, rollup) + level_index + (level, bitset) + in + (Z.of_int size_diff, ctxt) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.mli b/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.mli new file mode 100644 index 0000000000000000000000000000000000000000..1df195368aaa36659a0537d503917a057139f2d7 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_outbox_storage.mli @@ -0,0 +1,45 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** A module for managing state concerning a rollup's outbox. *) + +(** [record_applied_message ctxt rollup level ~message_index] marks the message + in the outbox of rollup [rollup] at level [level] and position + [message_index] as processed. Returns the size diff resulting from adding an + entry. The size diff may be 0 if an entry already exists, or negative if an + index is replaced with a new level. + + An attempt to apply an old level that has already been replaced fails with + an [Sc_rollup_outbox_level_expired] error. + + In case a message has already been applied for the given level and message + index, the function fails with an [Sc_rollup_outbox_message_already_applied] + error. *) +val record_applied_message : + Raw_context.t -> + Sc_rollup_repr.t -> + Raw_level_repr.t -> + message_index:int -> + (Z.t * Raw_context.t) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/sc_rollup_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_storage.ml index dc9df98ed8acf00677ecd9fd1d2e028a26edd16e..46bc8adeeffb9488571c6b491c151e22de20a59d 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_storage.ml @@ -85,65 +85,6 @@ let parameters_type ctxt rollup = let+ ctxt, res = Store.Parameters_type.find ctxt rollup in (res, ctxt) -module Outbox = struct - let level_index ctxt level = - let max_active_levels = - Constants_storage.sc_rollup_max_active_outbox_levels ctxt - in - Int32.rem (Raw_level_repr.to_int32 level) max_active_levels - - let record_applied_message ctxt rollup level ~message_index = - let open Lwt_tzresult_syntax in - (* Check that the 0 <= message index < maximum number of outbox messages per - level. *) - let*? () = - let max_outbox_messages_per_level = - Constants_storage.sc_rollup_max_outbox_messages_per_level ctxt - in - error_unless - Compare.Int.( - 0 <= message_index && message_index < max_outbox_messages_per_level) - Sc_rollup_invalid_outbox_message_index - in - let level_index = level_index ctxt level in - let* ctxt, level_and_bitset_opt = - Store.Applied_outbox_messages.find (ctxt, rollup) level_index - in - let*? bitset, ctxt = - let open Tzresult_syntax in - let* bitset, ctxt = - match level_and_bitset_opt with - | Some (existing_level, bitset) - when Raw_level_repr.(existing_level = level) -> - (* The level at the index is the same as requested. Fail if the - message has been applied already. *) - let* already_applied = Bitset.mem bitset message_index in - let* () = - error_when - already_applied - Sc_rollup_outbox_message_already_applied - in - return (bitset, ctxt) - | Some (existing_level, _bitset) - when Raw_level_repr.(level < existing_level) -> - fail Sc_rollup_outbox_level_expired - | Some _ | None -> - (* The old level is outdated or there is no previous bitset at - this index. *) - return (Bitset.empty, ctxt) - in - let* bitset = Bitset.add bitset message_index in - return (bitset, ctxt) - in - let+ ctxt, size_diff, _is_new = - Store.Applied_outbox_messages.add - (ctxt, rollup) - level_index - (level, bitset) - in - (Z.of_int size_diff, ctxt) -end - module Dal_slot = struct let slot_of_int_e n = let open Tzresult_syntax in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_storage.mli b/src/proto_alpha/lib_protocol/sc_rollup_storage.mli index 63632eae8502cf9a888e068872d9b6ee9d7f43b5..31a2d37d123076304d3ee6eecf3a2f86b7443533 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_storage.mli @@ -60,28 +60,6 @@ val parameters_type : Sc_rollup_repr.t -> (Script_repr.lazy_expr option * Raw_context.t) tzresult Lwt.t -(** A module for managing state concerning a rollup's outbox. *) -module Outbox : sig - (** [record_applied_message ctxt rollup level ~message_index] marks the - message in the outbox of rollup [rollup] at level [level] and position - [message_index] as processed. Returns the size diff resulting from - adding an entry. The size diff may be 0 if an entry already exists, or - negative if an index is replaced with a new level. - - An attempt to apply an old level that has already been replaced - fails with an [Sc_rollup_outbox_level_expired] error. - - In case a message has already been applied for the given level and message - index, the function fails with an - [Sc_rollup_outbox_message_already_applied] error. *) - val record_applied_message : - Raw_context.t -> - Sc_rollup_repr.t -> - Raw_level_repr.t -> - message_index:int -> - (Z.t * Raw_context.t) tzresult Lwt.t -end - module Dal_slot : sig (** [subscribe ctxt rollup slot_index] marks the [rollup] as subscribed to [slot_index] at the level indicated by [Raw_context.current_level ctxt]. diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml index 53c3d82d2777a426abc037d017079ee847ee726b..e1bad3da3cd463353e33f82d5cefbade71bef3fc 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml @@ -2412,7 +2412,7 @@ let test_limit_on_number_of_messages_during_commitment_period with_gap () = | _ -> false let record ctxt rollup level message_index = - Sc_rollup_storage.Outbox.record_applied_message + Sc_rollup_outbox_storage.record_applied_message ctxt rollup (Raw_level_repr.of_int32_exn @@ Int32.of_int level)