From eb171e44300bedad7dc1167779210aeae392b9de Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Thu, 2 Jun 2022 14:37:40 +0200 Subject: [PATCH] Scoru,Proto: introduce versioned commitments --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/dune | 5 + .../lib_protocol/sc_rollup_commitment_repr.ml | 146 ++++++++++-------- .../sc_rollup_commitment_repr.mli | 40 +++-- .../sc_rollup_data_version_sig.ml | 49 ++++++ .../lib_protocol/sc_rollup_stake_storage.ml | 4 +- src/proto_alpha/lib_protocol/storage.ml | 60 +++++-- .../test/integration/test_constants.ml | 16 +- ...ommitments in the rollup node (no_comm.out | 4 +- 9 files changed, 222 insertions(+), 103 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/sc_rollup_data_version_sig.ml diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 05802868ec10..f0e49e3602fb 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -53,6 +53,7 @@ "Sc_rollup_wasm", "Sc_rollups", "Skip_list_repr", + "Sc_rollup_data_version_sig", "Sc_rollup_inbox_repr", "Sc_rollup_commitment_repr", "Sc_rollup_proof_repr", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index c51418e6a106..ee039990e78f 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -84,6 +84,7 @@ Sc_rollup_wasm Sc_rollups Skip_list_repr + Sc_rollup_data_version_sig Sc_rollup_inbox_repr Sc_rollup_commitment_repr Sc_rollup_proof_repr @@ -304,6 +305,7 @@ sc_rollup_wasm.ml sc_rollup_wasm.mli sc_rollups.ml sc_rollups.mli skip_list_repr.ml skip_list_repr.mli + sc_rollup_data_version_sig.ml sc_rollup_inbox_repr.ml sc_rollup_inbox_repr.mli sc_rollup_commitment_repr.ml sc_rollup_commitment_repr.mli sc_rollup_proof_repr.ml sc_rollup_proof_repr.mli @@ -511,6 +513,7 @@ sc_rollup_wasm.ml sc_rollup_wasm.mli sc_rollups.ml sc_rollups.mli skip_list_repr.ml skip_list_repr.mli + sc_rollup_data_version_sig.ml sc_rollup_inbox_repr.ml sc_rollup_inbox_repr.mli sc_rollup_commitment_repr.ml sc_rollup_commitment_repr.mli sc_rollup_proof_repr.ml sc_rollup_proof_repr.mli @@ -738,6 +741,7 @@ sc_rollup_wasm.ml sc_rollup_wasm.mli sc_rollups.ml sc_rollups.mli skip_list_repr.ml skip_list_repr.mli + sc_rollup_data_version_sig.ml sc_rollup_inbox_repr.ml sc_rollup_inbox_repr.mli sc_rollup_commitment_repr.ml sc_rollup_commitment_repr.mli sc_rollup_proof_repr.ml sc_rollup_proof_repr.mli @@ -961,6 +965,7 @@ sc_rollup_wasm.ml sc_rollup_wasm.mli sc_rollups.ml sc_rollups.mli skip_list_repr.ml skip_list_repr.mli + sc_rollup_data_version_sig.ml sc_rollup_inbox_repr.ml sc_rollup_inbox_repr.mli sc_rollup_commitment_repr.ml sc_rollup_commitment_repr.mli sc_rollup_proof_repr.ml sc_rollup_proof_repr.mli diff --git a/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.ml index 9cfea7f6b44a..c336167752d5 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.ml @@ -55,71 +55,93 @@ module Hash = struct include Path_encoding.Make_hex (H) end -type t = { - compressed_state : State_hash.t; - inbox_level : Raw_level_repr.t; - predecessor : Hash.t; - number_of_messages : Number_of_messages.t; - number_of_ticks : Number_of_ticks.t; -} - -let pp fmt - { - compressed_state; - inbox_level; - predecessor; - number_of_messages; - number_of_ticks; - } = - Format.fprintf - fmt - "@[SCORU Commitment:@ compressed_state: %a@ inbox_level: %a@ \ - predecessor: %a@ number_of_messages: %ld@ number_of_ticks: %ld@]" - State_hash.pp - compressed_state - Raw_level_repr.pp - inbox_level - Hash.pp - predecessor - (Number_of_messages.to_int32 number_of_messages) - (Number_of_ticks.to_int32 number_of_ticks) - -let encoding = - let open Data_encoding in - conv - (fun { - compressed_state; - inbox_level; - predecessor; - number_of_messages; - number_of_ticks; - } -> - ( compressed_state, - inbox_level, - predecessor, - number_of_messages, - number_of_ticks )) - (fun ( compressed_state, - inbox_level, - predecessor, - number_of_messages, - number_of_ticks ) -> +module V1 = struct + type t = { + compressed_state : State_hash.t; + inbox_level : Raw_level_repr.t; + predecessor : Hash.t; + number_of_messages : Number_of_messages.t; + number_of_ticks : Number_of_ticks.t; + } + + let pp fmt { compressed_state; inbox_level; predecessor; number_of_messages; number_of_ticks; - }) - (obj5 - (req "compressed_state" State_hash.encoding) - (req "inbox_level" Raw_level_repr.encoding) - (req "predecessor" Hash.encoding) - (req "number_of_messages" Number_of_messages.encoding) - (req "number_of_ticks" Number_of_ticks.encoding)) - -let hash commitment = - let commitment_bytes = - Data_encoding.Binary.to_bytes_exn encoding commitment - in - Hash.hash_bytes [commitment_bytes] + } = + Format.fprintf + fmt + "@[SCORU Commitment:@ compressed_state: %a@ inbox_level: %a@ \ + predecessor: %a@ number_of_messages: %ld@ number_of_ticks: %ld@]" + State_hash.pp + compressed_state + Raw_level_repr.pp + inbox_level + Hash.pp + predecessor + (Number_of_messages.to_int32 number_of_messages) + (Number_of_ticks.to_int32 number_of_ticks) + + let encoding = + let open Data_encoding in + conv + (fun { + compressed_state; + inbox_level; + predecessor; + number_of_messages; + number_of_ticks; + } -> + ( compressed_state, + inbox_level, + predecessor, + number_of_messages, + number_of_ticks )) + (fun ( compressed_state, + inbox_level, + predecessor, + number_of_messages, + number_of_ticks ) -> + { + compressed_state; + inbox_level; + predecessor; + number_of_messages; + number_of_ticks; + }) + (obj5 + (req "compressed_state" State_hash.encoding) + (req "inbox_level" Raw_level_repr.encoding) + (req "predecessor" Hash.encoding) + (req "number_of_messages" Number_of_messages.encoding) + (req "number_of_ticks" Number_of_ticks.encoding)) + + let hash commitment = + let commitment_bytes = + Data_encoding.Binary.to_bytes_exn encoding commitment + in + Hash.hash_bytes [commitment_bytes] +end + +type versioned = V1 of V1.t + +let versioned_encoding = + let open Data_encoding in + union + [ + case + ~title:"V1" + (Tag 0) + V1.encoding + (function V1 commitment -> Some commitment) + (fun commitment -> V1 commitment); + ] + +include V1 + +let of_versioned = function V1 commitment -> commitment [@@inline] + +let to_versioned commitment = V1 commitment [@@inline] diff --git a/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.mli index 6d86d646d696..67a140795fa9 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_commitment_repr.mli @@ -24,6 +24,14 @@ (* *) (*****************************************************************************) +open Sc_rollup_repr + +module Hash : sig + include S.HASH + + include Storage_description.INDEX with type t := t +end + (** A commitment represents a claim about the state of the Inbox and PVM at some Inbox level. @@ -46,26 +54,24 @@ [compressed_state] and [number_of_ticks] can be proven/disproven by PVM execution, or equivalently, by an interactive proof game between conflicting parties, such that a correct executor always wins the game. - *) +*) +module V1 : sig + type t = { + compressed_state : State_hash.t; + inbox_level : Raw_level_repr.t; + predecessor : Hash.t; + number_of_messages : Number_of_messages.t; + number_of_ticks : Number_of_ticks.t; + } -open Sc_rollup_repr + val pp : Format.formatter -> t -> unit -module Hash : sig - include S.HASH + val encoding : t Data_encoding.t - include Storage_description.INDEX with type t := t + val hash : t -> Hash.t end -type t = { - compressed_state : State_hash.t; - inbox_level : Raw_level_repr.t; - predecessor : Hash.t; - number_of_messages : Number_of_messages.t; - number_of_ticks : Number_of_ticks.t; -} - -val pp : Format.formatter -> t -> unit - -val encoding : t Data_encoding.t +(** Versioning, see {!Sc_rollup_data_version_sig.S} for more information. *) +include Sc_rollup_data_version_sig.S with type t = V1.t -val hash : t -> Hash.t +include module type of V1 with type t = V1.t diff --git a/src/proto_alpha/lib_protocol/sc_rollup_data_version_sig.ml b/src/proto_alpha/lib_protocol/sc_rollup_data_version_sig.ml new file mode 100644 index 000000000000..dfd261a72ee4 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_data_version_sig.ml @@ -0,0 +1,49 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs *) +(* 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. *) +(* *) +(*****************************************************************************) + +(** The values are versioned, to let the possibility to modify + the values in future iterations of the protocol. + + We allow the possibility to modify the values by introducing + a {!versioned} value that is the only values written in the storage. + + In future versions, the versioning is supposed to let us reinterpret old + stored values within the new protocol implementation. That is, each + access to the storage will transform old stored values to the + current version. +*) + +module type S = sig + type t + + type versioned + + val versioned_encoding : versioned Data_encoding.t + + val of_versioned : versioned -> t + + val to_versioned : t -> versioned +end diff --git a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml index 1cb5ce8ea5bd..28064f93948a 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml @@ -235,10 +235,10 @@ let increase_commitment_stake_count ctxt rollup node = in return (size_diff, ctxt) -(* 76 for Commitments entry + 4 for Commitment_stake_count entry +(* 77 for Commitments entry + 4 for Commitment_stake_count entry + 4 for Commitment_added entry + 0 for Staker_count_update entry *) -let commitment_storage_size_in_bytes = 84 +let commitment_storage_size_in_bytes = 85 let refine_stake ctxt rollup staker commitment = let open Lwt_tzresult_syntax in diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 629d2cc0646c..f5f916ec2278 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1490,6 +1490,50 @@ module Sc_rollup = struct end)) (Make_index (Sc_rollup_repr.Index)) + module Make_versioned (Versioned_value : sig + val name : string + + module Index : Storage_description.INDEX + + include Sc_rollup_data_version_sig.S + end) = + struct + include + Make_indexed_carbonated_data_storage + (Make_subcontext (Registered) (Indexed_context.Raw_context) + (struct + let name = [Versioned_value.name] + end)) + (Make_index (Versioned_value.Index)) + (struct + type t = Versioned_value.versioned + + let encoding = Versioned_value.versioned_encoding + end) + + type value = Versioned_value.t + + let get ctxt key = + let open Lwt_result_syntax in + let* ctxt, versioned = get ctxt key in + return (ctxt, Versioned_value.of_versioned versioned) + + let find ctxt key = + let open Lwt_result_syntax in + let* ctxt, versioned = find ctxt key in + return (ctxt, Option.map Versioned_value.of_versioned versioned) + + let update ctxt key value = + update ctxt key (Versioned_value.to_versioned value) + + let init ctxt key value = init ctxt key (Versioned_value.to_versioned value) + + let add ctxt key value = add ctxt key (Versioned_value.to_versioned value) + + let add_or_remove ctxt key value = + add_or_remove ctxt key (Option.map Versioned_value.to_versioned value) + end + module PVM_kind = Indexed_context.Make_map (struct @@ -1580,18 +1624,12 @@ module Sc_rollup = struct let encoding = Data_encoding.int32 end) - module Commitments = - Make_indexed_carbonated_data_storage - (Make_subcontext (Registered) (Indexed_context.Raw_context) - (struct - let name = ["commitments"] - end)) - (Make_index (Sc_rollup_commitment_repr.Hash)) - (struct - type t = Sc_rollup_commitment_repr.t + module Commitments = Make_versioned (struct + include Sc_rollup_commitment_repr + module Index = Hash - let encoding = Sc_rollup_commitment_repr.encoding - end) + let name = "commitments" + end) module Commitment_stake_count = Make_indexed_carbonated_data_storage diff --git a/src/proto_alpha/lib_protocol/test/integration/test_constants.ml b/src/proto_alpha/lib_protocol/test/integration/test_constants.ml index bf2ed7bd16e2..1e14f31bc583 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_constants.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_constants.ml @@ -118,27 +118,25 @@ let test_sc_rollup_max_commitment_storage_cost_lt_deposit () = *) let test_sc_rollup_commitment_storage_size () = let open Protocol in - Assert.get_some - ~loc:__LOC__ - (Alpha_context.Sc_rollup.Number_of_messages.of_int32 3l) + Assert.get_some ~loc:__LOC__ (Sc_rollup_repr.Number_of_messages.of_int32 3l) >>=? fun number_of_messages -> Assert.get_some ~loc:__LOC__ - (Alpha_context.Sc_rollup.Number_of_ticks.of_int32 1232909l) + (Sc_rollup_repr.Number_of_ticks.of_int32 1232909l) >>=? fun number_of_ticks -> let commitment = - Alpha_context.Sc_rollup.Commitment. + Sc_rollup_commitment_repr.to_versioned { - predecessor = Alpha_context.Sc_rollup.Commitment.Hash.zero; - inbox_level = Alpha_context.Raw_level.of_int32_exn 21l; + predecessor = Sc_rollup_commitment_repr.Hash.zero; + inbox_level = Raw_level_repr.of_int32_exn 21l; number_of_messages; number_of_ticks; - compressed_state = Alpha_context.Sc_rollup.State_hash.zero; + compressed_state = Sc_rollup_repr.State_hash.zero; } in let commitment_bytes = Data_encoding.Binary.to_bytes_exn - Alpha_context.Sc_rollup.Commitment.encoding + Sc_rollup_commitment_repr.versioned_encoding commitment in let level = Alpha_context.Raw_level.of_int32_exn 5l in diff --git a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out index 8e084d4161fe..76a88dcb8da5 100644 --- a/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out +++ b/tezt/tests/expected/sc_rollup.ml/Alpha- observing the correct handling of commitments in the rollup node (no_comm.out @@ -127,7 +127,7 @@ Error: ./tezos-client --wait none cement commitment '[SC_ROLLUP_COMMITMENT_HASH]' from bootstrap1 for sc rollup '[SC_ROLLUP_HASH]' Node is bootstrapped. -Estimated gas: 3451.196 units (will add 100 for safety) +Estimated gas: 3451.198 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -147,7 +147,7 @@ This sequence of operations was run: payload fees(the block proposer) ....... +ꜩ0.000653 Cement the commitment [SC_ROLLUP_COMMITMENT_HASH] in the smart contract rollup at address [SC_ROLLUP_HASH] This operation cementing a commitment on a smart contract rollup was successfully applied - Consumed gas: 3451.196 + Consumed gas: 3451.198 ./tezos-client rpc get /chains/main/blocks/head/context/constants -- GitLab