From 23b13e763c2f4159f6323add405b08f5608b0858 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Fri, 19 Jan 2024 11:40:33 +0100 Subject: [PATCH 1/5] Alpha/test_double_attestation: check the full Misbehaviour_repr.t --- .../test/helpers/slashing_helpers.ml | 55 +++++++++++++++++++ .../test/helpers/slashing_helpers.mli | 29 ++++++++++ .../consensus/test_double_attestation.ml | 22 ++++---- 3 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml create mode 100644 src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml new file mode 100644 index 000000000000..31a9cb7c85fa --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml @@ -0,0 +1,55 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs *) +(* *) +(*****************************************************************************) + +module Misbehaviour_repr = struct + let pp fmt {Protocol.Misbehaviour_repr.level; round; kind} = + Format.fprintf + fmt + "misbehaviour: %s at level %a round %a" + (match kind with + | Double_baking -> "double baking" + | Double_attesting -> "double attesting" + | Double_preattesting -> "double preattesting") + Protocol.Raw_level_repr.pp + level + Protocol.Round_repr.pp + round + + include Compare.Make (struct + type t = Protocol.Misbehaviour_repr.t + + let compare = Protocol.Misbehaviour_repr.compare + end) + + let from_duplicate_operation (type a) + (duplicate_op : + a Protocol.Alpha_context.Kind.consensus Protocol.Alpha_context.operation) + = + let ( ({slot = _; level; round; block_payload_hash = _} : + Protocol.Alpha_context.consensus_content), + kind ) = + match duplicate_op.protocol_data.contents with + | Single (Preattestation consensus_content) -> + (consensus_content, Protocol.Misbehaviour_repr.Double_preattesting) + | Single (Attestation {consensus_content; _}) -> + (consensus_content, Protocol.Misbehaviour_repr.Double_attesting) + in + let level = + Protocol.Alpha_context.Raw_level.Internal_for_tests.to_repr level + in + let round = Protocol.Alpha_context.Round.Internal_for_tests.to_repr round in + {Protocol.Misbehaviour_repr.level; round; kind} + + let check_from_duplicate_operation ~loc misbehaviour duplicate_op = + Assert.equal + ~loc + equal + "misbehaviours are not equal" + pp + misbehaviour + (from_duplicate_operation duplicate_op) +end diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli new file mode 100644 index 000000000000..247dcc1f462b --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli @@ -0,0 +1,29 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs *) +(* *) +(*****************************************************************************) + +(** Helpers related to denunciations and slashing. *) + +(** Helpers related to {!Protocol.Misbehaviour_repr}. *) +module Misbehaviour_repr : sig + val pp : Format.formatter -> Protocol.Misbehaviour_repr.t -> unit + + (** Builds a misbehaviour object from either of the duplicate + (pre)attestations that constitute a double (pre)attestating + event. *) + val from_duplicate_operation : + 'kind Protocol.Alpha_context.Kind.consensus Protocol.Alpha_context.operation -> + Protocol.Misbehaviour_repr.t + + (** [check_from_duplicate_operation ~loc misbehaviour duplicate_op] + asserts that [misbehaviour] correctly describes a double signing + event involving [duplicate_op]. *) + val check_from_duplicate_operation : + loc:string -> + Tezos_raw_protocol_alpha.Misbehaviour_repr.t -> + 'kind Protocol.Alpha_context.Kind.consensus Protocol.Alpha_context.operation -> + unit tzresult Lwt.t +end diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml index 38148cdbf5b3..13ffbdeaca06 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml @@ -52,17 +52,17 @@ let block_fork ?excluding b = (blk_a, blk_b) (* Checks that there is exactly one denunciation for the given delegate *) -let check_denunciations ~(level : Raw_level.t) b delegate = +let check_denunciations ~loc b delegate duplicate_op = let open Lwt_result_syntax in let* denunciations = Context.get_denunciations (B b) in match denunciations with - | [(d, item)] when Signature.Public_key_hash.equal d delegate -> - assert (item.Denunciations_repr.misbehaviour.kind = Double_attesting) ; - assert ( - Raw_level_repr.to_int32 item.Denunciations_repr.misbehaviour.level - = Raw_level.to_int32 level) ; - return_unit - | _ -> assert false + | [(d, item)] -> + let* () = Assert.equal_pkh ~loc d delegate in + Slashing_helpers.Misbehaviour_repr.check_from_duplicate_operation + ~loc + item.misbehaviour + duplicate_op + | _ -> Test.fail ~__LOC__:loc "expected exactly one denunciation" let check_empty_denunciations b = let open Lwt_result_syntax in @@ -131,9 +131,7 @@ let test_valid_double_attestation_evidence () = let* full_balance = Context.Delegate.full_balance (B blk_a) baker in let* () = check_empty_denunciations blk_a in let* blk_final = Block.bake ~policy:(By_account baker) ~operation blk_a in - (* Check that parts of the frozen deposits are slashed *) - let*? double_level = Context.get_level (B blk_a) in - let* () = check_denunciations ~level:double_level blk_final delegate in + let* () = check_denunciations ~loc:__LOC__ blk_final delegate attestation_a in let* frozen_deposits_before = Context.Delegate.current_frozen_deposits (B blk_a) delegate in @@ -155,6 +153,8 @@ let test_valid_double_attestation_evidence () = frozen_deposits_right_after frozen_deposits_before in + (* Check that the right portion of the frozen deposits is slashed at + the end of the cycle. *) let* blk_eoc, metadata, _ = Block.bake_until_n_cycle_end_with_metadata ~policy:(By_account baker) -- GitLab From b8272a97ee352a2acb8831ab11f8f5457b9d51cc Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Thu, 18 Jan 2024 16:31:17 +0100 Subject: [PATCH 2/5] Alpha/test_adaptive_issuance_roundtrip: update denunciation compare and pp --- .../lib_protocol/test/helpers/assert.ml | 14 +++++ .../test/helpers/slashing_helpers.ml | 57 ++++++++++++++++-- .../test/helpers/slashing_helpers.mli | 18 ++++++ .../test_adaptive_issuance_roundtrip.ml | 58 +++---------------- 4 files changed, 93 insertions(+), 54 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/assert.ml b/src/proto_alpha/lib_protocol/test/helpers/assert.ml index 9f42a9e2ec7c..a07c78553576 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/assert.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/assert.ml @@ -312,6 +312,20 @@ let assert_equal_list_opt ~loc eq msg pp = msg (Format.pp_print_option (pp_print_list pp)) +(** Checks that both lists have the same elements, not taking the + order of these elements into account, but taking their + multiplicity into account. *) +let equal_list_any_order ~loc ~compare msg pp list1 list2 = + let ordered_list1 = List.sort compare list1 in + let ordered_list2 = List.sort compare list2 in + equal + ~loc + (List.equal (fun a b -> compare a b = 0)) + msg + (pp_print_list pp) + ordered_list1 + ordered_list2 + let to_json_string encoding x = x |> Data_encoding.Json.construct encoding diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml index 31a9cb7c85fa..e217c03994c9 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml @@ -6,7 +6,9 @@ (*****************************************************************************) module Misbehaviour_repr = struct - let pp fmt {Protocol.Misbehaviour_repr.level; round; kind} = + open Protocol.Misbehaviour_repr + + let pp fmt {level; round; kind} = Format.fprintf fmt "misbehaviour: %s at level %a round %a" @@ -34,15 +36,15 @@ module Misbehaviour_repr = struct kind ) = match duplicate_op.protocol_data.contents with | Single (Preattestation consensus_content) -> - (consensus_content, Protocol.Misbehaviour_repr.Double_preattesting) + (consensus_content, Double_preattesting) | Single (Attestation {consensus_content; _}) -> - (consensus_content, Protocol.Misbehaviour_repr.Double_attesting) + (consensus_content, Double_attesting) in let level = Protocol.Alpha_context.Raw_level.Internal_for_tests.to_repr level in let round = Protocol.Alpha_context.Round.Internal_for_tests.to_repr round in - {Protocol.Misbehaviour_repr.level; round; kind} + {level; round; kind} let check_from_duplicate_operation ~loc misbehaviour duplicate_op = Assert.equal @@ -53,3 +55,50 @@ module Misbehaviour_repr = struct misbehaviour (from_duplicate_operation duplicate_op) end + +module Denunciations_repr = struct + open Protocol.Denunciations_repr + + let pp_item fmt {operation_hash = _; rewarded; misbehaviour} = + Format.fprintf + fmt + "rewarded: %a; %a" + Signature.Public_key_hash.pp + rewarded + Misbehaviour_repr.pp + misbehaviour + + let compare_item_except_hash + {operation_hash = _; rewarded = r1; misbehaviour = m1} + {operation_hash = _; rewarded = r2; misbehaviour = m2} = + Compare.or_else (Protocol.Misbehaviour_repr.compare m1 m2) @@ fun () -> + Signature.Public_key_hash.compare r1 r2 +end + +module Full_denunciation = struct + open Protocol.Denunciations_repr + + type t = Signature.Public_key_hash.t * item + + let pp fmt (culprit, item) = + Format.fprintf + fmt + "culprit: %a; %a" + Signature.Public_key_hash.pp + culprit + Denunciations_repr.pp_item + item + + let compare_except_hash (culprit1, item1) (culprit2, item2) = + Compare.or_else (Signature.Public_key_hash.compare culprit1 culprit2) + @@ fun () -> Denunciations_repr.compare_item_except_hash item1 item2 + + let check_same_lists_any_order ~loc list1 list2 = + Assert.equal_list_any_order + ~loc + ~compare:compare_except_hash + "denunciation lists are not the same (not taking order into account)" + pp + list1 + list2 +end diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli index 247dcc1f462b..13ea1cb04edc 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli @@ -27,3 +27,21 @@ module Misbehaviour_repr : sig 'kind Protocol.Alpha_context.Kind.consensus Protocol.Alpha_context.operation -> unit tzresult Lwt.t end + +(** Helpers about "full denunciations", that is, a denunciation item + and its culprit. See type [t] of this module. *) +module Full_denunciation : sig + (** A denunciation item preceded by the culprit's pkh. Indeed, the + culprit isn't recorded inside the + {!Protocol.Denunciations_repr.item} because it serves as a key + in the protocol's storage instead. But we often need both + together in the tests. *) + type t = Signature.Public_key_hash.t * Protocol.Denunciations_repr.item + + (** Asserts that both lists contain the same elements. + + These elements may be ordered differently, but must have the + same multiplicity in both lists. *) + val check_same_lists_any_order : + loc:string -> t list -> t list -> unit tzresult Lwt.t +end diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml index 0ff4fac5628d..bd26fee083a2 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml @@ -1292,55 +1292,13 @@ let finalize_unstake src_name : (t, t) scenarios = (* ======== Slashing ======== *) -let check_pending_slashings (block, state) : unit tzresult Lwt.t = +let check_pending_slashings ~loc (block, state) : unit tzresult Lwt.t = let open Lwt_result_syntax in - let open Protocol.Denunciations_repr in let* denunciations_rpc = Context.get_denunciations (B block) in - let denunciations_obj_equal (pkh_1, {rewarded = r1; misbehaviour = m1; _}) - (pkh_2, {rewarded = r2; misbehaviour = m2; _}) = - Signature.Public_key_hash.equal pkh_1 pkh_2 - && Signature.Public_key_hash.equal r1 r2 - && Stdlib.(m1.kind = m2.kind) - in - let compare_denunciations (pkh_1, {rewarded = r1; misbehaviour = m1; _}) - (pkh_2, {rewarded = r2; misbehaviour = m2; _}) = - let c1 = Signature.Public_key_hash.compare pkh_1 pkh_2 in - if c1 <> 0 then c1 - else - let c2 = Signature.Public_key_hash.compare r1 r2 in - if c2 <> 0 then c2 - else Protocol.Misbehaviour_repr.compare_kind m1.kind m2.kind - in - let denunciations_rpc = List.sort compare_denunciations denunciations_rpc in - let denunciations_state = - List.sort compare_denunciations state.State.pending_slashes - in - let denunciations_equal = List.equal denunciations_obj_equal in - let denunciations_obj_pp fmt - (pkh, {rewarded; misbehaviour; operation_hash = _}) = - Format.fprintf - fmt - "slashed: %a; rewarded: %a; kind: %s@." - Signature.Public_key_hash.pp - pkh - Signature.Public_key_hash.pp - rewarded - (match misbehaviour.kind with - | Double_baking -> "double baking" - | Double_attesting -> "double attesting" - | Double_preattesting -> "double preattesting") - in - let denunciations_pp = Format.pp_print_list denunciations_obj_pp in - let* () = - Assert.equal - ~loc:__LOC__ - denunciations_equal - "Denunciations are not equal" - denunciations_pp - denunciations_rpc - denunciations_state - in - return_unit + Slashing_helpers.Full_denunciation.check_same_lists_any_order + ~loc + denunciations_rpc + state.State.pending_slashes (** Double attestation helpers *) let order_attestations ~correct_order op1 op2 = @@ -1584,7 +1542,7 @@ let update_state_denunciation (block, state) let make_denunciations_ ?(filter = fun {denounced; _} -> not denounced) (block, state) = let open Lwt_result_syntax in - let* () = check_pending_slashings (block, state) in + let* () = check_pending_slashings ~loc:__LOC__ (block, state) in let make_op state ({evidence; _} as dss) = if filter dss then let* state, denounced = update_state_denunciation (block, state) dss in @@ -2510,7 +2468,7 @@ module Slashing = struct (* bootstrap1 can be forbidden in this case, so we set another baker *) --> exclude_bakers ["delegate"; "bootstrap1"]) --> check_snapshot_balances "before slash" - --> exec_unit check_pending_slashings + --> exec_unit (check_pending_slashings ~loc:__LOC__) --> next_cycle --> assert_failure (exec_unit (fun (_block, state) -> @@ -2518,7 +2476,7 @@ module Slashing = struct failwith "ns_enable = true: slash not applied yet" else return_unit) --> check_snapshot_balances "before slash") - --> exec_unit check_pending_slashings + --> exec_unit (check_pending_slashings ~loc:__LOC__) --> next_cycle |+ Tag "denounce too late" --> next_cycle --> next_cycle --> assert_failure -- GitLab From 2351910dcc35048378d1c80db3a97d0466fa7054 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Thu, 18 Jan 2024 14:53:16 +0100 Subject: [PATCH 3/5] Alpha/test_adaptive_issuance_roundtrip: correctly fill out new fields --- .../test/helpers/slashing_helpers.ml | 7 +++++++ .../test/helpers/slashing_helpers.mli | 4 ++++ .../test_adaptive_issuance_roundtrip.ml | 18 +++++++++--------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml index e217c03994c9..97f7a712dd7f 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.ml @@ -54,6 +54,13 @@ module Misbehaviour_repr = struct pp misbehaviour (from_duplicate_operation duplicate_op) + + let from_duplicate_block (b : Block.t) = + let open Result_wrap_syntax in + let open Result_syntax in + let*@ level = Protocol.Raw_level_repr.of_int32 b.header.shell.level in + let*@ round = Protocol.Fitness_repr.round_from_raw b.header.shell.fitness in + return {kind = Double_baking; level; round} end module Denunciations_repr = struct diff --git a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli index 13ea1cb04edc..98fadb0c8763 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/slashing_helpers.mli @@ -26,6 +26,10 @@ module Misbehaviour_repr : sig Tezos_raw_protocol_alpha.Misbehaviour_repr.t -> 'kind Protocol.Alpha_context.Kind.consensus Protocol.Alpha_context.operation -> unit tzresult Lwt.t + + (** Builds a misbehaviour object from either of the duplicate blocks + that constitute a double baking event. *) + val from_duplicate_block : Block.t -> Protocol.Misbehaviour_repr.t tzresult end (** Helpers about "full denunciations", that is, a denunciation item diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml index bd26fee083a2..a806776639a1 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml @@ -166,6 +166,7 @@ type double_signing_state = { evidence : Context.t -> Protocol.Alpha_context.packed_operation; denounced : bool; level : Int32.t; + misbehaviour : Protocol.Misbehaviour_repr.t; } (** Module for the [State.t] type of asserted information about the system during a test. *) @@ -1342,6 +1343,9 @@ let double_bake_ delegate_name (block, state) = (* includes pending operations *) let* main_branch, state = bake ~baker:delegate_name (block, state) in let evidence = op_double_baking main_branch.header forked_block.header in + let*? misbehaviour = + Slashing_helpers.Misbehaviour_repr.from_duplicate_block main_branch + in let dss = { culprit = delegate.pkh; @@ -1349,6 +1353,7 @@ let double_bake_ delegate_name (block, state) = evidence; kind = Double_baking; level = block.header.shell.level; + misbehaviour; } in let state = @@ -1405,6 +1410,9 @@ let double_attest_op ?other_bakers ~op ~op_evidence ~kind delegate_name evidence; kind; level = block.header.shell.level; + misbehaviour = + Slashing_helpers.Misbehaviour_repr.from_duplicate_operation + attestation_a; } in let state = @@ -1462,7 +1470,7 @@ let get_pending_slashed_pct_for_delegate (block, state) delegate = aux 0 state.State.pending_slashes let update_state_denunciation (block, state) - {culprit; denounced; evidence = _; kind; level} = + {culprit; denounced; evidence = _; kind = _; level; misbehaviour} = let open Lwt_result_syntax in if denounced then (* If the double signing has already been denounced, a second denunciation should fail *) @@ -1490,14 +1498,6 @@ let update_state_denunciation (block, state) following cycle? *) return (state, denounced) else - let misbehaviour = - { - (* Fields level and round are unused for now. *) - level = Protocol.Raw_level_repr.of_int32_exn level; - round = Protocol.Round_repr.zero; - Protocol.Misbehaviour_repr.kind; - } - in (* for simplicity's sake (lol), the block producer and the payload producer are the same We also assume that the current state baking policy will be used for the next block *) let* rewarded, _, _, _ = -- GitLab From 50be604320391a47ffb12c9da0f65b6ea722f9e7 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 14 Feb 2024 15:16:11 +0100 Subject: [PATCH 4/5] Proto/AI/tests: use the actual level of the double signing event and do the same checks as in validate for too early or too late denunciation --- .../test_adaptive_issuance_roundtrip.ml | 118 +++++++++--------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml index a806776639a1..8e66ce9eb511 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml @@ -1352,7 +1352,7 @@ let double_bake_ delegate_name (block, state) = denounced = false; evidence; kind = Double_baking; - level = block.header.shell.level; + level = main_branch.header.shell.level; misbehaviour; } in @@ -1409,7 +1409,7 @@ let double_attest_op ?other_bakers ~op ~op_evidence ~kind delegate_name denounced = false; evidence; kind; - level = block.header.shell.level; + level = main_branch.header.shell.level; misbehaviour = Slashing_helpers.Misbehaviour_repr.from_duplicate_operation attestation_a; @@ -1480,64 +1480,67 @@ let update_state_denunciation (block, state) let next_level = Protocol.Alpha_context.Raw_level.(to_int32 @@ succ block_level) in - if level > next_level then + let inclusion_cycle = + cycle_from_level block.constants.blocks_per_cycle next_level + in + let ds_cycle = cycle_from_level block.constants.blocks_per_cycle level in + if Cycle.(ds_cycle > inclusion_cycle) then (* The denunciation is trying to be included too early *) return (state, denounced) + else if + Cycle.( + add ds_cycle Protocol.Constants_repr.max_slashing_period + <= inclusion_cycle) + then + (* The denunciation is too late and gets refused. *) + return (state, denounced) + else if get_pending_slashed_pct_for_delegate (block, state) culprit >= 100 + then + (* Culprit has been slashed too much, a denunciation is not added to the list. + TODO: is the double signing treated as included, or can it be included in the + following cycle? *) + return (state, denounced) else - let inclusion_cycle = - cycle_from_level block.constants.blocks_per_cycle next_level + (* for simplicity's sake (lol), the block producer and the payload producer are the same + We also assume that the current state baking policy will be used for the next block *) + let* rewarded, _, _, _ = + Block.get_next_baker ?policy:state.baking_policy block in - let ds_cycle = cycle_from_level block.constants.blocks_per_cycle level in - if Cycle.(succ ds_cycle < inclusion_cycle) then - (* denunciation is too late, gets refused *) - return (state, denounced) - else if get_pending_slashed_pct_for_delegate (block, state) culprit >= 100 - then - (* Culprit has been slashed too much, a denunciation is not added to the list. - TODO: is the double signing treated as included, or can it be included in the - following cycle? *) - return (state, denounced) - else - (* for simplicity's sake (lol), the block producer and the payload producer are the same - We also assume that the current state baking policy will be used for the next block *) - let* rewarded, _, _, _ = - Block.get_next_baker ?policy:state.baking_policy block - in - let culprit_name, culprit_account = - State.find_account_from_pkh culprit state - in - let state = - State.update_account - culprit_name - { - culprit_account with - slashed_cycles = inclusion_cycle :: culprit_account.slashed_cycles; - } - state - in - let new_pending_slash = - ( culprit, - { - Protocol.Denunciations_repr.rewarded; - misbehaviour; - operation_hash = Operation_hash.zero; - (* unused *) - } ) - in - (* TODO: better log... *) - Log.info - ~color:Log_module.event_color - "Including denunciation (misbehaviour cycle %a)" - Cycle.pp - ds_cycle ; - let state = - State. - { - state with - pending_slashes = new_pending_slash :: state.pending_slashes; - } - in - return (state, true) + let culprit_name, culprit_account = + State.find_account_from_pkh culprit state + in + let state = + State.update_account + culprit_name + { + culprit_account with + slashed_cycles = inclusion_cycle :: culprit_account.slashed_cycles; + } + state + in + let new_pending_slash = + ( culprit, + { + Protocol.Denunciations_repr.rewarded; + misbehaviour; + operation_hash = Operation_hash.zero; + (* unused *) + } ) + in + (* TODO: better log... *) + Log.info + ~color:Log_module.event_color + "Including denunciation (misbehaviour cycle %a)" + Cycle.pp + ds_cycle ; + let state = + State. + { + state with + pending_slashes = new_pending_slash :: state.pending_slashes; + } + in + return (state, true) let make_denunciations_ ?(filter = fun {denounced; _} -> not denounced) (block, state) = @@ -2484,8 +2487,7 @@ module Slashing = struct let ds = state.State.double_signings in let ds = match ds with [a] -> a | _ -> assert false in let level = - Protocol.Alpha_context.Raw_level.of_int32_exn - (Int32.succ ds.level) + Protocol.Alpha_context.Raw_level.of_int32_exn ds.level in let last_cycle = Cycle.add -- GitLab From 4f5dd37b9b6c992288444d6dfcb9b047bd482dd7 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 14 Feb 2024 15:21:50 +0100 Subject: [PATCH 5/5] Proto/AI/tests: simplify redundant fields --- .../test_adaptive_issuance_roundtrip.ml | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml index 8e66ce9eb511..7daa633e798d 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_roundtrip.ml @@ -162,10 +162,8 @@ let default_params = type double_signing_state = { culprit : Signature.Public_key_hash.t; - kind : Protocol.Misbehaviour_repr.kind; evidence : Context.t -> Protocol.Alpha_context.packed_operation; denounced : bool; - level : Int32.t; misbehaviour : Protocol.Misbehaviour_repr.t; } @@ -1347,14 +1345,7 @@ let double_bake_ delegate_name (block, state) = Slashing_helpers.Misbehaviour_repr.from_duplicate_block main_branch in let dss = - { - culprit = delegate.pkh; - denounced = false; - evidence; - kind = Double_baking; - level = main_branch.header.shell.level; - misbehaviour; - } + {culprit = delegate.pkh; denounced = false; evidence; misbehaviour} in let state = {state with double_signings = dss :: state.State.double_signings} @@ -1408,8 +1399,6 @@ let double_attest_op ?other_bakers ~op ~op_evidence ~kind delegate_name culprit = delegate.pkh; denounced = false; evidence; - kind; - level = main_branch.header.shell.level; misbehaviour = Slashing_helpers.Misbehaviour_repr.from_duplicate_operation attestation_a; @@ -1470,7 +1459,7 @@ let get_pending_slashed_pct_for_delegate (block, state) delegate = aux 0 state.State.pending_slashes let update_state_denunciation (block, state) - {culprit; denounced; evidence = _; kind = _; level; misbehaviour} = + {culprit; denounced; evidence = _; misbehaviour} = let open Lwt_result_syntax in if denounced then (* If the double signing has already been denounced, a second denunciation should fail *) @@ -1483,7 +1472,8 @@ let update_state_denunciation (block, state) let inclusion_cycle = cycle_from_level block.constants.blocks_per_cycle next_level in - let ds_cycle = cycle_from_level block.constants.blocks_per_cycle level in + let ds_level = Protocol.Raw_level_repr.to_int32 misbehaviour.level in + let ds_cycle = cycle_from_level block.constants.blocks_per_cycle ds_level in if Cycle.(ds_cycle > inclusion_cycle) then (* The denunciation is trying to be included too early *) return (state, denounced) @@ -2487,14 +2477,18 @@ module Slashing = struct let ds = state.State.double_signings in let ds = match ds with [a] -> a | _ -> assert false in let level = - Protocol.Alpha_context.Raw_level.of_int32_exn ds.level + Protocol.Alpha_context.Raw_level.Internal_for_tests + .from_repr + ds.misbehaviour.level in let last_cycle = Cycle.add (Block.current_cycle_of_level ~blocks_per_cycle: state.State.constants.blocks_per_cycle - ~current_level:ds.level) + ~current_level: + (Protocol.Raw_level_repr.to_int32 + ds.misbehaviour.level)) Protocol.Constants_repr.max_slashing_period in let (kind : Protocol.Alpha_context.Misbehaviour.kind) = @@ -2502,7 +2496,7 @@ module Slashing = struct Misbehaviour_repr.kind were moved to a separate file that doesn't have under/over Alpha_context versions. *) - match ds.kind with + match ds.misbehaviour.kind with | Double_baking -> Double_baking | Double_attesting -> Double_attesting | Double_preattesting -> Double_preattesting @@ -2562,12 +2556,14 @@ module Slashing = struct order" --> double_attest "delegate" --> next_cycle --> double_attest "delegate" --> make_denunciations - ~filter:(fun {level; denounced; _} -> - (not denounced) && level > 10l) + ~filter:(fun {denounced; misbehaviour = {level; _}; _} -> + (not denounced) + && Protocol.Raw_level_repr.to_int32 level > 10l) () --> make_denunciations - ~filter:(fun {level; denounced; _} -> - (not denounced) && level <= 10l) + ~filter:(fun {denounced; misbehaviour = {level; _}; _} -> + (not denounced) + && Protocol.Raw_level_repr.to_int32 level <= 10l) () --> check_is_forbidden "delegate") -- GitLab