From ed1763ff52f2aba13abae4718a6a5e91a2fd0459 Mon Sep 17 00:00:00 2001 From: Joel Bjornson Date: Wed, 23 Nov 2022 17:20:07 +0000 Subject: [PATCH 1/3] Proto/SCORU: make cost visible for game-repr module --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 2 +- src/proto_alpha/lib_protocol/dune | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index a3c629dcaf16..6dbc58cba712 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 739b8eaa7af8..1cd0cd5a3aa6 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 -- GitLab From a7096e8578911494d7a3c60cd2ebf7c271b3206d Mon Sep 17 00:00:00 2001 From: Joel Bjornson Date: Wed, 23 Nov 2022 14:34:28 +0000 Subject: [PATCH 2/3] Proto/SCORU: stub for play game cost --- src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml | 9 +++++++++ src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli | 4 ++++ .../lib_protocol/sc_rollup_refutation_storage.ml | 2 ++ 3 files changed, 15 insertions(+) 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 5e8aacaa7098..6fe43916fd46 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,15 @@ let loser_of_results ~alice_result ~bob_result = | false, true -> Some Alice | true, false -> Some Bob +let cost_play _game refutation = + match refutation.step with + | Dissection _states -> + (* TODO: plugin gas cost for dissections. *) + Gas_limit_repr.free + | Proof _proof -> + (* TODO: plugin gas cost for 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 5294dea50387..8cd384a2492d 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 6c7c9709eb84..ee70932874a1 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 -- GitLab From 2744ae5ffd3ed17ed622678fdf2586d62f5fb76f Mon Sep 17 00:00:00 2001 From: Joel Bjornson Date: Wed, 23 Nov 2022 14:59:56 +0000 Subject: [PATCH 3/3] Proto/SCORU: check dissection cost --- src/proto_alpha/lib_protocol/sc_rollup_costs.ml | 14 ++++++++++++++ .../lib_protocol/sc_rollup_costs.mli | 8 ++++++++ .../lib_protocol/sc_rollup_game_repr.ml | 17 +++++++++++------ .../lib_protocol/sc_rollup_tick_repr.ml | 5 +++++ .../lib_protocol/sc_rollup_tick_repr.mli | 4 ++++ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_costs.ml b/src/proto_alpha/lib_protocol/sc_rollup_costs.ml index 4f538e4fedab..b8730fe59aa4 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 5b47cc667a71..f4c05fe05e46 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 6fe43916fd46..4a59aa1c40ad 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml @@ -808,14 +808,19 @@ 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 -> - (* TODO: plugin gas cost for dissections. *) - Gas_limit_repr.free - | Proof _proof -> - (* TODO: plugin gas cost for proof. *) - Gas_limit_repr.free + | 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 = 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 3c90af0b6b17..690a612e7fd1 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 e3a584b5da21..1344b53f5502 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 -- GitLab