diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index a3c629dcaf16eeb0f7a065d5cdf97617cc379b98..6dbc58cba712e1218b498a7d5820937a7a499347 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -68,6 +68,7 @@ "Sc_rollup_inbox_repr", "Sc_rollup_commitment_repr", "Sc_rollup_proof_repr", + "Sc_rollup_costs", "Sc_rollup_game_repr", "Tx_rollup_level_repr", "Tx_rollup_l2_proof", @@ -162,7 +163,6 @@ "Liquidity_baking_lqt", "Liquidity_baking_migration", - "Sc_rollup_costs", "Sc_rollup_errors", "Sc_rollup_inbox_storage", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index 739b8eaa7af8ffeee34d3e54945c076ca9d0aaab..1cd0cd5a3aa63559d8d2028356661697ba6dfc88 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -94,6 +94,7 @@ Sc_rollup_inbox_repr Sc_rollup_commitment_repr Sc_rollup_proof_repr + Sc_rollup_costs Sc_rollup_game_repr Tx_rollup_level_repr Tx_rollup_l2_proof @@ -178,7 +179,6 @@ Liquidity_baking_cpmm Liquidity_baking_lqt Liquidity_baking_migration - Sc_rollup_costs Sc_rollup_errors Sc_rollup_inbox_storage Legacy_script_patches @@ -370,6 +370,7 @@ 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 + sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_game_repr.ml sc_rollup_game_repr.mli tx_rollup_level_repr.ml tx_rollup_level_repr.mli tx_rollup_l2_proof.ml tx_rollup_l2_proof.mli @@ -457,7 +458,6 @@ liquidity_baking_cpmm.ml liquidity_baking_lqt.ml liquidity_baking_migration.ml liquidity_baking_migration.mli - sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_errors.ml sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli legacy_script_patches.ml @@ -629,6 +629,7 @@ 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 + sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_game_repr.ml sc_rollup_game_repr.mli tx_rollup_level_repr.ml tx_rollup_level_repr.mli tx_rollup_l2_proof.ml tx_rollup_l2_proof.mli @@ -716,7 +717,6 @@ liquidity_baking_cpmm.ml liquidity_baking_lqt.ml liquidity_baking_migration.ml liquidity_baking_migration.mli - sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_errors.ml sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli legacy_script_patches.ml @@ -893,6 +893,7 @@ 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 + sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_game_repr.ml sc_rollup_game_repr.mli tx_rollup_level_repr.ml tx_rollup_level_repr.mli tx_rollup_l2_proof.ml tx_rollup_l2_proof.mli @@ -980,7 +981,6 @@ liquidity_baking_cpmm.ml liquidity_baking_lqt.ml liquidity_baking_migration.ml liquidity_baking_migration.mli - sc_rollup_costs.ml sc_rollup_costs.mli sc_rollup_errors.ml sc_rollup_inbox_storage.ml sc_rollup_inbox_storage.mli legacy_script_patches.ml diff --git a/src/proto_alpha/lib_protocol/sc_rollup_costs.ml b/src/proto_alpha/lib_protocol/sc_rollup_costs.ml index 4f538e4fedaba0f14c3c82b363c5f73c6b587f91..b8730fe59aa43edbb99f180e282af7a10b6f0fec 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_costs.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_costs.ml @@ -141,3 +141,17 @@ let cost_hash_bytes ~bytes_len = let open S_syntax in let v0 = S.safe_int bytes_len in S.safe_int 430 + v0 + (v0 lsr 3) + +let cost_compare a_size_in_bytes b_size_in_bytes = + let open S_syntax in + let size_in_bytes = Compare.Int.min a_size_in_bytes b_size_in_bytes in + let v0 = S.safe_int size_in_bytes in + S.safe_int 35 + ((v0 lsr 6) + (v0 lsr 7)) + +let cost_search_in_tick_list len tick_size = + S_syntax.(S.safe_int len * cost_compare tick_size tick_size) + +let cost_check_dissection ~number_of_states ~tick_size ~hash_size = + let open S_syntax in + cost_search_in_tick_list number_of_states tick_size + + (S.safe_int 2 * cost_compare hash_size hash_size) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_costs.mli b/src/proto_alpha/lib_protocol/sc_rollup_costs.mli index 5b47cc667a711f63cfe7ec33be85c8e216edfffa..f4c05fe05e46c4387efeda7c1d6c1237d4ec7693 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_costs.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_costs.mli @@ -80,3 +80,11 @@ val cost_serialize_external_inbox_message : bytes_len:int -> Gas_limit_repr.cost (** [cost_hash_bytes ~bytes_len] is the cost of hashing [bytes_len] bytes. *) val cost_hash_bytes : bytes_len:int -> Gas_limit_repr.cost + +(** [cost_check_dissection ~number_of_states ~tick_size ~hash_size] is the cost + of checking that a dissection with a given [number_of_states] used in a + refutation game is well-formed. This includes the comparison of a linear + number of ticks as well as the verification of two hashes of given + [hash_size]. *) +val cost_check_dissection : + number_of_states:int -> tick_size:int -> hash_size:int -> Gas_limit_repr.cost diff --git a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml index 5e8aacaa7098988e90321b86b3b3bafd0fb98144..4a59aa1c40ad0821639ff1ca530d266c39a92ca8 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml @@ -808,6 +808,20 @@ let loser_of_results ~alice_result ~bob_result = | false, true -> Some Alice | true, false -> Some Bob +(* TODO: https://gitlab.com/tezos/tezos/-/issues/2926 + This function is incomplete and needs to account for additional gas. *) +let cost_play _game refutation = + match refutation.step with + | Dissection states -> + let number_of_states = List.length states in + let hash_size = State_hash.size in + let tick_size = Sc_rollup_tick_repr.size_in_bytes refutation.choice in + Sc_rollup_costs.cost_check_dissection + ~number_of_states + ~tick_size + ~hash_size + | Proof _proof -> Gas_limit_repr.free + let play kind dal_parameters ~dal_attestation_lag ~stakers metadata game refutation = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli index 5294dea503879ac9eb0b77eefa47fe2f7460d52a..8cd384a2492df0473ba3a34b0bd8113a54d51d42 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli @@ -406,6 +406,10 @@ type timeout = { val timeout_encoding : timeout Data_encoding.t +(** [cost_play game refutation] returns the gas cost of [play] applied with [game] + and [refutation]. *) +val cost_play : t -> refutation -> Gas_limit_repr.cost + module Internal_for_tests : sig (** Checks that the tick count chosen by the current move is one of the ones in the current dissection. Returns a tuple containing diff --git a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml index 6c7c9709eb84d743fd4a0de4bfbb32f12843523f..ee70932874a15e5d490a451e56ca566fede98a32 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml @@ -294,6 +294,8 @@ let game_move ctxt rollup ~player ~opponent refutation = in let* ctxt, metadata = Sc_rollup_storage.get_metadata ctxt rollup in let dal = (Constants_storage.parametric ctxt).dal in + let play_cost = Sc_rollup_game_repr.cost_play game refutation in + let*? ctxt = Raw_context.consume_gas ctxt play_cost in let* move_result = Sc_rollup_game_repr.play kind diff --git a/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.ml index 3c90af0b6b17da1fcb7819f8e1d9b0c516027ea2..690a612e7fd11170172676aada4b56e6b92195f9 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.ml @@ -61,4 +61,9 @@ let ( = ) = equal let ( <> ) x y = not (x = y) +let size_in_bytes tick = + (* Same definition as in {!Michelson_v1_gas}. *) + let bits = numbits tick in + (7 + bits) / 8 + module Map = Map.Make (Z) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.mli index e3a584b5da2108b32bd7f628a0733b81c669b921..1344b53f55026ead3132d14c95fee6f46659cc7c 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_tick_repr.mli @@ -61,6 +61,10 @@ val of_z : Z.t -> t val to_z : t -> Z.t +(** [size_in_bytes tick] is the size in bytes of [tick]'s internal + representation. This function is used by the gas model. *) +val size_in_bytes : t -> int + val encoding : t Data_encoding.t val pp : Format.formatter -> t -> unit