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 8c6e139df83bb213c649bbf48137cdaaa4fa1e37..3db4a7d1ab13e558717b0cf1497b23571a840566 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/consensus_helpers.ml @@ -27,46 +27,29 @@ open Protocol open Alpha_context let test_consensus_operation ?construction_mode ?level ?block_payload_hash ?slot - ?round ~endorsed_block ~error ~is_preendorsement ~context ~loc () = + ?round ~endorsed_block ~error ~is_preendorsement ~loc () = (if is_preendorsement then - Op.preendorsement - ~endorsed_block - ?block_payload_hash - ?level - ?slot - ?round - context - () - >|=? fun op -> Operation.pack op - else - Op.endorsement - ~endorsed_block - ?block_payload_hash - ?level - ?slot - ?round - context - () - >|=? fun op -> Operation.pack op) - >>=? fun op -> + 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 ~operations:[op] endorsed_block >>= assert_error + 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 op in - validate_operation validation_state oph op + let oph = Operation.hash_packed operation in + validate_operation validation_state oph operation >|= Environment.wrap_tzresult >>= assert_error let delegate_of_first_slot b = let module V = Plugin.RPC.Validators in Context.get_endorsers b >|=? function - | {V.delegate; slots = s :: _ as slots; _} :: _ -> ((delegate, slots), s) + | {V.consensus_key; slots = s :: _; _} :: _ -> (consensus_key, s) | _ -> assert false let delegate_of_slot ?(different_slot = false) slot b = @@ -74,10 +57,10 @@ let delegate_of_slot ?(different_slot = false) slot b = Context.get_endorsers b >|=? fun endorsers -> List.find_map (function - | {V.delegate; slots = s :: _ as slots; _} + | {V.consensus_key; slots = s :: _; _} when if different_slot then not (Slot.equal s slot) else Slot.equal s slot -> - Some (delegate, slots) + Some consensus_key | _ -> None) endorsers |> function @@ -85,12 +68,10 @@ let delegate_of_slot ?(different_slot = false) slot b = | Some d -> d let test_consensus_op_for_next ~genesis ~kind ~next = - let dorsement ~endorsed_block ~delegate b = + let dorsement ~endorsed_block ~delegate = match kind with - | `Preendorsement -> - Op.preendorsement ~endorsed_block ~delegate b () >|=? Operation.pack - | `Endorsement -> - Op.endorsement ~endorsed_block ~delegate b () >|=? Operation.pack + | `Preendorsement -> Op.preendorsement ~delegate endorsed_block + | `Endorsement -> Op.endorsement ~delegate endorsed_block in Block.bake genesis >>=? fun b1 -> (match next with @@ -99,10 +80,10 @@ let test_consensus_op_for_next ~genesis ~kind ~next = >>=? fun b2 -> Incremental.begin_construction ~mempool_mode:true b1 >>=? fun inc -> delegate_of_first_slot (B b1) >>=? fun (delegate, slot) -> - dorsement ~endorsed_block:b1 ~delegate (B genesis) >>=? fun operation -> + dorsement ~endorsed_block:b1 ~delegate >>=? fun operation -> Incremental.add_operation inc operation >>=? fun inc -> delegate_of_slot ~different_slot:true slot (B b2) >>=? fun delegate -> - dorsement ~endorsed_block:b2 ~delegate (B b1) >>=? fun operation -> + dorsement ~endorsed_block:b2 ~delegate >>=? fun operation -> Incremental.add_operation inc operation >>= fun res -> let error_title = match next with diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index efcf8950a42c396a8a0ea7ec596d1a207d5f2141..13303b0a999f65e175924b636207a80b97c89671 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -31,6 +31,10 @@ type t = B of Block.t | I of Incremental.t let branch = function B b -> b.hash | I i -> (Incremental.predecessor i).hash +let pred_branch = function + | B b -> b.header.shell.predecessor + | I i -> (Incremental.predecessor i).hash + let level = function B b -> b.header.shell.level | I i -> Incremental.level i let get_level ctxt = @@ -129,14 +133,14 @@ let get_first_different_endorsers ctxt = let get_endorser ctxt = get_endorsers ctxt >|=? fun endorsers -> let endorser = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd endorsers in - (endorser.delegate, endorser.slots) + (endorser.consensus_key, endorser.slots) let get_endorser_slot ctxt pkh = get_endorsers ctxt >|=? fun endorsers -> List.find_map (function - | {Plugin.RPC.Validators.delegate; slots; _} -> - if Tezos_crypto.Signature.Public_key_hash.(delegate = pkh) then + | {Plugin.RPC.Validators.consensus_key; slots; _} -> + if Tezos_crypto.Signature.Public_key_hash.(consensus_key = pkh) then Some slots else None) endorsers @@ -146,7 +150,7 @@ let get_endorser_n ctxt n = let endorser = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth endorsers n in - (endorser.delegate, endorser.slots) + (endorser.consensus_key, endorser.slots) let get_endorsing_power_for_delegate ctxt ?levels pkh = Plugin.RPC.Validators.get rpc_ctxt ?levels ctxt >>=? fun endorsers -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 34ac44f4391d1d7e69764d411c921232e5406f93..7e9e4dd6a024c82523ed9d7a4f24d0ff5d156f64 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -32,6 +32,8 @@ type t = B of Block.t | I of Incremental.t val branch : t -> Tezos_crypto.Block_hash.t +val pred_branch : t -> Tezos_crypto.Block_hash.t + val get_level : t -> Raw_level.t tzresult (** Either retrieve the alpha context (in the [Incremental] case) or @@ -48,11 +50,13 @@ val get_endorsers : t -> Plugin.RPC.Validators.t list tzresult Lwt.t val get_first_different_endorsers : t -> (Plugin.RPC.Validators.t * Plugin.RPC.Validators.t) tzresult Lwt.t -(** Return the first element of the list returns by [get_endorsers]. *) +(** Return the first element [delegate,slot] of the list returns by + [get_endorsers], where [delegate] is the [consensus key] when + is set. *) val get_endorser : t -> (public_key_hash * Slot.t list) tzresult Lwt.t -(** Given a delegate public key hash [del], and a context [ctxt], - if [del] is in [get_endorsers ctxt] returns the [slots] of [del] otherwise +(** Given a [delegate], and a context [ctxt], if [delegate] is in + [get_endorsers ctxt] returns the [slots] of [delegate] otherwise return [None]. *) val get_endorser_slot : t -> public_key_hash -> Slot.t list option tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index e9dc7de381d967c7b83557eb6e8f7a9e1b2442db..1fcb323a02044c3e907987ffffa7cbcd1596bdf8 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -31,9 +31,8 @@ let pack_operation ctxt signature contents = Operation.pack ({shell = {branch}; protocol_data = {contents; signature}} : _ Operation.t) -let sign ?(watermark = Tezos_crypto.Signature.Generic_operation) sk ctxt +let sign ?(watermark = Tezos_crypto.Signature.Generic_operation) sk branch contents = - let branch = Context.branch ctxt in let unsigned = Data_encoding.Binary.to_bytes_exn Operation.unsigned_encoding @@ -53,75 +52,123 @@ let mk_block_payload_hash predecessor_hash payload_round (b : Block.t) = let hashes = List.map Operation.hash_packed non_consensus_operations in Block_payload.hash ~predecessor_hash ~payload_round hashes -(* ctxt is used for getting the branch in sign *) -let endorsement ?delegate ?slot ?level ?round ?block_payload_hash - ~endorsed_block ctxt ?(signing_context = ctxt) () = - let pred_hash = match ctxt with Context.B b -> b.hash | _ -> assert false in - (match delegate with - | None -> Context.get_endorser (B endorsed_block) - | Some v -> return v) - >>=? fun (delegate_pkh, slots) -> +let mk_consensus_content_signer_and_pred_branch ?delegate ?slot ?level ?round + ?block_payload_hash ?pred_branch endorsed_block = + let open Lwt_result_syntax in + let pred_branch = + match pred_branch with + | None -> endorsed_block.Block.header.shell.predecessor + | Some branch -> branch + in + let* delegate_pkh, slots = + match delegate with + | None -> Context.get_endorser (B endorsed_block) + | Some del -> ( + let* slots = Context.get_endorser_slot (B endorsed_block) del in + match slots with + | None -> return (del, []) + | Some slots -> return (del, slots)) + in let slot = match slot with None -> Stdlib.List.hd slots | Some slot -> slot in - (match level with - | None -> Context.get_level (B endorsed_block) - | Some level -> ok level) - >>?= fun level -> - (match round with - | None -> Block.get_round endorsed_block - | Some round -> ok round) - >>?= fun round -> + let* level = + match level with + | None -> + let*? level = Context.get_level (B endorsed_block) in + return level + | Some level -> return level + in + let* round = + match round with + | None -> + let*? round = Block.get_round endorsed_block in + return round + | Some round -> return round + in let block_payload_hash = match block_payload_hash with - | None -> mk_block_payload_hash pred_hash round endorsed_block + | None -> mk_block_payload_hash pred_branch round endorsed_block | Some block_payload_hash -> block_payload_hash 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) + +let raw_endorsement ?delegate ?slot ?level ?round ?block_payload_hash + ?pred_branch endorsed_block = + let open Lwt_result_syntax in + let* consensus_content, signer, pred_branch = + mk_consensus_content_signer_and_pred_branch + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?pred_branch + endorsed_block + in let op = Single (Endorsement consensus_content) in - Account.find delegate_pkh >>=? fun delegate -> return (sign ~watermark: Operation.(to_watermark (Endorsement Tezos_crypto.Chain_id.zero)) - delegate.sk - signing_context + signer + pred_branch op) -let preendorsement ?delegate ?slot ?level ?round ?block_payload_hash - ~endorsed_block ctxt ?(signing_context = ctxt) () = - let pred_hash = match ctxt with Context.B b -> b.hash | _ -> assert false in - (match delegate with - | None -> Context.get_endorser (B endorsed_block) - | Some v -> return v) - >>=? fun (delegate_pkh, slots) -> - let slot = - match slot with None -> Stdlib.List.hd slots | Some slot -> slot +let endorsement ?delegate ?slot ?level ?round ?block_payload_hash ?pred_branch + endorsed_block = + let open Lwt_result_syntax in + let* op = + raw_endorsement + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?pred_branch + endorsed_block in - (match level with - | None -> Context.get_level (B endorsed_block) - | Some level -> ok level) - >>?= fun level -> - (match round with - | None -> Block.get_round endorsed_block - | Some round -> ok round) - >>?= fun round -> - let block_payload_hash = - match block_payload_hash with - | None -> mk_block_payload_hash pred_hash round endorsed_block - | Some block_payload_hash -> block_payload_hash + return (Operation.pack op) + +let raw_preendorsement ?delegate ?slot ?level ?round ?block_payload_hash + ?pred_branch endorsed_block = + let open Lwt_result_syntax in + let* consensus_content, signer, pred_branch = + mk_consensus_content_signer_and_pred_branch + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?pred_branch + endorsed_block in - let consensus_content = {slot; level; round; block_payload_hash} in let op = Single (Preendorsement consensus_content) in - Account.find delegate_pkh >>=? fun delegate -> return (sign ~watermark: Operation.(to_watermark (Preendorsement Tezos_crypto.Chain_id.zero)) - delegate.sk - signing_context + signer + pred_branch op) +let preendorsement ?delegate ?slot ?level ?round ?block_payload_hash + ?pred_branch endorsed_block = + let open Lwt_result_syntax in + let* op = + raw_preendorsement + ?delegate + ?slot + ?level + ?round + ?block_payload_hash + ?pred_branch + endorsed_block + in + return (Operation.pack op) + let sign ?watermark sk ctxt (Contents_list contents) = Operation.pack (sign ?watermark sk ctxt contents) @@ -153,7 +200,8 @@ let batch_operations ?(recompute_counters = false) ~source ctxt >>=? fun operations -> Context.Contract.manager ctxt source >>=? fun account -> Environment.wrap_tzresult @@ Operation.of_list operations - >>?= fun operations -> return @@ sign account.sk ctxt operations + >>?= fun operations -> + return @@ sign account.sk (Context.branch ctxt) operations type gas_limit = Max | High | Low | Zero | Custom_gas of Gas.Arith.integral @@ -266,7 +314,8 @@ let combine_operations ?public_key ?counter ?spurious_operation ~source ctxt operations @ [op] in Environment.wrap_tzresult @@ Operation.of_list operations - >>?= fun operations -> return @@ sign account.sk ctxt operations + >>?= fun operations -> + return @@ sign account.sk (Context.branch ctxt) operations let manager_operation ?(force_reveal = false) ?counter ?(fee = Tez.zero) ?(gas_limit = High) ?storage_limit ?public_key ~source ctxt operation = @@ -360,11 +409,12 @@ let revelation ?(fee = Tez.zero) ?(gas_limit = High) ?(storage_limit = Z.zero) storage_limit; })) in - sign account.sk ctxt sop + sign account.sk (Context.branch ctxt) sop let failing_noop ctxt source arbitrary = let op = Contents_list (Single (Failing_noop arbitrary)) in - Account.find source >>=? fun account -> return @@ sign account.sk ctxt op + Account.find source >>=? fun account -> + return @@ sign account.sk (Context.branch ctxt) op let originated_contract_hash op = let nonce = Protocol.Origination_nonce.initial (Operation.hash_packed op) in @@ -393,7 +443,7 @@ let contract_origination_gen k ?force_reveal ?counter ?delegate ~script ~source ctxt operation - >|=? fun sop -> k (sign account.sk ctxt sop) + >|=? fun sop -> k (sign account.sk (Context.branch ctxt) sop) let contract_origination = contract_origination_gen (fun op -> (op, originated_contract op)) @@ -415,14 +465,7 @@ let register_global_constant ?force_reveal ?counter ?public_key ?fee ?gas_limit ~source ctxt operation - >|=? fun sop -> sign account.sk ctxt sop - -let miss_signed_endorsement ?level ~endorsed_block ctxt = - (match level with None -> Context.get_level ctxt | Some level -> ok level) - >>?= fun level -> - Context.get_endorser ctxt >>=? fun (real_delegate_pkh, slots) -> - let delegate = Account.find_alternate real_delegate_pkh in - endorsement ~delegate:(delegate.pkh, slots) ~level ~endorsed_block ctxt () + >|=? fun sop -> sign account.sk (Context.branch ctxt) sop let unsafe_transaction ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ?(parameters = Script.unit_parameter) ?(entrypoint = Entrypoint.default) @@ -438,7 +481,8 @@ let unsafe_transaction ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt top >>=? fun sop -> - Context.Contract.manager ctxt src >|=? fun account -> sign account.sk ctxt sop + Context.Contract.manager ctxt src >|=? fun account -> + sign account.sk (Context.branch ctxt) sop let transaction ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ?parameters ?entrypoint ctxt (src : Contract.t) (dst : Contract.t) @@ -470,7 +514,7 @@ let delegation ?force_reveal ?fee ?gas_limit ?counter ?storage_limit ctxt source top >>=? fun sop -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt sop + sign account.sk (Context.branch ctxt) sop let set_deposits_limit ?force_reveal ?fee ?gas_limit ?storage_limit ?counter ctxt source limit = @@ -486,7 +530,7 @@ let set_deposits_limit ?force_reveal ?fee ?gas_limit ?storage_limit ?counter top >>=? fun sop -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt sop + sign account.sk (Context.branch ctxt) sop let increase_paid_storage ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt ~source ~destination (amount : Z.t) = @@ -502,7 +546,7 @@ let increase_paid_storage ?force_reveal ?counter ?fee ?gas_limit ?storage_limit top >>=? fun sop -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt sop + sign account.sk (Context.branch ctxt) sop let activation ctxt (pkh : Tezos_crypto.Signature.Public_key_hash.t) activation_code = @@ -583,7 +627,7 @@ let proposals ctxt proposer ?period proposals = let open Lwt_result_syntax in let* contents = proposals_contents ctxt proposer ?period proposals in let* account = Account.find (Context.Contract.pkh proposer) in - return (sign account.sk ctxt (Contents_list contents)) + return (sign account.sk (Context.branch ctxt) (Contents_list contents)) let ballot_contents ctxt voter ?period proposal ballot = let open Lwt_result_syntax in @@ -595,7 +639,7 @@ let ballot ctxt voter ?period proposal ballot = let open Lwt_result_syntax in let* contents = ballot_contents ctxt voter ?period proposal ballot in let* account = Account.find (Context.Contract.pkh voter) in - return (sign account.sk ctxt (Contents_list contents)) + return (sign account.sk (Context.branch ctxt) (Contents_list contents)) let dummy_script = let open Micheline in @@ -651,7 +695,7 @@ let tx_rollup_origination ?force_reveal ?counter ?fee ?gas_limit ?storage_limit Tx_rollup_origination >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - let op = sign account.sk ctxt to_sign_op in + let op = sign account.sk (Context.branch ctxt) to_sign_op in (op, originated_tx_rollup op |> snd) let tx_rollup_submit_batch ?force_reveal ?counter ?fee ?burn_limit ?gas_limit @@ -668,7 +712,7 @@ let tx_rollup_submit_batch ?force_reveal ?counter ?fee ?burn_limit ?gas_limit (Tx_rollup_submit_batch {tx_rollup; content; burn_limit}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_commit ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) @@ -684,7 +728,7 @@ let tx_rollup_commit ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Tx_rollup_commit {tx_rollup; commitment}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_return_bond ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) = @@ -699,7 +743,7 @@ let tx_rollup_return_bond ?force_reveal ?counter ?fee ?gas_limit ?storage_limit (Tx_rollup_return_bond {tx_rollup}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_finalize ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) = @@ -714,7 +758,7 @@ let tx_rollup_finalize ?force_reveal ?counter ?fee ?gas_limit ?storage_limit (Tx_rollup_finalize_commitment {tx_rollup}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_remove_commitment ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) = @@ -729,7 +773,7 @@ let tx_rollup_remove_commitment ?force_reveal ?counter ?fee ?gas_limit (Tx_rollup_remove_commitment {tx_rollup}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_dispatch_tickets ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt ~(source : Contract.t) ~message_index @@ -753,7 +797,7 @@ let tx_rollup_dispatch_tickets ?force_reveal ?counter ?fee ?gas_limit }) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let transfer_ticket ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt ~(source : Contract.t) ~contents ~ty ~ticketer ~amount ~destination @@ -769,7 +813,7 @@ let transfer_ticket ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Transfer_ticket {contents; ty; ticketer; amount; destination; entrypoint}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_raw_reject ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) @@ -802,7 +846,7 @@ let tx_rollup_raw_reject ?force_reveal ?counter ?fee ?gas_limit ?storage_limit }) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let tx_rollup_reject ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (source : Contract.t) (tx_rollup : Tx_rollup.t) (level : Tx_rollup_level.t) @@ -863,7 +907,7 @@ let sc_rollup_origination ?force_reveal ?counter ?fee ?gas_limit ?storage_limit (Sc_rollup_originate {kind; boot_sector; origination_proof; parameters_ty}) in let* account = Context.Contract.manager ctxt src in - let op = sign account.sk ctxt to_sign_op in + let op = sign account.sk (Context.branch ctxt) to_sign_op in let t = originated_sc_rollup op |> fun addr -> (op, addr) in return t @@ -880,7 +924,7 @@ let sc_rollup_publish ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Sc_rollup_publish {rollup; commitment}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_cement ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) rollup commitment = @@ -895,7 +939,7 @@ let sc_rollup_cement ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Sc_rollup_cement {rollup; commitment}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_execute_outbox_message ?counter ?fee ?gas_limit ?storage_limit ?force_reveal ctxt (src : Contract.t) rollup cemented_commitment @@ -912,7 +956,7 @@ let sc_rollup_execute_outbox_message ?counter ?fee ?gas_limit ?storage_limit {rollup; cemented_commitment; output_proof}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_recover_bond ?counter ?fee ?gas_limit ?storage_limit ?force_reveal ctxt (source : Contract.t) (sc_rollup : Sc_rollup.t) = @@ -927,7 +971,7 @@ let sc_rollup_recover_bond ?counter ?fee ?gas_limit ?storage_limit ?force_reveal (Sc_rollup_recover_bond {sc_rollup}) >>=? fun to_sign_op -> Context.Contract.manager ctxt source >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_add_messages ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) messages = @@ -942,7 +986,7 @@ let sc_rollup_add_messages ?force_reveal ?counter ?fee ?gas_limit ?storage_limit (Sc_rollup_add_messages {messages}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_refute ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) rollup opponent refutation = @@ -957,7 +1001,7 @@ let sc_rollup_refute ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Sc_rollup_refute {rollup; opponent; refutation}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let sc_rollup_timeout ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) rollup stakers = @@ -972,7 +1016,7 @@ let sc_rollup_timeout ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Sc_rollup_timeout {rollup; stakers}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let dal_publish_slot_header ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) slot_header = @@ -987,7 +1031,7 @@ let dal_publish_slot_header ?force_reveal ?counter ?fee ?gas_limit (Dal_publish_slot_header {slot_header}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let originated_zk_rollup op = let packed = Operation.hash_packed op in @@ -1009,7 +1053,7 @@ let zk_rollup_origination ?force_reveal ?counter ?fee ?gas_limit ?storage_limit {public_parameters; circuits_info; init_state; nb_ops}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - let op = sign account.sk ctxt to_sign_op in + let op = sign account.sk (Context.branch ctxt) to_sign_op in originated_zk_rollup op |> fun addr -> (op, addr) let update_consensus_key ?force_reveal ?counter ?fee ?gas_limit ?storage_limit @@ -1025,14 +1069,15 @@ let update_consensus_key ?force_reveal ?counter ?fee ?gas_limit ?storage_limit (Update_consensus_key pkh) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let drain_delegate ctxt ~consensus_key ~delegate ~destination = let contents = Single (Drain_delegate {consensus_key; delegate; destination}) in Context.Contract.manager ctxt (Contract.Implicit consensus_key) - >|=? fun account -> sign account.sk ctxt (Contents_list contents) + >|=? fun account -> + sign account.sk (Context.branch ctxt) (Contents_list contents) let zk_rollup_publish ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) ~zk_rollup ~ops = @@ -1047,7 +1092,7 @@ let zk_rollup_publish ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Zk_rollup_publish {zk_rollup; ops}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op let zk_rollup_update ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (src : Contract.t) ~zk_rollup ~update = @@ -1062,4 +1107,4 @@ let zk_rollup_update ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt (Zk_rollup_update {zk_rollup; update}) >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> - sign account.sk ctxt to_sign_op + sign account.sk (Context.branch ctxt) to_sign_op diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index 13760980995c19848af3b0791627b6403a8cbf14..a7c26c428035c2ff7e297cc53028d76587220a1a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -47,39 +47,66 @@ val pack_operation : val sign : ?watermark:Tezos_crypto.Signature.watermark -> Tezos_crypto.Signature.secret_key -> - Context.t -> + Tezos_crypto.Block_hash.t -> packed_contents_list -> packed_operation -val endorsement : - ?delegate:public_key_hash * Slot.t list -> +(** Create an unpacked endorsement that is expected for given [Block.t]. + + 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]. + + Finally, the predecessor branch, [pred_branch] can be specified.*) +val raw_endorsement : + ?delegate:public_key_hash -> ?slot:Slot.t -> ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - endorsed_block:Block.t -> - Context.t -> - ?signing_context:Context.t -> - unit -> + ?pred_branch:Tezos_crypto.Block_hash.t -> + Block.t -> Kind.endorsement Operation.t tzresult Lwt.t -val preendorsement : - ?delegate:public_key_hash * Slot.t list -> +(** Create an unpacked preendorsement that is expected for a given + [Block.t]. + + Optional parameters are the same than {!raw_endorsement}. *) +val raw_preendorsement : + ?delegate:public_key_hash -> ?slot:Slot.t -> ?level:Raw_level.t -> ?round:Round.t -> ?block_payload_hash:Block_payload_hash.t -> - endorsed_block:Block.t -> - Context.t -> - ?signing_context:Context.t -> - unit -> + ?pred_branch:Tezos_crypto.Block_hash.t -> + Block.t -> Kind.preendorsement Operation.t tzresult Lwt.t -val miss_signed_endorsement : +(** Create a packed endorsement that is expected for a given + [Block.t] by packing the result of {!raw_endorsement}. *) +val endorsement : + ?delegate:public_key_hash -> + ?slot:Slot.t -> ?level:Raw_level.t -> - endorsed_block:Block.t -> - Context.t -> - Kind.endorsement Operation.t tzresult Lwt.t + ?round:Round.t -> + ?block_payload_hash:Block_payload_hash.t -> + ?pred_branch:Tezos_crypto.Block_hash.t -> + Block.t -> + Operation.packed tzresult Lwt.t + +(** Create a packed preendorsement that is expected for a given + [Block.t] by packing the result of {!raw_preendorsement}. *) +val preendorsement : + ?delegate:public_key_hash -> + ?slot:Slot.t -> + ?level:Raw_level.t -> + ?round:Round.t -> + ?block_payload_hash:Block_payload_hash.t -> + ?pred_branch:Tezos_crypto.Block_hash.t -> + Block.t -> + Operation.packed tzresult Lwt.t type gas_limit = | Max (** Max corresponds to the [max_gas_limit_per_operation] constant. *) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml index 9b994f926adf7f4967bd4f73b04a2b75f61f5aa9..8d332485f39f80113716c42007f8a588dea7eefe 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml @@ -205,9 +205,7 @@ let test_rewards_block_and_payload_producer () = total endorsing power). *) let endorsers = List.take_n 5 endorsers in List.map_ep - (fun endorser -> - Op.endorsement ~delegate:endorser ~endorsed_block:b1 (B genesis) () - >|=? Operation.pack) + (fun (endorser, _slots) -> Op.endorsement ~delegate:endorser b1) endorsers >>=? fun endos -> let endorsing_power = @@ -251,9 +249,7 @@ let test_rewards_block_and_payload_producer () = endorsers >>=? fun preendorsers -> List.map_ep - (fun endorser -> - Op.preendorsement ~delegate:endorser ~endorsed_block:b2 (B b1) () - >|=? Operation.pack) + (fun (endorser, _slots) -> Op.preendorsement ~delegate:endorser b2) preendorsers >>=? fun preendos -> Context.get_baker (B b1) ~round:Round.zero >>=? fun baker_b2 -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_consensus_key.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_consensus_key.ml index ceadc2560bfa46e69282ddd88f3ed6e0aaea9928..0401f4f2161c28477470df3490f8f0591d743936 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_consensus_key.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_consensus_key.ml @@ -188,21 +188,15 @@ let test_endorsement_with_consensus_key () = update_consensus_key blk' delegate consensus_pk >>=? fun b_pre -> Block.bake b_pre >>=? fun b -> let slot = Slot.of_int_do_not_use_except_for_parameters 0 in - Op.endorsement ~delegate:(account1_pkh, [slot]) ~endorsed_block:b (B b_pre) () - >>=? fun endorsement -> - Block.bake ~operation:(Operation.pack endorsement) b >>= fun res -> + Op.endorsement ~delegate:account1_pkh ~slot b >>=? fun endorsement -> + Block.bake ~operation:endorsement b >>= fun res -> Assert.proto_error ~loc:__LOC__ res (function | Operation.Invalid_signature -> true | _ -> false) >>=? fun () -> - Op.endorsement - ~delegate:(consensus_pkh, [slot]) - ~endorsed_block:b - (B b_pre) - () - >>=? fun endorsement -> - Block.bake ~operation:(Operation.pack endorsement) b - >>=? fun (_good_block : Block.t) -> return_unit + Op.endorsement ~delegate:consensus_pkh ~slot b >>=? fun endorsement -> + Block.bake ~operation:endorsement b >>=? fun (_good_block : Block.t) -> + return_unit let tests = Tztest. diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml index ec65b7f9073198dac76babb06f5d11f83af5fd91..5b5ce979ae4da4acdb1c03e0020c0a039d63febf 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml @@ -120,13 +120,11 @@ let test_valid_double_baking_followed_by_double_endorsing () = Context.get_first_different_endorsers (B blk_a) >>=? fun (e1, e2) -> let delegate = if Tezos_crypto.Signature.Public_key_hash.( = ) e1.delegate baker1 then - (e1.delegate, e1.slots) - else (e2.delegate, e2.slots) + e1.delegate + else e2.delegate in - Op.endorsement ~delegate ~endorsed_block:blk_a (B b) () - >>=? fun endorsement_a -> - Op.endorsement ~delegate ~endorsed_block:blk_b (B b) () - >>=? fun endorsement_b -> + Op.raw_endorsement ~delegate blk_a >>=? fun endorsement_a -> + Op.raw_endorsement ~delegate blk_b >>=? fun endorsement_b -> let operation = double_endorsement (B genesis) endorsement_a endorsement_b in Block.bake ~policy:(By_account baker1) ~operation blk_with_db_evidence >>=? fun blk_final -> @@ -169,13 +167,11 @@ let test_valid_double_endorsing_followed_by_double_baking () = Context.get_first_different_endorsers (B blk_a) >>=? fun (e1, e2) -> let delegate = if Tezos_crypto.Signature.Public_key_hash.( = ) e1.delegate baker1 then - (e1.delegate, e1.slots) - else (e2.delegate, e2.slots) + e1.delegate + else e2.delegate in - Op.endorsement ~delegate ~endorsed_block:blk_a (B blk_1) () - >>=? fun endorsement_a -> - Op.endorsement ~delegate ~endorsed_block:blk_b (B blk_2) () - >>=? fun endorsement_b -> + Op.raw_endorsement ~delegate blk_a >>=? fun endorsement_a -> + Op.raw_endorsement ~delegate blk_b >>=? fun endorsement_b -> let operation = double_endorsement (B genesis) endorsement_a endorsement_b in Block.bake ~policy:(By_account baker1) ~operation blk_a >>=? fun blk_with_de_evidence -> @@ -233,13 +229,8 @@ let test_payload_producer_gets_evidence_rewards () = endorsers >>=? fun preendorsers -> List.map_ep - (fun endorser -> - Op.preendorsement - ~delegate:endorser - ~endorsed_block:b_with_evidence - (B b1) - () - >|=? Operation.pack) + (fun (endorser, _slots) -> + Op.preendorsement ~delegate:endorser b_with_evidence) preendorsers >>=? fun preendos -> Block.bake 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 2cf9adeb10e0d5f686ff3271aff5555addaf8b9b..38d190702a4311ae1672deadea66c0381678fa4f 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 @@ -83,8 +83,8 @@ let test_valid_double_endorsement_evidence () = Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> Context.get_endorser (B blk_a) >>=? fun (delegate, _) -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> let operation = double_endorsement (B genesis) endorsement_a endorsement_b in Context.get_bakers (B blk_a) >>=? fun bakers -> let baker = Context.get_first_different_baker delegate bakers in @@ -134,8 +134,8 @@ let test_two_double_endorsement_evidences_leadsto_no_bake () = Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> Context.get_endorser (B blk_a) >>=? fun (delegate, _) -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> let operation = double_endorsement (B genesis) endorsement_a endorsement_b in Context.get_bakers (B blk_a) >>=? fun bakers -> let baker = Context.get_first_different_baker delegate bakers in @@ -146,8 +146,8 @@ let test_two_double_endorsement_evidences_leadsto_no_bake () = block_fork blk_with_evidence1 >>=? fun (blk_30, blk_40) -> Block.bake blk_30 >>=? fun blk_3 -> Block.bake blk_40 >>=? fun blk_4 -> - Op.endorsement ~endorsed_block:blk_3 (B blk_30) () >>=? fun endorsement_3 -> - Op.endorsement ~endorsed_block:blk_4 (B blk_40) () >>=? fun endorsement_4 -> + Op.raw_endorsement blk_3 >>=? fun endorsement_3 -> + Op.raw_endorsement blk_4 >>=? fun endorsement_4 -> let operation = double_endorsement (B blk_with_evidence1) endorsement_3 endorsement_4 in @@ -170,7 +170,7 @@ let test_two_double_endorsement_evidences_leadsto_no_bake () = let test_invalid_double_endorsement () = Context.init_n ~consensus_threshold:0 10 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun b -> - Op.endorsement ~endorsed_block:b (B genesis) () >>=? fun endorsement -> + Op.raw_endorsement b >>=? fun endorsement -> Block.bake ~operation:(Operation.pack endorsement) b >>=? fun b -> Op.double_endorsement (B b) endorsement endorsement |> fun operation -> Block.bake ~operation b >>= fun res -> @@ -188,8 +188,8 @@ let test_invalid_double_endorsement_variant () = block_fork b >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> double_endorsement (B genesis) ~correct_order:false @@ -210,8 +210,8 @@ let test_too_early_double_endorsement_evidence () = block_fork b >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> double_endorsement (B genesis) endorsement_a endorsement_b |> fun operation -> Block.bake ~operation genesis >>= fun res -> Assert.proto_error ~loc:__LOC__ res (function @@ -230,8 +230,8 @@ let test_too_late_double_endorsement_evidence () = block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> Block.bake_n ((max_slashing_period * Int32.to_int blocks_per_cycle) + 1) blk_a >>=? fun blk -> double_endorsement (B blk) endorsement_a endorsement_b |> fun operation -> @@ -250,29 +250,19 @@ let test_different_delegates () = block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> - Context.get_endorser (B blk_a) >>=? fun (endorser_a, a_slots) -> + Context.get_endorser (B blk_a) >>=? fun (endorser_a, _a_slots) -> Context.get_first_different_endorsers (B blk_b) >>=? fun (endorser_b1c, endorser_b2c) -> - let endorser_b, b_slots = + let endorser_b = if Tezos_crypto.Signature.Public_key_hash.( = ) endorser_a endorser_b1c.delegate - then (endorser_b2c.delegate, endorser_b2c.slots) - else (endorser_b1c.delegate, endorser_b1c.slots) + then endorser_b2c.delegate + else endorser_b1c.delegate in - Op.endorsement - ~delegate:(endorser_a, a_slots) - ~endorsed_block:blk_a - (B blk_1) - () - >>=? fun e_a -> - Op.endorsement - ~delegate:(endorser_b, b_slots) - ~endorsed_block:blk_b - (B blk_2) - () - >>=? fun e_b -> + Op.raw_endorsement ~delegate:endorser_a blk_a >>=? fun e_a -> + Op.raw_endorsement ~delegate:endorser_b blk_b >>=? fun e_b -> Block.bake ~operation:(Operation.pack e_b) blk_b >>=? fun (_ : Block.t) -> double_endorsement (B blk_b) e_a e_b |> fun operation -> Block.bake ~operation blk_b >>= fun res -> @@ -289,26 +279,16 @@ let test_wrong_delegate () = block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> - Context.get_endorser (B blk_a) >>=? fun (endorser_a, a_slots) -> - Op.endorsement - ~delegate:(endorser_a, a_slots) - ~endorsed_block:blk_a - (B blk_1) - () - >>=? fun endorsement_a -> - Context.get_endorser_n (B blk_b) 0 >>=? fun (endorser0, slots0) -> - Context.get_endorser_n (B blk_b) 1 >>=? fun (endorser1, slots1) -> - let endorser_b, b_slots = + Context.get_endorser (B blk_a) >>=? fun (endorser_a, _a_slots) -> + Op.raw_endorsement ~delegate:endorser_a blk_a >>=? fun endorsement_a -> + Context.get_endorser_n (B blk_b) 0 >>=? fun (endorser0, _slots0) -> + Context.get_endorser_n (B blk_b) 1 >>=? fun (endorser1, _slots1) -> + let endorser_b = if Tezos_crypto.Signature.Public_key_hash.equal endorser_a endorser0 then - (endorser1, slots1) - else (endorser0, slots0) + endorser1 + else endorser0 in - Op.endorsement - ~delegate:(endorser_b, b_slots) - ~endorsed_block:blk_b - (B blk_2) - () - >>=? fun endorsement_b -> + Op.raw_endorsement ~delegate:endorser_b blk_b >>=? fun endorsement_b -> double_endorsement (B blk_b) endorsement_a endorsement_b |> fun operation -> Block.bake ~operation blk_b >>= fun res -> Assert.proto_error ~loc:__LOC__ res (function @@ -340,19 +320,15 @@ let test_freeze_more_with_low_balance = Block.bake ~policy:(Block.By_account account1) blk_d1 >>=? fun blk_a -> Block.bake ~policy:(Block.By_account account1) blk_d2 >>=? fun blk_b -> get_endorsing_slots_for_account (B blk_a) account1 >>=? fun slots_a -> - Op.endorsement - ~delegate:(account1, slots_a) - ~endorsed_block:blk_a - (B blk_d1) - () - >>=? fun end_a -> + let slot = + match List.hd slots_a with None -> assert false | Some s -> s + in + Op.raw_endorsement ~delegate:account1 ~slot blk_a >>=? fun end_a -> get_endorsing_slots_for_account (B blk_b) account1 >>=? fun slots_b -> - Op.endorsement - ~delegate:(account1, slots_b) - ~endorsed_block:blk_b - (B blk_d2) - () - >>=? fun end_b -> + let slot = + match List.hd slots_b with None -> assert false | Some s -> s + in + Op.raw_endorsement ~delegate:account1 ~slot blk_b >>=? fun end_b -> let denunciation = double_endorsement (B b2) end_a end_b in Block.bake ~policy:(Excluding [account1]) b2 ~operations:[denunciation] in @@ -469,8 +445,8 @@ let test_two_double_endorsement_evidences_leads_to_duplicate_denunciation () = Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> Context.get_endorser (B blk_a) >>=? fun (delegate, _) -> - Op.endorsement ~endorsed_block:blk_a (B blk_1) () >>=? fun endorsement_a -> - Op.endorsement ~endorsed_block:blk_b (B blk_2) () >>=? fun endorsement_b -> + Op.raw_endorsement blk_a >>=? fun endorsement_a -> + Op.raw_endorsement blk_b >>=? fun endorsement_b -> let operation = double_endorsement (B genesis) endorsement_a endorsement_b in let operation2 = double_endorsement (B genesis) endorsement_b endorsement_a in Context.get_bakers (B blk_a) >>=? fun bakers -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml index bf01ef688b7be053210b2186db43d0d3429068cf..199f1d679b5a8bf78606dcd435824d2dd5841a0d 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml @@ -77,11 +77,11 @@ end = struct >>=? fun (genesis, _contracts) -> bake genesis >>=? fun b1 -> bake ~policy:(By_round 0) b1 >>=? fun b2_A -> - Op.endorsement ~endorsed_block:b1 (B genesis) () >>=? fun e -> - let operations = if include_endorsement then [Operation.pack e] else [] in + Op.endorsement b1 >>=? fun e -> + let operations = if include_endorsement then [e] else [] in bake ~policy:(By_round block_round) ~operations b1 >>=? fun b2_B -> - Op.preendorsement ~endorsed_block:b2_A (B b1) () >>=? fun op1 -> - Op.preendorsement ~endorsed_block:b2_B (B b1) () >>=? fun op2 -> + Op.raw_preendorsement b2_A >>=? fun op1 -> + Op.raw_preendorsement b2_B >>=? fun op2 -> let op = mk_evidence (B genesis) op1 op2 in bake b1 ~operations:[op] >>= fun res -> invalid_denunciation loc res @@ -119,7 +119,7 @@ end = struct let unexpected_success loc _ _ _ _ _ = Alcotest.fail (loc ^ ": Test should not succeed") - let expected_success _loc baker pred bbad (d1, _) (d2, _) = + let expected_success _loc baker pred bbad d1 d2 = (* same preendorsers in case denunciation succeeds*) Assert.equal_pkh ~loc:__LOC__ d1 d2 >>=? fun () -> Context.get_constants (B pred) @@ -200,12 +200,10 @@ end = struct Op.transaction (B genesis) addr addr Tez.one_mutez >>=? fun trans -> bake ~policy:(By_round 0) blk >>=? fun head_A -> bake ~policy:(By_round 0) blk ~operations:[trans] >>=? fun head_B -> - pick_endorsers (B head_A) >>=? fun (d1, d2) -> + pick_endorsers (B head_A) >>=? fun ((d1, _slots1), (d2, _slots2)) -> (* default: d1 = d2 *) - Op.preendorsement ~delegate:d1 ~endorsed_block:head_A (B blk) () - >>=? fun op1 -> - Op.preendorsement ~delegate:d2 ~endorsed_block:head_B (B blk) () - >>=? fun op2 -> + Op.raw_preendorsement ~delegate:d1 head_A >>=? fun op1 -> + Op.raw_preendorsement ~delegate:d2 head_B >>=? fun op2 -> let op1, op2 = order_preendorsements ~correct_order:true op1 op2 in (* bake `nb_blocks_before_denunciation` before double preend. denunciation *) bake_n nb_blocks_before_denunciation blk >>=? fun blk -> @@ -317,10 +315,8 @@ end = struct Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> Context.get_endorser (B blk_a) >>=? fun (delegate, _) -> - Op.preendorsement ~endorsed_block:blk_a (B blk_1) () - >>=? fun preendorsement_a -> - Op.preendorsement ~endorsed_block:blk_b (B blk_2) () - >>=? fun preendorsement_b -> + Op.raw_preendorsement blk_a >>=? fun preendorsement_a -> + Op.raw_preendorsement blk_b >>=? fun preendorsement_b -> let operation = double_preendorsement (B genesis) preendorsement_a preendorsement_b in 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 478b7301ebb761672564f722f1c476d5a982316e..3646a977e447f6ba2bccdd58168b6c5d8c11df84 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 @@ -44,9 +44,9 @@ let init_genesis ?policy () = (** 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 ~endorsed_block:b (B genesis) () >>=? fun op -> - Block.bake ~operations:[Operation.pack op] b >>=? fun b' -> return (b', b) + init_genesis () >>=? fun (_genesis, b) -> + Op.endorsement b >>=? fun operation -> + Block.bake ~operation b >>=? fun b' -> return (b', b) (****************************************************************) (* Tests *) @@ -68,11 +68,10 @@ let test_negative_slot () = Lwt.catch (fun () -> Op.endorsement - ~delegate:(delegate, [Slot.of_int_do_not_use_except_for_parameters (-1)]) - ~endorsed_block:b - (B genesis) - () - >>=? fun (_ : _ operation) -> + ~delegate + ~slot:(Slot.of_int_do_not_use_except_for_parameters (-1)) + b + >>=? fun (_ : packed_operation) -> failwith "negative slot should not be accepted by the binary format") (function | Data_encoding.Binary.Write_error _ -> return_unit | e -> Lwt.fail e) @@ -109,14 +108,12 @@ let test_non_normalized_slot () = (WithExceptions.Option.get ~loc:__LOC__ @@ List.hd slots) (WithExceptions.Option.get ~loc:__LOC__ @@ Slot.Set.min_elt set_slots) >>=? fun () -> - Op.endorsement - ~delegate:(delegate, List.rev slots) - ~endorsed_block:b - (B genesis) - () - >>=? fun op -> + 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 ~operations:[Operation.pack op] b >>= fun res -> + Block.bake ~policy ~operation b >>= fun res -> Assert.proto_error ~loc:__LOC__ res (function | Validate_errors.Consensus.Wrong_slot_used_for_consensus_operation {kind} @@ -127,10 +124,8 @@ let test_non_normalized_slot () = (** Wrong endorsement predecessor : apply an endorsement with an incorrect block predecessor. *) let test_wrong_endorsement_predecessor () = - init_genesis () >>=? fun (genesis, b) -> - Op.endorsement ~endorsed_block:b (B genesis) ~signing_context:(B b) () - >>=? fun operation -> - let operation = Operation.pack operation in + init_genesis () >>=? fun (_genesis, b) -> + Op.endorsement ~pred_branch:(Context.branch (B b)) b >>=? fun operation -> Block.bake ~operation b >>= fun res -> Assert.proto_error ~loc:__LOC__ res (function | Validate_errors.Consensus.Wrong_consensus_operation_branch {kind; _} @@ -143,9 +138,8 @@ let test_wrong_endorsement_predecessor () = let test_invalid_endorsement_level () = init_genesis () >>=? fun (genesis, b) -> Context.get_level (B genesis) >>?= fun genesis_level -> - Op.endorsement ~level:genesis_level ~endorsed_block:b (B genesis) () - >>=? fun op -> - Block.bake ~operations:[Operation.pack op] b >>= fun res -> + 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 -> @@ -154,13 +148,11 @@ let test_invalid_endorsement_level () = (** Duplicate endorsement : apply an endorsement that has already been applied. *) let test_duplicate_endorsement () = - init_genesis () >>=? fun (genesis, b) -> + init_genesis () >>=? fun (_genesis, b) -> Incremental.begin_construction b >>=? fun inc -> - Op.endorsement ~endorsed_block:b (B genesis) () >>=? fun operation -> - let operation = Operation.pack operation in + Op.endorsement b >>=? fun operation -> Incremental.add_operation inc operation >>=? fun inc -> - Op.endorsement ~endorsed_block:b (B genesis) () >>=? fun operation -> - let operation = Operation.pack operation in + 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; _} @@ -170,7 +162,7 @@ let test_duplicate_endorsement () = (** 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) -> + 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 @@ -183,13 +175,12 @@ let test_consensus_operation_endorsement_for_future_level () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) ~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) -> + 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 @@ -202,14 +193,13 @@ let test_consensus_operation_endorsement_for_predecessor_level () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) ~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 -> + 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 @@ -222,13 +212,12 @@ let test_consensus_operation_endorsement_for_old_level () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B next_block) ~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) -> + init_genesis () >>=? fun (_genesis, pred) -> Environment.wrap_tzresult (Round.of_int 21) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ @@ -240,13 +229,12 @@ let test_consensus_operation_endorsement_for_future_round () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) ~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) -> + init_genesis ~policy:(By_round 10) () >>=? fun (_genesis, pred) -> Environment.wrap_tzresult (Round.of_int 0) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ @@ -258,13 +246,12 @@ let test_consensus_operation_endorsement_for_old_round () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) ~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) -> + init_genesis () >>=? fun (_genesis, pred) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ ~is_preendorsement:false @@ -276,13 +263,12 @@ let test_consensus_operation_endorsement_on_competing_proposal () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** Wrong round : apply an endorsement with an incorrect round *) let test_wrong_round () = - init_genesis () >>=? fun (genesis, b) -> + init_genesis () >>=? fun (_genesis, b) -> Environment.wrap_tzresult (Round.of_int 2) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ @@ -294,13 +280,12 @@ let test_wrong_round () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) () (** Wrong level : apply an endorsement with an incorrect level *) let test_wrong_level () = - init_genesis () >>=? fun (genesis, b) -> - let context = Context.B genesis in + 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 @@ -313,12 +298,11 @@ let test_wrong_level () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context () (** Wrong payload hash : apply an endorsement with an incorrect payload hash *) let test_wrong_payload_hash () = - init_genesis () >>=? fun (genesis, b) -> + init_genesis () >>=? fun (_genesis, b) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ ~is_preendorsement:false @@ -330,11 +314,10 @@ let test_wrong_payload_hash () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) () let test_wrong_slot_used () = - init_genesis () >>=? fun (genesis, b) -> + init_genesis () >>=? fun (_genesis, b) -> Context.get_endorser (B b) >>=? fun (_, slots) -> (match slots with | _x :: y :: _ -> return y @@ -351,7 +334,6 @@ let test_wrong_slot_used () = when kind = Validate_errors.Consensus.Endorsement -> true | _ -> false) - ~context:(Context.B genesis) () (** Check that: @@ -376,13 +358,8 @@ let test_endorsement_threshold ~sufficient_threshold () = (sufficient_threshold && counter < consensus_threshold) || ((not sufficient_threshold) && new_counter < consensus_threshold) then - Op.endorsement - ~round - ~delegate:(delegate, slots) - ~endorsed_block:b - (B genesis) - () - >>=? fun endo -> return (new_counter, Operation.pack endo :: endos) + Op.endorsement ~round ~delegate b >>=? fun endo -> + return (new_counter, endo :: endos) else return (counter, endos)) (0, []) endorsers_list @@ -417,11 +394,9 @@ let test_preendorsement_endorsement_same_level () = Block.bake genesis >>=? fun b1 -> Incremental.begin_construction ~mempool_mode:true ~policy:(By_round 2) b1 >>=? fun i -> - Op.endorsement ~endorsed_block:b1 (B genesis) () >>=? fun op_endo -> - let op_endo = Alpha_context.Operation.pack op_endo in + Op.endorsement b1 >>=? fun op_endo -> Incremental.add_operation i op_endo >>=? fun (_i : Incremental.t) -> - Op.preendorsement ~endorsed_block:b1 (B genesis) () >>=? fun op_preendo -> - let op_preendo = Alpha_context.Operation.pack op_preendo in + Op.preendorsement b1 >>=? fun op_preendo -> Incremental.add_operation i op_preendo >>=? fun (_i : Incremental.t) -> return_unit @@ -437,8 +412,7 @@ let test_wrong_endorsement_slot_in_mempool_mode () = return (Some non_canonical_slot) | _ -> assert false) >>=? fun slot -> - Op.endorsement ~endorsed_block:b1 (B genesis) ?slot () >>=? fun endo -> - let endo = Operation.pack endo in + 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 @@ -470,11 +444,9 @@ let test_endorsement_grandparent () = Block.bake b_gp >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> (* Endorsement on grandparent *) - Op.endorsement ~endorsed_block:b_gp (B genesis) () >>=? fun op1 -> + Op.endorsement b_gp >>=? fun op1 -> (* Endorsement on parent *) - Op.endorsement ~endorsed_block:b (B b_gp) () >>=? fun op2 -> - let op1 = Alpha_context.Operation.pack op1 in - let op2 = Alpha_context.Operation.pack op2 in + 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 @@ -486,11 +458,9 @@ let test_double_endorsement_grandparent () = Block.bake b_gp >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> (* Endorsement on grandparent *) - Op.endorsement ~endorsed_block:b_gp (B genesis) () >>=? fun op1 -> + Op.endorsement b_gp >>=? fun op1 -> (* Endorsement on parent *) - Op.endorsement ~endorsed_block:b (B b_gp) () >>=? fun op2 -> - let op1 = Alpha_context.Operation.pack op1 in - let op2 = Alpha_context.Operation.pack op2 in + 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 -> @@ -510,12 +480,10 @@ let test_endorsement_grandparent_same_slot () = 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 ~endorsed_block:b ~delegate (B b_gp) () >>=? fun op2 -> + Op.endorsement ~delegate b >>=? fun op2 -> (* Endorsement on grandparent *) Consensus_helpers.delegate_of_slot slot (B b_gp) >>=? fun delegate -> - Op.endorsement ~endorsed_block:b_gp ~delegate (B genesis) () >>=? fun op1 -> - let op1 = Alpha_context.Operation.pack op1 in - let op2 = Alpha_context.Operation.pack op2 in + 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 @@ -525,8 +493,8 @@ 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 ~endorsed_block:b_gp (B genesis) () >>=? fun op -> - Block.bake ~operations:[Operation.pack op] b >>= fun res -> + 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 -> @@ -540,8 +508,7 @@ let test_endorsement_grandparent_full_construction () = Block.bake b_gp >>=? fun b -> Incremental.begin_construction b >>=? fun i -> (* Endorsement on grandparent *) - Op.endorsement ~endorsed_block:b_gp (B genesis) () >>=? fun op1 -> - let op1 = Alpha_context.Operation.pack op1 in + 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; _} diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml index 93c4b456c3028b9626fcee75e7e77b8140d7d646..edfa1f50b6356cd58b7d7248991c5bb4eed54c5a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml @@ -36,7 +36,7 @@ open Protocol open Alpha_context (** [baker] bakes and [endorser] endorses *) -let bake_and_endorse_once (b_pred, b_cur) baker endorser = +let bake_and_endorse_once (_b_pred, b_cur) baker endorser = let open Context in Context.get_endorsers (B b_cur) >>=? fun endorsers_list -> List.find_map @@ -48,11 +48,9 @@ let bake_and_endorse_once (b_pred, b_cur) baker endorser = endorsers_list |> function | None -> assert false - | Some delegate -> + | Some (delegate, _slots) -> Block.get_round b_cur >>?= fun round -> - Op.endorsement ~round ~delegate ~endorsed_block:b_cur (B b_pred) () - >>=? fun endorsement -> - let endorsement = Operation.pack endorsement in + Op.endorsement ~round ~delegate b_cur >>=? fun endorsement -> Block.bake ~policy:(By_account baker) ~operation:endorsement b_cur (** We test that: 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 eada47989ad94d39ae3b1f9b3b4575c0260de2e2..54e62f8b256acf84fc224bea3fddfa1e1362264a 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 @@ -48,7 +48,7 @@ let init_genesis ?policy () = (** Consensus operation for future level : apply a preendorsement with a level in the future *) let test_consensus_operation_preendorsement_for_future_level () = - init_genesis () >>=? fun (genesis, pred) -> + 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 @@ -61,13 +61,12 @@ let test_consensus_operation_preendorsement_for_future_level () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** Consensus operation for old level : apply a preendorsement with a level in the past *) let test_consensus_operation_preendorsement_for_old_level () = - init_genesis () >>=? fun (genesis, pred) -> + 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 @@ -80,13 +79,12 @@ let test_consensus_operation_preendorsement_for_old_level () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** Consensus operation for future round : apply a preendorsement with a round in the future *) let test_consensus_operation_preendorsement_for_future_round () = - init_genesis () >>=? fun (genesis, pred) -> + init_genesis () >>=? fun (_genesis, pred) -> Environment.wrap_tzresult (Round.of_int 21) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ @@ -98,13 +96,12 @@ let test_consensus_operation_preendorsement_for_future_round () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** Consensus operation for old round : apply a preendorsement with a round in the past *) let test_consensus_operation_preendorsement_for_old_round () = - init_genesis ~policy:(By_round 10) () >>=? fun (genesis, pred) -> + init_genesis ~policy:(By_round 10) () >>=? fun (_genesis, pred) -> Environment.wrap_tzresult (Round.of_int 0) >>?= fun round -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ @@ -116,13 +113,12 @@ let test_consensus_operation_preendorsement_for_old_round () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** 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) -> + init_genesis () >>=? fun (_genesis, pred) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ ~is_preendorsement:true @@ -134,13 +130,12 @@ let test_consensus_operation_preendorsement_on_competing_proposal () = when kind = Validate_errors.Consensus.Preendorsement -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, None) () (** Unexpected preendorsements in block : apply a preendorsement with an incorrect round *) let test_unexpected_preendorsements_in_blocks () = - init_genesis () >>=? fun (genesis, pred) -> + init_genesis () >>=? fun (_genesis, pred) -> Consensus_helpers.test_consensus_operation ~loc:__LOC__ ~is_preendorsement:true @@ -148,12 +143,11 @@ let test_unexpected_preendorsements_in_blocks () = ~error:(function | Validate_errors.Consensus.Unexpected_preendorsement_in_block -> true | _ -> false) - ~context:(Context.B genesis) () (** Round too high : apply a preendorsement with a too high round *) let test_too_high_round () = - init_genesis () >>=? fun (genesis, pred) -> + init_genesis () >>=? fun (_genesis, pred) -> let raw_level = Raw_level.of_int32 (Int32.of_int 2) in let level = match raw_level with Ok l -> l | Error _ -> assert false in Environment.wrap_tzresult (Round.of_int 1) >>?= fun round -> @@ -166,7 +160,6 @@ let test_too_high_round () = ~error:(function | Validate_errors.Consensus.Preendorsement_round_too_high _ -> true | _ -> false) - ~context:(Context.B genesis) ~construction_mode:(pred, Some pred.header.protocol_data) () @@ -175,11 +168,9 @@ let test_duplicate_preendorsement () = init_genesis () >>=? fun (genesis, _) -> Block.bake genesis >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun inc -> - Op.preendorsement ~endorsed_block:b (B genesis) () >>=? fun operation -> - let operation = Operation.pack operation in + Op.preendorsement b >>=? fun operation -> Incremental.add_operation inc operation >>=? fun inc -> - Op.preendorsement ~endorsed_block:b (B genesis) () >>=? fun operation -> - let operation = Operation.pack operation in + Op.preendorsement b >>=? fun operation -> Incremental.add_operation inc operation >>= fun res -> Assert.proto_error_with_info ~loc:__LOC__ 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 03ecf37cbafa399d97b1ea3023ffbe691b919e70..1b70fc2779f593cd2908cc976e89d6f4ea84af46 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 @@ -59,21 +59,21 @@ end = struct ?(post_process = Ok (fun _ -> return_unit)) ~loc () = Context.init_n ~consensus_threshold:1 5 () >>=? fun (genesis, _contracts) -> bake genesis >>=? fun b1 -> - Op.endorsement ~endorsed_block:b1 (B genesis) () >>=? fun endo -> - let endo = Operation.pack endo in + Op.endorsement b1 >>=? fun endo -> bake b1 ~operations:[endo] >>=? fun b2 -> - let ctxt = Context.B (preend_branch genesis b1 b2) in + 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 - ctxt - () + endorsed_block >>=? fun p -> - let operations = endo :: (mk_ops @@ Operation.pack p) in + let operations = endo :: (mk_ops @@ p) in bake ~payload_round ~locked_round @@ -182,8 +182,7 @@ end = struct ~get_delegate_and_slot:(fun _predpred _pred curr -> let module V = Plugin.RPC.Validators in Context.get_endorsers (B curr) >>=? function - | {V.delegate; slots = s :: _ as slots; _} :: _ -> - return (Some (delegate, slots), Some s) + | {V.delegate; slots = s :: _; _} :: _ -> return (Some delegate, Some s) | _ -> assert false (* there is at least one endorser with a slot *)) ~loc:__LOC__ @@ -195,9 +194,8 @@ end = struct ~get_delegate_and_slot:(fun _predpred _pred curr -> let module V = Plugin.RPC.Validators in Context.get_endorsers (B curr) >>=? function - | {V.delegate; V.slots = _ :: non_canonical_slot :: _ as slots; _} :: _ - -> - return (Some (delegate, slots), Some non_canonical_slot) + | {V.delegate; V.slots = _ :: non_canonical_slot :: _; _} :: _ -> + return (Some delegate, Some non_canonical_slot) | _ -> assert false (* there is at least one endorser with a slot *)) ~loc:__LOC__ @@ -217,9 +215,9 @@ end = struct ~get_delegate_and_slot:(fun _predpred _pred curr -> let module V = Plugin.RPC.Validators in Context.get_endorsers (B curr) >>=? function - | {V.delegate; _} :: {V.slots = s :: _ as slots; _} :: _ -> + | {V.delegate; _} :: {V.slots = s :: _; _} :: _ -> (* the canonical slot s is not owned by the delegate "delegate" !*) - return (Some (delegate, slots), Some s) + return (Some delegate, Some s) | _ -> assert false (* there is at least one endorser with a slot *)) ~loc:__LOC__ diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml index e8f358cc67d1598690eb5ab48bd57e8e6ad66823..d737eeb9fc789b2d80aa1244934a9a5a8ca435ef 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml @@ -302,17 +302,12 @@ let test_unrevealed () = let blocks_per_commitment = Int32.to_int csts.parametric.blocks_per_commitment in - let bake_and_endorse_block ?policy (pred_b, b) = + let bake_and_endorse_block ?policy (_pred_b, b) = let* slots = Context.get_endorsers (B b) in let* endorsements = List.map_es - (fun {Plugin.RPC.Validators.delegate; slots; _} -> - Op.endorsement - ~delegate:(delegate, slots) - ~endorsed_block:b - (B pred_b) - () - >>=? fun op -> Operation.pack op |> return) + (fun {Plugin.RPC.Validators.consensus_key; _} -> + Op.endorsement ~delegate:consensus_key b) slots in Block.bake ?policy ~operations:endorsements b diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml index bb5d8af9c57ff860997aa905654ed5ebb0288348..8531dfe0b5d62fb17b6953261f957458b3ca80fb 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml @@ -251,7 +251,7 @@ let test_unparsable_script () = Data_encoding.Binary.of_string_exn Operation.contents_list_encoding encoded_op - |> Op.sign account.sk (B b) + |> Op.sign account.sk (Context.branch (B b)) in (* Ensure that the application fails with [Ill_typed_contract]. *) let* i = Incremental.begin_construction b in @@ -292,7 +292,7 @@ let test_unparsable_script () = Data_encoding.Binary.of_string_exn Operation.contents_list_encoding encoded_bad_op - |> Op.sign account.sk (B b) + |> Op.sign account.sk (Context.branch (B b)) in (* Ensure that the operation is valid but the application fails with [Lazy_script_decode]. *) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml index c1ab45367f30c142cc3809a74f2290ba0ca3c2a2..fd437a82ae80f6c0d2e209345afeab4d8083d5aa 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml @@ -673,83 +673,37 @@ let transfer_to_itself_with_no_such_entrypoint () = Incremental.add_operation ~expect_apply_failure i transaction >>= fun _res -> return () -(** A module with a type that tracks a block's predecessor. *) -module State = struct - type t = {predecessor : Block.t option; current : Block.t} - - let init () = - let open Lwt_result_syntax in - let+ block, b1 = Context.init1 () in - ({predecessor = None; current = block}, b1) - - (** Applies an operation to a state and returns the resulting state. *) - let apply ~baker ~operation ~state = - let block = state.current in - let open Lwt_result_syntax in - let open Incremental in - let* inc = begin_construction ~policy:Block.(By_account baker) block in - let* inc = add_operation inc operation in - let* inc = - match state.predecessor with - | None -> return inc - | Some predecessor -> - (* Include all endorsements. *) - let* endorsers = Context.get_endorsers (B block) in - List.fold_left_es - (fun inc {Plugin.RPC.Validators.delegate; slots; _} -> - let* endorsement = - Op.endorsement - ~delegate:(delegate, slots) - ~endorsed_block:block - (B predecessor) - () - in - add_operation inc (Operation.pack endorsement)) - inc - endorsers - in - let+ next = finalize_block inc in - {predecessor = Some block; current = next} - - let current {current; _} = current - - (** Originates a contract with a [script] and an initial [credit] and +(** Originates a contract with a [script] and an initial [credit] and [storage]. *) - let contract_originate ~baker ~(state : t) ~script ~credit ~storage ~source = - let open Lwt_result_syntax in - let block = current state in - let code = Expr.from_string script in - let script = - Alpha_context.Script.{code = lazy_expr code; storage = lazy_expr storage} - in - let* op, dst = - Op.contract_origination_hash - (B block) - source - ~fee:Tez.zero - ~script - ~credit - in - let+ state = apply ~operation:op ~state ~baker in - (state, dst) - - (** Runs a transaction from a [source] to a [destination]. *) - let transfer ?force_reveal ?parameters ~baker ~state ~source ~destination - amount = - let open Lwt_result_syntax in - let block = current state in - let* operation = - Op.transaction - ?force_reveal - ?parameters - ~fee:Tez.zero - (B block) - source - destination - amount - in - apply ~operation ~state ~baker -end +let contract_originate ~baker ~block ~script ~credit ~storage ~source = + let open Lwt_result_syntax in + let code = Expr.from_string script in + let script = + Alpha_context.Script.{code = lazy_expr code; storage = lazy_expr storage} + in + let* op, dst = + Op.contract_origination_hash (B block) source ~fee:Tez.zero ~script ~credit + in + let+ state = + Block.bake ~policy:Block.(By_account baker) ~operations:[op] block + in + (state, dst) + +(** Runs a transaction from a [source] to a [destination]. *) +let transfer ?force_reveal ?parameters ~baker ~block ~source ~destination amount + = + let open Lwt_result_syntax in + let* operation = + Op.transaction + ?force_reveal + ?parameters + ~fee:Tez.zero + (B block) + source + destination + amount + in + Block.bake ~policy:Block.(By_account baker) ~operations:[operation] block (** The script of a contract that transfers its balance to the caller, and stores the parameter of the call. *) @@ -783,15 +737,15 @@ let script = balance. *) let test_storage_fees_and_internal_operation () = let open Lwt_result_syntax in - let* initial_state, b1 = State.init () in + let* initial_block, contract = Context.init1 ~consensus_threshold:0 () in let null_string = Expr.from_string "\"\"" in let caller = Account.new_account () in (* Initialize a caller account. *) - let* initial_state = - State.transfer - ~state:initial_state - ~baker:(Context.Contract.pkh b1) - ~source:b1 + let* initial_block = + transfer + ~block:initial_block + ~baker:(Context.Contract.pkh contract) + ~source:contract ~destination:(Contract.Implicit caller.pkh) Tez.one_mutez in @@ -799,36 +753,36 @@ let test_storage_fees_and_internal_operation () = initial storage, and an initial credit of [initial_amount]. And then, calls the originated contract from [caller] with a parameter that allocates additional storage. *) - let originate_and_call ~initial_state ~initial_amount = - let* state, contract_hash = - State.contract_originate - ~state:initial_state - ~baker:(Context.Contract.pkh b1) + let originate_and_call ~initial_block ~initial_amount = + let* block, contract_hash = + contract_originate + ~block:initial_block + ~baker:(Context.Contract.pkh contract) ~script - ~source:b1 + ~source:contract ~credit:initial_amount ~storage:null_string in let random_string = Expr.from_string "\"Abracadabra\"" in - State.transfer + transfer ~force_reveal:true ~parameters:(Alpha_context.Script.lazy_expr random_string) - ~state - ~baker:(Context.Contract.pkh b1) + ~block + ~baker:(Context.Contract.pkh contract) ~source:(Contract.Implicit caller.pkh) ~destination:(Contract.Originated contract_hash) Tez.zero in (* Ensure failure when the initial balance of the originated contract is not sufficient to pay storage fees. *) - let*! res = originate_and_call ~initial_state ~initial_amount:Tez.one_mutez in + let*! res = originate_and_call ~initial_block ~initial_amount:Tez.one_mutez in let* () = Assert.proto_error_with_info ~loc:__LOC__ res "Cannot pay storage fee" in (* Ensure success when the initial balance of the originated contract is sufficient to pay storage fees. *) - let+ (_ : State.t) = - originate_and_call ~initial_state ~initial_amount:Tez.one_cent + let+ (_ : Block.t) = + originate_and_call ~initial_block ~initial_amount:Tez.one_cent in () diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/generator_descriptors.ml b/src/proto_alpha/lib_protocol/test/integration/validate/generator_descriptors.ml index 42cd40c354ce994b658ba7f6972bf48e93ca66b3..170f0435c8da45468e937bacab956238f5ca8571 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/generator_descriptors.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/generator_descriptors.ml @@ -362,20 +362,8 @@ let dbl_endorsement_prelude state = | None -> return ([], state) | Some (b1, b2) -> let* delegate1, delegate2 = pick_two_endorsers (B b1) in - let* op1 = - Op.preendorsement - ~delegate:delegate1 - ~endorsed_block:b1 - (B state.block) - () - in - let* op2 = - Op.preendorsement - ~delegate:delegate1 - ~endorsed_block:b2 - (B state.block) - () - in + let* op1 = Op.raw_preendorsement ~delegate:delegate1 b1 in + let* op2 = Op.raw_preendorsement ~delegate:delegate1 b2 in let op1, op2 = let comp = Tezos_crypto.Operation_hash.compare @@ -388,12 +376,8 @@ let dbl_endorsement_prelude state = let slashable_preend = (op1, op2) :: state.dbl_endorsement.slashable_preend in - let* op3 = - Op.endorsement ~delegate:delegate2 ~endorsed_block:b1 (B state.block) () - in - let* op4 = - Op.endorsement ~delegate:delegate2 ~endorsed_block:b2 (B state.block) () - in + let* op3 = Op.raw_endorsement ~delegate:delegate2 b1 in + let* op4 = Op.raw_endorsement ~delegate:delegate2 b2 in let op3, op4 = let comp = Tezos_crypto.Operation_hash.compare @@ -607,18 +591,14 @@ let preendorsement_descriptor = let gen (delegate, ck_opt) = let* slots_opt = Context.get_endorser_slot (B state.block) delegate in let delegate = Option.value ~default:delegate ck_opt in - match (state.pred, slots_opt) with - | None, _ -> assert false - | Some _pred, None -> return_none - | Some pred, Some slots -> - let* op = - Op.preendorsement - ~delegate:(delegate, slots) - ~endorsed_block:state.block - (B pred) - () - in - return_some (Alpha_context.Operation.pack op) + match slots_opt with + | None -> return_none + | Some slots -> ( + match slots with + | [] -> return_none + | _ :: _ -> + let* op = Op.preendorsement ~delegate state.block in + return_some op) in List.filter_map_es gen state.delegates); } @@ -636,18 +616,14 @@ let endorsement_descriptor = let gen (delegate, ck_opt) = let* slots_opt = Context.get_endorser_slot (B state.block) delegate in let delegate = Option.value ~default:delegate ck_opt in - match (state.pred, slots_opt) with - | None, _ -> assert false - | Some _pred, None -> return_none - | Some pred, Some slots -> - let* op = - Op.endorsement - ~delegate:(delegate, slots) - ~endorsed_block:state.block - (B pred) - () - in - return_some (Alpha_context.Operation.pack op) + match slots_opt with + | None -> return_none + | Some slots -> ( + match slots with + | [] -> return_none + | _ :: _ -> + let* op = Op.endorsement ~delegate state.block in + return_some op) in List.filter_map_es gen state.delegates); } diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/validate_helpers.ml b/src/proto_alpha/lib_protocol/test/integration/validate/validate_helpers.ml index 48931342d2b9369c92a1af622673a73ad32c8cdc..ebf3c18cdd72517d6328a6c8bc73bafdb8484fd5 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/validate_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/validate_helpers.ml @@ -362,13 +362,13 @@ let secrets = let pick_two_endorsers ctxt = let module V = Plugin.RPC.Validators in Context.get_endorsers ctxt >>=? function - | a :: b :: _ -> return ((a.V.delegate, a.V.slots), (b.V.delegate, b.V.slots)) + | a :: b :: _ -> return (a.V.consensus_key, b.V.consensus_key) | _ -> assert false let pick_addr_endorser ctxt = let module V = Plugin.RPC.Validators in Context.get_endorsers ctxt >>=? function - | a :: _ -> return a.V.delegate + | a :: _ -> return a.V.consensus_key | _ -> assert false let init_params = @@ -379,7 +379,8 @@ let delegates_of_block block = let open Lwt_result_syntax in let+ validators = Context.get_endorsers (B block) in List.map - (fun Plugin.RPC.Validators.{delegate; slots; _} -> (delegate, slots)) + (fun Plugin.RPC.Validators.{consensus_key; slots; _} -> + (consensus_key, slots)) validators (** Sequential validation of an operation list. *)