From 601c234fd7aba89e66dd5e2aa4518d2d5cfb0620 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 17:17:25 +0200 Subject: [PATCH 1/5] Proto: no need for type Contract.contract --- .../lib_protocol/alpha_context.mli | 46 +++++++++---------- src/proto_alpha/lib_protocol/apply_results.ml | 2 +- .../lib_protocol/apply_results.mli | 2 +- .../lib_protocol/script_typed_ir.ml | 2 +- .../lib_protocol/script_typed_ir.mli | 2 +- .../lib_protocol/test/helpers/op.mli | 6 +-- 6 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 2956d3309622..5c95bd0da463 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1475,31 +1475,29 @@ end module Contract : sig include BASIC_DATA - type contract = t - val in_memory_size : t -> Cache_memory_helpers.sint - val rpc_arg : contract RPC_arg.arg + val rpc_arg : t RPC_arg.arg - val to_b58check : contract -> string + val to_b58check : t -> string - val of_b58check : string -> contract tzresult + val of_b58check : string -> t tzresult - val implicit_contract : public_key_hash -> contract + val implicit_contract : public_key_hash -> t - val is_implicit : contract -> public_key_hash option + val is_implicit : t -> public_key_hash option - val is_originated : contract -> Contract_hash.t option + val is_originated : t -> Contract_hash.t option - val exists : context -> contract -> bool tzresult Lwt.t + val exists : context -> t -> bool tzresult Lwt.t - val must_exist : context -> contract -> unit tzresult Lwt.t + val must_exist : context -> t -> unit tzresult Lwt.t - val allocated : context -> contract -> bool tzresult Lwt.t + val allocated : context -> t -> bool tzresult Lwt.t - val must_be_allocated : context -> contract -> unit tzresult Lwt.t + val must_be_allocated : context -> t -> unit tzresult Lwt.t - val list : context -> contract list Lwt.t + val list : context -> t list Lwt.t val get_manager_key : ?error:error -> context -> public_key_hash -> public_key tzresult Lwt.t @@ -1511,29 +1509,27 @@ module Contract : sig context -> public_key_hash -> public_key -> context tzresult Lwt.t val get_script_code : - context -> contract -> (context * Script.lazy_expr option) tzresult Lwt.t + context -> t -> (context * Script.lazy_expr option) tzresult Lwt.t - val get_script : - context -> contract -> (context * Script.t option) tzresult Lwt.t + val get_script : context -> t -> (context * Script.t option) tzresult Lwt.t val get_storage : - context -> contract -> (context * Script.expr option) tzresult Lwt.t + context -> t -> (context * Script.expr option) tzresult Lwt.t val get_counter : context -> public_key_hash -> Z.t tzresult Lwt.t - val get_balance : context -> contract -> Tez.t tzresult Lwt.t + val get_balance : context -> t -> Tez.t tzresult Lwt.t - val get_balance_carbonated : - context -> contract -> (context * Tez.t) tzresult Lwt.t + val get_balance_carbonated : context -> t -> (context * Tez.t) tzresult Lwt.t val fresh_contract_from_current_nonce : context -> (context * t) tzresult val originated_from_current_nonce : - since:context -> until:context -> contract list tzresult Lwt.t + since:context -> until:context -> t list tzresult Lwt.t - val get_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t + val get_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t - val get_balance_and_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t + val get_balance_and_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t module Legacy_big_map_diff : sig type item = private @@ -1558,7 +1554,7 @@ module Contract : sig val update_script_storage : context -> - contract -> + t -> Script.expr -> Lazy_storage.diffs option -> context tzresult Lwt.t @@ -1579,7 +1575,7 @@ module Contract : sig module Internal_for_tests : sig (** see [Contract_repr.originated_contract] for documentation *) - val originated_contract : Origination_nonce.Internal_for_tests.t -> contract + val originated_contract : Origination_nonce.Internal_for_tests.t -> t val paid_storage_space : context -> t -> Z.t tzresult Lwt.t end diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 25885de8ee7c..a2146f53b400 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -65,7 +65,7 @@ type packed_internal_manager_operation = -> packed_internal_manager_operation type 'kind internal_contents = { - source : Contract.contract; + source : Contract.t; operation : 'kind internal_manager_operation; nonce : int; } diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index 5b527e8a4817..a767d584d985 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -52,7 +52,7 @@ val manager_operation_of_internal_operation : 'kind internal_manager_operation -> 'kind Alpha_context.manager_operation type 'kind internal_contents = { - source : Contract.contract; + source : Contract.t; operation : 'kind internal_manager_operation; nonce : int; } diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.ml b/src/proto_alpha/lib_protocol/script_typed_ir.ml index 6c7e45d3c810..b1d02fdaea88 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.ml +++ b/src/proto_alpha/lib_protocol/script_typed_ir.ml @@ -1389,7 +1389,7 @@ and 'kind manager_operation = -> Kind.delegation manager_operation and 'kind internal_operation = { - source : Contract.contract; + source : Contract.t; operation : 'kind manager_operation; nonce : int; } diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.mli b/src/proto_alpha/lib_protocol/script_typed_ir.mli index f2d0f2b2f748..547886e26bb6 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.mli +++ b/src/proto_alpha/lib_protocol/script_typed_ir.mli @@ -1522,7 +1522,7 @@ and 'kind manager_operation = -> Kind.delegation manager_operation and 'kind internal_operation = { - source : Contract.contract; + source : Contract.t; operation : 'kind manager_operation; nonce : int; } diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index ef2354bd5a1b..5a4165ccb780 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -125,10 +125,10 @@ val contract_origination : ?gas_limit:Gas.Arith.integral -> ?storage_limit:Z.t -> Context.t -> - Contract.contract -> - (Operation.packed * Contract.contract) tzresult Lwt.t + Contract.t -> + (Operation.packed * Contract.t) tzresult Lwt.t -val originated_contract : Operation.packed -> Contract.contract +val originated_contract : Operation.packed -> Contract.t val register_global_constant : ?counter:Z.t -> -- GitLab From ae41d970d658331433d7e465f5e026e19049d55b Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 17:21:24 +0200 Subject: [PATCH 2/5] Proto: no need for type Contract_repr.contract --- src/proto_alpha/lib_protocol/contract_repr.ml | 4 +-- .../lib_protocol/contract_repr.mli | 29 +++++++++---------- .../lib_protocol/contract_storage.ml | 9 +++--- .../lib_protocol/contract_storage.mli | 4 +-- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index 5b57f87e1844..d6ddf1baefb0 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -39,8 +39,6 @@ include Compare.Make (struct | (Originated _, Implicit _) -> 1 end) -type contract = t - let blake2b_hash_size = let open Cache_memory_helpers in h1w +! string_size_gen 20 @@ -173,7 +171,7 @@ let rpc_arg = () module Index = struct - type t = contract + type nonrec t = t let path_length = 1 diff --git a/src/proto_alpha/lib_protocol/contract_repr.mli b/src/proto_alpha/lib_protocol/contract_repr.mli index 9c81fcbef669..ce6a926729fb 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.mli +++ b/src/proto_alpha/lib_protocol/contract_repr.mli @@ -42,57 +42,54 @@ type t = private | Implicit of Signature.Public_key_hash.t | Originated of Contract_hash.t -type contract = t - -include Compare.S with type t := contract +include Compare.S with type t := t val in_memory_size : t -> Cache_memory_helpers.sint (** {2 Implicit contracts} *) -val implicit_contract : Signature.Public_key_hash.t -> contract +val implicit_contract : Signature.Public_key_hash.t -> t -val is_implicit : contract -> Signature.Public_key_hash.t option +val is_implicit : t -> Signature.Public_key_hash.t option (** {2 Originated contracts} *) (** [originated_contract nonce] is the contract address originated from [nonce]. *) -val originated_contract : Origination_nonce.t -> contract +val originated_contract : Origination_nonce.t -> t (** [originated_contracts ~since ~until] is the contract addresses originated from [since] until [until]. The operation hash of nonce [since] and [until] must be the same or it will fail with an [assert]. [since] < [until] or the returned list is empty *) val originated_contracts : - since:Origination_nonce.t -> until:Origination_nonce.t -> contract list + since:Origination_nonce.t -> until:Origination_nonce.t -> t list -val is_originated : contract -> Contract_hash.t option +val is_originated : t -> Contract_hash.t option (** {2 Human readable notation} *) type error += Invalid_contract_notation of string (* `Permanent *) -val to_b58check : contract -> string +val to_b58check : t -> string -val of_b58check : string -> contract tzresult +val of_b58check : string -> t tzresult -val pp : Format.formatter -> contract -> unit +val pp : Format.formatter -> t -> unit -val pp_short : Format.formatter -> contract -> unit +val pp_short : Format.formatter -> t -> unit (** {2 Serializers} *) -val encoding : contract Data_encoding.t +val encoding : t Data_encoding.t (** [cases f g] exports the {!Data_encoding.cases} used to define {!encoding}. The only reason why we export that is to let {!Destination_repr.encoding} use it. This allows the latter to be compatible with {!encoding}, which is of key importance for backward compatibility reasons. *) -val cases : - ('a -> contract option) -> (contract -> 'a) -> 'a Data_encoding.case list +val cases : ('a -> t option) -> (t -> 'a) -> 'a Data_encoding.case list -val rpc_arg : contract RPC_arg.arg +val rpc_arg : t RPC_arg.arg module Index : Storage_description.INDEX with type t = t diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index 71dcf5dbf306..ff1c56bf652c 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -26,14 +26,13 @@ type error += | (* `Temporary *) - Balance_too_low of - Contract_repr.contract * Tez_repr.t * Tez_repr.t + Balance_too_low of Contract_repr.t * Tez_repr.t * Tez_repr.t | (* `Temporary *) - Counter_in_the_past of Contract_repr.contract * Z.t * Z.t + Counter_in_the_past of Contract_repr.t * Z.t * Z.t | (* `Branch *) - Counter_in_the_future of Contract_repr.contract * Z.t * Z.t + Counter_in_the_future of Contract_repr.t * Z.t * Z.t | (* `Temporary *) - Non_existing_contract of Contract_repr.contract + Non_existing_contract of Contract_repr.t | (* `Branch *) Empty_implicit_contract of Signature.Public_key_hash.t | (* `Branch *) diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index 660fbc4a407c..cd0fc0e6c98e 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -28,9 +28,9 @@ type error += | (* `Branch *) - Counter_in_the_future of Contract_repr.contract * Z.t * Z.t + Counter_in_the_future of Contract_repr.t * Z.t * Z.t | (* `Temporary *) - Non_existing_contract of Contract_repr.contract + Non_existing_contract of Contract_repr.t | (* `Permanent *) Inconsistent_public_key of Signature.Public_key.t * Signature.Public_key.t -- GitLab From 1a16f9be1ccf60f16923f490cf8c355f36510ab2 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 17:42:36 +0200 Subject: [PATCH 3/5] Proto/Tests: do not use Contract.is_implicit We've got Context.Contract.pkh for that already --- .../test/helpers/liquidity_baking_machine.ml | 6 +- .../integration/consensus/test_delegation.ml | 12 +--- .../consensus/test_helpers_rpcs.ml | 5 +- .../integration/michelson/test_sapling.ml | 10 +--- .../michelson/test_ticket_accounting.ml | 6 +- .../michelson/test_ticket_operations_diff.ml | 6 +- .../operations/test_failing_noop.ml | 19 +++---- .../integration/operations/test_tx_rollup.ml | 56 +++++++++---------- 8 files changed, 42 insertions(+), 78 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml b/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml index 5459e68cfd61..958e24ef27c4 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml @@ -119,10 +119,6 @@ let far_future = Script_timestamp.of_zint (Z.of_int 42_000) (* --------------------------------------------------------------------------- *) (** {1 Miscellaneous Helpers} *) - -let is_implicit_exn account = - match Contract.is_implicit account with Some k -> k | _ -> assert false - module List_helpers = struct let rec zip l r = match (l, r) with @@ -764,7 +760,7 @@ module ConcreteBaseMachine : let bake ~invariant ~baker ops env blk = Incremental.begin_construction - ~policy:(Block.By_account (is_implicit_exn baker)) + ~policy:(Block.By_account (Context.Contract.pkh baker)) blk >>= fun incr -> fold_m Incremental.add_operation incr ops >>= fun incr -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml index 2b46c3e97674..2810de0eb97e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml @@ -501,9 +501,7 @@ let test_unregistered_delegate_key_init_delegation ~fee () = let test_unregistered_delegate_key_switch_delegation ~fee () = Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap_pkh = - Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ - in + let bootstrap_pkh = Context.Contract.pkh bootstrap in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -620,9 +618,7 @@ let test_unregistered_delegate_key_init_delegation_credit ~fee ~amount () = let test_unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap_pkh = - Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ - in + let bootstrap_pkh = Context.Contract.pkh bootstrap in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -756,9 +752,7 @@ let test_unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount () = Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap_pkh = - Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ - in + let bootstrap_pkh = Context.Contract.pkh bootstrap in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml index 78df0e7cc262..367aa44d4ecf 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml @@ -32,9 +32,6 @@ Subject: On RPCs. *) -open Protocol -open Alpha_context - (* Test the baking_rights RPC. Future levels or cycles are not tested because it's hard in this framework, using only RPCs, to fabricate them. *) @@ -49,7 +46,7 @@ let test_baking_rights () = get Block.rpc_ctxt b ~all:true ~max_round >>=? fun rights -> assert (Compare.List_length_with.(rights = max_round + 1)) ; (* filtering by delegate *) - let d = Contract.is_implicit c1 |> WithExceptions.Option.get ~loc:__LOC__ in + let d = Context.Contract.pkh c1 in get Block.rpc_ctxt b ~all:true ~delegates:[d] >>=? fun rights -> assert (List.for_all (fun {delegate; _} -> delegate = d) rights) ; (* filtering by cycle *) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml index 704381c37c1a..b05e03bf67dc 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml @@ -642,10 +642,7 @@ module Interpreter_tests = struct (fun addr -> Tezos_sapling.Forge.make_output addr 1L (Bytes.create 8)) list_addr in - (let pkh = - Alpha_context.Contract.is_implicit src1 - |> WithExceptions.Option.get ~loc:__LOC__ - in + (let pkh = Context.Contract.pkh src1 in (* dummy context used only for pack_data *) Block.alpha_context [(Account.activator_account, Tez.of_mutez_exn 100_000_000_000L)] @@ -914,10 +911,7 @@ module Interpreter_tests = struct Incremental.add_operation incr operation >>=? fun incr -> (* We need to manually get the counter here *) let ctx = Incremental.alpha_ctxt incr in - let pkh = - Alpha_context.Contract.is_implicit src - |> WithExceptions.Option.get ~loc:__LOC__ - in + let pkh = Context.Contract.pkh src in Alpha_context.Contract.get_counter ctx pkh >>= wrap >>=? fun counter -> Op.transaction ~counter diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml index 0c1a1b0816ad..88c72683af3b 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml @@ -170,11 +170,7 @@ let init () = (** Initializes one address for operations and one baker. *) let init_for_operation () = Context.init2 ~consensus_threshold:0 () >|=? fun (block, (src0, src1)) -> - let baker = - match Alpha_context.Contract.is_implicit src0 with - | Some v -> v - | None -> assert false - in + let baker = Context.Contract.pkh src0 in (baker, src1, block) let two_ticketers block = diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml index 8a459257bc42..2d7a6be06fdc 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml @@ -182,11 +182,7 @@ let string_token ~ticketer content = let init ?tx_rollup_enable () = Context.init2 ?tx_rollup_enable ~consensus_threshold:0 () >|=? fun (block, (src0, src1)) -> - let baker = - match Alpha_context.Contract.is_implicit src0 with - | Some v -> v - | None -> assert false - in + let baker = Context.Contract.pkh src0 in (baker, src1, block) let originate block ~script ~storage ~src ~baker ~forges_tickets = diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml index 95bd9ea38a68..798c949977fe 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml @@ -37,21 +37,16 @@ applied. *) -open Protocol -open Alpha_context - (** try to apply a failing_noop and assert that the operation fails *) let failing_noop_must_fail_when_injected () = Context.init1 () >>=? fun (blk, contract) -> - Contract.is_implicit contract |> function - | None -> Alcotest.fail "only implicit accounts can sign" - | Some source -> - Op.failing_noop (B blk) source "tezos" >>=? fun operation -> - Block.bake ~operation blk >>= fun res -> - Assert.proto_error_with_info - ~loc:__LOC__ - res - "Failing_noop operations are not executed by the protocol" + let source = Context.Contract.pkh contract in + Op.failing_noop (B blk) source "tezos" >>=? fun operation -> + Block.bake ~operation blk >>= fun res -> + Assert.proto_error_with_info + ~loc:__LOC__ + res + "Failing_noop operations are not executed by the protocol" let tests = [Tztest.tztest "injection fails" `Quick failing_noop_must_fail_when_injected] diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 94b403ed8dad..99a82b4964b5 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -56,11 +56,6 @@ let check_proto_error_f f t = equals [e]. *) let check_proto_error e t = check_proto_error_f (( = ) e) t -let is_implicit_exn x = - match Alpha_context.Contract.is_implicit x with - | Some x -> x - | None -> raise (Invalid_argument "is_implicit_exn") - (** [test_disable_feature_flag] try to originate a tx rollup with the feature flag is deactivated and check it fails *) let test_disable_feature_flag () = @@ -106,7 +101,7 @@ let parsing_tests = initial_storage contract b - (is_implicit_exn contract) + (Context.Contract.pkh contract) >>= fun res -> if not tx_rollup_enable then Assert.error ~loc:__LOC__ res (function @@ -352,7 +347,7 @@ let make_deposit b tx_rollup l1_src addr = "Unit" l1_src b - (is_implicit_exn l1_src) + (Context.Contract.pkh l1_src) >>=? fun (contract, b) -> let parameters = print_deposit_arg (`Typed tx_rollup) (`Hash addr) in let fee = Test_tez.of_int 10 in @@ -370,7 +365,7 @@ let make_deposit b tx_rollup l1_src addr = >>=? fun ticket_hash -> let (deposit, cumulated_size) = Tx_rollup_message.make_deposit - (is_implicit_exn l1_src) + (Context.Contract.pkh l1_src) (Tx_rollup_l2_address.Indexable.value addr) ticket_hash (Tx_rollup_l2_qty.of_int64_exn 100_000L) @@ -422,7 +417,7 @@ let make_incomplete_commitment_for_batch context level tx_rollup withdraw_list = (** Check that the given contract has [count] pending bonded commitments *) let check_bond ctxt tx_rollup contract count = - let pkh = is_implicit_exn contract in + let pkh = Context.Contract.pkh contract in Tx_rollup_commitment.pending_bonded_commitments ctxt tx_rollup pkh >>=?? fun (_, pending) -> Alcotest.(check int "Pending bonded commitment count correct" count pending) ; @@ -484,14 +479,15 @@ module Nat_ticket = struct let withdrawal ctxt ~ticketer ?(claimer = ticketer) ?(amount = amount) tx_rollup : (Tx_rollup_withdraw.t * Tx_rollup_reveal.t) tzresult Lwt.t = ticket_hash ctxt ~ticketer ~tx_rollup >|=? fun ticket_hash -> - ( Tx_rollup_withdraw.{claimer = is_implicit_exn claimer; ticket_hash; amount}, + let claimer = Context.Contract.pkh claimer in + ( Tx_rollup_withdraw.{claimer; ticket_hash; amount}, Tx_rollup_reveal. { contents = Script.lazy_expr contents; ty = Script.lazy_expr ty; ticketer; amount; - claimer = is_implicit_exn claimer; + claimer; } ) let init_deposit_contract amount block account = @@ -525,7 +521,7 @@ module Nat_ticket = struct contents_nat in Contract_helpers.originate_contract_from_string - ~baker:(is_implicit_exn account) + ~baker:(Context.Contract.pkh account) ~source_contract:account ~script ~storage:"Unit" @@ -955,7 +951,7 @@ let test_inbox_count_too_big () = "Unit" contract b - (is_implicit_exn contract) + (Context.Contract.pkh contract) >>=? fun (deposit_contract, b) -> Incremental.begin_construction b >>=? fun i -> let rec fill_inbox i counter n = @@ -1055,7 +1051,7 @@ let test_additional_space_allocation_for_valid_deposit () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> let parameters = print_deposit_arg (`Typed tx_rollup) (`Hash pkh) in let fee = Test_tez.of_int 10 in @@ -1086,7 +1082,7 @@ let test_valid_deposit_inexistant_rollup () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let parameters = @@ -1115,7 +1111,7 @@ let test_invalid_deposit_not_ticket () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let parameters = print_deposit_arg (`Typed tx_rollup) (`Hash pkh) in @@ -1185,7 +1181,7 @@ let test_invalid_deposit_too_big_ticket () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let ticket_contents = @@ -1236,7 +1232,7 @@ let test_invalid_deposit_too_big_ticket_type () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let ticket_contents = @@ -1293,7 +1289,7 @@ let test_valid_deposit_big_ticket () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let ticket_contents = @@ -1333,7 +1329,7 @@ let test_invalid_entrypoint () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let parameters = print_deposit_arg (`Typed tx_rollup) (`Hash pkh) in @@ -1358,7 +1354,7 @@ let test_invalid_l2_address () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let parameters = @@ -1386,7 +1382,7 @@ let test_valid_deposit_invalid_amount () = "Unit" account b - (is_implicit_exn account) + (Context.Contract.pkh account) >>=? fun (contract, b) -> Incremental.begin_construction b >>=? fun i -> let parameters = print_deposit_arg (`Typed tx_rollup) (`Hash pkh) in @@ -1529,7 +1525,7 @@ let test_finalization () = the wrong batch count and ensures that that fails. *) let test_commitment_duplication () = context_init2 () >>=? fun (b, (contract1, contract2)) -> - let pkh1 = is_implicit_exn contract1 in + let pkh1 = Context.Contract.pkh contract1 in originate b contract1 >>=? fun (b, tx_rollup) -> Context.Contract.balance (B b) contract1 >>=? fun balance -> Context.Contract.balance (B b) contract2 >>=? fun balance2 -> @@ -1934,7 +1930,7 @@ let test_full_inbox () = allows bonds to be returned. *) let test_bond_finalization () = context_init1 () >>=? fun (b, contract1) -> - let pkh1 = is_implicit_exn contract1 in + let pkh1 = Context.Contract.pkh contract1 in originate b contract1 >>=? fun (b, tx_rollup) -> Context.Contract.balance (B b) contract1 >>=? fun balance -> (* Transactions in block 2, 3, 4 *) @@ -3270,7 +3266,7 @@ module Rejection = struct >>=? fun (b, account, _, tx_rollup, store, ticket_hash) -> hash_tree_from_store store >>= fun l2_context_hash -> (* 1. Create a batch with [n_withdraw] withdrawals. *) - let destination = is_implicit_exn account in + let destination = Context.Contract.pkh account in let qty = Tx_rollup_l2_qty.one in let operation = let open Tx_rollup_l2_batch.V1 in @@ -3968,7 +3964,7 @@ module Withdraw = struct Nat_ticket.ty_str) ~storage:"None" ~source_contract:account1 - ~baker:(is_implicit_exn account1) + ~baker:(Context.Contract.pkh account1) block >>=? fun (withdraw_contract, _script, block) -> return @@ -4049,7 +4045,7 @@ module Withdraw = struct } |} ~storage:"{}" ~source_contract:account - ~baker:(is_implicit_exn account) + ~baker:(Context.Contract.pkh account) block >>=? fun (forge_withdraw_deposit_contract, _script, block) -> return (forge_withdraw_deposit_contract, block) @@ -4141,7 +4137,7 @@ module Withdraw = struct Nat_ticket.ty_str) ~storage:"Unit" ~source_contract:account1 - ~baker:(is_implicit_exn account1) + ~baker:(Context.Contract.pkh account1) block >>=? fun (withdraw_dropping_contract, _script, block) -> let token_one = Nat_ticket.ex_token ~ticketer:deposit_contract in @@ -5172,7 +5168,7 @@ module Withdraw = struct let pkh_str = Tx_rollup_l2_address.to_b58check pkh in Nat_ticket.init_deposit_contract (Z.of_int64 max) b account1 >>=? fun (deposit_contract, _script, b) -> - let deposit_pkh = assert_some @@ Contract.is_implicit account1 in + let deposit_pkh = Context.Contract.pkh account1 in let deposit b = Nat_ticket.deposit_op b tx_rollup pkh_str account1 deposit_contract >>=? fun operation -> Block.bake ~operation b @@ -5251,7 +5247,7 @@ module Withdraw = struct originate b account1 >>=? fun (b, tx_rollup) -> Nat_ticket.init_deposit_contract (Z.of_int64 max) b account1 >>=? fun (deposit_contract, _script, b) -> - let deposit_pkh = assert_some @@ Contract.is_implicit account1 in + let deposit_pkh = Context.Contract.pkh account1 in let deposit b pkh = let pkh_str = Tx_rollup_l2_address.to_b58check pkh in Nat_ticket.deposit_op b tx_rollup pkh_str account1 deposit_contract -- GitLab From 8106ccee62754caae86e14175eb3a37e2b37c022 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Mon, 11 Apr 2022 16:18:36 +0200 Subject: [PATCH 4/5] Proto: simplify Contract.allocated --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/apply.ml | 2 +- src/proto_alpha/lib_protocol/contract_storage.ml | 9 +++------ src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- src/proto_alpha/lib_protocol/token.ml | 12 ++++++------ 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 5c95bd0da463..85944589f464 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1493,7 +1493,7 @@ module Contract : sig val must_exist : context -> t -> unit tzresult Lwt.t - val allocated : context -> t -> bool tzresult Lwt.t + val allocated : context -> t -> bool Lwt.t val must_be_allocated : context -> t -> unit tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 63fa38d88d70..376b23108034 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -956,7 +956,7 @@ let apply_transaction ~ctxt ~parameter ~source ~contract ~amount ~entrypoint error_when Tez.(amount = zero) (Empty_transaction contract) >>?= fun () -> (* If the implicit contract is not yet allocated at this point then the next transfer of tokens will allocate it. *) - Contract.allocated ctxt contract >|=? not) + Contract.allocated ctxt contract >|= ok >|=? not) >>=? fun allocated_destination_contract -> Token.transfer ctxt (`Contract source) (`Contract contract) amount >>=? fun (ctxt, balance_updates) -> diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index ff1c56bf652c..ddd661e42566 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -478,15 +478,12 @@ let delete c contract = Storage.Contract.Paid_storage_space.remove c contract >>= fun c -> Storage.Contract.Used_storage_space.remove c contract >|= ok -let allocated c contract = - Storage.Contract.Spendable_balance.find c contract >>=? function - | None -> return_false - | Some _ -> return_true +let allocated c contract = Storage.Contract.Spendable_balance.mem c contract let exists c contract = match Contract_repr.is_implicit contract with | Some _ -> return_true - | None -> allocated c contract + | None -> allocated c contract >|= ok let must_exist c contract = exists c contract >>=? function @@ -494,7 +491,7 @@ let must_exist c contract = | false -> fail (Non_existing_contract contract) let must_be_allocated c contract = - allocated c contract >>=? function + allocated c contract >>= function | true -> return_unit | false -> ( match Contract_repr.is_implicit contract with diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index cd0fc0e6c98e..3c76cb07aca1 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -38,7 +38,7 @@ type error += (** [allocated ctxt contract] returns [true] if and only if the contract is stored in [Storage.Contract.Balance]. *) -val allocated : Raw_context.t -> Contract_repr.t -> bool tzresult Lwt.t +val allocated : Raw_context.t -> Contract_repr.t -> bool Lwt.t (** [exists ctxt contract] returns [true] if and only if either the contract is originated or it is (implicit and) "allocated". *) diff --git a/src/proto_alpha/lib_protocol/token.ml b/src/proto_alpha/lib_protocol/token.ml index 67f1083a0039..325dea7a2800 100644 --- a/src/proto_alpha/lib_protocol/token.ml +++ b/src/proto_alpha/lib_protocol/token.ml @@ -58,15 +58,15 @@ type sink = [infinite_sink | container] let allocated ctxt stored = match stored with | `Contract contract -> - Contract_storage.allocated ctxt contract >|=? fun allocated -> - (ctxt, allocated) + Contract_storage.allocated ctxt contract >|= fun allocated -> + ok (ctxt, allocated) | `Collected_commitments bpkh -> - Commitment_storage.exists ctxt bpkh >|= ok >|=? fun allocated -> - (ctxt, allocated) + Commitment_storage.exists ctxt bpkh >|= fun allocated -> + ok (ctxt, allocated) | `Delegate_balance delegate -> let contract = Contract_repr.implicit_contract delegate in - Contract_storage.allocated ctxt contract >|=? fun allocated -> - (ctxt, allocated) + Contract_storage.allocated ctxt contract >|= fun allocated -> + ok (ctxt, allocated) | `Frozen_deposits delegate -> let contract = Contract_repr.implicit_contract delegate in Frozen_deposits_storage.allocated ctxt contract >|= fun allocated -> -- GitLab From 6bb2f17e3c5f492227df432a71912f5880ad823c Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Mon, 11 Apr 2022 16:24:14 +0200 Subject: [PATCH 5/5] Proto: simplify Contract.exists --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/contract_services.ml | 6 +++--- src/proto_alpha/lib_protocol/contract_storage.ml | 9 +++++---- src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- .../lib_protocol/liquidity_baking_migration.ml | 2 +- src/proto_alpha/lib_protocol/liquidity_baking_storage.ml | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 85944589f464..3c7f194115b4 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1489,7 +1489,7 @@ module Contract : sig val is_originated : t -> Contract_hash.t option - val exists : context -> t -> bool tzresult Lwt.t + val exists : context -> t -> bool Lwt.t val must_exist : context -> t -> unit tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/contract_services.ml b/src/proto_alpha/lib_protocol/contract_services.ml index 1f177396e90d..5c84635f5301 100644 --- a/src/proto_alpha/lib_protocol/contract_services.ml +++ b/src/proto_alpha/lib_protocol/contract_services.ml @@ -290,19 +290,19 @@ let[@coq_axiom_with_reason "gadt"] register () = register0 ~chunked:true S.list (fun ctxt () () -> Contract.list ctxt >|= ok) ; let register_field ~chunked s f = opt_register1 ~chunked s (fun ctxt contract () () -> - Contract.exists ctxt contract >>=? function + Contract.exists ctxt contract >>= function | true -> f ctxt contract >|=? Option.some | false -> return_none) in let register_field_with_query ~chunked s f = opt_register1 ~chunked s (fun ctxt contract query () -> - Contract.exists ctxt contract >>=? function + Contract.exists ctxt contract >>= function | true -> f ctxt contract query >|=? Option.some | false -> return_none) in let register_opt_field ~chunked s f = opt_register1 ~chunked s (fun ctxt contract () () -> - Contract.exists ctxt contract >>=? function + Contract.exists ctxt contract >>= function | true -> f ctxt contract | false -> return_none) in diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index ddd661e42566..3225ef3ee908 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -482,11 +482,11 @@ let allocated c contract = Storage.Contract.Spendable_balance.mem c contract let exists c contract = match Contract_repr.is_implicit contract with - | Some _ -> return_true - | None -> allocated c contract >|= ok + | Some _ -> Lwt.return_true + | None -> allocated c contract let must_exist c contract = - exists c contract >>=? function + exists c contract >>= function | true -> return_unit | false -> fail (Non_existing_contract contract) @@ -507,9 +507,10 @@ let fresh_contract_from_current_nonce c = let originated_from_current_nonce ~since:ctxt_since ~until:ctxt_until = Raw_context.get_origination_nonce ctxt_since >>?= fun since -> Raw_context.get_origination_nonce ctxt_until >>?= fun until -> - List.filter_es + List.filter_s (fun contract -> exists ctxt_until contract) (Contract_repr.originated_contracts ~since ~until) + >|= ok let check_counter_increment c manager counter = let contract = Contract_repr.implicit_contract manager in diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index 3c76cb07aca1..48421c4a1619 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -42,7 +42,7 @@ val allocated : Raw_context.t -> Contract_repr.t -> bool Lwt.t (** [exists ctxt contract] returns [true] if and only if either the contract is originated or it is (implicit and) "allocated". *) -val exists : Raw_context.t -> Contract_repr.t -> bool tzresult Lwt.t +val exists : Raw_context.t -> Contract_repr.t -> bool Lwt.t (** [must_exist ctxt contract] fails with the [Non_existing_contract] error if [exists ctxt contract] returns [false]. Even though this function is diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml index dd44969f8656..ad3717117a3c 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml @@ -175,7 +175,7 @@ let first_bootstrap_account = let check_tzBTC ~typecheck current_level ctxt f = Contract_repr.of_b58check mainnet_tzBTC_address >>?= fun tzBTC -> - Contract_storage.exists ctxt tzBTC >>=? function + Contract_storage.exists ctxt tzBTC >>= function | true -> (* If tzBTC exists, we're on mainnet and we use it as the token address in the CPMM. *) f ctxt tzBTC [] diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml index 450e28390abd..2c78115b223a 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml @@ -34,7 +34,7 @@ let get_toggle_ema ctxt = let on_cpmm_exists ctxt f = get_cpmm_address ctxt >>=? fun cpmm_contract -> - Contract_storage.exists ctxt cpmm_contract >>=? function + Contract_storage.exists ctxt cpmm_contract >>= function | false -> (* do nothing if the cpmm is not found *) return (ctxt, []) -- GitLab