From c08c14404b9b2d3731672fefb66ffdbad753df9c Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 14 Mar 2022 16:14:16 +0100 Subject: [PATCH 1/5] proto/alpha_ctxt : move contract before tx_rollup --- src/proto_alpha/lib_protocol/alpha_context.ml | 7 +- .../lib_protocol/alpha_context.mli | 226 +++++++++--------- .../test/integration/test_frozen_bonds.ml | 2 +- 3 files changed, 120 insertions(+), 115 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 6647d91934b1..adeddf3b5cc5 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -398,7 +398,12 @@ module Sapling = struct end end -module Bond_id = Bond_id_repr +module Bond_id = struct + include Bond_id_repr + + let fold_on_bond_ids = Contract_storage.fold_on_bond_ids +end + module Receipt = Receipt_repr module Delegate = struct diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 7c13a83a6fe9..dbd62561bc57 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1483,6 +1483,117 @@ module Ticket_hash : sig end 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 to_b58check : contract -> string + + val of_b58check : string -> contract tzresult + + val implicit_contract : public_key_hash -> contract + + val is_implicit : contract -> public_key_hash option + + val is_originated : contract -> Contract_hash.t option + + val exists : context -> contract -> bool tzresult Lwt.t + + val must_exist : context -> contract -> unit tzresult Lwt.t + + val allocated : context -> contract -> bool tzresult Lwt.t + + val must_be_allocated : context -> contract -> unit tzresult Lwt.t + + val list : context -> contract list Lwt.t + + val get_manager_key : + ?error:error -> context -> public_key_hash -> public_key tzresult Lwt.t + + val is_manager_key_revealed : + context -> public_key_hash -> bool tzresult Lwt.t + + val reveal_manager_key : + context -> public_key_hash -> public_key -> context tzresult Lwt.t + + val get_script_code : + context -> contract -> (context * Script.lazy_expr option) tzresult Lwt.t + + val get_script : + context -> contract -> (context * Script.t option) tzresult Lwt.t + + val get_storage : + context -> contract -> (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_carbonated : + context -> contract -> (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 + + val get_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t + + val get_balance_and_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t + + module Legacy_big_map_diff : sig + type item = private + | Update of { + big_map : Z.t; + diff_key : Script.expr; + diff_key_hash : Script_expr_hash.t; + diff_value : Script.expr option; + } + | Clear of Z.t + | Copy of {src : Z.t; dst : Z.t} + | Alloc of { + big_map : Z.t; + key_type : Script.expr; + value_type : Script.expr; + } + + type t = private item list + + val of_lazy_storage_diff : Lazy_storage.diffs -> t + end + + val update_script_storage : + context -> + contract -> + Script.expr -> + Lazy_storage.diffs option -> + context tzresult Lwt.t + + val used_storage_space : context -> t -> Z.t tzresult Lwt.t + + val increment_counter : context -> public_key_hash -> context tzresult Lwt.t + + val check_counter_increment : + context -> public_key_hash -> Z.t -> unit tzresult Lwt.t + + val raw_originate : + context -> + prepaid_bootstrap_storage:bool -> + t -> + script:Script.t * Lazy_storage.diffs option -> + context tzresult Lwt.t + + module Internal_for_tests : sig + (** see [Contract_repr.originated_contract] for documentation *) + val originated_contract : Origination_nonce.Internal_for_tests.t -> contract + end +end + module Tx_rollup_level : sig include BASIC_DATA @@ -1982,125 +2093,14 @@ module Bond_id : sig val pp : Format.formatter -> t -> unit val compare : t -> t -> int -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 to_b58check : contract -> string - - val of_b58check : string -> contract tzresult - - val implicit_contract : public_key_hash -> contract - - val is_implicit : contract -> public_key_hash option - - val is_originated : contract -> Contract_hash.t option - - val exists : context -> contract -> bool tzresult Lwt.t - - val must_exist : context -> contract -> unit tzresult Lwt.t - - val allocated : context -> contract -> bool tzresult Lwt.t - - val must_be_allocated : context -> contract -> unit tzresult Lwt.t - - val list : context -> contract list Lwt.t - - val get_manager_key : - ?error:error -> context -> public_key_hash -> public_key tzresult Lwt.t - - val is_manager_key_revealed : - context -> public_key_hash -> bool tzresult Lwt.t - - val reveal_manager_key : - context -> public_key_hash -> public_key -> context tzresult Lwt.t - - val get_script_code : - context -> contract -> (context * Script.lazy_expr option) tzresult Lwt.t - - val get_script : - context -> contract -> (context * Script.t option) tzresult Lwt.t - - val get_storage : - context -> contract -> (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_carbonated : - context -> contract -> (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 - - val get_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t - - val get_balance_and_frozen_bonds : context -> contract -> Tez.t tzresult Lwt.t val fold_on_bond_ids : context -> - contract -> + Contract.t -> order:[`Sorted | `Undefined] -> init:'a -> - f:(Bond_id.t -> 'a -> 'a Lwt.t) -> + f:(t -> 'a -> 'a Lwt.t) -> 'a Lwt.t - - module Legacy_big_map_diff : sig - type item = private - | Update of { - big_map : Z.t; - diff_key : Script.expr; - diff_key_hash : Script_expr_hash.t; - diff_value : Script.expr option; - } - | Clear of Z.t - | Copy of {src : Z.t; dst : Z.t} - | Alloc of { - big_map : Z.t; - key_type : Script.expr; - value_type : Script.expr; - } - - type t = private item list - - val of_lazy_storage_diff : Lazy_storage.diffs -> t - end - - val update_script_storage : - context -> - contract -> - Script.expr -> - Lazy_storage.diffs option -> - context tzresult Lwt.t - - val used_storage_space : context -> t -> Z.t tzresult Lwt.t - - val increment_counter : context -> public_key_hash -> context tzresult Lwt.t - - val check_counter_increment : - context -> public_key_hash -> Z.t -> unit tzresult Lwt.t - - val raw_originate : - context -> - prepaid_bootstrap_storage:bool -> - t -> - script:Script.t * Lazy_storage.diffs option -> - context tzresult Lwt.t - - module Internal_for_tests : sig - (** see [Contract_repr.originated_contract] for documentation *) - val originated_contract : Origination_nonce.Internal_for_tests.t -> contract - end end (** This simply re-exports {!Destination_repr}. *) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml index 532e3894a6bb..7528f37a8396 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml @@ -266,7 +266,7 @@ let test_total_stake ~user_is_delegate () = Token.transfer ctxt user_account deposit_account2 deposit_amount >>>=? fun (ctxt, _) -> (* Test folding on bond ids. *) - Contract.fold_on_bond_ids + Bond_id.fold_on_bond_ids ctxt user_contract ~init:[] -- GitLab From d0b98fa1c442c3de3a2d6c7fbe34ddc975dfc424 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 14 Mar 2022 16:16:01 +0100 Subject: [PATCH 2/5] proto: ticketer is of contract type in deposit param --- src/proto_alpha/bin_tx_rollup_node/daemon.ml | 34 ++++++++++++++++++- src/proto_alpha/lib_protocol/alpha_context.ml | 2 ++ .../lib_protocol/alpha_context.mli | 7 ++-- src/proto_alpha/lib_protocol/apply.ml | 15 +++----- .../lib_protocol/script_ir_translator.ml | 9 ++++- .../lib_protocol/tx_rollup_errors_repr.ml | 22 +++++++++++- .../lib_protocol/tx_rollup_repr.ml | 2 +- .../lib_protocol/tx_rollup_repr.mli | 2 +- 8 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/proto_alpha/bin_tx_rollup_node/daemon.ml b/src/proto_alpha/bin_tx_rollup_node/daemon.ml index 95f74db43b88..ce62e8d5ddc9 100644 --- a/src/proto_alpha/bin_tx_rollup_node/daemon.ml +++ b/src/proto_alpha/bin_tx_rollup_node/daemon.ml @@ -69,6 +69,32 @@ let interp_messages ctxt messages cumulated_size = let inbox = Inbox.{contents; cumulated_size} in (ctxt, Some inbox) +let parse_address : Script.node -> Protocol.Script_typed_ir.address tzresult = + let open Protocol in + let open Micheline in + function + | Bytes (loc, bytes) -> ( + match + Data_encoding.Binary.of_bytes_opt + Data_encoding.(tup2 Destination.encoding Entrypoint.value_encoding) + bytes + with + | Some (destination, entrypoint) -> + Ok Script_typed_ir.{destination; entrypoint} + | None -> error (Error.Tx_rollup_invalid_l2_address loc)) + | String (loc, s) -> + (match String.index_opt s '%' with + | None -> Ok (s, Entrypoint.default) + | Some pos -> + let len = String.length s - pos - 1 in + let name = String.sub s (pos + 1) len in + Environment.wrap_tzresult @@ Entrypoint.of_string_strict ~loc name + >>? fun entrypoint -> Ok (String.sub s 0 pos, entrypoint)) + >>? fun (addr, entrypoint) -> + Environment.wrap_tzresult @@ Destination.of_b58check addr + >>? fun destination -> Ok Script_typed_ir.{destination; entrypoint} + | _expr -> error Error.Tx_rollup_invalid_deposit + let parse_tx_rollup_l2_address : Script.node -> Protocol.Tx_rollup_l2_address.Indexable.value tzresult = let open Protocol in @@ -119,7 +145,13 @@ let parse_tx_rollup_deposit_parameters : | Int (_, invalid_amount) -> error (Error.Tx_rollup_invalid_ticket_amount invalid_amount) | _expr -> error Error.Tx_rollup_invalid_deposit) - >|? fun amount -> Tx_rollup.{ticketer; contents; ty; amount; destination} + >>? fun amount -> + parse_address ticketer >|? fun {destination = ticketer; entrypoint = _} -> + let ticketer = + (* ticketer is obligatory a contract, a tx_rollup can't forge ticket *) + match ticketer with Destination.Contract c -> c | _ -> assert false + in + Tx_rollup.{ticketer; contents; ty; amount; destination} | _expr -> error Error.Tx_rollup_invalid_deposit let extract_messages_from_block block_info rollup_id = diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index adeddf3b5cc5..045175199711 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -248,6 +248,7 @@ module Tx_rollup = struct let hash_ticket ctxt tx_rollup ~contents ~ticketer ~ty = let open Micheline in let owner = String (dummy_location, to_b58check tx_rollup) in + let ticketer = String (dummy_location, Contract.to_b58check ticketer) in Ticket_hash_builder.make ctxt ~ticketer ~ty ~contents ~owner module Internal_for_tests = struct @@ -256,6 +257,7 @@ module Tx_rollup = struct let hash_ticket_uncarbonated tx_rollup ~contents ~ticketer ~ty = let open Micheline in let owner = String (dummy_location, to_b58check tx_rollup) in + let ticketer = String (dummy_location, Contract.to_b58check ticketer) in Ticket_hash_builder.Internal_for_tests.make_uncarbonated ~ticketer ~ty diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index dbd62561bc57..78865d62d05b 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1638,7 +1638,7 @@ module Tx_rollup : sig type deposit_parameters = { contents : Script.node; ty : Script.node; - ticketer : Script.node; + ticketer : Contract.t; amount : Tx_rollup_l2_qty.t; destination : Tx_rollup_l2_address.Indexable.value; } @@ -1656,7 +1656,7 @@ module Tx_rollup : sig context -> t -> contents:Script.node -> - ticketer:Script.node -> + ticketer:Contract.t -> ty:Script.node -> (Ticket_hash.t * context) tzresult @@ -1673,7 +1673,7 @@ module Tx_rollup : sig val hash_ticket_uncarbonated : t -> contents:Script.node -> - ticketer:Script.node -> + ticketer:Contract.t -> ty:Script.node -> Ticket_hash.t tzresult end @@ -2085,6 +2085,7 @@ module Tx_rollup_errors : sig computed : Tx_rollup_message_result_hash.t; expected : Tx_rollup_message_result_hash.t; } + | Deposit_wrong_ticketer of Tx_rollup.t end module Bond_id : sig diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 0d578b622995..7f34645568cb 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1392,17 +1392,11 @@ let apply_external_manager_operation_content : >>?= fun (ty, ctxt) -> Script.force_decode_in_context ~consume_deserialization_gas ctxt contents >>?= fun (contents, ctxt) -> - Script_ir_translator.unparse_data - ctxt - Optimized - Script_typed_ir.address_t - {destination = Contract ticketer; entrypoint = Entrypoint.default} - >>=? fun (ticketer_node, ctxt) -> Tx_rollup.hash_ticket ctxt tx_rollup ~contents:(Micheline.root contents) - ~ticketer:(Micheline.root @@ Micheline.strip_locations ticketer_node) + ~ticketer ~ty:(Micheline.root ty) >>?= fun (ticket_hash, ctxt) -> (* Checking the operation is non-internal *) @@ -1455,9 +1449,9 @@ let apply_external_manager_operation_content : Script_typed_ir.ticket_t Micheline.dummy_location ty >>?= fun ty -> return (ticket, ty, ctxt) >>=? fun (ticket, ticket_ty, ctxt) -> Script_ir_translator.unparse_data ctxt Optimized ticket_ty ticket - >>=? fun (parameters, ctxt) -> + >>=? fun (parameters_node, ctxt) -> let parameters = - Script.lazy_expr (Micheline.strip_locations parameters) + Script.lazy_expr (Micheline.strip_locations parameters_node) in (* FIXME/TORU: #2488 the returned op will fail when ticket hardening is merged, it must be commented or fixed *) @@ -1465,7 +1459,6 @@ let apply_external_manager_operation_content : Script_typed_ir.Internal_operation { source; - (* TODO is 0 correct ? *) nonce = 0; operation = Transaction @@ -1477,7 +1470,7 @@ let apply_external_manager_operation_content : destination = Contract destination; entrypoint; }; - location = Micheline.location ticketer_node; + location = Micheline.location parameters_node; parameters_ty = ticket_ty; parameters = ticket; }; diff --git a/src/proto_alpha/lib_protocol/script_ir_translator.ml b/src/proto_alpha/lib_protocol/script_ir_translator.ml index 38df9c7a993c..2e4fe659c7f7 100644 --- a/src/proto_alpha/lib_protocol/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/script_ir_translator.ml @@ -2342,7 +2342,14 @@ let parse_tx_rollup_deposit_parameters : ok @@ Tx_rollup_l2_qty.of_int64_exn (Z.to_int64 v) | Int (_, v) -> error @@ Tx_rollup_invalid_ticket_amount v | expr -> error @@ Invalid_kind (location expr, [Int_kind], kind expr)) - >|? fun amount -> + >>? fun amount -> + parse_address ctxt ticketer + >>? fun ({destination = ticketer; entrypoint = _}, ctxt) -> + (match ticketer with + | Destination.Contract c -> ok c + | Tx_rollup tx_rollup -> + error @@ Tx_rollup_errors.Deposit_wrong_ticketer tx_rollup) + >|? fun ticketer -> (Tx_rollup.{ticketer; contents; ty; amount; destination}, ctxt) | expr -> error @@ Invalid_kind (location expr, [Seq_kind], kind expr) diff --git a/src/proto_alpha/lib_protocol/tx_rollup_errors_repr.ml b/src/proto_alpha/lib_protocol/tx_rollup_errors_repr.ml index 1a157f83383d..142dbd26f8ec 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_errors_repr.ml +++ b/src/proto_alpha/lib_protocol/tx_rollup_errors_repr.ml @@ -77,6 +77,7 @@ type error += computed : Tx_rollup_commitment_repr.Message_result_hash.t; expected : Tx_rollup_commitment_repr.Message_result_hash.t; } + | Deposit_wrong_ticketer of Tx_rollup_repr.t let () = let open Data_encoding in @@ -510,4 +511,23 @@ let () = Some (provided, computed, expected) | _ -> None) (fun (provided, computed, expected) -> - Wrong_rejection_hashes {provided; computed; expected}) + Wrong_rejection_hashes {provided; computed; expected}) ; + (* Deposit_wrong_ticketer *) + register_error_kind + `Permanent + ~id:"tx_rollup_deposit_wrong_ticketer" + ~title: + "The ticketer submitted in the ticket is a tx rollup instead of a \ + contract." + ~description: + "The ticketer provided with the ticket on the deposit transaction is a \ + tx_rollup which is not possible." + ~pp:(fun ppf tx_rollup -> + Format.fprintf + ppf + "A tx_rollup (%a) can't be the ticketer of a ticket" + Tx_rollup_repr.pp + tx_rollup) + (obj1 (req "tx_rollup" Tx_rollup_repr.encoding)) + (function Deposit_wrong_ticketer tx_rollup -> Some tx_rollup | _ -> None) + (fun tx_rollup -> Deposit_wrong_ticketer tx_rollup) diff --git a/src/proto_alpha/lib_protocol/tx_rollup_repr.ml b/src/proto_alpha/lib_protocol/tx_rollup_repr.ml index 92f5d5ef7229..3e8b68527902 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_repr.ml +++ b/src/proto_alpha/lib_protocol/tx_rollup_repr.ml @@ -158,7 +158,7 @@ let deposit_entrypoint = Entrypoint_repr.of_string_strict_exn "deposit" type deposit_parameters = { contents : Script_repr.node; ty : Script_repr.node; - ticketer : Script_repr.node; + ticketer : Contract_repr.t; amount : Tx_rollup_l2_qty.t; destination : Tx_rollup_l2_address.Indexable.value; } diff --git a/src/proto_alpha/lib_protocol/tx_rollup_repr.mli b/src/proto_alpha/lib_protocol/tx_rollup_repr.mli index 6c7cb21a5f91..016dc0e0dbc4 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_repr.mli +++ b/src/proto_alpha/lib_protocol/tx_rollup_repr.mli @@ -89,7 +89,7 @@ val deposit_entrypoint : Entrypoint_repr.t type deposit_parameters = { contents : Script_repr.node; ty : Script_repr.node; - ticketer : Script_repr.node; + ticketer : Contract_repr.t; amount : Tx_rollup_l2_qty.t; destination : Tx_rollup_l2_address.Indexable.value; } -- GitLab From 4153039d330b0f0e85955c2edfce5463fbd33126 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 14 Mar 2022 16:40:25 +0100 Subject: [PATCH 3/5] proto/ticket table: use dest instead of contract --- .../michelson/test_ticket_accounting.ml | 47 +++++-- .../michelson/test_ticket_balance.ml | 4 +- .../michelson/test_ticket_balance_key.ml | 6 +- .../michelson/test_ticket_manager.ml | 2 +- .../michelson/test_ticket_operations_diff.ml | 121 ++++++++++++------ .../lib_protocol/ticket_accounting.ml | 2 +- .../lib_protocol/ticket_balance_key.ml | 1 - .../lib_protocol/ticket_balance_key.mli | 2 +- .../ticket_balance_migration_for_j.ml | 5 +- .../lib_protocol/ticket_operations_diff.ml | 18 +-- .../lib_protocol/ticket_operations_diff.mli | 2 +- .../lib_protocol/ticket_token_map.ml | 5 +- 12 files changed, 147 insertions(+), 68 deletions(-) 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 86c5c4002d72..3c925bc5b76f 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 @@ -930,7 +930,11 @@ let test_update_ticket_self_diff () = in (* After update, we should have 10 added red tokens. *) let* (red_self_token_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:self red_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract self) + red_token in assert_balance ~loc:__LOC__ ctxt red_self_token_hash (Some 10) @@ -986,7 +990,7 @@ let test_update_self_ticket_transfer () = wrap @@ Ticket_balance_key.ticket_balance_key ctxt - ~owner:ticket_receiver + ~owner:(Destination.Contract ticket_receiver) red_token in assert_balance ~loc:__LOC__ ctxt red_receiver_token_hash (Some 10) @@ -1011,11 +1015,18 @@ let test_update_valid_transfer () = let ctxt = Incremental.alpha_ctxt incr in let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:self red_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract self) + red_token in let* (red_receiver_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:destination red_token + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract destination) + red_token in (* Set up the balance so that the self contract owns one ticket. *) let* (_, ctxt) = @@ -1066,7 +1077,11 @@ let test_update_transfer_tickets_to_self () = let ctxt = Incremental.alpha_ctxt incr in let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:self red_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract self) + red_token in (* Set up the balance so that the self contract owns ten tickets. *) let* (_, ctxt) = @@ -1159,7 +1174,11 @@ let test_update_valid_origination () = let ctxt = Incremental.alpha_ctxt incr in let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:self red_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract self) + red_token in (* Set up the balance so that the self contract owns one ticket. *) let* (_, ctxt) = @@ -1187,7 +1206,10 @@ let test_update_valid_origination () = from [self] to [destination]. *) let* (red_originated_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:originated red_token + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract originated) + red_token in assert_balance ~loc:__LOC__ ctxt red_originated_token_hash (Some 1) @@ -1209,7 +1231,10 @@ let test_update_self_origination () = let* red_token = string_ticket_token ticketer "red" in let* (red_originated_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:originated red_token + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract originated) + red_token in let operation = origination_operation ~src:self ~orig_contract:originated ~script @@ -1254,7 +1279,11 @@ let test_ticket_token_map_of_list_with_duplicates () = in (* After update, we should have 10 + 5 added red tokens. *) let* (red_self_token_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner:self red_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract self) + red_token in assert_balance ~loc:__LOC__ ctxt red_self_token_hash (Some 15) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml index 92677278b315..1b669cba1af3 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml @@ -77,7 +77,9 @@ let get_balance ctxt ~token ~owner = let assert_token_balance ~loc block token owner expected = let* incr = Incremental.begin_construction block in let ctxt = Incremental.alpha_ctxt incr in - let* (balance, _) = get_balance ctxt ~token ~owner in + let* (balance, _) = + get_balance ctxt ~token ~owner:(Destination.Contract owner) + in match (balance, expected) with | (Some b, Some e) -> Assert.equal_int ~loc (Z.to_int b) e | (Some b, None) -> diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml index 297d76db8659..823a3ec83858 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml @@ -62,7 +62,11 @@ let make_key ctxt ~ticketer ~ty ~content ~owner = let* (ex_token, ctxt) = make_ex_token ctxt ~ticketer ~ty ~content in let* owner = make_contract owner in let* (key, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner ex_token + wrap + @@ Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract owner) + ex_token in return (key, ctxt) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml index a2e8b496c81a..7d58ad42830a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml @@ -102,7 +102,7 @@ let ticket_balance_of_storage ctxt contract = let* (key, ctxt) = Ticket_balance_key.ticket_balance_key ctxt - ~owner:contract + ~owner:(Contract contract) ex_token in let acc = (key, amount) :: acc in 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 04e7b5ca8f9d..8b3176e9f6a1 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 @@ -41,7 +41,7 @@ open Script_typed_ir type ticket_token_diff = { ticket_token : Ticket_token.ex_token; total_amount : Script_int.n Script_int.num; - destinations : (Contract.t * Script_int.n Script_int.num) list; + destinations : (Destination.t * Script_int.n Script_int.num) list; } let to_local_ticket_token_diff @@ -116,7 +116,7 @@ let string_of_ticket_token ctxt Michelson_v1_printer.print_expr (Micheline.strip_locations x) -let string_of_contract_and_amounts cas = +let string_of_destination_and_amounts cas = Format.asprintf "[%a]" (Format.pp_print_list @@ -125,7 +125,7 @@ let string_of_contract_and_amounts cas = Format.fprintf fmt {|("%a", %s)|} - Contract.pp + Destination.pp contract (Script_int.to_string amount))) cas @@ -133,7 +133,7 @@ let string_of_contract_and_amounts cas = let string_of_ticket_operations_diff ctxt {ticket_token; total_amount; destinations} = let* ticket_token = string_of_ticket_token ctxt ticket_token in - let destinations = string_of_contract_and_amounts destinations in + let destinations = string_of_destination_and_amounts destinations in return (Printf.sprintf "(%s, %s, %s)" @@ -153,7 +153,7 @@ let assert_equal_ticket_token_diffs ctxt ~loc ticket_diffs total_amount; destinations = List.sort - (fun (c1, _) (c2, _) -> Contract.compare c1 c2) + (fun (c1, _) (c2, _) -> Destination.compare c1 c2) destinations; }) ticket_diffs @@ -179,8 +179,9 @@ let string_token ~ticketer content = {ticketer; contents_type = Script_typed_ir.string_key; contents} (** Initializes one address for operations and one baker. *) -let init () = - Context.init ~consensus_threshold:0 2 >|=? fun (block, contracts) -> +let init ?tx_rollup_enable () = + Context.init ?tx_rollup_enable ~consensus_threshold:0 2 + >|=? fun (block, contracts) -> let (src0, src1) = match contracts with src0 :: src1 :: _ -> (src0, src1) | _ -> assert false in @@ -420,7 +421,7 @@ let test_transfer_one_ticket () = { ticket_token = string_token ~ticketer "white"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; ] @@ -459,17 +460,17 @@ let test_transfer_multiple_tickets () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 5; - destinations = [(orig_contract, nat 5)]; + destinations = [(Destination.Contract orig_contract, nat 5)]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 2; - destinations = [(orig_contract, nat 2)]; + destinations = [(Destination.Contract orig_contract, nat 2)]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 3; - destinations = [(orig_contract, nat 3)]; + destinations = [(Destination.Contract orig_contract, nat 3)]; }; ] @@ -513,32 +514,32 @@ let test_transfer_different_tickets () = { ticket_token = string_token ~ticketer:ticketer1 "red"; total_amount = nat 2; - destinations = [(destination, nat 2)]; + destinations = [(Destination.Contract destination, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer1 "green"; total_amount = nat 2; - destinations = [(destination, nat 2)]; + destinations = [(Destination.Contract destination, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer1 "blue"; total_amount = nat 2; - destinations = [(destination, nat 2)]; + destinations = [(Destination.Contract destination, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer2 "red"; total_amount = nat 1; - destinations = [(destination, nat 1)]; + destinations = [(Destination.Contract destination, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer2 "green"; total_amount = nat 1; - destinations = [(destination, nat 1)]; + destinations = [(Destination.Contract destination, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer2 "blue"; total_amount = nat 1; - destinations = [(destination, nat 1)]; + destinations = [(Destination.Contract destination, nat 1)]; }; ] @@ -586,17 +587,29 @@ let test_transfer_to_two_contracts_with_different_tickets () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 2; - destinations = [(destination2, nat 1); (destination1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract destination1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 2; - destinations = [(destination2, nat 1); (destination1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract destination1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 2; - destinations = [(destination2, nat 1); (destination1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract destination1, nat 1); + ]; }; ] @@ -657,7 +670,7 @@ let test_originate_with_one_ticket () = { ticket_token = string_token ~ticketer "white"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; ] @@ -698,17 +711,17 @@ let test_originate_with_multiple_tickets () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 5; - destinations = [(orig_contract, nat 5)]; + destinations = [(Destination.Contract orig_contract, nat 5)]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 2; - destinations = [(orig_contract, nat 2)]; + destinations = [(Destination.Contract orig_contract, nat 2)]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 3; - destinations = [(orig_contract, nat 3)]; + destinations = [(Destination.Contract orig_contract, nat 3)]; }; ] @@ -760,32 +773,32 @@ let test_originate_with_different_tickets () = { ticket_token = string_token ~ticketer:ticketer1 "red"; total_amount = nat 2; - destinations = [(orig_contract, nat 2)]; + destinations = [(Destination.Contract orig_contract, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer1 "green"; total_amount = nat 2; - destinations = [(orig_contract, nat 2)]; + destinations = [(Destination.Contract orig_contract, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer1 "blue"; total_amount = nat 2; - destinations = [(orig_contract, nat 2)]; + destinations = [(Destination.Contract orig_contract, nat 2)]; }; { ticket_token = string_token ~ticketer:ticketer2 "red"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer2 "green"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer2 "blue"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; ] @@ -832,17 +845,29 @@ let test_originate_two_contracts_with_different_tickets () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 2; - destinations = [(orig_contract2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract orig_contract2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 2; - destinations = [(orig_contract2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract orig_contract2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 2; - destinations = [(orig_contract2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract orig_contract2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; ] @@ -896,17 +921,29 @@ let test_originate_and_transfer () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 2; - destinations = [(destination2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 2; - destinations = [(destination2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 2; - destinations = [(destination2, nat 1); (orig_contract1, nat 1)]; + destinations = + [ + (Destination.Contract destination2, nat 1); + (Destination.Contract orig_contract1, nat 1); + ]; }; ] @@ -954,17 +991,17 @@ let test_originate_big_map_with_tickets () = { ticket_token = string_token ~ticketer "red"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer "green"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer "blue"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; ] @@ -1034,17 +1071,17 @@ let test_transfer_big_map_with_tickets () = { ticket_token = string_token ~ticketer:ticketer_contract "red"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer_contract "green"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; { ticket_token = string_token ~ticketer:ticketer_contract "blue"; total_amount = nat 1; - destinations = [(orig_contract, nat 1)]; + destinations = [(Destination.Contract orig_contract, nat 1)]; }; ] diff --git a/src/proto_alpha/lib_protocol/ticket_accounting.ml b/src/proto_alpha/lib_protocol/ticket_accounting.ml index d4609a8c2894..42907e175179 100644 --- a/src/proto_alpha/lib_protocol/ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/ticket_accounting.ml @@ -121,7 +121,7 @@ let update_ticket_balances_for_self_contract ctxt ~self ticket_diffs = ctxt ~total_storage_diff ticket_token - [(self, amount)]) + [(Destination.Contract self, amount)]) (Z.zero, ctxt) ticket_diffs diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.ml b/src/proto_alpha/lib_protocol/ticket_balance_key.ml index a7a74b4a1962..6e19c970e68c 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.ml @@ -48,7 +48,6 @@ let ticket_balance_key ctxt ~owner let ticketer_address = Script_typed_ir.{destination = ticketer; entrypoint = Entrypoint.default} in - let owner = Destination.Contract owner in let owner_address = Script_typed_ir.{destination = owner; entrypoint = Entrypoint.default} in diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.mli b/src/proto_alpha/lib_protocol/ticket_balance_key.mli index 1b0900673f4b..bdf2e33c4039 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.mli +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.mli @@ -32,6 +32,6 @@ given [owner] and [ex_token]. *) val ticket_balance_key : Alpha_context.context -> - owner:Alpha_context.Contract.t -> + owner:Alpha_context.Destination.t -> Ticket_token.ex_token -> (Alpha_context.Ticket_hash.t * Alpha_context.context) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml b/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml index 5f11b580516e..da687b4f480c 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml +++ b/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml @@ -57,7 +57,10 @@ let update_contract_tickets ctxt contract = has_tickets storage >>=? fun (tickets, ctxt) -> - List.fold_left_es (add_ticket_balance contract) ctxt tickets + List.fold_left_es + (add_ticket_balance (Destination.Contract contract)) + ctxt + tickets let is_originated contract = match Contract.is_originated contract with Some _ -> true | _ -> false diff --git a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml index 25810b64df27..29d5327e06b7 100644 --- a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml @@ -26,14 +26,14 @@ open Alpha_context type ticket_transfer = { - destination : Contract.t; + destination : Destination.t; tickets : Ticket_scanner.ex_ticket list; } type ticket_token_diff = { ticket_token : Ticket_token.ex_token; total_amount : Script_int.n Script_int.num; - destinations : (Contract.t * Script_int.n Script_int.num) list; + destinations : (Destination.t * Script_int.n Script_int.num) list; } type error += Failed_to_get_script of Contract.t | Contract_not_originated @@ -67,9 +67,9 @@ let () = (** A carbonated map where the keys are contracts. *) module Contract_map = Carbonated_map.Make (struct - type t = Contract.t + type t = Destination.t - let compare = Contract.compare + let compare = Destination.compare let compare_cost _ = Ticket_costs.Constants.cost_compare_key_contract end) @@ -103,7 +103,7 @@ module Ticket_token_map = struct - The internal contract-indexed map cannot be empty. *) - let add ctxt ~ticket_token ~destination ~amount map = + let add ctxt ~ticket_token ~(destination : Destination.t) ~amount map = Ticket_token_map.update ctxt ticket_token @@ -209,13 +209,14 @@ let tickets_of_transaction ctxt ~destination ~entrypoint ~location ctxt has_tickets parameters - >>=? fun (tickets, ctxt) -> return (Some {destination; tickets}, ctxt) + >>=? fun (tickets, ctxt) -> + return (Some {destination = Contract destination; tickets}, ctxt) (** Extract tickets of an origination operation by scanning the storage. *) let tickets_of_origination ctxt ~preorigination script = match preorigination with | None -> fail Contract_not_originated - | Some destination -> + | Some contract -> (* TODO: #2351 Avoid having to parse the script here. We're not able to rely on caching due to issues with lazy storage. @@ -247,7 +248,8 @@ let tickets_of_origination ctxt ~preorigination script = ~include_lazy:true has_tickets storage - >|=? fun (tickets, ctxt) -> (Some {tickets; destination}, ctxt) + >|=? fun (tickets, ctxt) -> + (Some {tickets; destination = Destination.Contract contract}, ctxt) (* TODO: #2352 Extend operations scanning to support rollup-operations once ready. diff --git a/src/proto_alpha/lib_protocol/ticket_operations_diff.mli b/src/proto_alpha/lib_protocol/ticket_operations_diff.mli index 0c984a7c34b2..b32b38b434db 100644 --- a/src/proto_alpha/lib_protocol/ticket_operations_diff.mli +++ b/src/proto_alpha/lib_protocol/ticket_operations_diff.mli @@ -36,7 +36,7 @@ type ticket_token_diff = private { ticket_token : Ticket_token.ex_token; total_amount : Alpha_context.Script_int.n Alpha_context.Script_int.num; destinations : - (Alpha_context.Contract.t + (Alpha_context.Destination.t * Alpha_context.Script_int.n Alpha_context.Script_int.num) list; } diff --git a/src/proto_alpha/lib_protocol/ticket_token_map.ml b/src/proto_alpha/lib_protocol/ticket_token_map.ml index 79f2198c49c0..737d3149adfa 100644 --- a/src/proto_alpha/lib_protocol/ticket_token_map.ml +++ b/src/proto_alpha/lib_protocol/ticket_token_map.ml @@ -46,7 +46,10 @@ let key_of_ticket_token ctxt (Ticket_token.Ex_token {ticketer; _} as token) = for comparing tokens. Since an owner contract is required we use [ticketer] but any dummy value would work as long as it's consistent. *) - Ticket_balance_key.ticket_balance_key ctxt ~owner:ticketer token + Ticket_balance_key.ticket_balance_key + ctxt + ~owner:(Destination.Contract ticketer) + token let update ctxt key f m = key_of_ticket_token ctxt key >>=? fun (key_hash, ctxt) -> -- GitLab From 981a2098f9bc3cac879e27508d1e70af296273b4 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 14 Mar 2022 17:36:11 +0100 Subject: [PATCH 4/5] proto: rename function --- .../michelson/test_ticket_accounting.ml | 18 +++++++++--------- .../michelson/test_ticket_balance.ml | 2 +- .../michelson/test_ticket_balance_key.ml | 2 +- .../michelson/test_ticket_manager.ml | 2 +- .../lib_protocol/ticket_accounting.ml | 2 +- .../lib_protocol/ticket_balance_key.mli | 12 +++++++----- .../ticket_balance_migration_for_j.ml | 2 +- .../lib_protocol/ticket_token_map.ml | 2 +- 8 files changed, 22 insertions(+), 20 deletions(-) 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 3c925bc5b76f..7dd3ede5a198 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 @@ -931,7 +931,7 @@ let test_update_ticket_self_diff () = (* After update, we should have 10 added red tokens. *) let* (red_self_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract self) red_token @@ -988,7 +988,7 @@ let test_update_self_ticket_transfer () = let* () = let* (red_receiver_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract ticket_receiver) red_token @@ -1016,14 +1016,14 @@ let test_update_valid_transfer () = let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract self) red_token in let* (red_receiver_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract destination) red_token @@ -1078,7 +1078,7 @@ let test_update_transfer_tickets_to_self () = let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract self) red_token @@ -1175,7 +1175,7 @@ let test_update_valid_origination () = let* red_token = string_ticket_token ticketer "red" in let* (red_self_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract self) red_token @@ -1206,7 +1206,7 @@ let test_update_valid_origination () = from [self] to [destination]. *) let* (red_originated_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract originated) red_token @@ -1231,7 +1231,7 @@ let test_update_self_origination () = let* red_token = string_ticket_token ticketer "red" in let* (red_originated_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract originated) red_token @@ -1280,7 +1280,7 @@ let test_ticket_token_map_of_list_with_duplicates () = (* After update, we should have 10 + 5 added red tokens. *) let* (red_self_token_hash, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract self) red_token diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml index 1b669cba1af3..31a9cc3f96a4 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance.ml @@ -70,7 +70,7 @@ let originate = Contract_helpers.originate_contract_from_string let get_balance ctxt ~token ~owner = let* (key_hash, ctxt) = - wrap @@ Ticket_balance_key.ticket_balance_key ctxt ~owner token + wrap @@ Ticket_balance_key.of_ex_token ctxt ~owner token in wrap (Ticket_balance.get_balance ctxt key_hash) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml index 823a3ec83858..d0a972d392e4 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml @@ -63,7 +63,7 @@ let make_key ctxt ~ticketer ~ty ~content ~owner = let* owner = make_contract owner in let* (key, ctxt) = wrap - @@ Ticket_balance_key.ticket_balance_key + @@ Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract owner) ex_token diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml index 7d58ad42830a..5a7995413508 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_manager.ml @@ -100,7 +100,7 @@ let ticket_balance_of_storage ctxt contract = @@ List.fold_left_es (fun (acc, ctxt) (ex_token, amount) -> let* (key, ctxt) = - Ticket_balance_key.ticket_balance_key + Ticket_balance_key.of_ex_token ctxt ~owner:(Contract contract) ex_token diff --git a/src/proto_alpha/lib_protocol/ticket_accounting.ml b/src/proto_alpha/lib_protocol/ticket_accounting.ml index 42907e175179..e04e6c5c19fb 100644 --- a/src/proto_alpha/lib_protocol/ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/ticket_accounting.ml @@ -88,7 +88,7 @@ let ticket_balances_of_value ctxt ~include_lazy ty value = let update_ticket_balances ctxt ~total_storage_diff token destinations = List.fold_left_es (fun (tot_storage_diff, ctxt) (owner, delta) -> - Ticket_balance_key.ticket_balance_key ctxt ~owner token + Ticket_balance_key.of_ex_token ctxt ~owner token >>=? fun (key_hash, ctxt) -> Ticket_balance.adjust_balance ctxt key_hash ~delta >>=? fun (storage_diff, ctxt) -> diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.mli b/src/proto_alpha/lib_protocol/ticket_balance_key.mli index bdf2e33c4039..f3160da53d1d 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.mli +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.mli @@ -23,15 +23,17 @@ (* *) (*****************************************************************************) +open Alpha_context + (** This module exposes a function for generating a ticket-balance key-hash given an owner and a ticket-token. The key-hash is used for populating the global ticket-balance table that tracks ownership of tickets for different tokens. *) -(** [ticket_balance_key ctxt ~owner ex_token] returns the [key_hash] of the +(** [of_ex_token ctxt ~owner ex_token] returns the [key_hash] of the given [owner] and [ex_token]. *) -val ticket_balance_key : - Alpha_context.context -> - owner:Alpha_context.Destination.t -> +val of_ex_token : + context -> + owner:Destination.t -> Ticket_token.ex_token -> - (Alpha_context.Ticket_hash.t * Alpha_context.context) tzresult Lwt.t + (Ticket_hash.t * context) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml b/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml index da687b4f480c..096d4efe96aa 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml +++ b/src/proto_alpha/lib_protocol/ticket_balance_migration_for_j.ml @@ -28,7 +28,7 @@ open Alpha_context (* In the ticket balance table, credit the ticket [ticket] to the owner [contract]. *) let add_ticket_balance contract ctxt ticket = let (token, amount) = Ticket_token.token_and_amount_of_ex_ticket ticket in - Ticket_balance_key.ticket_balance_key ctxt ~owner:contract token + Ticket_balance_key.of_ex_token ctxt ~owner:contract token >>=? fun (hash, ctxt) -> Ticket_balance.adjust_balance ctxt hash ~delta:(Script_int.to_zint amount) >|=? fun ((_added_size : Z.t), ctxt) -> ctxt diff --git a/src/proto_alpha/lib_protocol/ticket_token_map.ml b/src/proto_alpha/lib_protocol/ticket_token_map.ml index 737d3149adfa..fd66f169381b 100644 --- a/src/proto_alpha/lib_protocol/ticket_token_map.ml +++ b/src/proto_alpha/lib_protocol/ticket_token_map.ml @@ -46,7 +46,7 @@ let key_of_ticket_token ctxt (Ticket_token.Ex_token {ticketer; _} as token) = for comparing tokens. Since an owner contract is required we use [ticketer] but any dummy value would work as long as it's consistent. *) - Ticket_balance_key.ticket_balance_key + Ticket_balance_key.of_ex_token ctxt ~owner:(Destination.Contract ticketer) token -- GitLab From e3eef8894aa9ca5f2aab969f170d97390e76abaa Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 14 Mar 2022 17:38:18 +0100 Subject: [PATCH 5/5] proto: remove Tx_rollup.hash_ticket --- src/proto_alpha/lib_protocol/alpha_context.ml | 21 +- .../lib_protocol/alpha_context.mli | 25 --- src/proto_alpha/lib_protocol/apply.ml | 20 +- .../integration/operations/test_tx_rollup.ml | 205 ++++++++++++------ .../lib_protocol/ticket_balance_key.ml | 60 ++--- .../lib_protocol/ticket_balance_key.mli | 10 + 6 files changed, 192 insertions(+), 149 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 045175199711..35f01f86d599 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -244,26 +244,7 @@ module Tx_rollup_message_result_hash = module Tx_rollup = struct include Tx_rollup_repr include Tx_rollup_storage - - let hash_ticket ctxt tx_rollup ~contents ~ticketer ~ty = - let open Micheline in - let owner = String (dummy_location, to_b58check tx_rollup) in - let ticketer = String (dummy_location, Contract.to_b58check ticketer) in - Ticket_hash_builder.make ctxt ~ticketer ~ty ~contents ~owner - - module Internal_for_tests = struct - include Tx_rollup_repr - - let hash_ticket_uncarbonated tx_rollup ~contents ~ticketer ~ty = - let open Micheline in - let owner = String (dummy_location, to_b58check tx_rollup) in - let ticketer = String (dummy_location, Contract.to_b58check ticketer) in - Ticket_hash_builder.Internal_for_tests.make_uncarbonated - ~ticketer - ~ty - ~contents - ~owner - end + module Internal_for_tests = Tx_rollup_repr end module Tx_rollup_state = struct diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 78865d62d05b..cac45477ced4 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1643,23 +1643,6 @@ module Tx_rollup : sig destination : Tx_rollup_l2_address.Indexable.value; } - (** [hash_ticket ctxt tx_rollup ~contents ~ticketer ~ty] computes the - hash of the ticket of type [ty ticket], of content [contents] and - of ticketer [ticketer]. - - The goal of the computed hash is twofold: - - {ul {li Identifying the ticket in the layer-2, and} - {li Registering in the table of tickets that [tx_rollup] - owns this ticket.}} *) - val hash_ticket : - context -> - t -> - contents:Script.node -> - ticketer:Contract.t -> - ty:Script.node -> - (Ticket_hash.t * context) tzresult - val originate : context -> (context * tx_rollup) tzresult Lwt.t module Set : Set.S with type elt = tx_rollup @@ -1668,14 +1651,6 @@ module Tx_rollup : sig (** see [tx_rollup_repr.originated_tx_rollup] for documentation *) val originated_tx_rollup : Origination_nonce.Internal_for_tests.t -> tx_rollup - - (** same as [hash_ticket] but uncarbonated *) - val hash_ticket_uncarbonated : - t -> - contents:Script.node -> - ticketer:Contract.t -> - ty:Script.node -> - Ticket_hash.t tzresult end end diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 7f34645568cb..ac33a92369dc 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1057,8 +1057,13 @@ let apply_transaction_to_rollup ~consume_deserialization_gas ~ctxt ~parameters >>?= fun (parameters, ctxt) -> Script_ir_translator.parse_tx_rollup_deposit_parameters ctxt parameters >>?= fun (Tx_rollup.{ticketer; contents; ty; amount; destination}, ctxt) -> - Tx_rollup.hash_ticket ctxt dst_rollup ~contents ~ticketer ~ty - >>?= fun (ticket_hash, ctxt) -> + Ticket_balance_key.of_script_node + ctxt + ~owner:(Tx_rollup dst_rollup) + ~ticketer + ~ty + ~contents + >>=? fun (ticket_hash, ctxt) -> (* If the ticket deposit fails on L2 for some reason (e.g. [Balance_overflow] in the recipient), then it is returned to [payer]. As [payer] is implicit, it cannot own @@ -1392,13 +1397,13 @@ let apply_external_manager_operation_content : >>?= fun (ty, ctxt) -> Script.force_decode_in_context ~consume_deserialization_gas ctxt contents >>?= fun (contents, ctxt) -> - Tx_rollup.hash_ticket + Ticket_balance_key.of_script_node ctxt - tx_rollup - ~contents:(Micheline.root contents) + ~owner:(Tx_rollup tx_rollup) ~ticketer + ~contents:(Micheline.root contents) ~ty:(Micheline.root ty) - >>?= fun (ticket_hash, ctxt) -> + >>=? fun (tx_rollup_ticket_hash, ctxt) -> (* Checking the operation is non-internal *) Option.value_e ~error: @@ -1408,7 +1413,8 @@ let apply_external_manager_operation_content : >>?= fun source_pkh -> (* Computing the withdrawal hash *) let withdrawal = - Tx_rollup_withdraw.{claimer = source_pkh; ticket_hash; amount} + Tx_rollup_withdraw. + {claimer = source_pkh; ticket_hash = tx_rollup_ticket_hash; amount} in let (withdrawals_merkle_root, withdraw_index) = Tx_rollup_withdraw.check_path withdraw_path withdrawal 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 73ad78a4be6f..7f205efa6fbe 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 @@ -190,29 +190,30 @@ let is_implicit_exn x = (** [make_ticket_key ty contents ticketer tx_rollup] computes the ticket hash of the ticket containing [contents] of type [ty], crafted by [ticketer] and owned by [tx_rollup]. *) -let make_ticket_key ~ty ~contents ~ticketer tx_rollup = - let open Tezos_micheline.Micheline in - let ticketer = - Bytes (0, Data_encoding.Binary.to_bytes_exn Contract.encoding ticketer) - in - match - Alpha_context.Tx_rollup.Internal_for_tests.hash_ticket_uncarbonated - ~ticketer - ~ty - ~contents - tx_rollup - with - | Ok x -> x - | Error _ -> raise (Invalid_argument "make_ticket_key") +let make_ticket_key ctxt ~ty ~contents ~ticketer tx_rollup = + (match ctxt with + | Context.B block -> + Incremental.begin_construction block >>=? fun incr -> return incr + | Context.I incr -> return incr) + >>=? fun incr -> + let ctxt = Incremental.alpha_ctxt incr in + wrap + @@ Ticket_balance_key.of_script_node + ctxt + ~owner:(Tx_rollup tx_rollup) + ~ticketer + ~ty + ~contents + >|=? fst (** [make_unit_ticket_key ticketer tx_rollup] computes the ticket hash of the unit ticket crafted by [ticketer] and owned by [tx_rollup]. *) -let make_unit_ticket_key ~ticketer tx_rollup = +let make_unit_ticket_key ctxt ~ticketer tx_rollup = let open Tezos_micheline.Micheline in let open Michelson_v1_primitives in let ty = Prim (0, T_unit, [], []) in let contents = Prim (0, D_Unit, [], []) in - make_ticket_key ~ty ~contents ~ticketer tx_rollup + make_ticket_key ctxt ~ty ~contents ~ticketer tx_rollup let rng_state = Random.State.make_self_init () @@ -702,7 +703,8 @@ let test_valid_deposit () = Incremental.begin_construction b >|=? Incremental.alpha_ctxt >>=? fun ctxt -> Context.Tx_rollup.inbox (B b) tx_rollup Tx_rollup_level.root >>=? function | {contents = [hash]; _} -> - let ticket_hash = make_unit_ticket_key ~ticketer:contract tx_rollup in + make_unit_ticket_key (B b) ~ticketer:contract tx_rollup + >>=? fun ticket_hash -> let (message, _size) = Tx_rollup_message.make_deposit (is_implicit_exn account) @@ -1624,20 +1626,19 @@ module Withdraw = struct let amount = Tx_rollup_l2_qty.of_int64_exn 10L - let ticket_hash ~ticketer ~tx_rollup = + let ticket_hash ctxt ~ticketer ~tx_rollup = make_ticket_key + ctxt ~ty:(Tezos_micheline.Micheline.root ty) ~contents:(Tezos_micheline.Micheline.root contents) ~ticketer tx_rollup - let withdrawal ~ticketer ?(recipient = ticketer) tx_rollup : - Tx_rollup_withdraw.t = - { - claimer = is_implicit_exn recipient; - ticket_hash = ticket_hash ~ticketer ~tx_rollup; - amount; - } + let withdrawal ctxt ~ticketer ?(claimer = ticketer) tx_rollup : + Tx_rollup_withdraw.t tzresult Lwt.t = + ticket_hash ctxt ~ticketer ~tx_rollup >|=? fun ticket_hash -> + Tx_rollup_withdraw. + {claimer = is_implicit_exn claimer; ticket_hash; amount} end (** [test_valid_withdraw] checks that a smart contract can deposit tickets to a @@ -1667,8 +1668,14 @@ module Withdraw = struct *) (* 1. Create a ticket and it's withdrawal *) - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in - + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* wrong ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> (* 2 Add a batch message to [b], a commitment for that inbox containing the withdrawal at index 0, and finalize that commitment *) @@ -1696,7 +1703,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -1720,7 +1727,9 @@ module Withdraw = struct "(Some (Pair 0x%s (Pair %d %s)))" (Hex.show (Hex.of_string - (Data_encoding.Binary.to_string_exn Contract.encoding account1))) + (Data_encoding.Binary.to_string_exn + Contract.encoding + withdraw_contract))) Nat_ticket.contents_nat (Tx_rollup_l2_qty.to_string Nat_ticket.amount) |> Expr.from_string |> Option.some @@ -1757,7 +1766,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract dummy_withdraw_proof @@ -1784,7 +1793,14 @@ module Withdraw = struct Op.tx_rollup_submit_batch (B b) account1 tx_rollup batch >>=? fun operation -> Block.bake ~operation b >>=? fun b -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -1806,7 +1822,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_path @@ -1828,8 +1844,14 @@ module Withdraw = struct Op.tx_rollup_submit_batch (B b) account1 tx_rollup batch >>=? fun operation -> Block.bake ~operation b >>=? fun b -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in - + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -1856,7 +1878,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract amount ~destination:withdraw_contract withdraw_path @@ -1881,7 +1903,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr @@ Expr.from_string "unit") - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_path @@ -1903,7 +1925,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr @@ Expr.from_string "2") ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_path @@ -1925,7 +1947,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:withdraw_contract + ~ticketer:account1 Nat_ticket.amount ~destination:withdraw_contract withdraw_path @@ -1946,9 +1968,14 @@ module Withdraw = struct Op.tx_rollup_submit_batch (B b) account1 tx_rollup batch >>=? fun operation -> Block.bake ~operation b >>=? fun b -> - let withdrawal1 : Tx_rollup_withdraw.t = - Nat_ticket.withdrawal ~ticketer:account1 tx_rollup - in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdrawal1 -> let withdrawal2 : Tx_rollup_withdraw.t = {withdrawal1 with amount = Tx_rollup_l2_qty.of_int64_exn 5L} in @@ -1978,7 +2005,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract invalid_withdraw_path @@ -2003,7 +2030,7 @@ module Withdraw = struct ~message_index:0 ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract invalid_withdraw_path @@ -2020,7 +2047,14 @@ module Withdraw = struct let test_invalid_withdraw_already_consumed () = context_init1_withdraw () >>=? fun (account1, tx_rollup, withdraw_contract, b) -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2041,7 +2075,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2059,7 +2093,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2079,12 +2113,14 @@ module Withdraw = struct let test_invalid_withdraw_someone_elses () = context_init2_withdraw () >>=? fun (account1, account2, tx_rollup, withdraw_contract, b) -> - let withdraw = - Nat_ticket.withdrawal - ~ticketer:account1 (* Explicit for clarity *) - ~recipient:account1 - tx_rollup - in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2107,7 +2143,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2133,7 +2169,14 @@ module Withdraw = struct b (is_implicit_exn account1) >>=? fun (withdraw_contract_unit_tickets, b) -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract_unit_tickets + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2154,7 +2197,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract_unit_tickets Nat_ticket.amount ~destination:withdraw_contract_unit_tickets withdraw_proof @@ -2176,7 +2219,14 @@ module Withdraw = struct let test_invalid_withdraw_bad_entrypoint () = context_init1_withdraw () >>=? fun (account1, tx_rollup, withdraw_contract, b) -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2197,7 +2247,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2227,14 +2277,13 @@ module Withdraw = struct let contents_nat = 1 in let contents = Expr.from_string (string_of_int contents_nat) in let amount = Tx_rollup_l2_qty.of_int64_exn 10L in - let ticket_hash = - make_ticket_key - ~ty:(Tezos_micheline.Micheline.root ty) - ~contents:(Tezos_micheline.Micheline.root contents) - ~ticketer:account1 - tx_rollup - in - + make_ticket_key + (B b) + ~ty:(Tezos_micheline.Micheline.root ty) + ~contents:(Tezos_micheline.Micheline.root contents) + ~ticketer:withdraw_contract + tx_rollup + >>=? fun ticket_hash -> (* 2.2 Create a withdrawal for the ticket *) let withdraw : Tx_rollup_withdraw.t = {claimer = is_implicit_exn account1; ticket_hash; amount} @@ -2272,7 +2321,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr contents) ~ty:(Script.lazy_expr ty) - ~ticketer:account1 + ~ticketer:withdraw_contract amount ~destination:withdraw_contract withdraw_proof @@ -2292,7 +2341,14 @@ module Withdraw = struct let test_too_late_withdrawal () = context_init1_withdraw () >>=? fun (account1, tx_rollup, withdraw_contract, b) -> - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2317,7 +2373,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2359,7 +2415,14 @@ module Withdraw = struct return_unit in - let withdraw = Nat_ticket.withdrawal ~ticketer:account1 tx_rollup in + Nat_ticket.withdrawal + (B b) + ~ticketer: + withdraw_contract + (* impossible ticketer but we forge the ticket here, this will fail when ticket table*) + ~claimer:account1 + tx_rollup + >>=? fun withdraw -> context_finalize_batch_with_withdrawals ~account:account1 ~tx_rollup @@ -2382,7 +2445,7 @@ module Withdraw = struct ~context_hash ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:account1 + ~ticketer:withdraw_contract Nat_ticket.amount ~destination:withdraw_contract withdraw_proof @@ -2404,7 +2467,7 @@ module Withdraw = struct let tests = [ - Tztest.tztest "Test withdraw" `Quick test_valid_withdraw; + Tztest.tztest "Test valid withdraw" `Quick test_valid_withdraw; Tztest.tztest "Test withdraw w/ missing commitment" `Quick diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.ml b/src/proto_alpha/lib_protocol/ticket_balance_key.ml index 6e19c970e68c..30775b0d7cb3 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.ml @@ -26,27 +26,15 @@ open Alpha_context -(* This function extracts nodes of: - - Ticketer - - Type of content - - Content - - Owner - to generate at ticket-balance key-hash. +(* This function extracts nodes only of: + - Ticketer + - Owner + to generate at ticket-balance key-hash. *) -let ticket_balance_key ctxt ~owner - (Ticket_token.Ex_token {ticketer; contents_type; contents}) = - let loc = Micheline.dummy_location in - Script_ir_translator.unparse_comparable_ty ~loc ctxt contents_type - >>?= fun (cont_ty_unstripped, ctxt) -> - (* We strip the annotations from the content type in order to map - tickets with the same content type, but with different annotations, to the - same hash. *) - Gas.consume ctxt (Script.strip_annotations_cost cont_ty_unstripped) - >>?= fun ctxt -> - let ty = Script.strip_annotations cont_ty_unstripped in - let ticketer = Destination.Contract ticketer in +let of_script_node ctxt ~owner ~ticketer ~ty ~contents = let ticketer_address = - Script_typed_ir.{destination = ticketer; entrypoint = Entrypoint.default} + Script_typed_ir. + {destination = Contract ticketer; entrypoint = Entrypoint.default} in let owner_address = Script_typed_ir.{destination = owner; entrypoint = Entrypoint.default} @@ -57,13 +45,6 @@ let ticket_balance_key ctxt ~owner Script_typed_ir.address_t ticketer_address >>=? fun (ticketer, ctxt) -> - Script_ir_translator.unparse_comparable_data - ~loc - ctxt - Script_ir_translator.Optimized_legacy - contents_type - contents - >>=? fun (contents, ctxt) -> Script_ir_translator.unparse_data ctxt Script_ir_translator.Optimized_legacy @@ -71,3 +52,30 @@ let ticket_balance_key ctxt ~owner owner_address >>=? fun (owner, ctxt) -> Lwt.return (Ticket_hash.make ctxt ~ticketer ~ty ~contents ~owner) + +(* This function extracts nodes of: + - Ticketer + - Type of content + - Content + - Owner + to generate at ticket-balance key-hash. +*) +let of_ex_token ctxt ~owner + (Ticket_token.Ex_token {ticketer; contents_type; contents}) = + let loc = Micheline.dummy_location in + Script_ir_translator.unparse_comparable_ty ~loc ctxt contents_type + >>?= fun (cont_ty_unstripped, ctxt) -> + (* We strip the annotations from the content type in order to map + tickets with the same content type, but with different annotations, to the + same hash. *) + Gas.consume ctxt (Script.strip_annotations_cost cont_ty_unstripped) + >>?= fun ctxt -> + let ty = Script.strip_annotations cont_ty_unstripped in + Script_ir_translator.unparse_comparable_data + ~loc + ctxt + Script_ir_translator.Optimized_legacy + contents_type + contents + >>=? fun (contents, ctxt) -> + of_script_node ctxt ~owner ~ticketer ~ty ~contents diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.mli b/src/proto_alpha/lib_protocol/ticket_balance_key.mli index f3160da53d1d..d8e7641d58be 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.mli +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.mli @@ -30,6 +30,16 @@ open Alpha_context global ticket-balance table that tracks ownership of tickets for different tokens. *) +(** [of_script_node ctxt ~owner ~ticketer ~ty ~contents] returns the [key_hash] of the + given [owner], [ticketer], [ty] and [contents] that describe a ticket. *) +val of_script_node : + context -> + owner:Destination.t -> + ticketer:Contract.t -> + ty:Script.node -> + contents:Script.node -> + (Ticket_hash.t * context) tzresult Lwt.t + (** [of_ex_token ctxt ~owner ex_token] returns the [key_hash] of the given [owner] and [ex_token]. *) val of_ex_token : -- GitLab