From 087f0b49d35ae125098ba356e70597b54e480842 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Tue, 7 Jun 2022 15:05:54 +0200 Subject: [PATCH] Scoru,Proto: make [stakers] both a record and private --- .../lib_client/operation_result.ml | 6 +-- .../lib_protocol/alpha_context.mli | 20 +++++----- src/proto_alpha/lib_protocol/apply.ml | 7 +--- .../lib_protocol/operation_repr.ml | 8 +--- .../lib_protocol/operation_repr.mli | 2 +- .../lib_protocol/sc_rollup_game_repr.ml | 40 +++++++++++-------- .../lib_protocol/sc_rollup_game_repr.mli | 8 ++-- .../sc_rollup_refutation_storage.ml | 30 ++++++-------- .../sc_rollup_refutation_storage.mli | 4 +- ... client) RPC regression tests- mempool.out | 38 +++++++++++------- ...e proxy) RPC regression tests- mempool.out | 38 +++++++++++------- 11 files changed, 108 insertions(+), 93 deletions(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 565636a05f2d..bf7586459ec5 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -321,15 +321,15 @@ let pp_manager_operation_content (type kind) source pp_result ppf Sc_rollup.Game.pp_refutation refutation (if is_opening_move then "(opening move of game)" else "") - | Sc_rollup_timeout {rollup; stakers} -> + | Sc_rollup_timeout {rollup; stakers = {alice; bob}} -> Format.fprintf ppf "Punish one of the two stakers %a and %a by timeout in the smart \ contract rollup at address %a" Sc_rollup.Staker.pp - (fst stakers) + alice Sc_rollup.Staker.pp - (snd stakers) + bob Sc_rollup.Address.pp rollup | Sc_rollup_atomic_batch diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 96f390c51cfb..9d24201b85df 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2860,6 +2860,12 @@ module Sc_rollup : sig dissection : (State_hash.t option * Tick.t) list; } + module Index : sig + type t = private {alice : Staker.t; bob : Staker.t} + + val make : Staker.t -> Staker.t -> t + end + val opponent : player -> player type step = @@ -2929,10 +2935,7 @@ module Sc_rollup : sig end module Refutation_storage : sig - type point = { - commitment : Sc_rollup_commitment_repr.t; - hash : Sc_rollup_commitment_repr.Hash.t; - } + type point = {commitment : Commitment.t; hash : Commitment.Hash.t} type conflict_point = point * point @@ -2946,15 +2949,12 @@ module Sc_rollup : sig (Game.outcome option * context) tzresult Lwt.t val timeout : - context -> - t -> - Staker.t * Staker.t -> - (Game.outcome * context) tzresult Lwt.t + context -> t -> Game.Index.t -> (Game.outcome * context) tzresult Lwt.t val apply_outcome : context -> t -> - Staker.t * Staker.t -> + Game.Index.t -> Game.outcome -> (Game.status * context * Receipt.balance_updates) tzresult Lwt.t end @@ -3492,7 +3492,7 @@ and _ manager_operation = -> Kind.sc_rollup_refute manager_operation | Sc_rollup_timeout : { rollup : Sc_rollup.t; - stakers : Sc_rollup.Staker.t * Sc_rollup.Staker.t; + stakers : Sc_rollup.Game.Index.t; } -> Kind.sc_rollup_timeout manager_operation | Sc_rollup_atomic_batch : { diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index c24991c1f402..6d7a25c7228d 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1817,11 +1817,8 @@ let apply_external_manager_operation_content : (match outcome with | None -> return (Sc_rollup.Game.Ongoing, ctxt, []) | Some o -> - Sc_rollup.Refutation_storage.apply_outcome - ctxt - rollup - (source, opponent) - o) + let stakers = Sc_rollup.Game.Index.make source opponent in + Sc_rollup.Refutation_storage.apply_outcome ctxt rollup stakers o) >>=? fun (status, ctxt, balance_updates) -> let consumed_gas = Gas.consumed ~since:before_operation ~until:ctxt in let result = diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index 774a5cf8f30c..616fd20326c5 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -414,7 +414,7 @@ and _ manager_operation = -> Kind.sc_rollup_refute manager_operation | Sc_rollup_timeout : { rollup : Sc_rollup_repr.t; - stakers : Sc_rollup_repr.Staker.t * Sc_rollup_repr.Staker.t; + stakers : Sc_rollup_game_repr.Index.t; } -> Kind.sc_rollup_timeout manager_operation | Sc_rollup_atomic_batch : { @@ -1078,11 +1078,7 @@ module Encoding = struct encoding = obj2 (req "rollup" Sc_rollup_repr.encoding) - (req - "stakers" - (tup2 - Sc_rollup_repr.Staker.encoding - Sc_rollup_repr.Staker.encoding)); + (req "stakers" Sc_rollup_game_repr.Index.encoding); select = (function | Manager (Sc_rollup_timeout _ as op) -> Some op | _ -> None); diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index c9f25012ed2e..53091cca0b43 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -480,7 +480,7 @@ and _ manager_operation = -> Kind.sc_rollup_refute manager_operation | Sc_rollup_timeout : { rollup : Sc_rollup_repr.t; - stakers : Sc_rollup_repr.Staker.t * Sc_rollup_repr.Staker.t; + stakers : Sc_rollup_game_repr.Index.t; } -> Kind.sc_rollup_timeout manager_operation (* [Sc_rollup_atomic_batch] executes an atomic batch of transactions 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 4c27d8180e90..8c36fdbf442d 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.ml @@ -107,19 +107,32 @@ let pp ppf game = game.pvm_name module Index = struct - type t = Staker.t * Staker.t + type t = {alice : Staker.t; bob : Staker.t} - let encoding = Data_encoding.tup2 Staker.encoding Staker.encoding + let make a b = + let alice, bob = + if Compare.Int.(Staker.compare a b > 0) then (b, a) else (a, b) + in + {alice; bob} + + let encoding = + let open Data_encoding in + conv + (fun {alice; bob} -> (alice, bob)) + (fun (alice, bob) -> make alice bob) + (obj2 (req "alice" Staker.encoding) (req "bob" Staker.encoding)) - let compare (a, b) (c, d) = + let compare {alice = a; bob = b} {alice = c; bob = d} = match Staker.compare a c with 0 -> Staker.compare b d | x -> x - let to_path (a, b) p = Staker.to_b58check a :: Staker.to_b58check b :: p + let to_path {alice; bob} p = + Staker.to_b58check alice :: Staker.to_b58check bob :: p let both_of_b58check_opt (a, b) = - Option.bind (Staker.of_b58check_opt b) (fun b_staker -> - Option.bind (Staker.of_b58check_opt a) (fun a_staker -> - Some (a_staker, b_staker))) + let ( let* ) = Option.bind in + let* a_staker = Staker.of_b58check_opt a in + let* b_staker = Staker.of_b58check_opt b in + Some (make a_staker b_staker) let of_path = function [a; b] -> both_of_b58check_opt (a, b) | _ -> None @@ -129,8 +142,8 @@ module Index = struct let descr = "A pair of stakers that index a smart contract rollup refutation game." in - let construct (a, b) = - Format.sprintf "%s-%s" (Staker.to_b58check a) (Staker.to_b58check b) + let construct {alice; bob} = + Format.sprintf "%s-%s" (Staker.to_b58check alice) (Staker.to_b58check bob) in let destruct s = match String.split_on_char '-' s with @@ -143,17 +156,12 @@ module Index = struct in RPC_arg.make ~descr ~name:"game_index" ~construct ~destruct () - let normalize (a, b) = - match Staker.compare a b with 1 -> (b, a) | _ -> (a, b) - - let staker stakers player = - let alice, bob = normalize stakers in - match player with Alice -> alice | Bob -> bob + let staker {alice; bob} = function Alice -> alice | Bob -> bob end let initial inbox ~pvm_name ~(parent : Sc_rollup_commitment_repr.t) ~(child : Sc_rollup_commitment_repr.t) ~refuter ~defender = - let alice, _ = Index.normalize (refuter, defender) in + let ({alice; _} : Index.t) = Index.make refuter defender in let alice_to_play = Staker.equal alice refuter in let tick = Sc_rollup_tick_repr.of_number_of_ticks child.number_of_ticks 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 2c33f0fe46cb..b62fc8196484 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_game_repr.mli @@ -180,7 +180,7 @@ val pp_dissection : val pp : Format.formatter -> t -> unit module Index : sig - type t = Staker.t * Staker.t + type t = private {alice : Staker.t; bob : Staker.t} (** [to_path i p] returns a new path with the path to the game indexed by [i] added as a prefix to path [p]. See [Path_encoding] module. *) @@ -196,13 +196,11 @@ module Index : sig val compare : t -> t -> int - (** The 'normal form' for indices is when the two stakers appear in - the pair in lexical order (we just use [Staker.compare]). *) - val normalize : t -> t + val make : Staker.t -> Staker.t -> t (** Given an index in normal form, resolve a given [player] ([Alice] or [Bob]) to the actual staker they represent. *) - val staker : Staker.t * Staker.t -> player -> Staker.t + val staker : t -> player -> Staker.t end (** To begin a game, first the conflict point in the commit tree is 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 04f94ea83152..08b55eca9bd5 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml @@ -146,7 +146,6 @@ let get_conflict_point ctxt rollup staker1 staker2 = let get_game ctxt rollup stakers = let open Lwt_tzresult_syntax in - let stakers = Sc_rollup_game_repr.Index.normalize stakers in let* ctxt, game = Store.Game.find (ctxt, rollup) stakers in match game with Some g -> return (g, ctxt) | None -> fail Sc_rollup_no_game @@ -184,7 +183,7 @@ let get_game ctxt rollup stakers = } *) let init_game ctxt rollup ~refuter ~defender = let open Lwt_tzresult_syntax in - let stakers = Sc_rollup_game_repr.Index.normalize (refuter, defender) in + let stakers = Sc_rollup_game_repr.Index.make refuter defender in let* ctxt, game = Store.Game.find (ctxt, rollup) stakers in match game with | Some _ -> fail Sc_rollup_game_already_started @@ -227,11 +226,13 @@ let init_game ctxt rollup ~refuter ~defender = let game_move ctxt rollup ~player ~opponent refutation ~is_opening_move = let open Lwt_tzresult_syntax in - let alice, bob = Sc_rollup_game_repr.Index.normalize (player, opponent) in + let ({alice; bob} as stakers : Sc_rollup_game_repr.Index.t) = + Sc_rollup_game_repr.Index.make player opponent + in let* game, ctxt = if is_opening_move then init_game ctxt rollup ~refuter:player ~defender:opponent - else get_game ctxt rollup (alice, bob) + else get_game ctxt rollup stakers in let* () = fail_unless @@ -245,25 +246,21 @@ let game_move ctxt rollup ~player ~opponent refutation ~is_opening_move = match move_result with | Either.Left outcome -> return (Some outcome, ctxt) | Either.Right new_game -> - let* ctxt, _ = Store.Game.update (ctxt, rollup) (alice, bob) new_game in + let* ctxt, _ = Store.Game.update (ctxt, rollup) stakers new_game in let* ctxt, _ = - Store.Game_timeout.update - (ctxt, rollup) - (alice, bob) - (timeout_level ctxt) + Store.Game_timeout.update (ctxt, rollup) stakers (timeout_level ctxt) in return (None, ctxt) let timeout ctxt rollup stakers = let open Lwt_tzresult_syntax in let level = (Raw_context.current_level ctxt).level in - let alice, bob = Sc_rollup_game_repr.Index.normalize stakers in - let* ctxt, game = Store.Game.find (ctxt, rollup) (alice, bob) in + let* ctxt, game = Store.Game.find (ctxt, rollup) stakers in match game with | None -> fail Sc_rollup_no_game | Some game -> let* ctxt, timeout_level = - Store.Game_timeout.get (ctxt, rollup) (alice, bob) + Store.Game_timeout.get (ctxt, rollup) stakers in let* () = fail_unless @@ -274,15 +271,14 @@ let timeout ctxt rollup stakers = let apply_outcome ctxt rollup stakers (outcome : Sc_rollup_game_repr.outcome) = let open Lwt_tzresult_syntax in - let alice, bob = Sc_rollup_game_repr.Index.normalize stakers in let losing_staker = Sc_rollup_game_repr.Index.staker stakers outcome.loser in let* ctxt, balance_updates = Stake_storage.remove_staker ctxt rollup losing_staker in - let* ctxt, _, _ = Store.Game.remove (ctxt, rollup) (alice, bob) in - let* ctxt, _, _ = Store.Game_timeout.remove (ctxt, rollup) (alice, bob) in - let* ctxt, _, _ = Store.Opponent.remove (ctxt, rollup) alice in - let* ctxt, _, _ = Store.Opponent.remove (ctxt, rollup) bob in + let* ctxt, _, _ = Store.Game.remove (ctxt, rollup) stakers in + let* ctxt, _, _ = Store.Game_timeout.remove (ctxt, rollup) stakers in + let* ctxt, _, _ = Store.Opponent.remove (ctxt, rollup) stakers.alice in + let* ctxt, _, _ = Store.Opponent.remove (ctxt, rollup) stakers.bob in return ( Sc_rollup_game_repr.Ended (outcome.reason, losing_staker), ctxt, diff --git a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli index 8e173fcfd172..faac56a0eaaf 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli @@ -104,7 +104,7 @@ val game_move : val timeout : Raw_context.t -> Sc_rollup_repr.t -> - Sc_rollup_repr.Staker.t * Sc_rollup_repr.Staker.t -> + Sc_rollup_game_repr.Index.t -> (Sc_rollup_game_repr.outcome * Raw_context.t) tzresult Lwt.t (** [apply_outcome ctxt rollup outcome] takes an [outcome] produced @@ -128,7 +128,7 @@ val timeout : val apply_outcome : Raw_context.t -> Sc_rollup_repr.t -> - Sc_rollup_repr.Staker.t * Sc_rollup_repr.Staker.t -> + Sc_rollup_game_repr.Index.t -> Sc_rollup_game_repr.outcome -> (Sc_rollup_game_repr.status * Raw_context.t * Receipt_repr.balance_updates) tzresult diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- mempool.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- mempool.out index 699a66517216..dc708871c7c2 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- mempool.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- mempool.out @@ -6056,16 +6056,20 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "$ref": "#/definitions/alpha.rollup_address" }, "stakers": { - "type": "array", - "items": [ - { + "type": "object", + "properties": { + "alice": { "$ref": "#/definitions/Signature.Public_key_hash" }, - { + "bob": { "$ref": "#/definitions/Signature.Public_key_hash" } + }, + "required": [ + "bob", + "alice" ], - "additionalItems": false + "additionalProperties": false } }, "required": [ @@ -15422,26 +15426,28 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "encoding": { "fields": [ { + "name": "alice", "layout": { "name": "public_key_hash", "kind": "Ref" }, - "kind": "anon", "data_kind": { "size": 21, "kind": "Float" - } + }, + "kind": "named" }, { + "name": "bob", "layout": { "name": "public_key_hash", "kind": "Ref" }, - "kind": "anon", "data_kind": { "size": 21, "kind": "Float" - } + }, + "kind": "named" } ] } @@ -21522,16 +21528,20 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "$ref": "#/definitions/alpha.rollup_address" }, "stakers": { - "type": "array", - "items": [ - { + "type": "object", + "properties": { + "alice": { "$ref": "#/definitions/Signature.Public_key_hash" }, - { + "bob": { "$ref": "#/definitions/Signature.Public_key_hash" } + }, + "required": [ + "bob", + "alice" ], - "additionalItems": false + "additionalProperties": false } }, "required": [ diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- mempool.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- mempool.out index e8b0b3ae6814..190541fe1c9e 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- mempool.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- mempool.out @@ -6077,16 +6077,20 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "$ref": "#/definitions/alpha.rollup_address" }, "stakers": { - "type": "array", - "items": [ - { + "type": "object", + "properties": { + "alice": { "$ref": "#/definitions/Signature.Public_key_hash" }, - { + "bob": { "$ref": "#/definitions/Signature.Public_key_hash" } + }, + "required": [ + "bob", + "alice" ], - "additionalItems": false + "additionalProperties": false } }, "required": [ @@ -15443,26 +15447,28 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "encoding": { "fields": [ { + "name": "alice", "layout": { "name": "public_key_hash", "kind": "Ref" }, - "kind": "anon", "data_kind": { "size": 21, "kind": "Float" - } + }, + "kind": "named" }, { + "name": "bob", "layout": { "name": "public_key_hash", "kind": "Ref" }, - "kind": "anon", "data_kind": { "size": 21, "kind": "Float" - } + }, + "kind": "named" } ] } @@ -21543,16 +21549,20 @@ curl -s 'http://localhost:[PORT]/describe/chains/main/mempool?recurse=yes' "$ref": "#/definitions/alpha.rollup_address" }, "stakers": { - "type": "array", - "items": [ - { + "type": "object", + "properties": { + "alice": { "$ref": "#/definitions/Signature.Public_key_hash" }, - { + "bob": { "$ref": "#/definitions/Signature.Public_key_hash" } + }, + "required": [ + "bob", + "alice" ], - "additionalItems": false + "additionalProperties": false } }, "required": [ -- GitLab