diff --git a/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml index 3db4a7d1ab13e558717b0cf1497b23571a840566..cd198517052d54790894a0689b2cc8ea690078cf 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml @@ -26,25 +26,119 @@ open Protocol open Alpha_context -let test_consensus_operation ?construction_mode ?level ?block_payload_hash ?slot - ?round ~endorsed_block ~error ~is_preendorsement ~loc () = - (if is_preendorsement then - Op.preendorsement ?block_payload_hash ?level ?slot ?round endorsed_block - else Op.endorsement ?block_payload_hash ?level ?slot ?round endorsed_block) - >>=? fun operation -> - let assert_error res = Assert.proto_error ~loc res error in - match construction_mode with - | None -> - (* meaning Application mode *) - Block.bake ~operation endorsed_block >>= assert_error - | Some (pred, protocol_data) -> - (* meaning partial construction or full construction mode, depending on - [protocol_data] *) - Block.get_construction_vstate ~protocol_data pred - >>=? fun (validation_state, _application_state) -> - let oph = Operation.hash_packed operation in - validate_operation validation_state oph operation - >|= Environment.wrap_tzresult >>= assert_error +type mode = Application | Construction | Mempool + +let show_mode = function + | Application -> "Application" + | Construction -> "Construction" + | Mempool -> "Mempool" + +type kind = Preendorsement | Endorsement + +(** Craft an endorsement or preendorsement, and bake a block + containing it (in application or construction modes) or inject it + into a mempool. When [error] is [None], check that it succeeds, + otherwise check that it fails as specified by [error]. + + By default, the (pre)endorsement is for the first slot and is + signed by the delegate that owns this slot. Moreover, the operation + points to the given [endorsed_block]: in other words, it has that + block's level, round, payload hash, and its branch is the + predecessor of that block. Optional arguments allow to override + these default parameters. + + The [predecessor] is used as the predecessor of the baked block or + the head of the mempool. When it is not provided, we use the + [endorsed_block] for this. *) +let test_consensus_operation ?delegate ?slot ?level ?round ?block_payload_hash + ?branch ~endorsed_block ?(predecessor = endorsed_block) ?error ~loc kind + mode = + let open Lwt_result_syntax in + let* operation = + match kind with + | Preendorsement -> + Op.preendorsement + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?branch + endorsed_block + | Endorsement -> + Op.endorsement + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?branch + endorsed_block + in + let check_error res = + match error with + | Some error -> Assert.proto_error ~loc res error + | None -> + let*? _ = res in + return_unit + in + match mode with + | Application -> + Block.bake ~baking_mode:Application ~operation predecessor >>= check_error + | Construction -> + Block.bake ~baking_mode:Baking ~operation predecessor >>= check_error + | Mempool -> + let*! res = + let* inc = + Incremental.begin_construction ~mempool_mode:true predecessor + in + let* inc = Incremental.add_operation inc operation in + (* Finalization doesn't do much in mempool mode, but some RPCs + still call it, so we check that it doesn't fail unexpectedly. *) + Incremental.finalize_block inc + in + check_error res + +let test_consensus_operation_all_modes_different_outcomes ?delegate ?slot ?level + ?round ?block_payload_hash ?branch ~endorsed_block ?predecessor ~loc + ?application_error ?construction_error ?mempool_error kind = + List.iter_es + (fun (mode, error) -> + test_consensus_operation + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?branch + ~endorsed_block + ?predecessor + ?error + ~loc:(Format.sprintf "%s (%s mode)" loc (show_mode mode)) + kind + mode) + [ + (Application, application_error); + (Construction, construction_error); + (Mempool, mempool_error); + ] + +let test_consensus_operation_all_modes ?delegate ?slot ?level ?round + ?block_payload_hash ?branch ~endorsed_block ?predecessor ?error ~loc kind = + test_consensus_operation_all_modes_different_outcomes + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?branch + ~endorsed_block + ?predecessor + ~loc + ?application_error:error + ?construction_error:error + ?mempool_error:error + kind let delegate_of_first_slot b = let module V = Plugin.RPC.Validators in diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index bb04d5fbb9b6127e31bde613a86eeb0a7d7e3ff5..740157f2f2643654923af6c1cda2609f8d279359 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -54,11 +54,11 @@ let mk_block_payload_hash payload_round (b : Block.t) = ~payload_round hashes -let mk_consensus_content_signer_and_pred_branch ?delegate ?slot ?level ?round - ?block_payload_hash ?pred_branch endorsed_block = +let mk_consensus_content_signer_and_branch ?delegate ?slot ?level ?round + ?block_payload_hash ?branch endorsed_block = let open Lwt_result_syntax in - let pred_branch = - match pred_branch with + let branch = + match branch with | None -> endorsed_block.Block.header.shell.predecessor | Some branch -> branch in @@ -95,19 +95,19 @@ let mk_consensus_content_signer_and_pred_branch ?delegate ?slot ?level ?round in let consensus_content = {slot; level; round; block_payload_hash} in let* signer = Account.find delegate_pkh in - return (consensus_content, signer.sk, pred_branch) + return (consensus_content, signer.sk, branch) -let raw_endorsement ?delegate ?slot ?level ?round ?block_payload_hash - ?pred_branch endorsed_block = +let raw_endorsement ?delegate ?slot ?level ?round ?block_payload_hash ?branch + endorsed_block = let open Lwt_result_syntax in - let* consensus_content, signer, pred_branch = - mk_consensus_content_signer_and_pred_branch + let* consensus_content, signer, branch = + mk_consensus_content_signer_and_branch ?delegate ?slot ?level ?round ?block_payload_hash - ?pred_branch + ?branch endorsed_block in let op = Single (Endorsement consensus_content) in @@ -115,10 +115,10 @@ let raw_endorsement ?delegate ?slot ?level ?round ?block_payload_hash (sign ~watermark:Operation.(to_watermark (Endorsement Chain_id.zero)) signer - pred_branch + branch op) -let endorsement ?delegate ?slot ?level ?round ?block_payload_hash ?pred_branch +let endorsement ?delegate ?slot ?level ?round ?block_payload_hash ?branch endorsed_block = let open Lwt_result_syntax in let* op = @@ -128,22 +128,22 @@ let endorsement ?delegate ?slot ?level ?round ?block_payload_hash ?pred_branch ?level ?round ?block_payload_hash - ?pred_branch + ?branch endorsed_block in return (Operation.pack op) -let raw_preendorsement ?delegate ?slot ?level ?round ?block_payload_hash - ?pred_branch endorsed_block = +let raw_preendorsement ?delegate ?slot ?level ?round ?block_payload_hash ?branch + endorsed_block = let open Lwt_result_syntax in - let* consensus_content, signer, pred_branch = - mk_consensus_content_signer_and_pred_branch + let* consensus_content, signer, branch = + mk_consensus_content_signer_and_branch ?delegate ?slot ?level ?round ?block_payload_hash - ?pred_branch + ?branch endorsed_block in let op = Single (Preendorsement consensus_content) in @@ -151,11 +151,11 @@ let raw_preendorsement ?delegate ?slot ?level ?round ?block_payload_hash (sign ~watermark:Operation.(to_watermark (Preendorsement Chain_id.zero)) signer - pred_branch + branch op) -let preendorsement ?delegate ?slot ?level ?round ?block_payload_hash - ?pred_branch endorsed_block = +let preendorsement ?delegate ?slot ?level ?round ?block_payload_hash ?branch + endorsed_block = let open Lwt_result_syntax in let* op = raw_preendorsement @@ -164,7 +164,7 @@ let preendorsement ?delegate ?slot ?level ?round ?block_payload_hash ?level ?round ?block_payload_hash - ?pred_branch + ?branch endorsed_block in return (Operation.pack op) diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index 09f4b988bb032ab3cef7161fae2c4c55e75f7d4c..56158fc3572b0172a14b7d6b95c19a8a95761e1f 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -56,17 +56,18 @@ val sign : Optional parameters allow to specify the endorsed values: [level], [round] and/or [block_payload_hash]. - They also allow to specify the endorser, [delegate], and/or the - [slot]. + They also allow to specify the endorser ([delegate]), and/or the + [slot]. These default to the first slot and its delegate. - Finally, the predecessor branch, [pred_branch] can be specified.*) + Finally, the operation [branch] can be specified. It defaults to the + predecessor of the endorsed block. *) val raw_endorsement : ?delegate:public_key_hash -> ?slot:Slot.t -> ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - ?pred_branch:Block_hash.t -> + ?branch:Block_hash.t -> Block.t -> Kind.endorsement Operation.t tzresult Lwt.t @@ -80,7 +81,7 @@ val raw_preendorsement : ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - ?pred_branch:Block_hash.t -> + ?branch:Block_hash.t -> Block.t -> Kind.preendorsement Operation.t tzresult Lwt.t @@ -92,7 +93,7 @@ val endorsement : ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - ?pred_branch:Block_hash.t -> + ?branch:Block_hash.t -> Block.t -> Operation.packed tzresult Lwt.t @@ -104,7 +105,7 @@ val preendorsement : ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - ?pred_branch:Block_hash.t -> + ?branch:Block_hash.t -> Block.t -> Operation.packed tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml index 911051eb3c3607b9d375e94f665215f28ca8f5df..9e5bc24de94edc155a3ce02b14089b9fc38b2bc3 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml @@ -137,12 +137,12 @@ let test_different_branch () = Block.bake genesis >>=? fun blk -> Context.get_endorser (B blk) >>=? fun (endorser, _slots) -> Op.raw_endorsement ~delegate:endorser blk >>=? fun endorsement_a -> - Op.raw_endorsement ~pred_branch:Block_hash.zero ~delegate:endorser blk + Op.raw_endorsement ~branch:Block_hash.zero ~delegate:endorser blk >>=? fun endorsement_b -> let operation = double_endorsement (B blk) endorsement_a endorsement_b in Block.bake ~operation blk >>=? fun _blk -> Op.raw_preendorsement ~delegate:endorser blk >>=? fun preendorsement_a -> - Op.raw_preendorsement ~pred_branch:Block_hash.zero ~delegate:endorser blk + Op.raw_preendorsement ~branch:Block_hash.zero ~delegate:endorser blk >>=? fun preendorsement_b -> let operation = double_preendorsement (B blk) preendorsement_a preendorsement_b diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml index 549f19572291997b1c3a57cfae0b38cf0a0e77bb..a1d12e676c44f88685242058b94d2c832bdfe965 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml @@ -41,31 +41,68 @@ let init_genesis ?policy () = Context.init_n ~consensus_threshold:0 5 () >>=? fun (genesis, _contracts) -> Block.bake ?policy genesis >>=? fun b -> return (genesis, b) -(** inject an endorsement and return the block with the endorsement and its - parent. *) -let inject_the_first_endorsement () = - init_genesis () >>=? fun (_genesis, b) -> - Op.endorsement b >>=? fun operation -> - Block.bake ~operation b >>=? fun b' -> return (b', b) - -(****************************************************************) -(* Tests *) -(****************************************************************) - -(** Apply a single endorsement from the slot 0 endorser. *) +(** {1 Positive tests} *) + +(** Correct endorsement from the slot 0 endorser. *) let test_simple_endorsement () = - inject_the_first_endorsement () >>=? fun (_, _) -> return_unit + let open Lwt_result_syntax in + let* _genesis, endorsed_block = init_genesis () in + Consensus_helpers.test_consensus_operation_all_modes + ~loc:__LOC__ + ~endorsed_block + Endorsement (** Test that the endorsement's branch does not affect its validity. *) -let test_endorsement_with_arbitrary_branch () = - init_genesis () >>=? fun (_genesis, blk) -> - Op.endorsement ~pred_branch:Block_hash.zero blk >>=? fun operation -> - Block.bake ~operation blk >>=? fun _blk -> return_unit +let test_arbitrary_branch () = + let open Lwt_result_syntax in + let* _genesis, endorsed_block = init_genesis () in + Consensus_helpers.test_consensus_operation_all_modes + ~loc:__LOC__ + ~endorsed_block + ~branch:Block_hash.zero + Endorsement + +(** Correct endorsement with a level and a round that are both + different from {!test_simple_endorsement}. *) +let test_non_zero_round () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* endorsed_block = Block.bake ~policy:(By_round 10) b in + Consensus_helpers.test_consensus_operation_all_modes + ~loc:__LOC__ + ~endorsed_block + Endorsement + +(** Fitness gap: this is a straightforward update from Emmy to Tenderbake, + that is, check that the level is incremented in a child block. *) +let test_fitness_gap () = + let open Lwt_result_syntax in + let* _genesis, pred_b = init_genesis () in + let* operation = Op.endorsement pred_b in + let* b = Block.bake ~operation pred_b in + let fitness = + match Fitness.from_raw b.header.shell.fitness with + | Ok fitness -> fitness + | _ -> assert false + in + let pred_fitness = + match Fitness.from_raw pred_b.header.shell.fitness with + | Ok fitness -> fitness + | _ -> assert false + in + let level = Fitness.level fitness in + let pred_level = Fitness.level pred_fitness in + let level_diff = + Int32.sub (Raw_level.to_int32 level) (Raw_level.to_int32 pred_level) + in + Assert.equal_int32 ~loc:__LOC__ level_diff 1l + +(** {1 Negative tests} -(****************************************************************) -(* The following test scenarios are supposed to raise errors. *) -(****************************************************************) + The following test scenarios are supposed to raise errors. *) + +(** {2 Wrong slot} *) (** Apply an endorsement with a negative slot. *) let test_negative_slot () = @@ -83,225 +120,217 @@ let test_negative_slot () = (function | Data_encoding.Binary.Write_error _ -> return_unit | e -> Lwt.fail e) -(** Apply an endorsement with a non-normalized slot (that is, not the smallest - possible). *) -let test_non_normalized_slot () = - Context.init_n 5 () >>=? fun (genesis, _contracts) -> - Block.bake genesis >>=? fun b -> - Context.get_endorsers (B b) >>=? fun endorsers_list -> - (* find an endorsers with more than 1 slot *) - List.find_map - (function - | {Plugin.RPC.Validators.delegate; slots; _} -> - if Compare.List_length_with.(slots > 1) then Some (delegate, slots) - else None) - endorsers_list - |> function - | None -> assert false - | Some (delegate, slots) -> - let set_slots = Slot.Set.of_list slots in - (* no duplicated slots *) - Assert.equal_int - ~loc:__LOC__ - (Slot.Set.cardinal set_slots) - (List.length slots) - >>=? fun () -> - (* the first slot should be the smallest slot *) - Assert.equal - ~loc:__LOC__ - (fun x y -> Slot.compare x y = 0) - "the first slot is not the smallest" - Slot.pp - (WithExceptions.Option.get ~loc:__LOC__ @@ List.hd slots) - (WithExceptions.Option.get ~loc:__LOC__ @@ Slot.Set.min_elt set_slots) - >>=? fun () -> - let slot = - match List.hd (List.rev slots) with None -> assert false | Some s -> s - in - Op.endorsement ~delegate ~slot b >>=? fun operation -> - let policy = Block.Excluding [delegate] in - Block.bake ~policy ~operation b >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Wrong_slot_used_for_consensus_operation - {kind} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - -(** Invalid_endorsement_level: apply an endorsement with an incorrect - level (i.e. the predecessor level). *) -let test_invalid_endorsement_level () = - init_genesis () >>=? fun (genesis, b) -> - Context.get_level (B genesis) >>?= fun genesis_level -> - Op.endorsement ~level:genesis_level b >>=? fun operation -> - Block.bake ~operation b >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - -(** Duplicate endorsement : apply an endorsement that has already been applied. *) -let test_duplicate_endorsement () = - init_genesis () >>=? fun (_genesis, b) -> - Incremental.begin_construction b >>=? fun inc -> - Op.endorsement b >>=? fun operation -> - Incremental.add_operation inc operation >>=? fun inc -> - Op.endorsement b >>=? fun operation -> - Incremental.add_operation inc operation >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Conflicting_consensus_operation {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - -(** Consensus operation for future level : apply an endorsement with a level in the future *) -let test_consensus_operation_endorsement_for_future_level () = - init_genesis () >>=? fun (_genesis, pred) -> - let raw_level = Raw_level.of_int32 (Int32.of_int 10) in - let level = match raw_level with Ok l -> l | Error _ -> assert false in - Consensus_helpers.test_consensus_operation +(** Endorsement with a non-normalized slot (that is, a slot that + belongs to the delegate but is not the delegate's smallest slot). *) +let test_not_smallest_slot () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* endorsers = Context.get_endorsers (B b) in + let delegate, slots = + (* Find an endorser with more than 1 slot. *) + WithExceptions.Option.get + ~loc:__LOC__ + (List.find_map + (fun {RPC.Validators.delegate; slots; _} -> + if Compare.List_length_with.(slots > 1) then Some (delegate, slots) + else None) + endorsers) + in + (* Check that the slots are sorted and have no duplicates. *) + let rec check_sorted = function + | [] | [_] -> true + | x :: (y :: _ as t) -> Slot.compare x y < 0 && check_sorted t + in + assert (check_sorted slots) ; + let slot = + match slots with [] | [_] -> assert false | _ :: slot :: _ -> slot + in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~level + ~endorsed_block:b + ~delegate + ~slot ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_future_level {kind; _} + | Validate_errors.Consensus.Wrong_slot_used_for_consensus_operation + {kind; _} when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () - -(** Consensus operation for old level : apply an endorsement one level in the past *) -let test_consensus_operation_endorsement_for_predecessor_level () = - init_genesis () >>=? fun (_genesis, pred) -> - let raw_level = Raw_level.of_int32 (Int32.of_int 0) in - let level = match raw_level with Ok l -> l | Error _ -> assert false in - Consensus_helpers.test_consensus_operation + Endorsement + +(** Endorsement with a slot that does not belong to the delegate. *) +let test_other_delegate_slot () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* endorsers = Context.get_endorsers (B b) in + let delegate, other_delegate_slot = + match endorsers with + | [] | [_] -> assert false (* at least two delegates with rights *) + | {delegate; _} :: {slots; _} :: _ -> + (delegate, WithExceptions.Option.get ~loc:__LOC__ (List.hd slots)) + in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~level + ~endorsed_block:b + ~delegate + ~slot:other_delegate_slot ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - ~construction_mode:(pred, None) - () - -(** Consensus operation for old level : apply an endorsement with more than one level in the past *) -let test_consensus_operation_endorsement_for_old_level () = - init_genesis () >>=? fun (genesis, pred) -> - Block.bake genesis >>=? fun _next_block -> - let raw_level = Raw_level.of_int32 (Int32.of_int 0) in - let level = match raw_level with Ok l -> l | Error _ -> assert false in - Consensus_helpers.test_consensus_operation + | Alpha_context.Operation.Invalid_signature -> true | _ -> false) + Endorsement + +(** {2 Wrong level} *) + +let error_old_level = function + | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} + when kind = Validate_errors.Consensus.Endorsement -> + true + | _ -> false + +(** Endorsement that is one level too old, aka grandparent endorsement + (the endorsement is expected to point to the level of the + predecessor of the block/mempool containing the endorsement, but + instead it points to the grandparent's level). + + This endorsement should fail in a block (application or + construction), but be accepted in mempool mode. *) +let test_one_level_too_old () = + let open Lwt_result_syntax in + let* _genesis, grandparent = init_genesis () in + let* predecessor = Block.bake grandparent in + Consensus_helpers.test_consensus_operation_all_modes_different_outcomes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~level - ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - ~construction_mode:(pred, None) - () - -(** Consensus operation for future round : apply an endorsement with a round in the future *) -let test_consensus_operation_endorsement_for_future_round () = - init_genesis () >>=? fun (_genesis, pred) -> - Environment.wrap_tzresult (Round.of_int 21) >>?= fun round -> - Consensus_helpers.test_consensus_operation + ~endorsed_block:grandparent + ~predecessor + ~application_error:error_old_level + ~construction_error:error_old_level + ?mempool_error:None + Endorsement + +(** Endorsement that is two levels too old (pointing to the + great-grandparent instead of the predecessor). It should fail in + all modes. *) +let test_two_levels_too_old () = + let open Lwt_result_syntax in + let* _genesis, greatgrandparent = init_genesis () in + let* grandparent = Block.bake greatgrandparent in + let* predecessor = Block.bake grandparent in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~round - ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_future_round {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - ~construction_mode:(pred, None) - () - -(** Consensus operation for old round : apply an endorsement with a round in the past *) -let test_consensus_operation_endorsement_for_old_round () = - init_genesis ~policy:(By_round 10) () >>=? fun (_genesis, pred) -> - Environment.wrap_tzresult (Round.of_int 0) >>?= fun round -> - Consensus_helpers.test_consensus_operation + ~endorsed_block:greatgrandparent + ~predecessor + ~error:error_old_level + Endorsement + +let error_future_level = function + | Validate_errors.Consensus.Consensus_operation_for_future_level {kind; _} + when kind = Validate_errors.Consensus.Endorsement -> + true + | _ -> false + +(** Endorsement that is one level in the future (pointing to the same + level as the block/mempool containing the endorsement instead of + its predecessor/head). It should fail in all modes. *) +let test_one_level_in_the_future () = + let open Lwt_result_syntax in + let* _genesis, predecessor = init_genesis () in + let* next_level_block = Block.bake predecessor in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~round - ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_old_round {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - ~construction_mode:(pred, None) - () - -(** Consensus operation on competing proposal : apply an endorsement on a competing proposal *) -let test_consensus_operation_endorsement_on_competing_proposal () = - init_genesis () >>=? fun (_genesis, pred) -> - Consensus_helpers.test_consensus_operation + ~endorsed_block:next_level_block + ~predecessor + ~error:error_future_level + Endorsement + +(** Endorsement that is two levels in the future. It should fail in + all modes. *) +let test_two_levels_future () = + let open Lwt_result_syntax in + let* _genesis, predecessor = init_genesis () in + let* next_level_block = Block.bake predecessor in + let* after_next_level_block = Block.bake next_level_block in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:pred - ~block_payload_hash:Block_payload_hash.zero - ~error:(function - | Validate_errors.Consensus.Wrong_payload_hash_for_consensus_operation - {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - ~construction_mode:(pred, None) - () - -(** Wrong round : apply an endorsement with an incorrect round *) -let test_wrong_round () = - init_genesis () >>=? fun (_genesis, b) -> - Environment.wrap_tzresult (Round.of_int 2) >>?= fun round -> - Consensus_helpers.test_consensus_operation + ~endorsed_block:after_next_level_block + ~predecessor + ~error:error_future_level + Endorsement + +(** {2 Wrong round} *) + +let error_old_round = function + | Validate_errors.Consensus.Consensus_operation_for_old_round {kind; _} + when kind = Validate_errors.Consensus.Endorsement -> + true + | _ -> false + +(** Endorsement that is one round too old. It should fail in all modes. *) +let test_one_round_too_old () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* round0_block = Block.bake b in + let* predecessor = Block.bake ~policy:(By_round 1) b in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:b - ~round - ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_future_round {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - () - -(** Wrong level : apply an endorsement with an incorrect level *) -let test_wrong_level () = - init_genesis () >>=? fun (_genesis, b) -> - (* let context = Context.B genesis in*) - let raw_level = Raw_level.of_int32 (Int32.of_int 0) in - let level = match raw_level with Ok l -> l | Error _ -> assert false in - Consensus_helpers.test_consensus_operation + ~endorsed_block:round0_block + ~predecessor + ~error:error_old_round + Endorsement + +(** Endorsement that is many rounds too old. It should fail in all modes. *) +let test_many_rounds_too_old () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* round5_block = Block.bake ~policy:(By_round 5) b in + let* predecessor = Block.bake ~policy:(By_round 15) b in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:b - ~level - ~error:(function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - () + ~endorsed_block:round5_block + ~predecessor + ~error:error_old_round + Endorsement + +let error_future_round = function + | Validate_errors.Consensus.Consensus_operation_for_future_round {kind; _} + when kind = Validate_errors.Consensus.Endorsement -> + true + | _ -> false + +(** Endorsement that is one round in the future. It should fail in all modes. *) +let test_one_round_in_the_future () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* predecessor = Block.bake b in + let* round1_block = Block.bake ~policy:(By_round 1) b in + Consensus_helpers.test_consensus_operation_all_modes + ~loc:__LOC__ + ~endorsed_block:round1_block + ~predecessor + ~error:error_future_round + Endorsement + +(** Endorsement that is many rounds in the future. It should fail in + all modes. *) +let test_many_rounds_future () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* predecessor = Block.bake ~policy:(By_round 5) b in + let* round15_block = Block.bake ~policy:(By_round 15) b in + Consensus_helpers.test_consensus_operation_all_modes + ~loc:__LOC__ + ~endorsed_block:round15_block + ~predecessor + ~error:error_future_round + Endorsement -(** Wrong payload hash : apply an endorsement with an incorrect payload hash *) +(** {2 Wrong payload hash} *) + +(** Endorsement with an incorrect payload hash. *) let test_wrong_payload_hash () = - init_genesis () >>=? fun (_genesis, b) -> - Consensus_helpers.test_consensus_operation + let open Lwt_result_syntax in + let* _genesis, endorsed_block = init_genesis () in + Consensus_helpers.test_consensus_operation_all_modes ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:b + ~endorsed_block ~block_payload_hash:Block_payload_hash.zero ~error:(function | Validate_errors.Consensus.Wrong_payload_hash_for_consensus_operation @@ -309,27 +338,128 @@ let test_wrong_payload_hash () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - () - -let test_wrong_slot_used () = - init_genesis () >>=? fun (_genesis, b) -> - Context.get_endorser (B b) >>=? fun (_, slots) -> - (match slots with - | _x :: y :: _ -> return y - | _ -> failwith "Slots size should be at least of 2 ") - >>=? fun slot -> - Consensus_helpers.test_consensus_operation - ~loc:__LOC__ - ~is_preendorsement:false - ~endorsed_block:b - ~slot - ~error:(function - | Validate_errors.Consensus.Wrong_slot_used_for_consensus_operation - {kind; _} + Endorsement + +(** {1 Conflict tests} + + Some positive and some negative tests. *) + +let assert_conflict_error ~loc res = + Assert.proto_error ~loc res (function + | Validate_errors.Consensus.Conflicting_consensus_operation {kind; _} when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - () + +(** Test that endorsements conflict with: + - an identical endorsement, and + - an endorsement on the same block with a different branch. *) +let test_conflict () = + let open Lwt_result_syntax in + let* _genesis, b = init_genesis () in + let* op = Op.endorsement b in + let* op_different_branch = Op.endorsement ~branch:Block_hash.zero b in + (* Test in application and construction (aka baking) modes *) + let assert_conflict loc baking_mode tested_op = + Block.bake ~baking_mode ~operations:[op; tested_op] b + >>= assert_conflict_error ~loc + in + let* () = assert_conflict __LOC__ Application op in + let* () = assert_conflict __LOC__ Application op_different_branch in + let* () = assert_conflict __LOC__ Baking op in + let* () = assert_conflict __LOC__ Baking op_different_branch in + (* Test in mempool mode. *) + let* inc = Incremental.begin_construction ~mempool_mode:true b in + let* inc = Incremental.validate_operation inc op in + let assert_mempool_conflict loc tested_op = + Incremental.validate_operation inc tested_op >>= assert_conflict_error ~loc + in + let* () = assert_mempool_conflict __LOC__ op in + let* () = assert_mempool_conflict __LOC__ op_different_branch in + return_unit + +(** In mempool mode, test that grandparent endorsements conflict with: + - an identical endorsement, and + - an endorsement on the same block with a different branch. + + This test would make no sense in application or construction modes, + since grandparent endorsements fail anyway (as can be observed in + {!test_one_level_too_old}). *) +let test_grandparent_conflict () = + let open Lwt_result_syntax in + let* _genesis, grandparent = init_genesis () in + let* predecessor = Block.bake grandparent in + let* op = Op.endorsement grandparent in + let* op_different_branch = + Op.endorsement ~branch:Block_hash.zero grandparent + in + let* inc = Incremental.begin_construction ~mempool_mode:true predecessor in + let* inc = Incremental.validate_operation inc op in + let assert_conflict loc tested_op = + Incremental.validate_operation inc tested_op >>= assert_conflict_error ~loc + in + let* () = assert_conflict __LOC__ op in + let* () = assert_conflict __LOC__ op_different_branch in + return_unit + +(** In mempool mode, test that there is no conflict between an + endorsement and a preendorsement for the same slot (here the first + slot), same level, and same round. *) +let test_no_conflict_with_preendorsement_mempool () = + let open Lwt_result_syntax in + let* _genesis, endorsed_block = init_genesis () in + let* op_endo = Op.endorsement endorsed_block in + let* op_preendo = Op.preendorsement endorsed_block in + let* inc = Incremental.begin_construction ~mempool_mode:true endorsed_block in + let* inc = Incremental.add_operation inc op_endo in + let* inc = Incremental.add_operation inc op_preendo in + let* _inc = Incremental.finalize_block inc in + return_unit + +(** In application and construction (aka baking) modes, test that + there is no conflict between an endorsement and a preendorsement + for the same slot (here the first slot). Note that the operations + don't have the same level because the required levels for them to + be valid are different. *) +let test_no_conflict_with_preendorsement_block () = + let open Lwt_result_syntax in + let* _genesis, predecessor = init_genesis () in + let* round0_block = Block.bake predecessor in + let* op_endo = Op.endorsement predecessor in + let* op_preendo = Op.preendorsement round0_block in + let bake_both_ops baking_mode = + Block.bake + ~baking_mode + ~payload_round:(Some Round.zero) + ~locked_round:(Some Round.zero) + ~policy:(By_round 1) + ~operations:[op_endo; op_preendo] + predecessor + in + let* (_ : Block.t) = bake_both_ops Application in + let* (_ : Block.t) = bake_both_ops Baking in + return_unit + +(** In mempool mode, test that there is no conflict between a normal + endorsement (for the predecessor) and a grandparent endorsement, + both for the same slot (here the first slot). There is no similar + test in application and construction modes because grandparent + endorsements are not valid then. *) +let test_no_conflict_with_grandparent () = + let open Lwt_result_syntax in + let* _genesis, grandparent = init_genesis () in + let* predecessor = Block.bake grandparent in + let* op_normal = Op.endorsement predecessor in + let* op_grandparent = Op.endorsement grandparent in + let* inc = Incremental.begin_construction ~mempool_mode:true predecessor in + let* inc = Incremental.add_operation inc op_normal in + let* inc = Incremental.add_operation inc op_grandparent in + let* _inc = Incremental.finalize_block inc in + return_unit + +(** {1 Consensus threshold tests} + + Both positive and negative tests. *) (** Check that: - a block with not enough endorsement cannot be baked; @@ -363,207 +493,46 @@ let test_endorsement_threshold ~sufficient_threshold () = if sufficient_threshold then return_unit else Assert.proto_error_with_info ~loc:__LOC__ b "Not enough endorsements" -(** Fitness gap: this is a straightforward update from Emmy to Tenderbake, that - is, check that the level is incremented in a child block. *) -let test_fitness_gap () = - inject_the_first_endorsement () >>=? fun (b, pred_b) -> - let fitness = - match Fitness.from_raw b.header.shell.fitness with - | Ok fitness -> fitness - | _ -> assert false - in - let pred_fitness = - match Fitness.from_raw pred_b.header.shell.fitness with - | Ok fitness -> fitness - | _ -> assert false - in - let level = Fitness.level fitness in - let pred_level = Fitness.level pred_fitness in - let level_diff = - Int32.sub (Raw_level.to_int32 level) (Raw_level.to_int32 pred_level) - in - Assert.equal_int32 ~loc:__LOC__ level_diff 1l - -let test_preendorsement_endorsement_same_level () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b1 -> - Incremental.begin_construction ~mempool_mode:true ~policy:(By_round 2) b1 - >>=? fun i -> - Op.endorsement b1 >>=? fun op_endo -> - Incremental.add_operation i op_endo >>=? fun (_i : Incremental.t) -> - Op.preendorsement b1 >>=? fun op_preendo -> - Incremental.add_operation i op_preendo >>=? fun (_i : Incremental.t) -> - return_unit - -(** Test for endorsement injection with wrong slot in mempool mode. This - test is expected to fail *) -let test_wrong_endorsement_slot_in_mempool_mode () = - Context.init_n ~consensus_threshold:1 5 () >>=? fun (genesis, _) -> - Block.bake genesis >>=? fun b1 -> - let module V = Plugin.RPC.Validators in - (Context.get_endorsers (B b1) >>=? function - | {V.slots = _ :: non_canonical_slot :: _; _} :: _ -> - (* we didn't use min slot for the injection. It's bad !*) - return (Some non_canonical_slot) - | _ -> assert false) - >>=? fun slot -> - Op.endorsement ?slot b1 >>=? fun endo -> - Incremental.begin_construction ~mempool_mode:true b1 >>=? fun i -> - Incremental.add_operation i endo >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Wrong_slot_used_for_consensus_operation {kind} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - -(** Endorsement for next level *) -let test_endorsement_for_next_level () = - init_genesis () >>=? fun (genesis, _) -> - Consensus_helpers.test_consensus_op_for_next - ~genesis - ~kind:`Endorsement - ~next:`Level - -(** Endorsement for next round *) -let test_endorsement_for_next_round () = - init_genesis () >>=? fun (genesis, _) -> - Consensus_helpers.test_consensus_op_for_next - ~genesis - ~kind:`Endorsement - ~next:`Round - -(** Endorsement of grandparent *) -let test_endorsement_grandparent () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b_gp -> - Block.bake b_gp >>=? fun b -> - Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> - (* Endorsement on grandparent *) - Op.endorsement b_gp >>=? fun op1 -> - (* Endorsement on parent *) - Op.endorsement b >>=? fun op2 -> - (* Both should be accepted by the mempool *) - Incremental.add_operation i op1 >>=? fun i -> - Incremental.add_operation i op2 >>=? fun (_i : Incremental.t) -> return_unit - -(** Double inclusion of grandparent endorsement *) -let test_double_endorsement_grandparent () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b_gp -> - Block.bake b_gp >>=? fun b -> - Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> - (* Endorsement on grandparent *) - Op.endorsement b_gp >>=? fun op1 -> - (* Endorsement on parent *) - Op.endorsement b >>=? fun op2 -> - (* The first grand parent endorsement should be accepted by the - mempool but the second rejected. *) - Incremental.add_operation i op1 >>=? fun i -> - Incremental.add_operation i op1 >>= fun res -> - Assert.proto_error_with_info - ~loc:__LOC__ - res - "Double inclusion of consensus operation" - >>=? fun () -> - Incremental.add_operation i op2 >>=? fun (_i : Incremental.t) -> return_unit - -(** Endorsement of grandparent on same slot as parent *) -let test_endorsement_grandparent_same_slot () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b_gp -> - Block.bake b_gp >>=? fun b -> - Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> - (* Endorsement on parent *) - Consensus_helpers.delegate_of_first_slot (B b) >>=? fun (delegate, slot) -> - Op.endorsement ~delegate b >>=? fun op2 -> - (* Endorsement on grandparent *) - Consensus_helpers.delegate_of_slot slot (B b_gp) >>=? fun delegate -> - Op.endorsement ~delegate b_gp >>=? fun op1 -> - (* Both should be accepted by the mempool *) - Incremental.add_operation i op1 >>=? fun i -> - Incremental.add_operation i op2 >>=? fun (_i : Incremental.t) -> return_unit - -(** Endorsement of grandparent in application mode should be rejected *) -let test_endorsement_grandparent_application () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b_gp -> - Block.bake b_gp >>=? fun b -> - Op.endorsement b_gp >>=? fun operation -> - Block.bake ~operation b >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - -(** Endorsement of grandparent in full construction mode should be rejected *) -let test_endorsement_grandparent_full_construction () = - Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> - Block.bake genesis >>=? fun b_gp -> - Block.bake b_gp >>=? fun b -> - Incremental.begin_construction b >>=? fun i -> - (* Endorsement on grandparent *) - Op.endorsement b_gp >>=? fun op1 -> - Incremental.add_operation i op1 >>= fun res -> - Assert.proto_error ~loc:__LOC__ res (function - | Validate_errors.Consensus.Consensus_operation_for_old_level {kind; _} - when kind = Validate_errors.Consensus.Endorsement -> - true - | _ -> false) - let tests = [ + (* Positive tests *) Tztest.tztest "Simple endorsement" `Quick test_simple_endorsement; - Tztest.tztest - "Endorsement with arbitrary branch" - `Quick - test_endorsement_with_arbitrary_branch; - Tztest.tztest "Endorsement with slot -1" `Quick test_negative_slot; - Tztest.tztest - "Endorsement wrapped with non-normalized slot" - `Quick - test_non_normalized_slot; + Tztest.tztest "Arbitrary branch" `Quick test_arbitrary_branch; + Tztest.tztest "Non-zero round" `Quick test_non_zero_round; Tztest.tztest "Fitness gap" `Quick test_fitness_gap; - (* Fail scenarios *) - Tztest.tztest - "Invalid endorsement level" - `Quick - test_invalid_endorsement_level; - Tztest.tztest "Duplicate endorsement" `Quick test_duplicate_endorsement; - Tztest.tztest - "Endorsement for future level" - `Quick - test_consensus_operation_endorsement_for_future_level; - Tztest.tztest - "Endorsement for predecessor level" - `Quick - test_consensus_operation_endorsement_for_old_level; - Tztest.tztest - "Endorsement for old level" - `Quick - test_consensus_operation_endorsement_for_old_level; - Tztest.tztest - "Endorsement for future round" - `Quick - test_consensus_operation_endorsement_for_future_round; - Tztest.tztest - "Endorsement for old round" - `Quick - test_consensus_operation_endorsement_for_old_round; + (* Negative tests *) + (* Wrong slot *) + Tztest.tztest "Endorsement with slot -1" `Quick test_negative_slot; + Tztest.tztest "Non-normalized slot" `Quick test_not_smallest_slot; + Tztest.tztest "Slot of another delegate" `Quick test_other_delegate_slot; + (* Wrong level *) + Tztest.tztest "One level too old" `Quick test_one_level_too_old; + Tztest.tztest "Two levels too old" `Quick test_two_levels_too_old; + Tztest.tztest "One level in the future" `Quick test_one_level_in_the_future; + Tztest.tztest "Two levels in the future" `Quick test_two_levels_future; + (* Wrong round *) + Tztest.tztest "One round too old" `Quick test_one_round_too_old; + Tztest.tztest "Many rounds too old" `Quick test_many_rounds_too_old; + Tztest.tztest "One round in the future" `Quick test_one_round_in_the_future; + Tztest.tztest "Many rounds in the future" `Quick test_many_rounds_future; + (* Wrong payload hash *) + Tztest.tztest "Wrong payload hash" `Quick test_wrong_payload_hash; + (* Conflict tests (some negative, some positive) *) + Tztest.tztest "Conflict" `Quick test_conflict; + Tztest.tztest "Grandparent conflict" `Quick test_grandparent_conflict; Tztest.tztest - "Endorsement on competing proposal" + "No conflict with preendorsement (mempool)" `Quick - test_consensus_operation_endorsement_on_competing_proposal; - Tztest.tztest "Wrong level for consensus operation" `Quick test_wrong_level; - Tztest.tztest "Wrong round for consensus operation" `Quick test_wrong_round; + test_no_conflict_with_preendorsement_mempool; Tztest.tztest - "Wrong payload hash for consensus operation" + "No conflict with preendorsement (block)" `Quick - test_wrong_payload_hash; + test_no_conflict_with_preendorsement_block; Tztest.tztest - "Wrong slot used for consensus operation" + "No conflict with grandparent endorsement" `Quick - test_wrong_slot_used; + test_no_conflict_with_grandparent; + (* Consensus threshold tests (one positive and one negative) *) Tztest.tztest "sufficient endorsement threshold" `Quick @@ -572,40 +541,4 @@ let tests = "insufficient endorsement threshold" `Quick (test_endorsement_threshold ~sufficient_threshold:false); - Tztest.tztest - "Endorsement/Preendorsement at same level" - `Quick - test_preendorsement_endorsement_same_level; - Tztest.tztest - "Wrong endorsement slot in mempool mode" - `Quick - test_wrong_endorsement_slot_in_mempool_mode; - Tztest.tztest - "Endorsement for next level" - `Quick - test_endorsement_for_next_level; - Tztest.tztest - "Endorsement for next round" - `Quick - test_endorsement_for_next_round; - Tztest.tztest - "Endorsement for grandparent" - `Quick - test_endorsement_grandparent; - Tztest.tztest - "Double endorsement of grandparent" - `Quick - test_double_endorsement_grandparent; - Tztest.tztest - "Endorsement for grandparent on same slot as parent" - `Quick - test_endorsement_grandparent_same_slot; - Tztest.tztest - "Endorsement for grandparent in application mode" - `Quick - test_endorsement_grandparent_application; - Tztest.tztest - "Endorsement for grandparent in full construction mode" - `Quick - test_endorsement_grandparent_full_construction; ] diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml index aab600a1713057d7265d219a64640a9e554eba36..40b0695d7b54f4a6b3553bbfc3be88daac4085ba 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml @@ -51,7 +51,7 @@ let init_genesis ?policy () = let test_preendorsement_with_arbitrary_branch () = Context.init1 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun blk -> - Op.preendorsement ~pred_branch:Block_hash.zero blk >>=? fun operation -> + Op.preendorsement ~branch:Block_hash.zero blk >>=? fun operation -> Incremental.begin_construction ~mempool_mode:true blk >>=? fun inc -> Incremental.validate_operation inc operation >>=? fun _inc -> return_unit @@ -62,7 +62,6 @@ let test_consensus_operation_preendorsement_for_future_level () = let level = match raw_level with Ok l -> l | Error _ -> assert false in Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~level ~error:(function @@ -70,8 +69,8 @@ let test_consensus_operation_preendorsement_for_future_level () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () + Preendorsement + Mempool (** Consensus operation for old level : apply a preendorsement with a level in the past *) let test_consensus_operation_preendorsement_for_old_level () = @@ -80,7 +79,6 @@ let test_consensus_operation_preendorsement_for_old_level () = let level = match raw_level with Ok l -> l | Error _ -> assert false in Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~level ~error:(function @@ -88,8 +86,8 @@ let test_consensus_operation_preendorsement_for_old_level () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () + Preendorsement + Mempool (** Consensus operation for future round : apply a preendorsement with a round in the future *) let test_consensus_operation_preendorsement_for_future_round () = @@ -97,7 +95,6 @@ let test_consensus_operation_preendorsement_for_future_round () = Environment.wrap_tzresult (Round.of_int 21) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~round ~error:(function @@ -105,8 +102,8 @@ let test_consensus_operation_preendorsement_for_future_round () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () + Preendorsement + Mempool (** Consensus operation for old round : apply a preendorsement with a round in the past *) let test_consensus_operation_preendorsement_for_old_round () = @@ -114,7 +111,6 @@ let test_consensus_operation_preendorsement_for_old_round () = Environment.wrap_tzresult (Round.of_int 0) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~round ~error:(function @@ -122,15 +118,14 @@ let test_consensus_operation_preendorsement_for_old_round () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () + Preendorsement + Mempool (** Consensus operation on competing proposal : apply a preendorsement on a competing proposal *) let test_consensus_operation_preendorsement_on_competing_proposal () = init_genesis () >>=? fun (_genesis, pred) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~block_payload_hash:Block_payload_hash.zero ~error:(function @@ -139,20 +134,20 @@ let test_consensus_operation_preendorsement_on_competing_proposal () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~construction_mode:(pred, None) - () + Preendorsement + Mempool (** Unexpected preendorsements in block : apply a preendorsement with an incorrect round *) let test_unexpected_preendorsements_in_blocks () = init_genesis () >>=? fun (_genesis, pred) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~error:(function | Validate_errors.Consensus.Unexpected_preendorsement_in_block -> true | _ -> false) - () + Preendorsement + Application (** Round too high : apply a preendorsement with a too high round *) let test_too_high_round () = @@ -162,15 +157,14 @@ let test_too_high_round () = Environment.wrap_tzresult (Round.of_int 1) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ - ~is_preendorsement:true ~endorsed_block:pred ~round ~level ~error:(function | Validate_errors.Consensus.Preendorsement_round_too_high _ -> true | _ -> false) - ~construction_mode:(pred, Some pred.header.protocol_data) - () + Preendorsement + Construction (** Duplicate preendorsement : apply a preendorsement that has already been applied. *) let test_duplicate_preendorsement () = diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml index 14d283a6ee9fcdf5489d885308df4c369338fa4d..5002e086384c1bcf0eae55a73ec1e889e80aae87 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml @@ -51,7 +51,6 @@ end = struct let aux_simple_preendorsement_inclusion ?(payload_round = Some Round.zero) ?(locked_round = Some Round.zero) ?(block_round = 1) ?(preend_round = Round.zero) - ?(preend_branch = fun _predpred pred _curr -> pred) ?(preendorsed_block = fun _predpred _pred curr -> curr) ?(mk_ops = fun op -> [op]) ?(get_delegate_and_slot = @@ -61,17 +60,9 @@ end = struct bake genesis >>=? fun b1 -> Op.endorsement b1 >>=? fun endo -> bake b1 ~operations:[endo] >>=? fun b2 -> - let pred_branch = - Some (Context.branch (Context.B (preend_branch genesis b1 b2))) - in let endorsed_block = preendorsed_block genesis b1 b2 in get_delegate_and_slot genesis b1 b2 >>=? fun (delegate, slot) -> - Op.preendorsement - ?delegate - ?slot - ?pred_branch - ~round:preend_round - endorsed_block + Op.preendorsement ?delegate ?slot ~round:preend_round endorsed_block >>=? fun p -> let operations = endo :: (mk_ops @@ p) in bake