From ab671801350c6e3470a4e66e21e5819670dfd523 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Mon, 30 May 2022 13:27:54 +0200 Subject: [PATCH 1/2] Proto,Scoru: adds already disputed staker in error's payload --- .../lib_protocol/sc_rollup_errors.ml | 70 +++++++++++++++++-- .../sc_rollup_refutation_storage.ml | 7 +- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_errors.ml b/src/proto_alpha/lib_protocol/sc_rollup_errors.ml index 9509f468a14d..6f7245fb7116 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_errors.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_errors.ml @@ -44,7 +44,11 @@ type error += | (* `Temporary *) Sc_rollup_game_already_started | (* `Temporary *) Sc_rollup_wrong_turn | (* `Temporary *) Sc_rollup_no_game - | (* `Temporary *) Sc_rollup_staker_in_game + | (* `Temporary *) + Sc_rollup_staker_in_game of + [ `Refuter of Signature.public_key_hash + | `Defender of Signature.public_key_hash + | `Both of Signature.public_key_hash * Signature.public_key_hash ] | (* `Temporary *) Sc_rollup_timeout_level_not_reached | (* `Temporary *) Sc_rollup_max_number_of_messages_reached_for_commitment_period @@ -74,10 +78,66 @@ let () = `Temporary ~id:"Sc_rollup_staker_in_game" ~title:"Staker is already playing a game" - ~description:"Attempt to start a game where one staker is already busy" - Data_encoding.unit - (function Sc_rollup_staker_in_game -> Some () | _ -> None) - (fun () -> Sc_rollup_staker_in_game) ; + ~description:"Attempted to start a game where one staker is already busy" + ~pp:(fun ppf staker -> + let busy ppf = function + | `Refuter sc -> + Format.fprintf + ppf + "the refuter (%a) is" + Signature.Public_key_hash.pp + sc + | `Defender sc -> + Format.fprintf + ppf + "the defender (%a) is" + Signature.Public_key_hash.pp + sc + | `Both (refuter, defender) -> + Format.fprintf + ppf + "both the refuter (%a) and the defender (%a) are" + Signature.Public_key_hash.pp + refuter + Signature.Public_key_hash.pp + defender + in + Format.fprintf + ppf + "Attempted to start a game where %a already busy." + busy + staker) + Data_encoding.( + obj1 + (req + "staker_in_game" + (union + [ + case + (Tag 0) + ~title:"Refuter" + Signature.Public_key_hash.encoding + (function `Refuter sc -> Some sc | _ -> None) + (fun sc -> `Refuter sc); + case + (Tag 1) + ~title:"Defender" + Signature.Public_key_hash.encoding + (function `Defender sc -> Some sc | _ -> None) + (fun sc -> `Defender sc); + case + (Tag 2) + ~title:"Both" + (obj2 + (req "refuter" Signature.Public_key_hash.encoding) + (req "defender" Signature.Public_key_hash.encoding)) + (function + | `Both (refuter, defender) -> Some (refuter, defender) + | _ -> None) + (fun (refuter, defender) -> `Both (refuter, defender)); + ]))) + (function Sc_rollup_staker_in_game x -> Some x | _ -> None) + (fun x -> Sc_rollup_staker_in_game x) ; register_error_kind `Temporary ~id:"Sc_rollup_timeout_level_not_reached" 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 c07fc2b390ad..dbef6ab064a3 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml @@ -186,7 +186,12 @@ let init_game ctxt rollup ~refuter ~defender = let* _ = match (opp_1, opp_2) with | None, None -> return () - | _ -> fail Sc_rollup_staker_in_game + | Some refuter, None -> + fail (Sc_rollup_staker_in_game (`Refuter refuter)) + | None, Some defender -> + fail (Sc_rollup_staker_in_game (`Defender defender)) + | Some refuter, Some defender -> + fail (Sc_rollup_staker_in_game (`Both (refuter, defender))) in let* (_, child), ctxt = get_conflict_point ctxt rollup refuter defender in let* child, ctxt = -- GitLab From 5a2d742c289e84d277a12d06d6f4934661bbeab0 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Mon, 30 May 2022 13:28:34 +0200 Subject: [PATCH 2/2] Proto,Scoru: avoid double look-up for commitment infos --- .../lib_protocol/alpha_context.mli | 7 ++++- .../sc_rollup_refutation_storage.ml | 26 ++++++++++++------- .../sc_rollup_refutation_storage.mli | 7 ++++- .../test/unit/test_sc_rollup_storage.ml | 20 +++++++------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index bf05cfa85ca4..712e27b6aa05 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2923,7 +2923,12 @@ module Sc_rollup : sig end module Refutation_storage : sig - type conflict_point = Commitment.Hash.t * Commitment.Hash.t + type point = { + commitment : Sc_rollup_commitment_repr.t; + hash : Sc_rollup_commitment_repr.Hash.t; + } + + type conflict_point = point * point val game_move : context -> 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 dbef6ab064a3..04f94ea83152 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml @@ -31,7 +31,12 @@ module Commitment_storage = Sc_rollup_commitment_storage module Commitment_hash = Commitment.Hash module Stake_storage = Sc_rollup_stake_storage -type conflict_point = Commitment_hash.t * Commitment_hash.t +type point = { + commitment : Sc_rollup_commitment_repr.t; + hash : Commitment_hash.t; +} + +type conflict_point = point * point (** TODO: #2902 replace with protocol constant and consider good value. *) let timeout_period_in_blocks = 500 @@ -118,7 +123,10 @@ let get_conflict_point ctxt rollup staker1 staker2 = if Commitment_hash.(commit1_info.predecessor = commit2_info.predecessor) then (* Same predecessor means we've found the conflict points. *) - return ((commit1, commit2), ctxt) + return + ( ( {hash = commit1; commitment = commit1_info}, + {hash = commit2; commitment = commit2_info} ), + ctxt ) else (* Different predecessors means they run in parallel. *) (traverse_in_parallel [@ocaml.tailcall]) @@ -193,12 +201,10 @@ let init_game ctxt rollup ~refuter ~defender = | Some refuter, Some defender -> fail (Sc_rollup_staker_in_game (`Both (refuter, defender))) in - let* (_, child), ctxt = get_conflict_point ctxt rollup refuter defender in - let* child, ctxt = - Commitment_storage.get_commitment_unsafe ctxt rollup child - in - let* parent, ctxt = - Commitment_storage.get_commitment_unsafe ctxt rollup child.predecessor + let* ( ( {hash = _parent; commitment = parent_info}, + {hash = _child; commitment = child_info} ), + ctxt ) = + get_conflict_point ctxt rollup refuter defender in let* ctxt, inbox = Store.Inbox.get ctxt rollup in let* kind = Store.PVM_kind.get ctxt rollup in @@ -206,8 +212,8 @@ let init_game ctxt rollup ~refuter ~defender = Sc_rollup_game_repr.initial inbox ~pvm_name:(Sc_rollups.Kind.name_of kind) - ~parent - ~child + ~parent:parent_info + ~child:child_info ~refuter ~defender in 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 44a9bdcd1e96..8e173fcfd172 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli @@ -26,7 +26,12 @@ module Commitment_hash = Sc_rollup_commitment_repr.Hash -type conflict_point = Commitment_hash.t * Commitment_hash.t +type point = { + commitment : Sc_rollup_commitment_repr.t; + hash : Commitment_hash.t; +} + +type conflict_point = point * point (** [game_move ctxt rollup player opponent refutation is_opening_move] handles the storage-side logic for when one of the players makes a 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 ea4308be7f63..dae2d174aea3 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 @@ -1518,7 +1518,7 @@ let test_finds_conflict_point_at_lcc () = staker1 staker2 in - assert_commitment_hash_equal ~loc:__LOC__ ctxt left c1 + assert_commitment_hash_equal ~loc:__LOC__ ctxt left.hash c1 let test_finds_conflict_point_beneath_lcc () = let* ctxt, rollup, staker1, staker2 = @@ -1587,8 +1587,8 @@ let test_finds_conflict_point_beneath_lcc () = staker1 staker2 in - let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt left c2 in - assert_commitment_hash_equal ~loc:__LOC__ ctxt right c3 + let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt left.hash c2 in + assert_commitment_hash_equal ~loc:__LOC__ ctxt right.hash c3 let test_conflict_point_is_first_point_of_disagreement () = let* ctxt, rollup, staker1, staker2 = @@ -1675,8 +1675,8 @@ let test_conflict_point_is_first_point_of_disagreement () = staker1 staker2 in - let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt left c2 in - assert_commitment_hash_equal ~loc:__LOC__ ctxt right c3 + let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt left.hash c2 in + assert_commitment_hash_equal ~loc:__LOC__ ctxt right.hash c3 let test_conflict_point_computation_fits_in_gas_limit () = (* Worst case of conflict point computation: two branches of maximum @@ -1769,8 +1769,10 @@ let test_conflict_point_computation_fits_in_gas_limit () = staker1 staker2 in - let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt left branch_1.(0) in - assert_commitment_hash_equal ~loc:__LOC__ ctxt right branch_2.(0) + let* () = + assert_commitment_hash_equal ~loc:__LOC__ ctxt left.hash branch_1.(0) + in + assert_commitment_hash_equal ~loc:__LOC__ ctxt right.hash branch_2.(0) let test_no_conflict_point_one_staker_at_lcc_preboot () = let* ctxt, rollup, staker1, staker2 = @@ -2210,8 +2212,8 @@ let test_concurrent_refinement_point_of_conflict () = staker1 staker2 in - let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt c1 c1' in - assert_commitment_hash_equal ~loc:__LOC__ ctxt c2 c2' + let* () = assert_commitment_hash_equal ~loc:__LOC__ ctxt c1.hash c1'.hash in + assert_commitment_hash_equal ~loc:__LOC__ ctxt c2.hash c2'.hash let test_concurrent_refinement_cement () = let* before_ctxt, rollup, staker1, staker2 = -- GitLab