diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon.ml b/src/proto_alpha/bin_sc_rollup_node/daemon.ml index b8c1454b69eb70463f5322b322dbd839df8c0a50..d68562f09f49462f20d22c03b6c56633193b7fd9 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 982460e53953e6c87c7999ab26577bf922975ea7..e5750f99b15c8ba78d853c6c7b8ad93d9d4f5c99 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/client_proto_args.ml b/src/proto_alpha/lib_client/client_proto_args.ml index 55dab37898eccf7e8dec4cb14b4935f781d4aa22..698ba5b6467e535feba6410d40386677962ca452 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 c10aaaa08278c7c73eb89fbee438dd655b93c632..8cad6eeb0f0a10bc068f86fda364724452c381b2 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/injection.ml b/src/proto_alpha/lib_client/injection.ml index bec0a611639205922f1063c497241756ab86c71a..cbedd923c65ebbd727e4774815d25f7149635f61 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -340,7 +340,8 @@ 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 +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 _ -> + | 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" @@ -517,7 +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 _ -> + | 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 e0c8d5a1e1c57a0f534e7a299b054af8c9330b9e..6b3944bc26501a435927520960ca298d0d563f63 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -422,6 +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 {value} -> + Format.fprintf ppf "Increment counter by %a" Z.pp_print value let pp_balance_updates ppf balance_updates = let open Receipt in @@ -863,7 +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 @@ -904,6 +908,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 +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 _ as op -> pp_incr_counter_result op in pp_operation_result ~operation_name:manager_operation_name 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 8ad1a7e444a6b6ed82305764e118947d6f322dde..d0010bdbcb13ea46bcbf229060824f6e6568bfa2 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_injector/l1_operation.ml b/src/proto_alpha/lib_injector/l1_operation.ml index e82d6668f88d75be8a90714a929b9518ead9814e..7f4fb1f3b2201f13678efcb71d937a222cb0d988 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 35182bbcdc57121b6400b72ee4f2af12cd110490..5be4ddb98d7230618389ccc7b6e4f69ac1f05104 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 032c6b2ffc2c3225aba09f8d0fbb73a39b034a63..73a5657badee488d1976b0f9bf1709f1b240be8f 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -619,3 +619,5 @@ module Cache = Cache_repr module Internal_for_tests = struct let to_raw x = x 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 1c9a7ed52b97c9cdbc58dbeb1ba1a9f83c5b7f7b..b1a6dee13e58c82fd8a62c9155d8ad8cf776cbed 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -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,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 and counter = Z.t @@ -4629,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 : { @@ -4704,6 +4710,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 +5066,10 @@ 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 : sig + val incr_counter : context -> Z.t -> (context * Z.t) tzresult Lwt.t + + 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 1dfb872f9ccab3e5c7301e775fe12edae85a0200..e48cb73ef9c807b7c797ae06a496f71413c88da9 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 14235f19a699785940202b9a499741ff6932bf8d..a28ab62f073eee202802c132a9eb2fe5df8a700b 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.ml b/src/proto_alpha/lib_protocol/apply.ml index 95e8eb0ea38965bd4c3381a3570844e80acd04e9..b04534c21ead28fa59a64db2c9cd3cb5cff4aa41 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.incr_counter ctxt 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 346b542c012daef2ef0c84fa9df10ef34b11f9a4..4008ca6a7c8e84bf378082f75a540ecd8301bfde 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -193,6 +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 let migration_origination_result_to_successful_manager_operation_result ({ @@ -311,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 [ @@ -1237,6 +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 module Encoding = struct type 'kind case = @@ -1918,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 = @@ -1980,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 = @@ -2047,6 +2075,7 @@ 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 = @@ -2953,6 +2982,23 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Zk_rollup_publish _; _}, _ -> 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/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index b468b0bae19ac3a71b48b2a44cd2e526dd892107..eda31d424483243d89caa1cea5211fe11b75fd01 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 0000000000000000000000000000000000000000..3454a4c3dab3c0496befb19e75d3c6571e0da9d2 --- /dev/null +++ b/src/proto_alpha/lib_protocol/counter_challenge.ml @@ -0,0 +1,12 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..50b4b7853523dbc2d74afbed4e7ca5bde55bf280 --- /dev/null +++ b/src/proto_alpha/lib_protocol/counter_challenge.mli @@ -0,0 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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/dune b/src/proto_alpha/lib_protocol/dune index b53da12c6614c4b1843968f3e638c45d8d25312d..c981f540a1edc9d4a2c6219899897d8614052e80 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 a66d09dc635a20c44950d4449ea96aea0d7711e4..ea69777204607f8f2be931c2392e08593f423a78 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -127,6 +127,8 @@ module Kind = struct 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 @@ -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,18 @@ 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 { @@ -1776,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 @@ -1830,6 +1852,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 +2116,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 73e42f975ed4bf6bb00973e8579c35ac925c1b50..4e6dfdf8ed10c9c23543ba8ba12f7ae9ea71b0b3 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,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 type 'a consensus_operation_type = @@ -582,6 +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 (** Counters are used as anti-replay protection mechanism in manager operations: each manager account stores a counter and @@ -839,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 : { @@ -913,5 +919,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 9b47de7bd99c9524adb13c9e133ad6602fd731e4..35720cb803ce06fb988e1dadd84b216daf0d6d57 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 diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index c7ae069a2062f75ebfc52521644225faaf403b88..6ca4ccadcc0d62c9b85363e144e363aaf36c32f7 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} diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 3291ae729c4fafaa9b3e1ac0f82998c00a1d3a2e..37093d6d1d651686bd0aa8ef3efcd258621f13c5 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 5fbc14b0f8ad12ebbdba5da836b8300f28ce9cbb..249c332915685a4dcc8e94fc3d19b9e9a9aa3e2d 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 1dea72fd81c166050b15a9b056c69c7867bd7903..1e8ce6a65c7fa790ae68a26561b151a467489825 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 afcec61e65e4a3e501b6d47c2d4ea69267bf815f..83c42b7c884966b1e678c28754796f14c14fe293 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 0000000000000000000000000000000000000000..fbb601c4d9cd4f82f49e9ef4267d478c32b84a50 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_counter_challenge.ml @@ -0,0 +1,68 @@ +(** 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 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 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 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 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 5d352f63cd9951a10cb4532247169ecad21b85bc..bd2c5911c99d0f70cec501c10b39d289001cd945 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 9df7b4fa13f78723a4fbcdf7ed23ac01af6554e8..25b098194cdc32536b4e0cb907e0480c8a2d434f 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 0000000000000000000000000000000000000000..19cd8c28d7cbce43a56d162a08d6159cff892f32 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/unit/test_counter_challenge.ml @@ -0,0 +1,57 @@ +(** 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 + Lwt_result.return ctx + +let test_init_and_add _ = + let* ctx = init_context in + let test = + let* ctx, value = Counter_challenge.incr_counter ctx (Z.of_int 5) in + assert (value = Z.of_int 5) ; + 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 test = + 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 + +let tests = + [ + Tztest.tztest "add" `Quick test_add; + Tztest.tztest + "initial before add if storage not exists" + `Quick + test_init_and_add; + Tztest.tztest "get" `Quick test_get; + ] diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index 83414ab00446f2e0b37bcf6e15d09af57b7b3f92..488d4680f4226c5fa488883ce59e27e732847391 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 diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 2b4567c19262cb38bb13ab11987d95540dc6c271..49389ee3cb392a0d6e409f1b52ab1ebddfcc1251 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -836,6 +836,29 @@ 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 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 3c792ac843be9a08a3b9fc7c76c2ebc0816247c6..c267f10206aa045009852ab2f141f9a2facefd1d 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -662,6 +662,24 @@ 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 + +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 new file mode 100644 index 0000000000000000000000000000000000000000..fefdded7c45de0fe5fd55e658383611b398fd72f --- /dev/null +++ b/tezt/tests/counter_challenge.ml @@ -0,0 +1,107 @@ +(*****************************************************************************) +(* *) +(* 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 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 = 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") ; + 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 = 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") ; + 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 = Client.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_from_multiple_source protocols; diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 8198ec646e1e836812f710d476ad305f37045985..23ec115b216f5b02fd514c8634f7746e2c95357a 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] ;