diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index d049b8164..48993996e 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -910,6 +910,7 @@ module Operation : sig val hash: _ operation -> Operation_hash.t val hash_raw: raw -> Operation_hash.t + val hash_packed: packed_operation -> Operation_hash.t val acceptable_passes: packed_operation -> int list diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.ml b/src/proto_alpha/lib_protocol/src/operation_repr.ml index 35071e29d..74d30f165 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/src/operation_repr.ml @@ -675,6 +675,12 @@ let hash (o : _ operation) = protocol_data_encoding (Operation_data o.protocol_data) in Operation.hash { shell = o.shell ; proto } +let hash_packed (o : packed_operation) = + let proto = + Data_encoding.Binary.to_bytes_exn + protocol_data_encoding + o.protocol_data in + Operation.hash { shell = o.shell ; proto } type ('a, 'b) eq = Eq : ('a, 'a) eq diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.mli b/src/proto_alpha/lib_protocol/src/operation_repr.mli index a1546d954..544193692 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/src/operation_repr.mli @@ -156,6 +156,7 @@ val raw: _ operation -> raw val hash_raw: raw -> Operation_hash.t val hash: _ operation -> Operation_hash.t +val hash_packed: packed_operation -> Operation_hash.t val acceptable_passes: packed_operation -> int list diff --git a/src/proto_alpha/lib_protocol/test/double_endorsement.ml b/src/proto_alpha/lib_protocol/test/double_endorsement.ml index ae5534e36..3230e8111 100644 --- a/src/proto_alpha/lib_protocol/test/double_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/double_endorsement.ml @@ -55,7 +55,7 @@ let valid_double_endorsement_evidence () = Context.get_endorser (B blk_a) 0 >>=? fun delegate -> Op.endorsement ~delegate (B blk_a) [0] >>=? fun endorsement_a -> Op.endorsement ~delegate (B blk_b) [0] >>=? fun endorsement_b -> - Block.bake ~operations:[endorsement_a] blk_a >>=? fun blk_a -> + Block.bake ~operations:[Operation.pack endorsement_a] blk_a >>=? fun blk_a -> (* Block.bake ~operations:[endorsement_b] blk_b >>=? fun _ -> *) Op.double_endorsement (B blk_a) endorsement_a endorsement_b >>=? fun operation -> @@ -84,7 +84,7 @@ let invalid_double_endorsement () = Block.bake b >>=? fun b -> Op.endorsement (B b) [0] >>=? fun endorsement -> - Block.bake ~operation:endorsement b >>=? fun b -> + Block.bake ~operation:(Operation.pack endorsement) b >>=? fun b -> Op.double_endorsement (B b) endorsement endorsement >>=? fun operation -> Block.bake ~operation b >>= fun res -> diff --git a/src/proto_alpha/lib_protocol/test/endorsement.ml b/src/proto_alpha/lib_protocol/test/endorsement.ml index 42bfb2934..63ca33f9a 100644 --- a/src/proto_alpha/lib_protocol/test/endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/endorsement.ml @@ -68,7 +68,7 @@ let simple_endorsement () = (Contract.implicit_contract endorser) >>=? fun initial_balance -> Block.bake ~policy:(Excluding [endorser]) - ~operations:[op] + ~operations:[Operation.pack op] b >>=? fun b2 -> assert_endorser_balance_consistency ~loc:__LOC__ (B b2) ~nb_endorsement:1 endorser initial_balance @@ -87,7 +87,7 @@ let max_endorsement () = let delegate = endorser.delegate in Context.Contract.balance (B b) (Contract.implicit_contract delegate) >>=? fun balance -> Op.endorsement ~delegate (B b) endorser.slots >>=? fun op -> - return (delegate :: delegates, op :: ops, (List.length endorser.slots, balance) :: balances) + return (delegate :: delegates, Operation.pack op :: ops, (List.length endorser.slots, balance) :: balances) ) ([], [], []) endorsers >>=? fun (delegates, ops, previous_balances) -> @@ -116,6 +116,7 @@ let consistent_priority () = Context.Contract.balance (B b) (Contract.implicit_contract endorser) >>=? fun balance -> Op.endorsement ~delegate:endorser (B b) [slot] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~policy:( Excluding [ endorser ] ) ~operation b >>=? fun b -> assert_endorser_balance_consistency ~loc:__LOC__ ~priority:15 @@ -140,6 +141,7 @@ let consistent_priorities () = Context.Contract.balance (B b) (Contract.implicit_contract endorser) >>=? fun balance -> Op.endorsement ~delegate:endorser (B b) [slot] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~policy:( Excluding [ endorser ] ) ~operation b >>=? fun b -> assert_endorser_balance_consistency ~loc:__LOC__ ~priority @@ -156,6 +158,7 @@ let reward_retrieval () = Context.get_endorser (B b) slot >>=? fun endorser -> Context.Contract.balance (B b) (Contract.implicit_contract endorser) >>=? fun balance -> Op.endorsement ~delegate:endorser (B b) [slot] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~policy:(Excluding [ endorser ]) ~operation b >>=? fun b -> (* Bake (preserved_cycles + 1) cycles *) fold_left_s (fun b _ -> @@ -176,6 +179,7 @@ let wrong_endorsement_predecessor () = Context.get_endorser (B b) 0 >>=? fun genesis_endorser -> Block.bake b >>=? fun b' -> Op.endorsement ~delegate:genesis_endorser ~signing_context:(B b') (B b) [0] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~operation b' >>= fun res -> Assert.proto_error ~loc:__LOC__ res begin function @@ -190,6 +194,7 @@ let invalid_endorsement_level () = Context.get_level (B b) >>=? fun genesis_level -> Block.bake b >>=? fun b -> Op.endorsement ~level:genesis_level (B b) [0] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~operation b >>= fun res -> Assert.proto_error ~loc:__LOC__ res begin function @@ -202,8 +207,10 @@ let duplicate_endorsement () = Context.init 5 >>=? fun (b, _) -> Incremental.begin_construction b >>=? fun inc -> Op.endorsement (B b) [0] >>=? fun operation -> + let operation = Operation.pack operation in Incremental.add_operation inc operation >>=? fun inc -> Op.endorsement (B b) [0] >>=? fun operation -> + let operation = Operation.pack operation in Incremental.add_operation inc operation >>= fun res -> Assert.proto_error ~loc:__LOC__ res begin function @@ -219,6 +226,7 @@ let invalid_endorsement_slot () = Context.get_endorser (B b) 0 >>=? fun endorser -> Op.endorsement ~delegate:endorser (B b) [endorsers_per_block + 1] >>=? fun operation -> + let operation = Operation.pack operation in Block.bake ~operation b >>= fun res -> Assert.proto_error ~loc:__LOC__ res begin function diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 7eb6ab367..fc2ab3cf5 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -15,7 +15,7 @@ open Alpha_context type t = { hash : Block_hash.t ; header : Block_header.t ; - operations : Operation.t list ; + operations : Operation.packed list ; context : Tezos_protocol_environment_memory.Context.t ; } type block = t @@ -152,7 +152,7 @@ module Forge = struct | { expected_commitment = true } -> Some (fst (Proto_Nonce.generate ())) | { expected_commitment = false } -> None end >>=? fun seed_nonce_hash -> - let hashes = List.map Operation.hash operations in + let hashes = List.map Operation.hash_packed operations in let operations_hash = Operation_list_list_hash.compute [Operation_list_hash.compute hashes] in let header = make_header @@ -229,7 +229,6 @@ let genesis ?(blocks_per_roll_snapshot = Constants_repr.default.blocks_per_roll_snapshot) ?(blocks_per_voting_period = Constants_repr.default.blocks_per_voting_period) ?(time_between_blocks = Constants_repr.default.time_between_blocks) - ?(first_free_baking_slot = Constants_repr.default.first_free_baking_slot) ?(endorsers_per_block = Constants_repr.default.endorsers_per_block) ?(hard_gas_limit_per_operation = Constants_repr.default.hard_gas_limit_per_operation) ?(hard_gas_limit_per_block = Constants_repr.default.hard_gas_limit_per_block) @@ -276,7 +275,6 @@ let genesis blocks_per_roll_snapshot ; blocks_per_voting_period ; time_between_blocks ; - first_free_baking_slot ; endorsers_per_block ; hard_gas_limit_per_operation ; hard_gas_limit_per_block ; diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index bf1ada329..d12c98bf9 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -13,7 +13,7 @@ open Alpha_context type t = { hash : Block_hash.t ; header : Block_header.t ; - operations : Operation.t list ; + operations : Operation.packed list ; context : Tezos_protocol_environment_memory.Context.t ; (** Resulting context *) } type block = t @@ -46,7 +46,7 @@ module Forge : sig The header can then be modified and applied with [apply]. *) val forge_header: ?policy:baker_policy -> - ?operations: Operation.t list -> + ?operations: Operation.packed list -> t -> header tzresult Lwt.t (** Sets seed_nonce_hash of a header *) @@ -67,7 +67,6 @@ val genesis: ?blocks_per_roll_snapshot:int32 -> ?blocks_per_voting_period:int32 -> ?time_between_blocks:Period_repr.t list -> - ?first_free_baking_slot:int -> ?endorsers_per_block:int -> ?hard_gas_limit_per_operation:Z.t -> ?hard_gas_limit_per_block:Z.t -> @@ -93,7 +92,7 @@ val genesis: (** Applies a header and its operations to a block and obtains a new block *) val apply: Forge.header -> - ?operations: Operation.t list -> + ?operations: Operation.packed list -> t -> t tzresult Lwt.t (** @@ -103,8 +102,8 @@ val apply: *) val bake: ?policy: baker_policy -> - ?operation: Operation.t -> - ?operations: Operation.t list -> + ?operation: Operation.packed -> + ?operations: Operation.packed list -> t -> t tzresult Lwt.t (** Bakes [n] blocks. *) diff --git a/src/proto_alpha/lib_protocol/test/helpers/incremental.ml b/src/proto_alpha/lib_protocol/test/helpers/incremental.ml index 4cc352dbf..2c63a54ce 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/incremental.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/incremental.ml @@ -13,7 +13,7 @@ open Alpha_context type t = { predecessor: Block.t ; state: M.validation_state ; - rev_operations: Operation.t list ; + rev_operations: Operation.packed list ; header: Block_header.t ; delegate: Account.t ; } @@ -88,7 +88,7 @@ let finalize_block st = let operations = List.rev st.rev_operations in let operations_hash = Operation_list_list_hash.compute [ - Operation_list_hash.compute (List.map Operation.hash operations) + Operation_list_hash.compute (List.map Operation.hash_packed operations) ] in let header = { st.header with diff --git a/src/proto_alpha/lib_protocol/test/helpers/incremental.mli b/src/proto_alpha/lib_protocol/test/helpers/incremental.mli index 7677d4404..de6597905 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/incremental.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/incremental.mli @@ -23,7 +23,7 @@ val begin_construction: Block.t -> incremental tzresult Lwt.t val add_operation: - incremental -> Operation.t -> incremental tzresult Lwt.t + incremental -> Operation.packed -> incremental tzresult Lwt.t val finalize_block: incremental -> Block.t 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 24e0b2da6..283c57604 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -10,19 +10,20 @@ open Proto_alpha open Alpha_context -let sign ?(watermark = Signature.Generic_operation) sk ctxt contents = +let sign ?(watermark = Signature.Generic_operation) + sk ctxt contents = let branch = Context.branch ctxt in let unsigned = Data_encoding.Binary.to_bytes_exn Operation.unsigned_encoding - ({ branch }, contents) in + ({ branch }, Contents_list contents) in let signature = Some (Signature.sign ~watermark sk unsigned) in - { shell = { branch } ; - protocol_data = { - contents ; - signature ; - } ; - } + ({ shell = { branch } ; + protocol_data = { + contents ; + signature ; + } ; + } : _ Operation.t) let endorsement ?delegate ?level ctxt = fun ?(signing_context=ctxt) slots -> @@ -38,40 +39,77 @@ let endorsement ?delegate ?level ctxt = | Some level -> return level end >>=? fun level -> let op = - let operations = Endorsements { block = Context.branch ctxt ; level ; slots = slots } in - Sourced_operation (Consensus_operation operations) in + Single + (Endorsements + { block = Context.branch ctxt ; level ; slots = slots }) in return (sign ~watermark:Signature.Endorsement delegate.sk signing_context op) -let manager_operations ?(fee = Tez.zero) +let sign ?watermark sk ctxt (Contents_list contents) = + Operation.pack (sign ?watermark sk ctxt contents) + +let manager_operation + ?(fee = Tez.zero) ?(gas_limit = Constants_repr.default.hard_gas_limit_per_operation) ?(storage_limit = Constants_repr.default.hard_storage_limit_per_operation) - ?public_key ~source ctxt operations = + ?public_key ~source ctxt operation = Context.Contract.counter ctxt source >>=? fun counter -> Context.Contract.manager ctxt source >>=? fun account -> let public_key = Option.unopt ~default:account.pk public_key in let counter = Int32.succ counter in - Context.Contract.is_manager_key_revealed ctxt source >>=? begin function - | true -> return operations - | false -> return @@ (Reveal public_key) :: operations end >>=? fun operations -> - return @@ Manager_operations { - source ; - fee ; - counter ; - operations ; - gas_limit ; - storage_limit ; - } + Context.Contract.is_manager_key_revealed ctxt source >>=? function + | true -> + let op = + Manager_operation { + source ; + fee ; + counter ; + operation ; + gas_limit ; + storage_limit ; + } in + return (Contents_list (Single op)) + | false -> + let op_reveal = + Manager_operation { + source ; + fee = Tez.zero ; + counter ; + operation = Reveal public_key ; + gas_limit = Z.of_int 20 ; + storage_limit = 0L ; + } in + let op = + Manager_operation { + source ; + fee ; + counter = Int32.succ counter ; + operation ; + gas_limit ; + storage_limit ; + } in + return (Contents_list (Cons (op_reveal, Single op))) let revelation ctxt public_key = let pkh = Signature.Public_key.hash public_key in - let contract = Contract.implicit_contract pkh in - manager_operations ~source:contract ~public_key ctxt [] >>=? fun mop -> - let sop = Sourced_operation mop in - Context.Contract.manager ctxt contract >>=? fun account -> + let source = Contract.implicit_contract pkh in + Context.Contract.counter ctxt source >>=? fun counter -> + Context.Contract.manager ctxt source >>=? fun account -> + let counter = Int32.succ counter in + let sop = + Contents_list + (Single + (Manager_operation { + source ; + fee = Tez.zero ; + counter ; + operation = Reveal public_key ; + gas_limit = Z.of_int 20 ; + storage_limit = 0L ; + })) in return @@ sign account.sk ctxt sop -let originated_contract (op:Operation.t) = - let nonce = Contract.initial_origination_nonce (Operation.hash op) in +let originated_contract (op: Operation.packed) = + let nonce = Contract.initial_origination_nonce (Operation.hash_packed op) in Contract.originated_contract nonce exception Impossible @@ -84,7 +122,8 @@ let origination ?delegate ?script let default_credit = Tez.of_mutez @@ Int64.of_int 1000001 in let default_credit = Option.unopt_exn Impossible default_credit in let credit = Option.unopt ~default:default_credit credit in - let operations = [Origination { + let operation = + Origination { manager ; delegate ; script ; @@ -92,11 +131,10 @@ let origination ?delegate ?script delegatable ; credit ; preorigination ; - }] in - manager_operations ?public_key ?fee ?gas_limit ?storage_limit - ~source ctxt operations >>=? fun mop -> - let sop = Sourced_operation mop in - let op:Operation.t = sign account.sk ctxt sop in + } in + manager_operation ?public_key ?fee ?gas_limit ?storage_limit + ~source ctxt operation >>=? fun sop -> + let op = sign account.sk ctxt sop in return (op , originated_contract op) let miss_signed_endorsement ?level ctxt slot = @@ -117,16 +155,14 @@ let transaction ?fee ?gas_limit ?storage_limit ?parameters ctxt parameters; destination=dst; } in - manager_operations ?fee ?gas_limit ?storage_limit - ~source:src ctxt [top] >>=? fun mop -> - let sop = Sourced_operation mop in + manager_operation ?fee ?gas_limit ?storage_limit + ~source:src ctxt top >>=? fun sop -> Context.Contract.manager ctxt src >>=? fun account -> return @@ sign account.sk ctxt sop let delegation ?fee ctxt source dst = let top = Delegation dst in - manager_operations ?fee ~source ctxt [top] >>=? fun mop -> - let sop = Sourced_operation mop in + manager_operation ?fee ~source ctxt top >>=? fun sop -> Context.Contract.manager ctxt source >>=? fun account -> return @@ sign account.sk ctxt sop @@ -137,42 +173,36 @@ let activation ctxt (pkh : Signature.Public_key_hash.t) activation_code = encrypted public key hash" Signature.Public_key_hash.pp pkh end >>=? fun id -> let contents = - Anonymous_operations - [ Activation { id ; activation_code } ] - in + Single (Activate_account { id ; activation_code } ) in let branch = Context.branch ctxt in return { shell = { branch } ; - protocol_data = { - contents ; - signature = None ; - } ; + protocol_data = Operation_data { + contents ; + signature = None ; + } ; } let double_endorsement ctxt op1 op2 = let contents = - Anonymous_operations [ - Double_endorsement_evidence {op1 ; op2} - ] in + Single (Double_endorsement_evidence {op1 ; op2}) in let branch = Context.branch ctxt in return { shell = { branch } ; - protocol_data = { - contents ; - signature = None ; - } ; + protocol_data = Operation_data { + contents ; + signature = None ; + } ; } let double_baking ctxt bh1 bh2 = let contents = - Anonymous_operations [ - Double_baking_evidence {bh1 ; bh2} - ] in + Single (Double_baking_evidence {bh1 ; bh2}) in let branch = Context.branch ctxt in return { shell = { branch } ; - protocol_data = { - contents ; - signature = None ; - } ; + protocol_data = Operation_data { + contents ; + signature = None ; + } ; } diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index 798093d8d..b826de12d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -14,11 +14,11 @@ val endorsement: ?delegate:public_key_hash -> ?level:Raw_level.t -> Context.t -> ?signing_context:Context.t -> - int list -> Operation.t tzresult Lwt.t + int list -> Kind.endorsements Operation.t tzresult Lwt.t val miss_signed_endorsement: ?level:Raw_level.t -> - Context.t -> int -> Operation.t tzresult Lwt.t + Context.t -> int -> Kind.endorsements Operation.t tzresult Lwt.t val transaction: ?fee:Tez.tez -> @@ -29,15 +29,15 @@ val transaction: Contract.t -> Contract.t -> Tez.t -> - Operation.t tzresult Lwt.t + Operation.packed tzresult Lwt.t val delegation: ?fee:Tez.tez -> Context.t -> Contract.t -> public_key_hash option -> - Operation.t tzresult Lwt.t + Operation.packed tzresult Lwt.t val revelation: - Context.t -> public_key -> Operation.t tzresult Lwt.t + Context.t -> public_key -> Operation.packed tzresult Lwt.t val origination: ?delegate:public_key_hash -> @@ -53,20 +53,24 @@ val origination: ?storage_limit:int64 -> Context.t -> Contract.contract -> - (Operation.t * Contract.contract) tzresult Lwt.t + (Operation.packed * Contract.contract) tzresult Lwt.t val originated_contract: - Operation.t -> Contract.contract + Operation.packed -> Contract.contract val double_endorsement: - Context.t -> Operation.t -> Operation.t - -> Operation.t tzresult Lwt.t + Context.t -> + Kind.endorsements Operation.t -> + Kind.endorsements Operation.t -> + Operation.packed tzresult Lwt.t val double_baking: - Context.t -> Block_header.block_header -> Block_header.block_header - -> Operation.t tzresult Lwt.t + Context.t -> + Block_header.block_header -> + Block_header.block_header -> + Operation.packed tzresult Lwt.t val activation: Context.t -> Signature.Public_key_hash.t -> Blinded_public_key_hash.activation_code -> - Operation.t tzresult Lwt.t + Operation.packed tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/jbuild b/src/proto_alpha/lib_protocol/test/jbuild index 082f3cb25..105ad647c 100644 --- a/src/proto_alpha/lib_protocol/test/jbuild +++ b/src/proto_alpha/lib_protocol/test/jbuild @@ -17,10 +17,10 @@ -open Tezos_alpha_test_helpers )))) -;;(alias -;; ((name buildtest) -;; (package tezos-protocol-alpha) -;; (deps (main.exe)))) +(alias + ((name buildtest) + (package tezos-protocol-alpha) + (deps (main.exe)))) ; runs only the `Quick tests (alias @@ -34,10 +34,10 @@ (package tezos-protocol-alpha) (action (chdir ${ROOT} (run ${exe:main.exe} -v))))) -;;(alias -;; ((name runtest) -;; (package tezos-protocol-alpha) -;; (deps ((alias runtest_proto_alpha))))) +(alias + ((name runtest) + (package tezos-protocol-alpha) + (deps ((alias runtest_proto_alpha))))) (alias ((name runtest_indent)