From bce5bdc7cfa827c3f74b7a3623b872969ee323bc Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Thu, 27 Oct 2022 16:27:01 +0100 Subject: [PATCH 1/6] Implement increment counter as a manager operation --- src/proto_alpha/bin_sc_rollup_node/daemon.ml | 3 +- .../bin_sc_rollup_node/injector.ml | 3 +- src/proto_alpha/lib_client/injection.ml | 10 ++- .../lib_client/operation_result.ml | 4 +- src/proto_alpha/lib_injector/l1_operation.ml | 1 + src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/alpha_context.ml | 5 ++ .../lib_protocol/alpha_context.mli | 20 +++++- src/proto_alpha/lib_protocol/apply.ml | 6 ++ src/proto_alpha/lib_protocol/apply_results.ml | 9 ++- .../lib_protocol/apply_results.mli | 3 + .../lib_protocol/counter_challenge.ml | 11 +++ .../lib_protocol/counter_challenge.mli | 5 ++ src/proto_alpha/lib_protocol/dune | 4 ++ .../lib_protocol/operation_repr.ml | 22 ++++++ .../lib_protocol/operation_repr.mli | 7 ++ src/proto_alpha/lib_protocol/storage.ml | 9 +++ src/proto_alpha/lib_protocol/storage.mli | 6 ++ .../lib_protocol/test/helpers/block.ml | 10 ++- .../lib_protocol/test/helpers/op.ml | 15 +++++ .../lib_protocol/test/helpers/op.mli | 12 ++++ .../test/integration/operations/main.ml | 1 + .../operations/test_counter_challenge.ml | 67 +++++++++++++++++++ .../test_manager_operation_validation.ml | 3 +- .../lib_protocol/test/unit/main.ml | 1 + .../test/unit/test_counter_challenge.ml | 60 +++++++++++++++++ src/proto_alpha/lib_protocol/validate.ml | 1 + 27 files changed, 286 insertions(+), 13 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/counter_challenge.ml create mode 100644 src/proto_alpha/lib_protocol/counter_challenge.mli create mode 100644 src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml create mode 100644 src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon.ml b/src/proto_alpha/bin_sc_rollup_node/daemon.ml index b8c1454b69e..d68562f09f4 100644 --- a/src/proto_alpha/bin_sc_rollup_node/daemon.ml +++ b/src/proto_alpha/bin_sc_rollup_node/daemon.ml @@ -125,7 +125,8 @@ module Make (PVM : Pvm.S) = struct | Tx_rollup_submit_batch _ | Tx_rollup_commit _ | Tx_rollup_return_bond _ | Tx_rollup_finalize_commitment _ | Tx_rollup_remove_commitment _ | Tx_rollup_rejection _ | Tx_rollup_dispatch_tickets _ | Transfer_ticket _ - | Sc_rollup_originate _ | Zk_rollup_origination _ | Zk_rollup_publish _ -> + | Sc_rollup_originate _ | Zk_rollup_origination _ | Zk_rollup_publish _ + | Incr_counter _ -> false in if not (is_for_my_rollup operation) then return_unit diff --git a/src/proto_alpha/bin_sc_rollup_node/injector.ml b/src/proto_alpha/bin_sc_rollup_node/injector.ml index 982460e5395..e5750f99b15 100644 --- a/src/proto_alpha/bin_sc_rollup_node/injector.ml +++ b/src/proto_alpha/bin_sc_rollup_node/injector.ml @@ -151,7 +151,8 @@ module Parameters : | Dal_publish_slot_header _ | Sc_rollup_originate _ | Sc_rollup_execute_outbox_message _ | Sc_rollup_recover_bond _ | Sc_rollup_dal_slot_subscribe _ | Zk_rollup_origination _ - | Zk_rollup_publish _ -> + | Zk_rollup_publish _ + | Incr_counter _ -> (* These operations should never be handled by this injector *) assert false diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index bec0a611639..948a02b84d0 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -340,7 +340,9 @@ let estimated_gas_single (type kind) | Sc_rollup_dal_slot_subscribe_result {consumed_gas; _} -> Ok consumed_gas | Zk_rollup_origination_result {consumed_gas; _} -> Ok consumed_gas - | Zk_rollup_publish_result {consumed_gas; _} -> Ok consumed_gas) + | Zk_rollup_publish_result {consumed_gas; _} -> Ok consumed_gas + | Incr_counter_result _ -> Ok Gas.Arith.zero) + | Skipped _ -> error_with "Cannot estimate gas of skipped operation" (* There must be another error for this to happen, and it should not @@ -424,7 +426,8 @@ let estimated_storage_single (type kind) ~tx_rollup_origination_size | Sc_rollup_cement_result _ | Sc_rollup_publish_result _ | Sc_rollup_refute_result _ | Sc_rollup_timeout_result _ | Sc_rollup_recover_bond_result _ - | Sc_rollup_dal_slot_subscribe_result _ -> + | Sc_rollup_dal_slot_subscribe_result _ + | Incr_counter_result _ -> Ok Z.zero) | Skipped _ -> error_with "Cannot estimate storage of skipped operation" @@ -517,7 +520,8 @@ let originated_contracts_single (type kind) | Sc_rollup_timeout_result _ | Sc_rollup_execute_outbox_message_result _ | Sc_rollup_recover_bond_result _ | Sc_rollup_dal_slot_subscribe_result _ | Zk_rollup_origination_result _ - | Zk_rollup_publish_result _ -> + | Zk_rollup_publish_result _ + | Incr_counter_result _ -> Ok []) | Skipped _ -> error_with "Cannot know originated contracts of skipped operation" diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index e0c8d5a1e1c..7a9ebc2f4dd 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -422,6 +422,7 @@ let pp_manager_operation_content (type kind) source ppf Format.fprintf ppf "Zk rollup origination:@,From: %a" Contract.pp source | Zk_rollup_publish _ -> Format.fprintf ppf "Zk rollup publish:@,From: %a" Contract.pp source + | Incr_counter _ -> () let pp_balance_updates ppf balance_updates = let open Receipt in @@ -863,7 +864,6 @@ let pp_manager_operation_contents_result ppf op_result = pp_consumed_gas ppf consumed_gas ; pp_balance_updates ppf balance_updates in - let manager_operation_name (type kind) (result : kind successful_manager_operation_result) = match result with @@ -904,6 +904,7 @@ let pp_manager_operation_contents_result ppf op_result = "data availability slot header publishing" | Zk_rollup_origination_result _ -> "zk rollup originate" | Zk_rollup_publish_result _ -> "zk rollup publish" + | Incr_counter_result _ -> "increment counter" in let pp_manager_operation_contents_result (type kind) ppf (result : kind successful_manager_operation_result) = @@ -949,6 +950,7 @@ let pp_manager_operation_contents_result ppf op_result = pp_dal_publish_slot_header_result op | Zk_rollup_origination_result _ as op -> pp_zk_rollup_origination_result op | Zk_rollup_publish_result _ as op -> pp_zk_rollup_publish_result op + | Incr_counter_result _ -> () in pp_operation_result ~operation_name:manager_operation_name diff --git a/src/proto_alpha/lib_injector/l1_operation.ml b/src/proto_alpha/lib_injector/l1_operation.ml index e82d6668f88..7f4fb1f3b22 100644 --- a/src/proto_alpha/lib_injector/l1_operation.ml +++ b/src/proto_alpha/lib_injector/l1_operation.ml @@ -108,6 +108,7 @@ module Manager_operation = struct | Sc_rollup_dal_slot_subscribe _ -> sc_rollup_dal_slot_subscribe_case | Zk_rollup_origination _ -> zk_rollup_origination_case | Zk_rollup_publish _ -> zk_rollup_publish_case + | Incr_counter _ -> incr_counter_case let pp_kind ppf op = let open Operation.Encoding.Manager_operations in diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 35182bbcdc5..5be4ddb98d7 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -174,6 +174,7 @@ "Sc_rollup_storage", "Sc_rollup_refutation_storage", "Zk_rollup_errors", + "Counter_challenge", "Dal_slot_storage", diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 032c6b2ffc2..91c089ec0ec 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -619,3 +619,8 @@ module Cache = Cache_repr module Internal_for_tests = struct let to_raw x = x end + +module Counter_challenge_alpha = struct + include Counter_challenge + +end diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 1c9a7ed52b9..89072343936 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4034,7 +4034,7 @@ module Cache : sig end (** This module re-exports definitions from {!Lazy_storage_kind}. *) -module Kind : sig + module Kind : sig type preendorsement_consensus_kind = Preendorsement_consensus_kind type endorsement_consensus_kind = Endorsement_consensus_kind @@ -4135,6 +4135,8 @@ module Kind : sig type zk_rollup_publish = Zk_rollup_publish_kind + type incr_counter = Incr_counter_kind + type 'a manager = | Reveal_manager_kind : reveal manager | Transaction_manager_kind : transaction manager @@ -4171,6 +4173,7 @@ module Kind : sig : sc_rollup_dal_slot_subscribe manager | Zk_rollup_origination_manager_kind : zk_rollup_origination manager | Zk_rollup_publish_manager_kind : zk_rollup_publish manager + | Incr_counter_manager_kind : incr_counter manager end (** All the definitions below are re-exported from {!Operation_repr}. *) @@ -4427,6 +4430,8 @@ and _ manager_operation = ops : (Zk_rollup.Operation.t * Zk_rollup.Ticket.t option) list; } -> Kind.zk_rollup_publish manager_operation + | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation + and counter = Z.t @@ -4704,6 +4709,8 @@ module Operation : sig val zk_rollup_origination_case : Kind.zk_rollup_origination case val zk_rollup_publish_case : Kind.zk_rollup_publish case + + val incr_counter_case : Kind.incr_counter case end end @@ -5058,3 +5065,14 @@ module Fees : sig val check_storage_limit : context -> storage_limit:Z.t -> unit tzresult end + + +(** This module re-exports definitions from {!Counter_challenge}. *) +module Counter_challenge_alpha : sig + val incr_counter : + context -> + Contract.t -> + Z.t -> + (context * Z.t) tzresult Lwt.t + + end \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 95e8eb0ea38..1308f32cb0c 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1426,6 +1426,10 @@ let apply_manager_operation : | Zk_rollup_publish {zk_rollup; ops} -> Zk_rollup_apply.publish ~ctxt_before_op ~ctxt ~zk_rollup ~l2_ops:ops + | Incr_counter {value} -> + Counter_challenge_alpha.incr_counter ctxt source_contract value >|=? fun (ctxt, value) -> + (ctxt, Incr_counter_result { value } , []) + type success_or_failure = Success of context | Failure let apply_internal_operations ctxt ~payer ~chain_id ops = @@ -1675,6 +1679,8 @@ let burn_manager_storage_fees : ( ctxt, storage_limit, Zk_rollup_publish_result {payload with balance_updates} ) + | Incr_counter_result _ as result -> return (ctxt, storage_limit, result) + (** [burn_internal_storage_fees ctxt smopr storage_limit payer] burns the storage fees associated to an internal operation result [smopr]. diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 346b542c012..3c7fd66e460 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -193,6 +193,7 @@ type _ successful_manager_operation_result = paid_storage_size_diff : Z.t; } -> Kind.zk_rollup_publish successful_manager_operation_result + | Incr_counter_result: {value: Z.t} -> Kind.incr_counter successful_manager_operation_result let migration_origination_result_to_successful_manager_operation_result ({ @@ -1237,6 +1238,8 @@ let equal_manager_kind : | Kind.Zk_rollup_publish_manager_kind, Kind.Zk_rollup_publish_manager_kind -> Some Eq | Kind.Zk_rollup_publish_manager_kind, _ -> None + | Kind.Incr_counter_manager_kind , Kind.Incr_counter_manager_kind -> None + | Kind.Incr_counter_manager_kind , _ -> None module Encoding = struct type 'kind case = @@ -2051,7 +2054,7 @@ let contents_and_result_encoding = type 'kind contents_result_list = | Single_result : 'kind contents_result -> 'kind contents_result_list - | Cons_result : + | Cons_result : 'kind Kind.manager contents_result * 'rest Kind.manager contents_result_list -> ('kind * 'rest) Kind.manager contents_result_list @@ -2073,7 +2076,7 @@ let contents_result_list_encoding = | Contents_result o :: os -> ( of_list os >>? fun (Contents_result_list os) -> match (o, os) with - | Manager_operation_result _, Single_result (Manager_operation_result _) + | Manager_operation_result _, Single_result (Manager_operation_result _) -> Ok (Contents_result_list (Cons_result (o, os))) | Manager_operation_result _, Cons_result _ -> @@ -2953,6 +2956,8 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Zk_rollup_publish _; _}, _ -> None + | Manager_operation {operation = Incr_counter _; _ }, _ -> None + let rec kind_equal_list : type kind kind2. diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index b468b0bae19..eda31d42448 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -298,6 +298,9 @@ and _ successful_manager_operation_result = paid_storage_size_diff : Z.t; } -> Kind.zk_rollup_publish successful_manager_operation_result + | Incr_counter_result: { + value: Z.t + } -> Kind.incr_counter successful_manager_operation_result and packed_successful_manager_operation_result = | Successful_manager_result : diff --git a/src/proto_alpha/lib_protocol/counter_challenge.ml b/src/proto_alpha/lib_protocol/counter_challenge.ml new file mode 100644 index 00000000000..20c2696e6ef --- /dev/null +++ b/src/proto_alpha/lib_protocol/counter_challenge.ml @@ -0,0 +1,11 @@ +let incr_counter ctxt contract value = + let open Storage.Contract in + Counter_challenge_store.find ctxt contract >>=? fun maybe_v -> + match maybe_v with + | Some v -> + let value = Z.add v value in + Counter_challenge_store.update ctxt contract value >|=? fun ctxt -> + (ctxt, value) + | None -> + Counter_challenge_store.init ctxt contract value >|=? fun ctxt -> + (ctxt, value) diff --git a/src/proto_alpha/lib_protocol/counter_challenge.mli b/src/proto_alpha/lib_protocol/counter_challenge.mli new file mode 100644 index 00000000000..e54956e8bf0 --- /dev/null +++ b/src/proto_alpha/lib_protocol/counter_challenge.mli @@ -0,0 +1,5 @@ +val incr_counter : + Raw_context.t -> + Contract_repr.t -> + Z.t -> + (Raw_context.t * Z.t) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index b53da12c661..c981f540a1e 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -192,6 +192,7 @@ Sc_rollup_storage Sc_rollup_refutation_storage Zk_rollup_errors + Counter_challenge Dal_slot_storage Alpha_context Script_string @@ -459,6 +460,7 @@ sc_rollup_storage.ml sc_rollup_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli zk_rollup_errors.ml + counter_challenge.ml counter_challenge.mli dal_slot_storage.ml dal_slot_storage.mli alpha_context.ml alpha_context.mli script_string.ml script_string.mli @@ -706,6 +708,7 @@ sc_rollup_storage.ml sc_rollup_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli zk_rollup_errors.ml + counter_challenge.ml counter_challenge.mli dal_slot_storage.ml dal_slot_storage.mli alpha_context.ml alpha_context.mli script_string.ml script_string.mli @@ -958,6 +961,7 @@ sc_rollup_storage.ml sc_rollup_storage.mli sc_rollup_refutation_storage.ml sc_rollup_refutation_storage.mli zk_rollup_errors.ml + counter_challenge.ml counter_challenge.mli dal_slot_storage.ml dal_slot_storage.mli alpha_context.ml alpha_context.mli script_string.ml script_string.mli diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index a66d09dc635..53458e77a82 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -126,6 +126,8 @@ module Kind = struct type zk_rollup_origination = Zk_rollup_origination_kind type zk_rollup_publish = Zk_rollup_publish_kind + + type incr_counter = Incr_counter_kind type 'a manager = | Reveal_manager_kind : reveal manager @@ -163,6 +165,7 @@ module Kind = struct : sc_rollup_dal_slot_subscribe manager | Zk_rollup_origination_manager_kind : zk_rollup_origination manager | Zk_rollup_publish_manager_kind : zk_rollup_publish manager + | Incr_counter_manager_kind : incr_counter manager end type 'a consensus_operation_type = @@ -489,6 +492,7 @@ and _ manager_operation = ops : (Zk_rollup_operation_repr.t * Zk_rollup_ticket_repr.t option) list; } -> Kind.zk_rollup_publish manager_operation + | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation and counter = Z.t @@ -527,6 +531,7 @@ let manager_kind : type kind. kind manager_operation -> kind Kind.manager = Kind.Sc_rollup_dal_slot_subscribe_manager_kind | Zk_rollup_origination _ -> Kind.Zk_rollup_origination_manager_kind | Zk_rollup_publish _ -> Kind.Zk_rollup_publish_manager_kind + | Incr_counter _ -> Incr_counter_manager_kind type packed_manager_operation = | Manager : 'kind manager_operation -> packed_manager_operation @@ -631,6 +636,8 @@ let zk_rollup_operation_create_tag = zk_rollup_operation_tag_offset + 0 let zk_rollup_operation_publish_tag = zk_rollup_operation_tag_offset + 1 +let incr_counter_tag = 42 + module Encoding = struct open Data_encoding @@ -654,6 +661,16 @@ module Encoding = struct } -> 'kind case + let incr_counter_case = + MCase { + tag = incr_counter_tag; + name = "increment counter"; + encoding = obj1 (req "value" Data_encoding.z); + select = (function Manager (Incr_counter _ as op ) -> Some op | _ -> None); + proj = (function Incr_counter { value } -> value); + inj = (fun value -> Incr_counter { value }); + } + let reveal_case = MCase { @@ -1648,6 +1665,8 @@ module Encoding = struct (extract op, mcase.proj operation)); inj = (fun (op, contents) -> rebuild op (mcase.inj contents)); } + + let incr_counter_case = make_manager_case 142 Manager_operations.incr_counter_case let reveal_case = make_manager_case 107 Manager_operations.reveal_case @@ -1830,6 +1849,7 @@ module Encoding = struct make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make incr_counter_case; ] let contents_list_encoding = @@ -2093,6 +2113,8 @@ let equal_manager_operation_kind : | Zk_rollup_origination _, _ -> None | Zk_rollup_publish _, Zk_rollup_publish _ -> Some Eq | Zk_rollup_publish _, _ -> None + | Incr_counter _, Incr_counter _ -> Some Eq + | Incr_counter _, _ -> None let equal_contents_kind : type a b. a contents -> b contents -> (a, b) eq option = diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index 73e42f975ed..c089b9c8293 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -158,6 +158,8 @@ module Kind : sig type zk_rollup_publish = Zk_rollup_publish_kind + type incr_counter = Incr_counter_kind + type 'a manager = | Reveal_manager_kind : reveal manager | Transaction_manager_kind : transaction manager @@ -194,6 +196,8 @@ module Kind : sig : sc_rollup_dal_slot_subscribe manager | Zk_rollup_origination_manager_kind : zk_rollup_origination manager | Zk_rollup_publish_manager_kind : zk_rollup_publish manager + | Incr_counter_manager_kind : incr_counter manager + end type 'a consensus_operation_type = @@ -582,6 +586,7 @@ and _ manager_operation = (* See {!Zk_rollup_apply} *) } -> Kind.zk_rollup_publish manager_operation + | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation (** Counters are used as anti-replay protection mechanism in manager operations: each manager account stores a counter and @@ -913,5 +918,7 @@ module Encoding : sig val zk_rollup_origination_case : Kind.zk_rollup_origination case val zk_rollup_publish_case : Kind.zk_rollup_publish case + + val incr_counter_case : Kind.incr_counter case end end diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 9b47de7bd99..87fb13e5755 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -273,6 +273,15 @@ module Contract = struct end) (Encoding.Z) + + module Counter_challenge_store = + Indexed_context.Make_map + (Registered) + (struct + let name = ["counter_challenge"] + end) + (Encoding.Z) + (* Consume gas for serialization and deserialization of expr in this module *) module Make_carbonated_map_expr (N : Storage_sigs.NAME) : diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index c7ae069a206..296629e8b49 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -180,6 +180,12 @@ module Contract : sig and type t := Raw_context.t and type local_context := local_context + module Counter_challenge_store : + Indexed_data_storage + with type key = Contract_repr.t + and type value = Z.t + and type t := Raw_context.t + module Code : Non_iterable_indexed_carbonated_data_storage with type key = Contract_repr.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 3291ae729c4..37093d6d1d6 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -871,7 +871,8 @@ let bake_n_with_all_balance_updates ?(baking_mode = Application) ?policy | Sc_rollup_execute_outbox_message_result _ | Sc_rollup_recover_bond_result _ | Sc_rollup_dal_slot_subscribe_result _ - | Zk_rollup_origination_result _ | Zk_rollup_publish_result _ -> + | Zk_rollup_origination_result _ | Zk_rollup_publish_result _ + | Incr_counter_result _-> balance_updates_rev | Transaction_result ( Transaction_to_contract_result {balance_updates; _} @@ -926,10 +927,13 @@ let bake_n_with_origination_results ?(baking_mode = Application) ?policy n b = | Successful_manager_result (Sc_rollup_recover_bond_result _) | Successful_manager_result (Sc_rollup_dal_slot_subscribe_result _) | Successful_manager_result (Zk_rollup_origination_result _) - | Successful_manager_result (Zk_rollup_publish_result _) -> + | Successful_manager_result (Zk_rollup_publish_result _) + | Successful_manager_result (Incr_counter_result _) -> origination_results_rev | Successful_manager_result (Origination_result x) -> - Origination_result x :: origination_results_rev) + Origination_result x :: origination_results_rev + + ) origination_results_rev metadata.implicit_operations_results in diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index 5fbc14b0f8a..249c3329156 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -1044,3 +1044,18 @@ let zk_rollup_publish ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt >>=? fun to_sign_op -> Context.Contract.manager ctxt src >|=? fun account -> sign account.sk ctxt to_sign_op + +let incr_counter ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt + contract value = + manager_operation + ?force_reveal + ?counter + ?fee + ?gas_limit + ?storage_limit + ~source:contract + ctxt + (Incr_counter {value}) + >>=? fun to_sign_op -> + Context.Contract.manager ctxt contract >|=? fun account -> + sign account.sk ctxt to_sign_op diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index 1dea72fd81c..1e8ce6a65c7 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -773,3 +773,15 @@ val zk_rollup_publish : zk_rollup:Zk_rollup.t -> ops:(Zk_rollup.Operation.t * Zk_rollup.Ticket.t option) list -> Operation.packed tzresult Lwt.t + + +val incr_counter: ?force_reveal:bool -> + ?counter:Z.t -> + ?fee:Tez.t -> + ?gas_limit:gas_limit -> + ?storage_limit:counter -> + Context.t -> + Contract.t -> + Z.t -> + Operation.packed tzresult Lwt.t + \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/main.ml b/src/proto_alpha/lib_protocol/test/integration/operations/main.ml index afcec61e65e..83c42b7c884 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/main.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/main.ml @@ -46,5 +46,6 @@ let () = ("sc rollup", Test_sc_rollup.tests); ("sc rollup transfer", Test_sc_rollup_transfer.tests); ("zk rollup", Test_zk_rollup.tests); + ("counter challenge", Test_counter_challenge.tests ) ] |> Lwt_main.run diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml new file mode 100644 index 00000000000..990ca03f2f7 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml @@ -0,0 +1,67 @@ +(** Testing + ------- + Component: Protocol (transfer) + Invocation: dune exec \ + src/proto_alpha/lib_protocol/test/integration/operations/main.exe \ + -- test "^counter challenge$" + Subject: Quantities transfer between contracts. +*) +open Protocol + +(* Converts contract representation through the Alpha_context barrier *) +let convert_contract_repr contract = + match contract with + | Alpha_context.Contract.Implicit x -> Contract_repr.Implicit x + | Alpha_context.Contract.Originated x -> Contract_repr.Originated x + +let assert_stored_value ctx contract expected = + let contract = convert_contract_repr contract in + Storage.Contract.Counter_challenge_store.get ctx contract + >|=? (fun value -> assert (value = expected)) + >|= Environment.wrap_tzresult >>=? return + +let test_init_and_add _ = + let open Lwt_result_syntax in + let expected = Z.of_int 3 in + let* b, contract = Context.init1 () in + let* operation = Op.incr_counter (B b) contract expected in + let* b = + Incremental.begin_construction b >>=? fun inc -> + Incremental.add_operation inc operation >>=? fun inc -> + Incremental.finalize_block inc + in + let* ctx = + Block.to_alpha_ctxt b >|=? Alpha_context.Internal_for_tests.to_raw + in + assert_stored_value ctx contract expected + +let test_add _ = + let open Lwt_result_syntax in + let value_to_add = Z.of_int 5 in + let expected = Z.of_int 10 in + let* b, contracts = + Context.init_with_constants_n + {Context.default_test_constants with consensus_threshold = 0} + 1 + in + let contract = Stdlib.List.hd contracts in + let* op1 = Op.incr_counter (B b) contract value_to_add in + let* b = + Incremental.begin_construction b >>=? fun inc -> + Incremental.add_operation inc op1 >>=? fun inc -> + Incremental.finalize_block inc >>=? fun b -> + Op.incr_counter (B b) contract value_to_add >>=? fun op2 -> + Incremental.begin_construction b >>=? fun inc -> + Incremental.add_operation inc op2 >>=? fun inc -> + Incremental.finalize_block inc + in + let* ctx = + Block.to_alpha_ctxt b >|=? Alpha_context.Internal_for_tests.to_raw + in + assert_stored_value ctx contract expected + +let tests = + [ + Tztest.tztest "init before add if storage not exist" `Quick test_init_and_add; + Tztest.tztest "adds value to storage" `Quick test_add; + ] diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/test_manager_operation_validation.ml b/src/proto_alpha/lib_protocol/test/integration/validate/test_manager_operation_validation.ml index 5d352f63cd9..bd2c5911c99 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/test_manager_operation_validation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/test_manager_operation_validation.ml @@ -86,7 +86,8 @@ let ensure_kind infos kind = | Sc_rollup_recover_bond _, K_Sc_rollup_recover_bond | Dal_publish_slot_header _, K_Dal_publish_slot_header | Zk_rollup_origination _, K_Zk_rollup_origination - | Zk_rollup_publish _, K_Zk_rollup_publish -> + | Zk_rollup_publish _, K_Zk_rollup_publish + | Incr_counter _ , _ -> return_unit | ( ( Transaction _ | Origination _ | Register_global_constant _ | Delegation _ | Set_deposits_limit _ | Update_consensus_key _ diff --git a/src/proto_alpha/lib_protocol/test/unit/main.ml b/src/proto_alpha/lib_protocol/test/unit/main.ml index 9df7b4fa13f..25b098194cd 100644 --- a/src/proto_alpha/lib_protocol/test/unit/main.ml +++ b/src/proto_alpha/lib_protocol/test/unit/main.ml @@ -87,5 +87,6 @@ let () = Unit_test.spec "Delegate_consensus_key.ml" Test_consensus_key.tests; Unit_test.spec "local_contexts" Test_local_contexts.tests; Unit_test.spec "dal slot proof" Test_dal_slot_proof.tests; + Unit_test.spec "test counter challenge" Test_counter_challenge.tests; ] |> Lwt_main.run diff --git a/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml b/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml new file mode 100644 index 00000000000..e78168876a8 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml @@ -0,0 +1,60 @@ +(** Testing + ------- + Component: Counter challenge + Invocation: dune exec \ + src/proto_alpha/lib_protocol/test/unit/main.exe \ + -- test "test counter challenge" + Subject: Unit test for counter challenge +*) + +open Protocol +open Lwt_result_syntax + +let init_context = + let* b, contract = Context.init1 () in + let* inc = Incremental.begin_construction b in + let ctx = Incremental.alpha_ctxt inc in + let ctx = Alpha_context.Internal_for_tests.to_raw ctx in + let contract = + match contract with + | Alpha_context.Contract.Implicit x -> Contract_repr.Implicit x + | Alpha_context.Contract.Originated x -> Contract_repr.Originated x + in + Lwt_result.return (ctx, contract) + +let test_init_and_add _ = + let* ctx, contract = init_context in + let test = + let* ctx, value = + Counter_challenge.incr_counter ctx contract (Z.of_int 5) + in + assert (value = Z.of_int 5) ; + let* storage_value = + Storage.Contract.Counter_challenge_store.get ctx contract + in + assert (storage_value = value) ; + return () + in + Lwt_result.map_error (fun _ -> []) test + +let test_add _ = + let to_add = Z.of_int 5 in + let* ctx, contract = init_context in + let test = + let* _, value = + Counter_challenge.incr_counter ctx contract to_add >>=? fun (ctx, _) -> + Counter_challenge.incr_counter ctx contract to_add + in + assert (value = Z.of_int 10) ; + return () + in + Lwt_result.map_error (fun _ -> []) test + +let tests = + [ + Tztest.tztest "add" `Quick test_add; + Tztest.tztest + "initial before add if storage not exists" + `Quick + test_init_and_add; + ] diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index 83414ab0044..488d4680f42 100644 --- a/src/proto_alpha/lib_protocol/validate.ml +++ b/src/proto_alpha/lib_protocol/validate.ml @@ -2443,6 +2443,7 @@ module Manager = struct Dal_apply.validate_publish_slot_header vi.ctxt slot_header | Zk_rollup_origination _ | Zk_rollup_publish _ -> assert_zk_rollup_feature_enabled vi + | Incr_counter _ -> return_unit in (* Gas should no longer be consumed below this point, because it would not take into account any gas consumed during the pattern -- GitLab From 1f0f5f7f1f44b8a95b4de736739dda7eb8dd87c0 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Mon, 31 Oct 2022 17:05:56 +0000 Subject: [PATCH 2/6] refactor to use global counter instead of local counter --- src/proto_alpha/lib_client/injection.ml | 9 ++-- .../lib_client/operation_result.ml | 3 +- src/proto_alpha/lib_protocol/alpha_context.ml | 5 +-- .../lib_protocol/alpha_context.mli | 17 +++----- src/proto_alpha/lib_protocol/apply.ml | 2 +- .../lib_protocol/counter_challenge.ml | 24 ++++++----- .../lib_protocol/counter_challenge.mli | 33 +++++++++++--- src/proto_alpha/lib_protocol/storage.ml | 19 ++++---- src/proto_alpha/lib_protocol/storage.mli | 9 ++-- .../operations/test_counter_challenge.ml | 17 ++++---- .../test/unit/test_counter_challenge.ml | 43 +++++++++---------- 11 files changed, 97 insertions(+), 84 deletions(-) diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index 948a02b84d0..cbedd923c65 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -342,7 +342,6 @@ let estimated_gas_single (type kind) | Zk_rollup_origination_result {consumed_gas; _} -> Ok consumed_gas | Zk_rollup_publish_result {consumed_gas; _} -> Ok consumed_gas | Incr_counter_result _ -> Ok Gas.Arith.zero) - | Skipped _ -> error_with "Cannot estimate gas of skipped operation" (* There must be another error for this to happen, and it should not @@ -426,8 +425,9 @@ let estimated_storage_single (type kind) ~tx_rollup_origination_size | Sc_rollup_cement_result _ | Sc_rollup_publish_result _ | Sc_rollup_refute_result _ | Sc_rollup_timeout_result _ | Sc_rollup_recover_bond_result _ - | Sc_rollup_dal_slot_subscribe_result _ - | Incr_counter_result _ -> + | Sc_rollup_dal_slot_subscribe_result _ + (* We do not charge for global counter increments as we believe in free *) + | Incr_counter_result _ -> Ok Z.zero) | Skipped _ -> error_with "Cannot estimate storage of skipped operation" @@ -520,8 +520,7 @@ let originated_contracts_single (type kind) | Sc_rollup_timeout_result _ | Sc_rollup_execute_outbox_message_result _ | Sc_rollup_recover_bond_result _ | Sc_rollup_dal_slot_subscribe_result _ | Zk_rollup_origination_result _ - | Zk_rollup_publish_result _ - | Incr_counter_result _ -> + | Zk_rollup_publish_result _ | Incr_counter_result _ -> Ok []) | Skipped _ -> error_with "Cannot know originated contracts of skipped operation" diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 7a9ebc2f4dd..d1fe96b74e3 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -422,7 +422,8 @@ let pp_manager_operation_content (type kind) source ppf Format.fprintf ppf "Zk rollup origination:@,From: %a" Contract.pp source | Zk_rollup_publish _ -> Format.fprintf ppf "Zk rollup publish:@,From: %a" Contract.pp source - | Incr_counter _ -> () + | Incr_counter {value} -> + Format.fprintf ppf "Increment counter by %a" Z.pp_print value let pp_balance_updates ppf balance_updates = let open Receipt in diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 91c089ec0ec..73a5657bade 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -620,7 +620,4 @@ module Internal_for_tests = struct let to_raw x = x end -module Counter_challenge_alpha = struct - include Counter_challenge - -end +module Counter_challenge = Counter_challenge diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 89072343936..c6a1f20ad36 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4034,7 +4034,7 @@ module Cache : sig end (** This module re-exports definitions from {!Lazy_storage_kind}. *) - module Kind : sig +module Kind : sig type preendorsement_consensus_kind = Preendorsement_consensus_kind type endorsement_consensus_kind = Endorsement_consensus_kind @@ -4430,8 +4430,7 @@ and _ manager_operation = ops : (Zk_rollup.Operation.t * Zk_rollup.Ticket.t option) list; } -> Kind.zk_rollup_publish manager_operation - | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation - + | Incr_counter : {value : Z.t} -> Kind.incr_counter manager_operation and counter = Z.t @@ -5066,13 +5065,9 @@ module Fees : sig val check_storage_limit : context -> storage_limit:Z.t -> unit tzresult end - (** This module re-exports definitions from {!Counter_challenge}. *) -module Counter_challenge_alpha : sig - val incr_counter : - context -> - Contract.t -> - Z.t -> - (context * Z.t) tzresult Lwt.t +module Counter_challenge : sig + val incr_counter : context -> Z.t -> (context * Z.t) tzresult Lwt.t - end \ No newline at end of file + val get_counter : Raw_context.t -> Z.t tzresult Lwt.t +end diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 1308f32cb0c..b04534c21ea 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1427,7 +1427,7 @@ let apply_manager_operation : Zk_rollup_apply.publish ~ctxt_before_op ~ctxt ~zk_rollup ~l2_ops:ops | Incr_counter {value} -> - Counter_challenge_alpha.incr_counter ctxt source_contract value >|=? fun (ctxt, value) -> + Counter_challenge.incr_counter ctxt value >|=? fun (ctxt, value) -> (ctxt, Incr_counter_result { value } , []) type success_or_failure = Success of context | Failure diff --git a/src/proto_alpha/lib_protocol/counter_challenge.ml b/src/proto_alpha/lib_protocol/counter_challenge.ml index 20c2696e6ef..5de67c262ee 100644 --- a/src/proto_alpha/lib_protocol/counter_challenge.ml +++ b/src/proto_alpha/lib_protocol/counter_challenge.ml @@ -1,11 +1,13 @@ -let incr_counter ctxt contract value = - let open Storage.Contract in - Counter_challenge_store.find ctxt contract >>=? fun maybe_v -> - match maybe_v with - | Some v -> - let value = Z.add v value in - Counter_challenge_store.update ctxt contract value >|=? fun ctxt -> - (ctxt, value) - | None -> - Counter_challenge_store.init ctxt contract value >|=? fun ctxt -> - (ctxt, value) + +let incr_counter ctx value = + let open Lwt_result_syntax in + let* stored_opt = Storage.Counter_challenge_store.find ctx in + let stored = Option.value stored_opt ~default:Z.zero in + let new_value = Z.add stored value in + let*! ctx = Storage.Counter_challenge_store.add ctx new_value in + return (ctx, new_value) + + +let get_counter ctx = + let open Storage in + Counter_challenge_store.get ctx \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/counter_challenge.mli b/src/proto_alpha/lib_protocol/counter_challenge.mli index e54956e8bf0..50b4b785352 100644 --- a/src/proto_alpha/lib_protocol/counter_challenge.mli +++ b/src/proto_alpha/lib_protocol/counter_challenge.mli @@ -1,5 +1,28 @@ -val incr_counter : - Raw_context.t -> - Contract_repr.t -> - Z.t -> - (Raw_context.t * Z.t) tzresult Lwt.t +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Trili Tech *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +val incr_counter : Raw_context.t -> Z.t -> (Raw_context.t * Z.t) tzresult Lwt.t + +val get_counter : Raw_context.t -> Z.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 87fb13e5755..35720cb803c 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -92,6 +92,16 @@ module type Simple_single_data_storage = sig val init : Raw_context.t -> value -> Raw_context.t tzresult Lwt.t end +module Counter_challenge_store : + Single_data_storage with + type value = Z.t + and type t := Raw_context.t = + Make_single_data_storage (Registered) (Raw_context) + (struct + let name = ["counter_challenge"] + end) + (Encoding.Z) + module Block_round : Simple_single_data_storage with type value = Round_repr.t = Make_single_data_storage (Registered) (Raw_context) (struct @@ -273,15 +283,6 @@ module Contract = struct end) (Encoding.Z) - - module Counter_challenge_store = - Indexed_context.Make_map - (Registered) - (struct - let name = ["counter_challenge"] - end) - (Encoding.Z) - (* Consume gas for serialization and deserialization of expr in this module *) module Make_carbonated_map_expr (N : Storage_sigs.NAME) : diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 296629e8b49..6ca4ccadcc0 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -47,6 +47,9 @@ module type Simple_single_data_storage = sig val init : Raw_context.t -> value -> Raw_context.t tzresult Lwt.t end +module Counter_challenge_store : + Single_data_storage with type value = Z.t and type t := Raw_context.t + module Block_round : Simple_single_data_storage with type value = Round_repr.t type deposits = {initial_amount : Tez_repr.t; current_amount : Tez_repr.t} @@ -180,12 +183,6 @@ module Contract : sig and type t := Raw_context.t and type local_context := local_context - module Counter_challenge_store : - Indexed_data_storage - with type key = Contract_repr.t - and type value = Z.t - and type t := Raw_context.t - module Code : Non_iterable_indexed_carbonated_data_storage with type key = Contract_repr.t diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml index 990ca03f2f7..fbb601c4d9c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml @@ -14,13 +14,14 @@ let convert_contract_repr contract = | Alpha_context.Contract.Implicit x -> Contract_repr.Implicit x | Alpha_context.Contract.Originated x -> Contract_repr.Originated x -let assert_stored_value ctx contract expected = - let contract = convert_contract_repr contract in - Storage.Contract.Counter_challenge_store.get ctx contract - >|=? (fun value -> assert (value = expected)) - >|= Environment.wrap_tzresult >>=? return +let assert_stored_value ctx expected = + let open Lwt_result_syntax in + Lwt.map Environment.wrap_tzresult + @@ let* value = Storage.Counter_challenge_store.get ctx in + let () = assert (value = expected) in + return_unit -let test_init_and_add _ = +let test_init_and_add () = let open Lwt_result_syntax in let expected = Z.of_int 3 in let* b, contract = Context.init1 () in @@ -33,7 +34,7 @@ let test_init_and_add _ = let* ctx = Block.to_alpha_ctxt b >|=? Alpha_context.Internal_for_tests.to_raw in - assert_stored_value ctx contract expected + assert_stored_value ctx expected let test_add _ = let open Lwt_result_syntax in @@ -58,7 +59,7 @@ let test_add _ = let* ctx = Block.to_alpha_ctxt b >|=? Alpha_context.Internal_for_tests.to_raw in - assert_stored_value ctx contract expected + assert_stored_value ctx expected let tests = [ diff --git a/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml b/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml index e78168876a8..19cd8c28d7c 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml @@ -11,42 +11,38 @@ open Protocol open Lwt_result_syntax let init_context = - let* b, contract = Context.init1 () in + let* b, _contract = Context.init1 () in let* inc = Incremental.begin_construction b in let ctx = Incremental.alpha_ctxt inc in let ctx = Alpha_context.Internal_for_tests.to_raw ctx in - let contract = - match contract with - | Alpha_context.Contract.Implicit x -> Contract_repr.Implicit x - | Alpha_context.Contract.Originated x -> Contract_repr.Originated x - in - Lwt_result.return (ctx, contract) + Lwt_result.return ctx let test_init_and_add _ = - let* ctx, contract = init_context in + let* ctx = init_context in let test = - let* ctx, value = - Counter_challenge.incr_counter ctx contract (Z.of_int 5) - in + let* ctx, value = Counter_challenge.incr_counter ctx (Z.of_int 5) in assert (value = Z.of_int 5) ; - let* storage_value = - Storage.Contract.Counter_challenge_store.get ctx contract - in - assert (storage_value = value) ; - return () + let* storage_value = Storage.Counter_challenge_store.get ctx in + return @@ assert (storage_value = value) in Lwt_result.map_error (fun _ -> []) test let test_add _ = + let* ctx = init_context in let to_add = Z.of_int 5 in - let* ctx, contract = init_context in let test = - let* _, value = - Counter_challenge.incr_counter ctx contract to_add >>=? fun (ctx, _) -> - Counter_challenge.incr_counter ctx contract to_add - in - assert (value = Z.of_int 10) ; - return () + let* ctx, _value = Counter_challenge.incr_counter ctx to_add in + let* _ctx, value = Counter_challenge.incr_counter ctx to_add in + return @@ assert (value = Z.of_int 10) + in + Lwt_result.map_error (fun _ -> []) test + +let test_get _ = + let* ctx = init_context in + let test = + let* ctx, _value = Counter_challenge.incr_counter ctx (Z.of_int 5) in + let* value = Counter_challenge.get_counter ctx in + return @@ assert (value = Z.of_int 5) in Lwt_result.map_error (fun _ -> []) test @@ -57,4 +53,5 @@ let tests = "initial before add if storage not exists" `Quick test_init_and_add; + Tztest.tztest "get" `Quick test_get; ] -- GitLab From 3d5ccd63b27426a69b10d70d774b8ec772bc3109 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Mon, 31 Oct 2022 17:14:19 +0000 Subject: [PATCH 3/6] add pp for incr counter result --- src/proto_alpha/lib_client/operation_result.ml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index d1fe96b74e3..dc9b88514cd 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -865,6 +865,9 @@ let pp_manager_operation_contents_result ppf op_result = pp_consumed_gas ppf consumed_gas ; pp_balance_updates ppf balance_updates in + let pp_incr_counter_result ( + Incr_counter_result {value}) = Format.fprintf ppf "@New global counter value: %a" Z.pp_print value ; + in let manager_operation_name (type kind) (result : kind successful_manager_operation_result) = match result with @@ -951,7 +954,7 @@ let pp_manager_operation_contents_result ppf op_result = pp_dal_publish_slot_header_result op | Zk_rollup_origination_result _ as op -> pp_zk_rollup_origination_result op | Zk_rollup_publish_result _ as op -> pp_zk_rollup_publish_result op - | Incr_counter_result _ -> () + | Incr_counter_result _ as op -> pp_incr_counter_result op in pp_operation_result ~operation_name:manager_operation_name -- GitLab From 394c69d6a79636f46bdfd3d8df08fd7f51501f34 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Tue, 1 Nov 2022 18:03:57 +0000 Subject: [PATCH 4/6] For counter challenge, implement RPCs, get counter, client integration and tezt tests --- .../lib_client/client_proto_args.ml | 8 ++ .../lib_client/client_proto_args.mli | 4 + .../lib_client/operation_result.ml | 2 +- .../client_proto_context_commands.ml | 78 +++++++++++++++++ .../lib_protocol/alpha_context.mli | 6 +- .../lib_protocol/alpha_services.ml | 24 +++++- .../lib_protocol/alpha_services.mli | 5 ++ src/proto_alpha/lib_protocol/apply_results.ml | 55 ++++++++++-- .../lib_protocol/counter_challenge.ml | 1 - .../lib_protocol/operation_repr.ml | 29 ++++--- .../lib_protocol/operation_repr.mli | 5 +- tezt/lib_tezos/client.ml | 16 ++++ tezt/lib_tezos/client.mli | 16 ++++ tezt/tests/counter_challenge.ml | 85 +++++++++++++++++++ tezt/tests/main.ml | 1 + 15 files changed, 308 insertions(+), 27 deletions(-) create mode 100644 tezt/tests/counter_challenge.ml diff --git a/src/proto_alpha/lib_client/client_proto_args.ml b/src/proto_alpha/lib_client/client_proto_args.ml index 55dab37898e..698ba5b6467 100644 --- a/src/proto_alpha/lib_client/client_proto_args.ml +++ b/src/proto_alpha/lib_client/client_proto_args.ml @@ -1035,3 +1035,11 @@ let fee_parameter_args = force_low_fee_arg fee_cap_arg burn_cap_arg)) + +module Counter_challenge_params = struct + let incr_counter_parameter = + Clic.parameter (fun _ s -> + match Int64.of_string_opt s with + | Some z -> return (Z.of_int64 z) + | None -> failwith "'%s' is not valid, should be a int64 value" s) +end \ No newline at end of file diff --git a/src/proto_alpha/lib_client/client_proto_args.mli b/src/proto_alpha/lib_client/client_proto_args.mli index c10aaaa0827..8cad6eeb0f0 100644 --- a/src/proto_alpha/lib_client/client_proto_args.mli +++ b/src/proto_alpha/lib_client/client_proto_args.mli @@ -264,3 +264,7 @@ module Sc_rollup_params : sig end val fee_parameter_args : (Injection.fee_parameter, full) Clic.arg + +module Counter_challenge_params: sig + val incr_counter_parameter : (Z.t, full) Clic.parameter +end diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index dc9b88514cd..6b3944bc265 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -866,7 +866,7 @@ let pp_manager_operation_contents_result ppf op_result = pp_balance_updates ppf balance_updates in let pp_incr_counter_result ( - Incr_counter_result {value}) = Format.fprintf ppf "@New global counter value: %a" Z.pp_print value ; + Incr_counter_result {value}) = Format.fprintf ppf "@,New global counter value: %a" Z.pp_print value ; in let manager_operation_name (type kind) (result : kind successful_manager_operation_result) = diff --git a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml index 8ad1a7e444a..d0010bdbcb1 100644 --- a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml +++ b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml @@ -712,6 +712,22 @@ let commands_ro () = cctxt#answer "%a %s" Tez.pp limit Operation_result.tez_sym in return_unit); + command + ~group + ~desc:"Get counter challenge value" + no_options + (prefixes ["get"; "counter"; "challenge"; "value"] @@ stop) + (fun () (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* value = + Alpha_services.Counter_challenge_services.get_counter_challenge_ctr + cctxt + (cctxt#chain, cctxt#block) + in + let*! () = + cctxt#answer "Counter Challenge's counter is %a" Z.pp_print value + in + return_unit); ] (* ----------------------------------------------------------------------------*) @@ -3416,6 +3432,68 @@ let commands_rw () = () in return_unit); + command + ~group + ~desc:"Increment counter challenge" + (args7 + gas_limit_arg + fee_arg + dry_run_switch + verbose_signing_switch + simulate_switch + fee_parameter_args + storage_limit_arg) + (prefixes ["increment"; "counter"; "by"] + @@ Clic.param + ~name:"value" + ~desc:"The amount to increment by" + Counter_challenge_params.incr_counter_parameter + @@ prefixes ["src"] + @@ Client_keys.Public_key_hash.source_param + ~desc:"The implicit account to operate on" + @@ stop) + (fun ( gas_limit, + fee, + dry_run, + verbose_signing, + simulation, + fee_parameter, + storage_limit ) + value + source + cctxt -> + let open Lwt_result_syntax in + let* _, src_pk, src_sk = Client_keys.get_key cctxt source in + let operations = + Annotated_manager_operation.Single_manager + (Annotated_manager_operation.Manager_info + { + source = None; + fee = Limit.of_option fee; + gas_limit = Limit.unknown; + storage_limit = Limit.of_option storage_limit; + counter = None; + operation = Incr_counter {value}; + }) + in + let* _ = + Injection.inject_manager_operation + ~chain:cctxt#chain + ~block:cctxt#block + ~dry_run + ~source + ~verbose_signing + ~simulation + ~storage_limit:(Limit.of_option storage_limit) + ~src_pk + ~src_sk + ~fee:(Limit.of_option fee) + ~gas_limit:(Limit.of_option gas_limit) + ~fee_parameter + cctxt + operations + in + return_unit); ] let commands network () = diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index c6a1f20ad36..b1a6dee13e5 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4633,6 +4633,8 @@ module Operation : sig val zk_rollup_publish_case : Kind.zk_rollup_publish Kind.manager case + val incr_counter_case : Kind.incr_counter Kind.manager case + module Manager_operations : sig type 'b case = | MCase : { @@ -5069,5 +5071,5 @@ end module Counter_challenge : sig val incr_counter : context -> Z.t -> (context * Z.t) tzresult Lwt.t - val get_counter : Raw_context.t -> Z.t tzresult Lwt.t -end + val get_counter : context -> Z.t tzresult Lwt.t +end \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/alpha_services.ml b/src/proto_alpha/lib_protocol/alpha_services.ml index 1dfb872f9cc..e48cb73ef9c 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.ml +++ b/src/proto_alpha/lib_protocol/alpha_services.ml @@ -300,6 +300,27 @@ module Cache = struct RPC_context.make_call0 S.contract_rank ctxt block () contract end +module Counter_challenge_services = struct + module S = struct + let get_counter_challenge_ctr = + RPC_service.get_service + ~description:"Gets value of the Counter Challenge's counter" + ~query:RPC_query.empty + ~output:Data_encoding.z + RPC_path.(custom_root / "context" / "counter-challenge-counter") + end + + + let get_counter_challenge_ctr ctxt block = + RPC_context.make_call0 S.get_counter_challenge_ctr ctxt block () () + + let register () = + let open Services_registration in + register0 ~chunked:false S.get_counter_challenge_ctr (fun ctxt () () -> + Alpha_context.Counter_challenge.get_counter ctxt + ) +end + let register () = Contract.register () ; Constants.register () ; @@ -310,4 +331,5 @@ let register () = Sapling.register () ; Liquidity_baking.register () ; Cache.register () ; - Tx_rollup.register () + Tx_rollup.register () ; + Counter_challenge_services.register (); diff --git a/src/proto_alpha/lib_protocol/alpha_services.mli b/src/proto_alpha/lib_protocol/alpha_services.mli index 14235f19a69..a28ab62f073 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.mli +++ b/src/proto_alpha/lib_protocol/alpha_services.mli @@ -93,3 +93,8 @@ module Cache : sig end val register : unit -> unit + +module Counter_challenge_services : sig + val get_counter_challenge_ctr : + 'a #RPC_context.simple -> 'a -> Z.t shell_tzresult Lwt.t +end \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 3c7fd66e460..4008ca6a7c8 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -193,7 +193,10 @@ type _ successful_manager_operation_result = paid_storage_size_diff : Z.t; } -> Kind.zk_rollup_publish successful_manager_operation_result - | Incr_counter_result: {value: Z.t} -> Kind.incr_counter successful_manager_operation_result + | Incr_counter_result : { + value : Z.t; + } + -> Kind.incr_counter successful_manager_operation_result let migration_origination_result_to_successful_manager_operation_result ({ @@ -312,6 +315,17 @@ module Manager_result = struct ~proj:(function Reveal_result {consumed_gas} -> consumed_gas) ~inj:(fun consumed_gas -> Reveal_result {consumed_gas}) + let incr_counter_case = + make + ~op_case:Operation.Encoding.Manager_operations.incr_counter_case + ~encoding:Data_encoding.(obj1 (req "value" Data_encoding.z)) + ~select:(function + | Successful_manager_result (Incr_counter_result _ as op) -> Some op + | _ -> None) + ~kind:Kind.Incr_counter_manager_kind + ~proj:(function Incr_counter_result {value} -> value) + ~inj:(fun value -> Incr_counter_result {value}) + let transaction_contract_variant_cases = union [ @@ -1238,8 +1252,8 @@ let equal_manager_kind : | Kind.Zk_rollup_publish_manager_kind, Kind.Zk_rollup_publish_manager_kind -> Some Eq | Kind.Zk_rollup_publish_manager_kind, _ -> None - | Kind.Incr_counter_manager_kind , Kind.Incr_counter_manager_kind -> None - | Kind.Incr_counter_manager_kind , _ -> None + | Kind.Incr_counter_manager_kind, Kind.Incr_counter_manager_kind -> None + | Kind.Incr_counter_manager_kind, _ -> None module Encoding = struct type 'kind case = @@ -1921,6 +1935,16 @@ module Encoding = struct -> Some (op, res) | _ -> None) + + let incr_counter_case = + make_manager_case + Operation.Encoding.incr_counter_case + Manager_result.incr_counter_case + (function + | Contents_and_result + ((Manager_operation {operation = Incr_counter _; _} as op), res) -> + Some (op, res) + | _ -> None) end let contents_result_encoding = @@ -1983,6 +2007,7 @@ let contents_result_encoding = make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make incr_counter_case; ] let contents_and_result_encoding = @@ -2050,11 +2075,12 @@ let contents_and_result_encoding = make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make incr_counter_case; ] type 'kind contents_result_list = | Single_result : 'kind contents_result -> 'kind contents_result_list - | Cons_result : + | Cons_result : 'kind Kind.manager contents_result * 'rest Kind.manager contents_result_list -> ('kind * 'rest) Kind.manager contents_result_list @@ -2076,7 +2102,7 @@ let contents_result_list_encoding = | Contents_result o :: os -> ( of_list os >>? fun (Contents_result_list os) -> match (o, os) with - | Manager_operation_result _, Single_result (Manager_operation_result _) + | Manager_operation_result _, Single_result (Manager_operation_result _) -> Ok (Contents_result_list (Cons_result (o, os))) | Manager_operation_result _, Cons_result _ -> @@ -2956,8 +2982,23 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Zk_rollup_publish _; _}, _ -> None - | Manager_operation {operation = Incr_counter _; _ }, _ -> None - + | ( Manager_operation {operation = Incr_counter _; _}, + Manager_operation_result + {operation_result = Applied (Incr_counter_result _); _} ) -> + Some Eq + | ( Manager_operation {operation = Incr_counter _; _}, + Manager_operation_result + {operation_result = Backtracked (Incr_counter_result _, _); _} ) -> + Some Eq + | ( Manager_operation {operation = Incr_counter _; _}, + Manager_operation_result + {operation_result = Skipped Incr_counter_manager_kind; _} ) -> + Some Eq + | ( Manager_operation {operation = Incr_counter _; _}, + Manager_operation_result + {operation_result = Failed (Incr_counter_manager_kind, _); _} ) -> + Some Eq + | Manager_operation {operation = Incr_counter _; _}, _ -> None let rec kind_equal_list : type kind kind2. diff --git a/src/proto_alpha/lib_protocol/counter_challenge.ml b/src/proto_alpha/lib_protocol/counter_challenge.ml index 5de67c262ee..3454a4c3dab 100644 --- a/src/proto_alpha/lib_protocol/counter_challenge.ml +++ b/src/proto_alpha/lib_protocol/counter_challenge.ml @@ -1,4 +1,3 @@ - let incr_counter ctx value = let open Lwt_result_syntax in let* stored_opt = Storage.Counter_challenge_store.find ctx in diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index 53458e77a82..ea697772046 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -126,7 +126,7 @@ module Kind = struct type zk_rollup_origination = Zk_rollup_origination_kind type zk_rollup_publish = Zk_rollup_publish_kind - + type incr_counter = Incr_counter_kind type 'a manager = @@ -492,7 +492,7 @@ and _ manager_operation = ops : (Zk_rollup_operation_repr.t * Zk_rollup_ticket_repr.t option) list; } -> Kind.zk_rollup_publish manager_operation - | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation + | Incr_counter : {value : Z.t} -> Kind.incr_counter manager_operation and counter = Z.t @@ -661,15 +661,17 @@ module Encoding = struct } -> 'kind case - let incr_counter_case = - MCase { - tag = incr_counter_tag; - name = "increment counter"; - encoding = obj1 (req "value" Data_encoding.z); - select = (function Manager (Incr_counter _ as op ) -> Some op | _ -> None); - proj = (function Incr_counter { value } -> value); - inj = (fun value -> Incr_counter { value }); - } + let incr_counter_case = + MCase + { + tag = incr_counter_tag; + name = "increment counter"; + encoding = obj1 (req "value" Data_encoding.z); + select = + (function Manager (Incr_counter _ as op) -> Some op | _ -> None); + proj = (function Incr_counter {value} -> value); + inj = (fun value -> Incr_counter {value}); + } let reveal_case = MCase @@ -1665,8 +1667,6 @@ module Encoding = struct (extract op, mcase.proj operation)); inj = (fun (op, contents) -> rebuild op (mcase.inj contents)); } - - let incr_counter_case = make_manager_case 142 Manager_operations.incr_counter_case let reveal_case = make_manager_case 107 Manager_operations.reveal_case @@ -1795,6 +1795,9 @@ module Encoding = struct zk_rollup_operation_publish_tag Manager_operations.zk_rollup_publish_case + let incr_counter_case = + make_manager_case 142 Manager_operations.incr_counter_case + let contents_encoding = let make (Case {tag; name; encoding; select; proj; inj}) = case diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index c089b9c8293..4e6dfdf8ed1 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -197,7 +197,6 @@ module Kind : sig | Zk_rollup_origination_manager_kind : zk_rollup_origination manager | Zk_rollup_publish_manager_kind : zk_rollup_publish manager | Incr_counter_manager_kind : incr_counter manager - end type 'a consensus_operation_type = @@ -586,7 +585,7 @@ and _ manager_operation = (* See {!Zk_rollup_apply} *) } -> Kind.zk_rollup_publish manager_operation - | Incr_counter: {value : Z.t} -> Kind.incr_counter manager_operation + | Incr_counter : {value : Z.t} -> Kind.incr_counter manager_operation (** Counters are used as anti-replay protection mechanism in manager operations: each manager account stores a counter and @@ -844,6 +843,8 @@ module Encoding : sig val zk_rollup_publish_case : Kind.zk_rollup_publish Kind.manager case + val incr_counter_case : Kind.incr_counter Kind.manager case + module Manager_operations : sig type 'b case = | MCase : { diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 2b4567c1926..eadc75fd2a2 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -836,6 +836,22 @@ let spawn_transfer ?hooks ?log_output ?endpoint ?(wait = "none") ?burn_cap ?fee @ (if simulation then ["--simulation"] else []) @ if force then ["--force"] else []) +let increment_counter ?hooks ?log_output ?endpoint ?burn_cap + ?gas_limit ?storage_limit ?counter ?entrypoint ?(simulation = false) + ?(force = false) src value client = + let command = + ["increment"; "counter"; "by"; Z.to_string value] + @ ["src"; src] + @ optional_arg "burn-cap" Tez.to_string burn_cap + @ optional_arg "gas-limit" string_of_int gas_limit + @ optional_arg "storage-limit" string_of_int storage_limit + @ optional_arg "counter" string_of_int counter + @ optional_arg "entrypoint" Fun.id entrypoint + @ (if simulation then ["--simulation"] else []) + @ if force then ["--force"] else [] + in + spawn_command ?log_output ?endpoint ?hooks client command + let transfer ?hooks ?log_output ?endpoint ?wait ?burn_cap ?fee ?gas_limit ?storage_limit ?counter ?entrypoint ?arg ?simulation ?force ?expect_failure ~amount ~giver ~receiver client = diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 3c792ac843b..2ffdd528336 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -662,6 +662,22 @@ val spawn_transfer : t -> Process.t +val increment_counter: + ?hooks:Process.hooks -> + ?log_output:bool -> + ?endpoint:endpoint -> + ?burn_cap:Tez.t -> + ?gas_limit:int -> + ?storage_limit:int -> + ?counter:int -> + ?entrypoint:string -> + ?simulation:bool -> + ?force:bool -> + string -> + Z.t -> + t -> + Process.t + (** Run [octez-client multiple transfers from giver using json_batch]. *) val multiple_transfers : ?log_output:bool -> diff --git a/tezt/tests/counter_challenge.ml b/tezt/tests/counter_challenge.ml new file mode 100644 index 00000000000..15a056ce0e8 --- /dev/null +++ b/tezt/tests/counter_challenge.ml @@ -0,0 +1,85 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2021 Nomadic Labs *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(* Testing + ------- + Component: Precheck + Invocation: dune exec tezt/tests/main.exe -- --file counter_challenge.ml + Subject: Tests that covers differents cases of manager operations +*) + +open Tezt_tezos + +let increment_counter_command value src = + ["increment"; "counter"; "by"; Int64.to_string value; "src"; src] + +let increment_counter client value src = + let* client_output = + Client.increment_counter src value client ~gas_limit:1000 + |> Process.check_and_read_stdout + in + return client_output + +let get_counter client = + let command = ["get"; "counter"; "challenge"; "value"] in + let* client_output = + Client.spawn_command client command |> Process.check_and_read_stdout + in + return client_output + +let test_init_and_add = + Protocol.register_test + ~__FILE__ + ~title:"initialize and add to counter (challenge)" + ~tags:[] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let src = Constant.bootstrap1.public_key_hash in + let* _stdout = increment_counter client (Z.of_int 5) src in + let* result = get_counter client in + let trimmed_result = String.trim result in + trimmed_result |> Printf.printf "%s\n" ; + assert (trimmed_result = "Counter Challenge's counter is 5") ; + Lwt.return () + +let test_add = + Protocol.register_test + ~__FILE__ + ~title:"add to counter (challenge)" + ~tags:[] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let src = Constant.bootstrap1.public_key_hash in + let* _stdout = increment_counter client (Z.of_int 5) src in + let* _stdout = increment_counter client (Z.of_int 12) src in + let* result = get_counter client in + let trimmed_result = String.trim result in + trimmed_result |> Printf.printf "%s\n" ; + assert (trimmed_result = "Counter Challenge's counter is 17") ; + Lwt.return () + +let register ~protocols = + test_init_and_add protocols ; + test_add protocols diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 8198ec646e1..23ec115b216 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -108,6 +108,7 @@ let register_protocol_agnostic_tests () = Light.register ~protocols:[Alpha] ; Liquidity_baking_per_block_votes.register ~protocols ; Manager_operations.register ~protocols ; + Counter_challenge.register ~protocols:[Alpha]; Mockup.register ~protocols ; Mockup.register_global_constants ~protocols:[Alpha] ; Monitor_operations.register ~protocols:[Alpha] ; -- GitLab From 220357a589b95fc5bdc98e086bef00555c617c94 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Wed, 2 Nov 2022 17:48:19 +0000 Subject: [PATCH 5/6] test incrementing from multiple account's work --- tezt/tests/counter_challenge.ml | 39 ++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/tezt/tests/counter_challenge.ml b/tezt/tests/counter_challenge.ml index 15a056ce0e8..b521c997c63 100644 --- a/tezt/tests/counter_challenge.ml +++ b/tezt/tests/counter_challenge.ml @@ -65,10 +65,7 @@ let test_init_and_add = Lwt.return () let test_add = - Protocol.register_test - ~__FILE__ - ~title:"add to counter (challenge)" - ~tags:[] + Protocol.register_test ~__FILE__ ~title:"add to counter (challenge)" ~tags:[] @@ fun protocol -> let* client = Client.init_mockup ~protocol () in let src = Constant.bootstrap1.public_key_hash in @@ -80,6 +77,38 @@ let test_add = assert (trimmed_result = "Counter Challenge's counter is 17") ; Lwt.return () +let test_add_from_multiple_source = + Protocol.register_test + ~__FILE__ + ~title:"add to counter from multiple source (challenge)" + ~tags:[] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let srcs = + [ + Constant.bootstrap1; + Constant.bootstrap2; + Constant.bootstrap3; + Constant.bootstrap4; + Constant.bootstrap5; + ] + in + let* _stdout = + let z_value = Z.of_int 5 in + List.fold_left + (fun acc (src: Account.key) -> + let* _acc = acc in + let* _stdout = increment_counter client z_value src.public_key_hash in + Lwt.return_unit) + Lwt.return_unit + srcs + in + let* result = get_counter client in + let trimmed_result = String.trim result in + assert (trimmed_result = "Counter Challenge's counter is 25") ; + Lwt.return () + let register ~protocols = test_init_and_add protocols ; - test_add protocols + test_add protocols; + test_add_from_multiple_source protocols; -- GitLab From a6b301776672cae400cf459eaeb429be9b6c0461 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Wed, 2 Nov 2022 17:52:04 +0000 Subject: [PATCH 6/6] refactor client func into Client --- tezt/lib_tezos/client.ml | 15 +++++++++++---- tezt/lib_tezos/client.mli | 4 +++- tezt/tests/counter_challenge.ml | 13 +++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index eadc75fd2a2..49389ee3cb3 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -836,11 +836,11 @@ let spawn_transfer ?hooks ?log_output ?endpoint ?(wait = "none") ?burn_cap ?fee @ (if simulation then ["--simulation"] else []) @ if force then ["--force"] else []) -let increment_counter ?hooks ?log_output ?endpoint ?burn_cap - ?gas_limit ?storage_limit ?counter ?entrypoint ?(simulation = false) - ?(force = false) src value client = +let increment_counter ?hooks ?log_output ?endpoint ?burn_cap ?gas_limit + ?storage_limit ?counter ?entrypoint ?(simulation = false) ?(force = false) + src value client = let command = - ["increment"; "counter"; "by"; Z.to_string value] + ["increment"; "counter"; "by"; Z.to_string value] @ ["src"; src] @ optional_arg "burn-cap" Tez.to_string burn_cap @ optional_arg "gas-limit" string_of_int gas_limit @@ -852,6 +852,13 @@ let increment_counter ?hooks ?log_output ?endpoint ?burn_cap in spawn_command ?log_output ?endpoint ?hooks client command +let get_counter client = + let command = ["get"; "counter"; "challenge"; "value"] in + let* client_output = + spawn_command client command |> Process.check_and_read_stdout + in + return client_output + let transfer ?hooks ?log_output ?endpoint ?wait ?burn_cap ?fee ?gas_limit ?storage_limit ?counter ?entrypoint ?arg ?simulation ?force ?expect_failure ~amount ~giver ~receiver client = diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 2ffdd528336..c267f10206a 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -662,7 +662,7 @@ val spawn_transfer : t -> Process.t -val increment_counter: +val increment_counter : ?hooks:Process.hooks -> ?log_output:bool -> ?endpoint:endpoint -> @@ -678,6 +678,8 @@ val increment_counter: t -> Process.t +val get_counter : t -> string Lwt.t + (** Run [octez-client multiple transfers from giver using json_batch]. *) val multiple_transfers : ?log_output:bool -> diff --git a/tezt/tests/counter_challenge.ml b/tezt/tests/counter_challenge.ml index b521c997c63..fefdded7c45 100644 --- a/tezt/tests/counter_challenge.ml +++ b/tezt/tests/counter_challenge.ml @@ -42,13 +42,6 @@ let increment_counter client value src = in return client_output -let get_counter client = - let command = ["get"; "counter"; "challenge"; "value"] in - let* client_output = - Client.spawn_command client command |> Process.check_and_read_stdout - in - return client_output - let test_init_and_add = Protocol.register_test ~__FILE__ @@ -58,7 +51,7 @@ let test_init_and_add = let* client = Client.init_mockup ~protocol () in let src = Constant.bootstrap1.public_key_hash in let* _stdout = increment_counter client (Z.of_int 5) src in - let* result = get_counter client in + let* result = Client.get_counter client in let trimmed_result = String.trim result in trimmed_result |> Printf.printf "%s\n" ; assert (trimmed_result = "Counter Challenge's counter is 5") ; @@ -71,7 +64,7 @@ let test_add = let src = Constant.bootstrap1.public_key_hash in let* _stdout = increment_counter client (Z.of_int 5) src in let* _stdout = increment_counter client (Z.of_int 12) src in - let* result = get_counter client in + let* result = Client.get_counter client in let trimmed_result = String.trim result in trimmed_result |> Printf.printf "%s\n" ; assert (trimmed_result = "Counter Challenge's counter is 17") ; @@ -103,7 +96,7 @@ let test_add_from_multiple_source = Lwt.return_unit srcs in - let* result = get_counter client in + let* result = Client.get_counter client in let trimmed_result = String.trim result in assert (trimmed_result = "Counter Challenge's counter is 25") ; Lwt.return () -- GitLab