From aa03e2c38ad738b532e3ff602bd3debd4e9711c9 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Mon, 26 Sep 2022 14:16:48 +0200 Subject: [PATCH 1/3] Proto,Test: add helpers for frozen bonds --- .../lib_protocol/test/helpers/assert.ml | 69 ++++++++++++++----- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/assert.ml b/src/proto_alpha/lib_protocol/test/helpers/assert.ml index 49b4e62d7998..b0609e6506fe 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/assert.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/assert.ml @@ -214,27 +214,60 @@ open Context (* Some asserts for account operations *) -(** [balance_is b c amount] checks that the current balance of contract [c] is - [amount]. - Default balance type is [Main], pass [~kind] with [Deposit], [Fees] or - [Rewards] for the others. *) -let balance_is ~loc b contract expected = - Contract.balance b contract >>=? fun balance -> - equal_tez ~loc balance expected - -(** [balance_was_operated ~operand b c old_balance amount] checks that the - current balance of contract [c] is [operand old_balance amount] and - returns the current balance. - Default balance type is [Main], pass [~kind] with [Deposit], [Fees] or - [Rewards] for the others. *) -let balance_was_operated ~operand ~loc b contract old_balance amount = +let contract_property_is property ~loc b contract expected = + property b contract >>=? fun balance -> equal_tez ~loc balance expected + +(** [balance_is b c amount] checks that the current balance [b] of contract [c] + is [amount]. +*) +let balance_is = contract_property_is Contract.balance + +(** [frozen_bonds_is b c amount] checks that the current frozen bonds of + contract [c] is [amount]. +*) +let frozen_bonds_is = contract_property_is Contract.frozen_bonds + +let balance_or_frozen_bonds_was_operated ~is_balance ~operand ~loc b contract + old_balance amount = operand old_balance amount |> Environment.wrap_tzresult >>?= fun expected -> - balance_is ~loc b contract expected + let f = if is_balance then balance_is else frozen_bonds_is in + f ~loc b contract expected +(** [balance_was_credited ~loc ctxt contract old_balance amount] checks + that [contract]'s balance was credited [amount] tez in comparison to + [old_balance]. +*) let balance_was_credited = - balance_was_operated ~operand:Alpha_context.Tez.( +? ) - -let balance_was_debited = balance_was_operated ~operand:Alpha_context.Tez.( -? ) + balance_or_frozen_bonds_was_operated + ~is_balance:true + ~operand:Alpha_context.Tez.( +? ) + +(** [balance_was_credited ~loc ctxt contract old_balance amount] checks + that [contract]'s balance was debited [amount] tez in comparison to + [old_balance]. +*) +let balance_was_debited = + balance_or_frozen_bonds_was_operated + ~is_balance:true + ~operand:Alpha_context.Tez.( -? ) + +(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks + that [contract]'s frozen bonds was credited [amount] tez in comparison to + [old_balance]. +*) +let frozen_bonds_was_credited = + balance_or_frozen_bonds_was_operated + ~is_balance:false + ~operand:Alpha_context.Tez.( +? ) + +(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks + that [contract]'s frozen bonds was credited [amount] tez in comparison to + [old_balance]. +*) +let frozen_bonds_was_debited = + balance_or_frozen_bonds_was_operated + ~is_balance:false + ~operand:Alpha_context.Tez.( -? ) let pp_print_list pp out xs = let list_pp fmt = -- GitLab From fdfadb7f9f2cb7d9826c6a353bd24be67eec2097 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Mon, 26 Sep 2022 14:17:11 +0200 Subject: [PATCH 2/3] Scoru,Proto: slash the two players when the game ends in a draw --- .../lib_protocol/sc_rollup_refutation_storage.ml | 9 ++++++++- .../lib_protocol/sc_rollup_refutation_storage.mli | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) 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 9931865f1ea2..819c1d4c28a9 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.ml @@ -367,7 +367,14 @@ let apply_game_result ctxt rollup (stakers : Sc_rollup_game_repr.Index.t) in let balances_updates = balance_updates_loser @ balance_updates_winner in return (ctxt, balances_updates) - | Draw -> return (ctxt, []) + | Draw -> + let* ctxt, balances_updates_alice = + Stake_storage.remove_staker ctxt rollup stakers.alice + in + let* ctxt, balances_updates_bob = + Stake_storage.remove_staker ctxt rollup stakers.bob + in + return (ctxt, balances_updates_alice @ balances_updates_bob) in let* ctxt, _, _ = Store.Game_timeout.remove (ctxt, rollup) stakers in let* ctxt, _, _ = Store.Opponent.remove (ctxt, rollup) stakers.alice 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 e01bf187b773..6b4a68028cd6 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_refutation_storage.mli @@ -155,7 +155,8 @@ val get_timeout : (** [apply_game_result ctxt rollup game_result] takes a [game_result] produced by [timeout] or [game_move] and performs the necessary end-of-game cleanup: remove the game itself from the store and punish the losing - player by removing their stake. + player by removing their stake. In the case where the game ended in + a draw, both players are slashed. This is mostly just calling [remove_staker], so it can fail with the same errors as that. However, if it is called on an [game_result] -- GitLab From 81acef3c87b2fd230f0b0ebbf702ac0e87b4ea6b Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Mon, 26 Sep 2022 14:17:27 +0200 Subject: [PATCH 3/3] Scoru,Test: adapt draw tests to cover the slash --- .../integration/operations/test_sc_rollup.ml | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml index 0a977a29a2a5..50f1c057c4c0 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml @@ -1740,13 +1740,16 @@ let test_draw_with_two_invalid_moves () = let choice = Sc_rollup.Tick.initial in dumb_proof ~choice in - let* p1_final_move_op = Op.sc_rollup_refute (B block) p1 rollup p2_pkh (Some p1_refutation) in add_op block p1_final_move_op in + (* Get the frozen bonds for the two players before the draw. *) + let* frozen_bonds_p1 = Context.Contract.frozen_bonds (B block) p1 in + let* frozen_bonds_p2 = Context.Contract.frozen_bonds (B block) p2 in + (* Player2 will also send an invalid final move. *) let* incr = let* p2_refutation = @@ -1762,7 +1765,28 @@ let test_draw_with_two_invalid_moves () = (* As both players played invalid moves, the game ends in a draw. *) let expected_game_status : Sc_rollup.Game.status = Ended Draw in - assert_refute_result ~game_status:expected_game_status incr + let* () = assert_refute_result ~game_status:expected_game_status incr in + + (* The two players should have been slashed. *) + let* constants = Context.get_constants (I incr) in + let stake_amount = constants.parametric.sc_rollup.stake_amount in + let* () = + Assert.frozen_bonds_was_debited + ~loc:__LOC__ + (I incr) + p1 + frozen_bonds_p1 + stake_amount + in + let* () = + Assert.frozen_bonds_was_debited + ~loc:__LOC__ + (I incr) + p2 + frozen_bonds_p2 + stake_amount + in + return_unit (** Test that timeout a player during the final move ends the game if the other player played. *) -- GitLab