From 2a37e8856c3786565337ae1aae4db097910e766e Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 01:54:59 +0900 Subject: [PATCH 1/9] Proto: Move ticket to token conversion function This change is required to avoid circular dependency in the next commit. --- src/proto_alpha/lib_protocol/apply.ml | 2 +- .../test/integration/michelson/test_ticket_manager.ml | 4 +++- src/proto_alpha/lib_protocol/ticket_accounting.ml | 4 +++- src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml | 4 +++- src/proto_alpha/lib_protocol/ticket_operations_diff.ml | 2 +- src/proto_alpha/lib_protocol/ticket_scanner.ml | 4 ++++ src/proto_alpha/lib_protocol/ticket_scanner.mli | 5 +++++ src/proto_alpha/lib_protocol/ticket_token.ml | 4 ---- src/proto_alpha/lib_protocol/ticket_token.mli | 4 ---- src/proto_alpha/lib_protocol/zk_rollup_apply.ml | 2 +- 10 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 71cc2bda7be3..2051f19cbe62 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -452,7 +452,7 @@ let apply_transaction_to_tx_rollup ~ctxt ~parameters_ty ~parameters ~payer {payload_size = ticket_size; limit}) >>=? fun () -> let ex_token, ticket_amount = - Ticket_token.token_and_amount_of_ex_ticket ex_ticket + Ticket_scanner.ex_token_and_amount_of_ex_ticket ex_ticket in Ticket_balance_key.of_ex_token ctxt ~owner:(Tx_rollup dst_rollup) ex_token >>=? fun (ticket_hash, 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 f8984e995394..711a6974ec7a 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 @@ -56,7 +56,9 @@ let init_env () = let collect_token_amounts ctxt tickets = let accum (tokens, ctxt) ticket = - let token, amount = Ticket_token.token_and_amount_of_ex_ticket ticket in + let token, amount = + Ticket_scanner.ex_token_and_amount_of_ex_ticket ticket + in let tokens = (token, Script_int.(to_zint (amount :> n num))) :: tokens in return (tokens, ctxt) in diff --git a/src/proto_alpha/lib_protocol/ticket_accounting.ml b/src/proto_alpha/lib_protocol/ticket_accounting.ml index 33d7aeaff594..3019d07dca03 100644 --- a/src/proto_alpha/lib_protocol/ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/ticket_accounting.ml @@ -78,7 +78,9 @@ let ticket_balances_of_value ctxt ~include_lazy ty value = >>=? fun (tickets, ctxt) -> List.fold_left_e (fun (acc, ctxt) ticket -> - let token, amount = Ticket_token.token_and_amount_of_ex_ticket ticket in + let token, amount = + Ticket_scanner.ex_token_and_amount_of_ex_ticket ticket + in Gas.consume ctxt Ticket_costs.Constants.cost_collect_tickets_step >|? fun ctxt -> ( (token, Script_int.to_zint (amount :> Script_int.n Script_int.num)) diff --git a/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml b/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml index abc4fb54e37f..cb4005939c5f 100644 --- a/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml @@ -51,7 +51,9 @@ let () = let token_and_amount ctxt ex_ticket = Gas.consume ctxt Ticket_costs.Constants.cost_collect_tickets_step >|? fun ctxt -> - let token, amount = Ticket_token.token_and_amount_of_ex_ticket ex_ticket in + let token, amount = + Ticket_scanner.ex_token_and_amount_of_ex_ticket ex_ticket + in ((token, Script_int.(to_zint (amount :> n num))), ctxt) (** Extracts the ticket-token and amount from an ex_ticket value and returns diff --git a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml index 01f67f8fea7b..e4fff65f597f 100644 --- a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml @@ -239,7 +239,7 @@ let add_transfer_to_token_map ctxt token_map {destination; tickets} = List.fold_left_es (fun (token_map, ctxt) ticket -> let ticket_token, amount = - Ticket_token.token_and_amount_of_ex_ticket ticket + Ticket_scanner.ex_token_and_amount_of_ex_ticket ticket in Ticket_token_map.add ctxt ~ticket_token ~destination ~amount token_map) (token_map, ctxt) diff --git a/src/proto_alpha/lib_protocol/ticket_scanner.ml b/src/proto_alpha/lib_protocol/ticket_scanner.ml index ec48d66213db..04075d107f45 100644 --- a/src/proto_alpha/lib_protocol/ticket_scanner.ml +++ b/src/proto_alpha/lib_protocol/ticket_scanner.ml @@ -557,3 +557,7 @@ let ex_ticket_size ctxt (Ex_ticket (ty, ticket)) = Gas.consume ctxt val_size_cost >>?= fun ctxt -> (* gas *) return (Saturation_repr.add ty_size val_size, ctxt) + +let ex_token_and_amount_of_ex_ticket + (Ex_ticket (contents_type, {Script_typed_ir.ticketer; contents; amount})) = + (Ticket_token.Ex_token {ticketer; contents_type; contents}, amount) diff --git a/src/proto_alpha/lib_protocol/ticket_scanner.mli b/src/proto_alpha/lib_protocol/ticket_scanner.mli index c76c2830fe4b..90521e3f3e1d 100644 --- a/src/proto_alpha/lib_protocol/ticket_scanner.mli +++ b/src/proto_alpha/lib_protocol/ticket_scanner.mli @@ -99,3 +99,8 @@ val ex_ticket_size : (Saturation_repr.may_saturate Saturation_repr.t * Alpha_context.context) tzresult Lwt.t + +(** [ex_token_and_amount_of_ex_ticket ex_ticket] returns the token and amount of + the given ticket [ex_ticket]. *) +val ex_token_and_amount_of_ex_ticket : + ex_ticket -> Ticket_token.ex_token * Script_typed_ir.ticket_amount diff --git a/src/proto_alpha/lib_protocol/ticket_token.ml b/src/proto_alpha/lib_protocol/ticket_token.ml index 2e495f30acf6..536e362bdada 100644 --- a/src/proto_alpha/lib_protocol/ticket_token.ml +++ b/src/proto_alpha/lib_protocol/ticket_token.ml @@ -33,7 +33,3 @@ type ex_token = } -> ex_token -let token_and_amount_of_ex_ticket - (Ticket_scanner.Ex_ticket - (contents_type, {Script_typed_ir.ticketer; contents; amount})) = - (Ex_token {ticketer; contents_type; contents}, amount) diff --git a/src/proto_alpha/lib_protocol/ticket_token.mli b/src/proto_alpha/lib_protocol/ticket_token.mli index 6a4c655ac189..ebcbd435be1b 100644 --- a/src/proto_alpha/lib_protocol/ticket_token.mli +++ b/src/proto_alpha/lib_protocol/ticket_token.mli @@ -40,7 +40,3 @@ type ex_token = } -> ex_token -(** [token_and_amount_of_ex_ticket ex_ticket] returns the token and amount of - the given ticket [ex_ticket]. *) -val token_and_amount_of_ex_ticket : - Ticket_scanner.ex_ticket -> ex_token * Script_typed_ir.ticket_amount diff --git a/src/proto_alpha/lib_protocol/zk_rollup_apply.ml b/src/proto_alpha/lib_protocol/zk_rollup_apply.ml index c5b9e070db70..c2ab9534c372 100644 --- a/src/proto_alpha/lib_protocol/zk_rollup_apply.ml +++ b/src/proto_alpha/lib_protocol/zk_rollup_apply.ml @@ -188,7 +188,7 @@ let transaction_to_zk_rollup ~ctxt ~parameters_ty ~parameters ~dst_rollup ~since {payload_size = ticket_size; limit}) in let ex_token, ticket_amount = - Ticket_token.token_and_amount_of_ex_ticket ex_ticket + Ticket_scanner.ex_token_and_amount_of_ex_ticket ex_ticket in (* Compute the ticket hash with zk rollup as owner *) let* ticket_hash, ctxt = -- GitLab From fe27a8a50c526477c7f34aca27a03f9ca3faeb0f Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 10:53:44 +0900 Subject: [PATCH 2/9] Proto: Move ticket receipt to above alpha context There is no need for ticket receipt to live below the alpha context. This change is needed to avoid circular dependency in the next commit. --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 4 ++-- src/proto_alpha/lib_protocol/alpha_context.ml | 4 ---- .../lib_protocol/alpha_context.mli | 19 ------------------- src/proto_alpha/lib_protocol/dune | 17 +++++++++-------- ...cket_receipt_repr.ml => ticket_receipt.ml} | 18 ++++++++++-------- ...et_receipt_repr.mli => ticket_receipt.mli} | 10 ++++++---- 6 files changed, 27 insertions(+), 45 deletions(-) rename src/proto_alpha/lib_protocol/{ticket_receipt_repr.ml => ticket_receipt.ml} (88%) rename src/proto_alpha/lib_protocol/{ticket_receipt_repr.mli => ticket_receipt.mli} (94%) diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 35182bbcdc57..a6fbc9a7f5b1 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -106,7 +106,6 @@ "Migration_repr", "Carbonated_map_costs", "Carbonated_map", - "Ticket_receipt_repr", "Raw_context_intf", "Raw_context", @@ -204,6 +203,8 @@ "Michelson_v1_gas", "Script_list", "Script_tc_context", + "Ticket_token", + "Ticket_receipt", "Apply_operation_result", "Apply_internal_results", "Apply_results", @@ -215,7 +216,6 @@ "Script_tc_errors_registration", "Ticket_costs", "Ticket_scanner", - "Ticket_token", "Ticket_balance_key", "Ticket_lazy_storage_diff", diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 032c6b2ffc2c..1edf9aa6825f 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -609,10 +609,6 @@ module Ticket_balance = struct include Ticket_storage end -module Ticket_receipt = struct - include Ticket_receipt_repr -end - module Token = Token module Cache = Cache_repr diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 9a3e7fafaf5b..c0496c62d1bd 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4910,25 +4910,6 @@ module Ticket_balance : sig end end -(** This module re-exports definitions from {!Ticket_receipt_repr}. *) -module Ticket_receipt : sig - type update = {account : Destination.t; amount : Z.t} - - type ticket_token = { - ticketer : Contract.t; - contents_type : Script.expr; - contents : Script.expr; - } - - type item = {ticket_token : ticket_token; updates : update list} - - type t = item list - - val item_encoding : item Data_encoding.t - - val encoding : t Data_encoding.t -end - module First_level_of_protocol : sig (** Get the level of the first block of this protocol. *) val get : context -> Raw_level.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index b53da12c6614..28834a1c4a19 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -132,7 +132,6 @@ Migration_repr Carbonated_map_costs Carbonated_map - Ticket_receipt_repr Raw_context_intf Raw_context Storage_costs @@ -218,6 +217,8 @@ Michelson_v1_gas Script_list Script_tc_context + Ticket_token + Ticket_receipt Apply_operation_result Apply_internal_results Apply_results @@ -229,7 +230,6 @@ Script_tc_errors_registration Ticket_costs Ticket_scanner - Ticket_token Ticket_balance_key Ticket_lazy_storage_diff Tx_rollup_parameters @@ -398,7 +398,6 @@ migration_repr.ml migration_repr.mli carbonated_map_costs.ml carbonated_map_costs.mli carbonated_map.ml carbonated_map.mli - ticket_receipt_repr.ml ticket_receipt_repr.mli raw_context_intf.ml raw_context.ml raw_context.mli storage_costs.ml storage_costs.mli @@ -485,6 +484,8 @@ michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli + ticket_token.ml ticket_token.mli + ticket_receipt.ml ticket_receipt.mli apply_operation_result.ml apply_operation_result.mli apply_internal_results.ml apply_internal_results.mli apply_results.ml apply_results.mli @@ -496,7 +497,6 @@ script_tc_errors_registration.ml script_tc_errors_registration.mli ticket_costs.ml ticket_costs.mli ticket_scanner.ml ticket_scanner.mli - ticket_token.ml ticket_token.mli ticket_balance_key.ml ticket_balance_key.mli ticket_lazy_storage_diff.ml ticket_lazy_storage_diff.mli tx_rollup_parameters.ml tx_rollup_parameters.mli @@ -524,6 +524,7 @@ contract_services.ml contract_services.mli delegate_services.ml delegate_services.mli voting_services.ml voting_services.mli + ticket_services.ml ticket_services.mli tx_rollup_services.ml tx_rollup_services.mli alpha_services.ml alpha_services.mli main.ml main.mli @@ -645,7 +646,6 @@ migration_repr.ml migration_repr.mli carbonated_map_costs.ml carbonated_map_costs.mli carbonated_map.ml carbonated_map.mli - ticket_receipt_repr.ml ticket_receipt_repr.mli raw_context_intf.ml raw_context.ml raw_context.mli storage_costs.ml storage_costs.mli @@ -732,6 +732,8 @@ michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli + ticket_token.ml ticket_token.mli + ticket_receipt.ml ticket_receipt.mli apply_operation_result.ml apply_operation_result.mli apply_internal_results.ml apply_internal_results.mli apply_results.ml apply_results.mli @@ -743,7 +745,6 @@ script_tc_errors_registration.ml script_tc_errors_registration.mli ticket_costs.ml ticket_costs.mli ticket_scanner.ml ticket_scanner.mli - ticket_token.ml ticket_token.mli ticket_balance_key.ml ticket_balance_key.mli ticket_lazy_storage_diff.ml ticket_lazy_storage_diff.mli tx_rollup_parameters.ml tx_rollup_parameters.mli @@ -897,7 +898,6 @@ migration_repr.ml migration_repr.mli carbonated_map_costs.ml carbonated_map_costs.mli carbonated_map.ml carbonated_map.mli - ticket_receipt_repr.ml ticket_receipt_repr.mli raw_context_intf.ml raw_context.ml raw_context.mli storage_costs.ml storage_costs.mli @@ -984,6 +984,8 @@ michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli + ticket_token.ml ticket_token.mli + ticket_receipt.ml ticket_receipt.mli apply_operation_result.ml apply_operation_result.mli apply_internal_results.ml apply_internal_results.mli apply_results.ml apply_results.mli @@ -995,7 +997,6 @@ script_tc_errors_registration.ml script_tc_errors_registration.mli ticket_costs.ml ticket_costs.mli ticket_scanner.ml ticket_scanner.mli - ticket_token.ml ticket_token.mli ticket_balance_key.ml ticket_balance_key.mli ticket_lazy_storage_diff.ml ticket_lazy_storage_diff.mli tx_rollup_parameters.ml tx_rollup_parameters.mli diff --git a/src/proto_alpha/lib_protocol/ticket_receipt_repr.ml b/src/proto_alpha/lib_protocol/ticket_receipt.ml similarity index 88% rename from src/proto_alpha/lib_protocol/ticket_receipt_repr.ml rename to src/proto_alpha/lib_protocol/ticket_receipt.ml index 6b2c836a287f..602a0be29329 100644 --- a/src/proto_alpha/lib_protocol/ticket_receipt_repr.ml +++ b/src/proto_alpha/lib_protocol/ticket_receipt.ml @@ -23,12 +23,14 @@ (* *) (*****************************************************************************) -type update = {account : Destination_repr.t; amount : Z.t} +open Alpha_context + +type update = {account : Destination.t; amount : Z.t} type ticket_token = { - ticketer : Contract_repr.t; - contents_type : Script_repr.expr; - contents : Script_repr.expr; + ticketer : Contract.t; + contents_type : Script.expr; + contents : Script.expr; } type item = {ticket_token : ticket_token; updates : update list} @@ -40,7 +42,7 @@ let update_encoding = conv (fun {account; amount} -> (account, amount)) (fun (account, amount) -> {account; amount}) - (obj2 (req "account" Destination_repr.encoding) (req "amount" z)) + (obj2 (req "account" Destination.encoding) (req "amount" z)) let ticket_token_encoding = let open Data_encoding in @@ -50,9 +52,9 @@ let ticket_token_encoding = (fun (ticketer, contents_type, contents) -> {ticketer; contents_type; contents}) (obj3 - (req "ticketer" Contract_repr.encoding) - (req "content_type" Script_repr.expr_encoding) - (req "content" Script_repr.expr_encoding)) + (req "ticketer" Contract.encoding) + (req "content_type" Script.expr_encoding) + (req "content" Script.expr_encoding)) let item_encoding = let open Data_encoding in diff --git a/src/proto_alpha/lib_protocol/ticket_receipt_repr.mli b/src/proto_alpha/lib_protocol/ticket_receipt.mli similarity index 94% rename from src/proto_alpha/lib_protocol/ticket_receipt_repr.mli rename to src/proto_alpha/lib_protocol/ticket_receipt.mli index e3c6e30a08f6..92a3612522d2 100644 --- a/src/proto_alpha/lib_protocol/ticket_receipt_repr.mli +++ b/src/proto_alpha/lib_protocol/ticket_receipt.mli @@ -23,17 +23,19 @@ (* *) (*****************************************************************************) +open Alpha_context + (** A module for representing the increase/decrease of tickets in the storage. It will be used to display ticket update information in the operation receipt. *) (** Represents that [account]'s storage has delta [amount] for a given ticket *) -type update = {account : Destination_repr.t; amount : Z.t} +type update = {account : Destination.t; amount : Z.t} (** A ticket token *) type ticket_token = { - ticketer : Contract_repr.t; - contents_type : Script_repr.expr; - contents : Script_repr.expr; + ticketer : Contract.t; + contents_type : Script.expr; + contents : Script.expr; } (** List of updates for a [ticket] *) -- GitLab From e5634933723d9a67a9766cf44f9931c844a4785a Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:13:58 +0900 Subject: [PATCH 3/9] Proto: Move unparsed ticket token definition We move the definition of unparsed ticket token out of ticket_receipt. This will make possible to share the definition with the RPC implementation in the following commits. --- .../lib_client/operation_result.ml | 2 +- .../michelson/test_ticket_accounting.ml | 2 +- .../lib_protocol/ticket_receipt.ml | 22 ++----------- .../lib_protocol/ticket_receipt.mli | 9 +----- src/proto_alpha/lib_protocol/ticket_token.ml | 31 ++++++++++++++----- src/proto_alpha/lib_protocol/ticket_token.mli | 23 +++++++++----- .../lib_protocol/ticket_token_map.ml | 2 +- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index e0c8d5a1e1c5..5d18d7f875c6 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -533,7 +533,7 @@ let pp_ticket_receipt ppf ticket_receipt = updates in let pp_item ppf {ticket_token; updates} = - let {ticketer; contents_type; contents} = ticket_token in + let Ticket_token.{ticketer; contents_type; contents} = ticket_token in Format.fprintf ppf "Ticketer: %a@,Content type: %a@,Content: %a@,%a" 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 2a9b1945703d..ac711717acdd 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 @@ -121,7 +121,7 @@ let assert_equal_ticket_receipt ~loc given expected = @@ let*? ticketer = Contract.of_b58check ticketer in let contents = Expr.from_string (Printf.sprintf "%S" content) in let contents_type = Expr.from_string "string" in - let ticket_token = Ticket_receipt.{ticketer; contents_type; contents} in + let ticket_token = Ticket_token.{ticketer; contents_type; contents} in let updates = List.map (fun (account, amount) -> diff --git a/src/proto_alpha/lib_protocol/ticket_receipt.ml b/src/proto_alpha/lib_protocol/ticket_receipt.ml index 602a0be29329..8f16bf97db82 100644 --- a/src/proto_alpha/lib_protocol/ticket_receipt.ml +++ b/src/proto_alpha/lib_protocol/ticket_receipt.ml @@ -27,13 +27,7 @@ open Alpha_context type update = {account : Destination.t; amount : Z.t} -type ticket_token = { - ticketer : Contract.t; - contents_type : Script.expr; - contents : Script.expr; -} - -type item = {ticket_token : ticket_token; updates : update list} +type item = {ticket_token : Ticket_token.unparsed_token; updates : update list} type t = item list @@ -44,25 +38,13 @@ let update_encoding = (fun (account, amount) -> {account; amount}) (obj2 (req "account" Destination.encoding) (req "amount" z)) -let ticket_token_encoding = - let open Data_encoding in - conv - (fun {ticketer; contents_type; contents} -> - (ticketer, contents_type, contents)) - (fun (ticketer, contents_type, contents) -> - {ticketer; contents_type; contents}) - (obj3 - (req "ticketer" Contract.encoding) - (req "content_type" Script.expr_encoding) - (req "content" Script.expr_encoding)) - let item_encoding = let open Data_encoding in conv (fun {ticket_token; updates} -> (ticket_token, updates)) (fun (ticket_token, updates) -> {ticket_token; updates}) (obj2 - (req "ticket_token" ticket_token_encoding) + (req "ticket_token" Ticket_token.unparsed_token_encoding) (req "updates" (list update_encoding))) let encoding = Data_encoding.list item_encoding diff --git a/src/proto_alpha/lib_protocol/ticket_receipt.mli b/src/proto_alpha/lib_protocol/ticket_receipt.mli index 92a3612522d2..d1397553d26a 100644 --- a/src/proto_alpha/lib_protocol/ticket_receipt.mli +++ b/src/proto_alpha/lib_protocol/ticket_receipt.mli @@ -31,15 +31,8 @@ open Alpha_context (** Represents that [account]'s storage has delta [amount] for a given ticket *) type update = {account : Destination.t; amount : Z.t} -(** A ticket token *) -type ticket_token = { - ticketer : Contract.t; - contents_type : Script.expr; - contents : Script.expr; -} - (** List of updates for a [ticket] *) -type item = {ticket_token : ticket_token; updates : update list} +type item = {ticket_token : Ticket_token.unparsed_token; updates : update list} (** A list of ticket tokens and their corresponding updates *) type t = item list diff --git a/src/proto_alpha/lib_protocol/ticket_token.ml b/src/proto_alpha/lib_protocol/ticket_token.ml index 536e362bdada..ebf39367f0fe 100644 --- a/src/proto_alpha/lib_protocol/ticket_token.ml +++ b/src/proto_alpha/lib_protocol/ticket_token.ml @@ -25,11 +25,28 @@ open Alpha_context -type ex_token = - | Ex_token : { - ticketer : Contract.t; - contents_type : 'a Script_typed_ir.comparable_ty; - contents : 'a; - } - -> ex_token +type 'a parsed_token = { + ticketer : Contract.t; + contents_type : 'a Script_typed_ir.comparable_ty; + contents : 'a; +} +type ex_token = Ex_token : 'a parsed_token -> ex_token + +type unparsed_token = { + ticketer : Contract.t; + contents_type : Script.expr; + contents : Script.expr; +} + +let unparsed_token_encoding = + let open Data_encoding in + conv + (fun {ticketer; contents_type; contents} -> + (ticketer, contents_type, contents)) + (fun (ticketer, contents_type, contents) -> + {ticketer; contents_type; contents}) + (obj3 + (req "ticketer" Contract.encoding) + (req "content_type" Script.expr_encoding) + (req "content" Script.expr_encoding)) diff --git a/src/proto_alpha/lib_protocol/ticket_token.mli b/src/proto_alpha/lib_protocol/ticket_token.mli index ebcbd435be1b..b79865fadbf0 100644 --- a/src/proto_alpha/lib_protocol/ticket_token.mli +++ b/src/proto_alpha/lib_protocol/ticket_token.mli @@ -30,13 +30,22 @@ open Alpha_context a ticket comprises a ticket-token and an amount. *) +type 'a parsed_token = { + ticketer : Contract.t; + contents_type : 'a Script_typed_ir.comparable_ty; + contents : 'a; +} + (** A type for representing existentially quantified ticket-tokens. A ticket-token consists of a pair of ticketer and contents. *) -type ex_token = - | Ex_token : { - ticketer : Contract.t; - contents_type : 'a Script_typed_ir.comparable_ty; - contents : 'a; - } - -> ex_token +type ex_token = Ex_token : 'a parsed_token -> ex_token + +(** Unparsed version of [parsed_token]. + Used to encode/decode ticket-token in receipt, RPC, etc. *) +type unparsed_token = { + ticketer : Contract.t; + contents_type : Script.expr; + contents : Script.expr; +} +val unparsed_token_encoding : unparsed_token Data_encoding.t diff --git a/src/proto_alpha/lib_protocol/ticket_token_map.ml b/src/proto_alpha/lib_protocol/ticket_token_map.ml index 99f9d7e280d5..9daab389c4f8 100644 --- a/src/proto_alpha/lib_protocol/ticket_token_map.ml +++ b/src/proto_alpha/lib_protocol/ticket_token_map.ml @@ -140,7 +140,7 @@ let to_ticket_receipt ctxt ~owner ticket_token_map = let ty = Script.strip_annotations ty_unstripped in let*? ctxt = Gas.consume ctxt (Script.strip_locations_cost ty) in let contents_type = Micheline.strip_locations ty in - let ticket_token = Ticket_receipt.{ticketer; contents_type; contents} in + let ticket_token = Ticket_token.{ticketer; contents_type; contents} in let update = Ticket_receipt.{ticket_token; updates = [{account = owner; amount}]} in -- GitLab From 582bca1b4d66810b20337736eaa9d121014070be Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:19:28 +0900 Subject: [PATCH 4/9] Proto: Factor out function to hash ticket token We will use the newly exposed function when implementing the RPC in the next commit. --- .../lib_protocol/ticket_balance_key.ml | 61 +++++++++++-------- .../lib_protocol/ticket_balance_key.mli | 12 ++++ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/proto_alpha/lib_protocol/ticket_balance_key.ml b/src/proto_alpha/lib_protocol/ticket_balance_key.ml index bdc4b2675d39..915f803d1f7f 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.ml @@ -26,6 +26,37 @@ open Alpha_context +let make ctxt ~owner ~ticketer ~contents_type ~contents = + let open Lwt_result_syntax in + let ticketer_address = + Script_typed_ir. + {destination = Contract ticketer; entrypoint = Entrypoint.default} + in + let owner_address = + Script_typed_ir.{destination = owner; entrypoint = Entrypoint.default} + in + let* ticketer, ctxt = + Script_ir_translator.unparse_data + ctxt + Script_ir_unparser.Optimized_legacy + Script_typed_ir.address_t + ticketer_address + in + let* owner, ctxt = + Script_ir_translator.unparse_data + ctxt + Script_ir_unparser.Optimized_legacy + Script_typed_ir.address_t + owner_address + in + Lwt.return + @@ Ticket_hash.make + ctxt + ~ticketer:(Micheline.root ticketer) + ~ty:contents_type + ~contents + ~owner:(Micheline.root owner) + (* This function extracts nodes of: - Ticketer - Type of content @@ -49,29 +80,9 @@ let of_ex_token ctxt ~owner contents_type contents >>=? fun (contents, ctxt) -> - let ticketer_address = - Script_typed_ir. - {destination = Contract ticketer; entrypoint = Entrypoint.default} - in - let owner_address = - Script_typed_ir.{destination = owner; entrypoint = Entrypoint.default} - in - Script_ir_translator.unparse_data + make ctxt - Script_ir_unparser.Optimized_legacy - Script_typed_ir.address_t - ticketer_address - >>=? fun (ticketer, ctxt) -> - Script_ir_translator.unparse_data - ctxt - Script_ir_unparser.Optimized_legacy - Script_typed_ir.address_t - owner_address - >>=? fun (owner, ctxt) -> - Lwt.return - (Ticket_hash.make - ctxt - ~ticketer:(Micheline.root ticketer) - ~ty - ~contents:(Micheline.root contents) - ~owner:(Micheline.root owner)) + ~owner + ~ticketer + ~contents_type:ty + ~contents:(Micheline.root 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..53af20ca9ada 100644 --- a/src/proto_alpha/lib_protocol/ticket_balance_key.mli +++ b/src/proto_alpha/lib_protocol/ticket_balance_key.mli @@ -30,6 +30,18 @@ open Alpha_context global ticket-balance table that tracks ownership of tickets for different tokens. *) +(** [make ~owner ~ticketer ~contents_type ~contents] returns [key_hash] of the + given [owner], [ticketer], [contents_type] and [contents]. Note that the + [location] of the [Script.node] values [contents_type] and [contents] are + irrelevant since [Ticket_hash.make] will strip the locations before calculating the hash. *) +val make : + context -> + owner:Destination.t -> + ticketer:Contract.t -> + contents_type: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 From 18f3cdb241a2895a7a27679a19118182f1c913e8 Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:22:54 +0900 Subject: [PATCH 5/9] Proto/RPC: RPC for ticket balance of owner+ticket --- src/proto_alpha/lib_plugin/RPC.ml | 31 ++++++++++++++++++++++++++++++- src/proto_alpha/lib_protocol/dune | 1 - 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index d4175efb1af9..ff6f6b65c0c0 100644 --- a/src/proto_alpha/lib_plugin/RPC.ml +++ b/src/proto_alpha/lib_plugin/RPC.ml @@ -1698,6 +1698,17 @@ module Contract = struct ~query:RPC_query.empty ~output:(option z) RPC_path.(path /: Contract.rpc_arg / "storage" / "paid_space") + + let ticket_balance = + let open Data_encoding in + RPC_service.post_service + ~description: + "Access the contract's balance of ticket with specified ticketer, \ + content type, and content." + ~query:RPC_query.empty + ~input:Ticket_token.unparsed_token_encoding + ~output:n + RPC_path.(path /: Contract.rpc_arg / "ticket_balance") end let get_contract contract f = @@ -1756,7 +1767,22 @@ module Contract = struct S.get_paid_storage_space (fun ctxt contract () () -> get_contract contract @@ fun _ -> - Contract.paid_storage_space ctxt contract >>=? return_some) + Contract.paid_storage_space ctxt contract >>=? return_some) ; + Registration.register1 + ~chunked:false + S.ticket_balance + (fun ctxt contract () Ticket_token.{ticketer; contents_type; contents} -> + let open Lwt_result_syntax in + let* ticket_hash, ctxt = + Ticket_balance_key.make + ctxt + ~owner:(Contract contract) + ~ticketer + ~contents_type:(Micheline.root contents_type) + ~contents:(Micheline.root contents) + in + let* amount, _ctxt = Ticket_balance.get_balance ctxt ticket_hash in + return @@ Option.value amount ~default:Z.zero) let get_storage_normalized ctxt block ~contract ~unparsing_mode = RPC_context.make_call1 @@ -1794,6 +1820,9 @@ module Contract = struct (Contract.Originated contract) () () + + let ticket_balance ctxt block contract key = + RPC_context.make_call1 S.ticket_balance ctxt block contract () key end module Big_map = struct diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index 28834a1c4a19..fb09c19b20f2 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -524,7 +524,6 @@ contract_services.ml contract_services.mli delegate_services.ml delegate_services.mli voting_services.ml voting_services.mli - ticket_services.ml ticket_services.mli tx_rollup_services.ml tx_rollup_services.mli alpha_services.ml alpha_services.mli main.ml main.mli -- GitLab From 898b26c7d3b9a103994ce23b6f87aa14e701d3df Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:24:19 +0900 Subject: [PATCH 6/9] Tezt: RPCs for ticket_balance --- tezt/lib_tezos/RPC.ml | 17 +++++++++++++++++ tezt/lib_tezos/RPC.mli | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/tezt/lib_tezos/RPC.ml b/tezt/lib_tezos/RPC.ml index 21027cdae474..c0bc5fed69ba 100644 --- a/tezt/lib_tezos/RPC.ml +++ b/tezt/lib_tezos/RPC.ml @@ -703,6 +703,23 @@ let get_chain_block_context_contract_storage ?(chain = "main") ?(block = "head") ["chains"; chain; "blocks"; block; "context"; "contracts"; id; "storage"] Fun.id +let post_chain_block_context_contract_ticket_balance ?(chain = "main") + ?(block = "head") ~id ~data () = + make + POST + [ + "chains"; + chain; + "blocks"; + block; + "context"; + "contracts"; + id; + "ticket_balance"; + ] + ~data + JSON.as_int + let get_chain_block_context_sc_rollup ?(chain = "main") ?(block = "head") () = make GET ["chains"; chain; "blocks"; block; "context"; "sc_rollup"] Fun.id diff --git a/tezt/lib_tezos/RPC.mli b/tezt/lib_tezos/RPC.mli index ec4ea6364cba..e0b687151302 100644 --- a/tezt/lib_tezos/RPC.mli +++ b/tezt/lib_tezos/RPC.mli @@ -728,6 +728,14 @@ val get_chain_block_context_contract_script : val get_chain_block_context_contract_storage : ?chain:string -> ?block:string -> id:string -> unit -> JSON.t t +(** RPC [POST /chains//blocks//context/contracts//ticket_balance] + + [chain] defaults to ["main"]. + [block] defaults to ["head"]. +*) +val post_chain_block_context_contract_ticket_balance : + ?chain:string -> ?block:string -> id:string -> data:JSON.u -> unit -> int t + (** {2 Smart contract rollup RPC module} *) (** RPC: [GET chains//blocks//context/sc_rollup] *) -- GitLab From f890e8868953f162ad4be8447cc68ab9e61225c2 Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:28:30 +0900 Subject: [PATCH 7/9] Tezt: Rename variables to match diagram in comment --- tezt/tests/ticket_updates_in_receipt.ml | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tezt/tests/ticket_updates_in_receipt.ml b/tezt/tests/ticket_updates_in_receipt.ml index b3ec40212edc..3642152ed429 100644 --- a/tezt/tests/ticket_updates_in_receipt.ml +++ b/tezt/tests/ticket_updates_in_receipt.ml @@ -58,7 +58,7 @@ let test_ticket_updates_in_receipt = ~tags:["client"; "michelson"] @@ fun protocol -> let* client = Client.init_mockup ~protocol () in - let* create_and_send = + let* kt_a = Client.originate_contract ~alias:"tickets_create_and_send.tz" ~amount:Tez.zero @@ -68,7 +68,7 @@ let test_ticket_updates_in_receipt = ~burn_cap:Tez.one client in - let* store_fst_and_rely_snd = + let* kt_b = Client.originate_contract ~alias:"tickets_store_fst_and_rely_snd.tz" ~amount:Tez.zero @@ -79,7 +79,7 @@ let test_ticket_updates_in_receipt = ~burn_cap:Tez.one client in - let* receive_and_store = + let* kt_c = Client.originate_contract ~alias:"tickets_receive_and_store.tz" ~amount:Tez.one @@ -95,12 +95,8 @@ let test_ticket_updates_in_receipt = ~burn_cap:(Tez.of_int 2) ~amount:Tez.zero ~giver:"bootstrap2" - ~receiver:create_and_send - ~arg: - (Format.sprintf - {|(Pair "%s" "%s")|} - store_fst_and_rely_snd - receive_and_store) + ~receiver:kt_a + ~arg:(Format.sprintf {|(Pair "%s" "%s")|} kt_b kt_c) ~hooks client in -- GitLab From ec75cb1e5286f9d1f6327c6fc0a7a7ddc49a10d8 Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:36:34 +0900 Subject: [PATCH 8/9] Tezt: Test ticket_balance RPC --- .../Alpha- Ticket updates in receipt.out | 0 tezt/tests/main.ml | 2 +- ...n_receipt.ml => ticket_receipt_and_rpc.ml} | 78 ++++++++++++++++++- 3 files changed, 75 insertions(+), 5 deletions(-) rename tezt/tests/expected/{ticket_updates_in_receipt.ml => ticket_receipt_and_rpc.ml}/Alpha- Ticket updates in receipt.out (100%) rename tezt/tests/{ticket_updates_in_receipt.ml => ticket_receipt_and_rpc.ml} (71%) diff --git a/tezt/tests/expected/ticket_updates_in_receipt.ml/Alpha- Ticket updates in receipt.out b/tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out similarity index 100% rename from tezt/tests/expected/ticket_updates_in_receipt.ml/Alpha- Ticket updates in receipt.out rename to tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 8198ec646e1e..dcf83b7f7756 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -135,7 +135,7 @@ let register_protocol_agnostic_tests () = Synchronisation_heuristic.register ~protocols:[Alpha] ; Tenderbake.register ~protocols:[Alpha] ; Test_contract_bls12_381.register ~protocols:[Alpha] ; - Ticket_updates_in_receipt.register ~protocols:[Alpha] ; + Ticket_receipt_and_rpc.register ~protocols:[Alpha] ; Timelock.register ~protocols ; Tickets.register ~protocols ; Tx_rollup.register ~protocols ; diff --git a/tezt/tests/ticket_updates_in_receipt.ml b/tezt/tests/ticket_receipt_and_rpc.ml similarity index 71% rename from tezt/tests/ticket_updates_in_receipt.ml rename to tezt/tests/ticket_receipt_and_rpc.ml index 3642152ed429..492acdf18997 100644 --- a/tezt/tests/ticket_updates_in_receipt.ml +++ b/tezt/tests/ticket_receipt_and_rpc.ml @@ -26,12 +26,36 @@ (* Testing ------- Component: Michelson - Invocation: dune exec tezt/tests/main.exe -- --file ticket_updates_in_receipt.ml - Subject: Regression tests for ticket updates in receipt + Invocation: dune exec tezt/tests/main.exe -- --file ticket_receipt_and_rpc.ml + Subject: Regression tests for ticket receipt and ticket RPC *) let hooks = Tezos_regression.hooks +let check_ticket_balance client ~contract ~ticketer ~content_type ~content + ~expected = + let post_body = + Ezjsonm.value_from_string + @@ sf + {|{"ticketer": "%s", "content_type": {"prim": "%s"}, "content": {"string": "%s"}}|} + ticketer + content_type + content + in + let* actual = + RPC.Client.call client + @@ RPC.post_chain_block_context_contract_ticket_balance + ~id:contract + ~data:post_body + () + in + return + @@ Check.( + (actual = expected) + int + ~__LOC__ + ~error_msg:"expected ticket amount %R, got %L") + (* Checks how the ticket updates in receipt look like in various cases, such as: - Contract stores multiple tickets of different types. - Contract stores multiple tickets of the same type. @@ -51,7 +75,7 @@ let hooks = Tezos_regression.hooks tz_a -> KT_A (store 1 red and 2 green) *) -let test_ticket_updates_in_receipt = +let test_ticket_receipt_and_rpc = Protocol.register_regression_test ~__FILE__ ~title:"Ticket updates in receipt" @@ -100,6 +124,52 @@ let test_ticket_updates_in_receipt = ~hooks client in + (* Check that the ticket balance are expected via [ticket_balance] RPC. *) + let* () = + check_ticket_balance + client + ~contract:kt_a + ~ticketer:kt_a + ~content_type:"string" + ~content:"red" + ~expected:1 + in + let* () = + check_ticket_balance + client + ~contract:kt_a + ~ticketer:kt_a + ~content_type:"string" + ~content:"green" + ~expected:2 + in + let* () = + check_ticket_balance + client + ~contract:kt_a + ~ticketer:kt_a + ~content_type:"string" + ~content:"blue" + ~expected:0 + in + let* () = + check_ticket_balance + client + ~contract:kt_b + ~ticketer:kt_a + ~content_type:"string" + ~content:"blue" + ~expected:1 + in + let* () = + check_ticket_balance + client + ~contract:kt_c + ~ticketer:kt_a + ~content_type:"string" + ~content:"blue" + ~expected:1 + in unit -let register ~protocols = test_ticket_updates_in_receipt protocols +let register ~protocols = test_ticket_receipt_and_rpc protocols -- GitLab From af5bc596fee364cf7d4e06db7c7576ca8280d6ba Mon Sep 17 00:00:00 2001 From: lin Date: Sat, 15 Oct 2022 11:36:43 +0900 Subject: [PATCH 9/9] Doc: Update change log for ticket_balance RPC --- docs/protocols/alpha.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index bc85d6ef6c59..286ee7758088 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -32,6 +32,9 @@ Breaking Changes RPC Changes ----------- +- Add RPC to get contract's balance of ticket with specified ticketer, content type, and content: + ``POST /chains//blocks//context/contracts//ticket_balance``. (MR :gl:`!6488`) + Operation receipts ------------------ -- GitLab