From 45145dbfacb67506831d8c28e2b175a07806c561 Mon Sep 17 00:00:00 2001 From: Ilya Peresadin Date: Fri, 28 Oct 2022 10:42:45 +0100 Subject: [PATCH 1/4] Implement shared counter protocol part --- src/proto_alpha/lib_client/injection.ml | 8 ++--- .../lib_client/operation_result.ml | 6 ++++ src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 2 ++ src/proto_alpha/lib_protocol/alpha_context.ml | 2 ++ .../lib_protocol/alpha_context.mli | 8 +++++ src/proto_alpha/lib_protocol/apply.ml | 12 +++++-- src/proto_alpha/lib_protocol/apply_results.ml | 33 +++++++++++++++++++ .../lib_protocol/apply_results.mli | 3 ++ src/proto_alpha/lib_protocol/dune | 1 + .../lib_protocol/operation_repr.ml | 7 ++++ .../lib_protocol/operation_repr.mli | 5 +++ .../shared_global_counter_storage.ml | 4 +++ .../shared_global_counter_storage.mli | 2 ++ src/proto_alpha/lib_protocol/storage.ml | 7 ++++ src/proto_alpha/lib_protocol/storage.mli | 2 ++ .../lib_protocol/test/helpers/block.ml | 5 +-- .../test/integration/validate/test_sanity.ml | 2 +- src/proto_alpha/lib_protocol/validate.ml | 1 + 18 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/shared_global_counter_storage.ml create mode 100644 src/proto_alpha/lib_protocol/shared_global_counter_storage.mli diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index bec0a611639..ffd37a16b3c 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 + | Increment_global_counter_result {consumed_gas} -> Ok consumed_gas) | Skipped _ -> error_with "Cannot estimate gas of skipped operation" (* There must be another error for this to happen, and it should not @@ -424,8 +425,7 @@ 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 _ -> - Ok Z.zero) + | Sc_rollup_dal_slot_subscribe_result _ | Increment_global_counter_result _ -> Ok Z.zero) | Skipped _ -> error_with "Cannot estimate storage of skipped operation" (* There must be another error for this to happen, and it should not @@ -517,7 +517,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 _ | Increment_global_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 5d18d7f875c..8b73857c7a1 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 + | Increment_global_counter -> + Format.fprintf ppf "Global counter increment:@,From: %a" Contract.pp source let pp_balance_updates ppf balance_updates = let open Receipt in @@ -863,6 +865,8 @@ let pp_manager_operation_contents_result ppf op_result = pp_consumed_gas ppf consumed_gas ; pp_balance_updates ppf balance_updates in + let pp_increment_global_counter_result (Increment_global_counter_result {consumed_gas}) = pp_consumed_gas ppf consumed_gas + in let manager_operation_name (type kind) (result : kind successful_manager_operation_result) = @@ -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" + | Increment_global_counter_result _ -> "increment global 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 + | Increment_global_counter_result _ as op -> pp_increment_global_counter_result op in pp_operation_result ~operation_name:manager_operation_name diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 29acacd89e7..215104176c3 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -132,6 +132,8 @@ "Cache_repr", "Zk_rollup_storage", + + "Shared_global_counter_storage", "Contract_delegate_storage", "Stake_storage", diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 1edf9aa6825..2bb0f93af24 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -612,6 +612,8 @@ end module Token = Token module Cache = Cache_repr +module Shared_global_counter = Shared_global_counter_storage + module Internal_for_tests = struct let to_raw x = x end diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index f905bf8b2d8..f5da461bfeb 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4156,6 +4156,8 @@ module Kind : sig type zk_rollup_publish = Zk_rollup_publish_kind + type increment_global_counter = Increment_global_counter_kind + type 'a manager = | Reveal_manager_kind : reveal manager | Transaction_manager_kind : transaction manager @@ -4192,6 +4194,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 + | Increment_global_counter_manager_kind : increment_global_counter manager end (** All the definitions below are re-exported from {!Operation_repr}. *) @@ -4448,6 +4451,7 @@ and _ manager_operation = ops : (Zk_rollup.Operation.t * Zk_rollup.Ticket.t option) list; } -> Kind.zk_rollup_publish manager_operation + | Increment_global_counter : Kind.increment_global_counter manager_operation and counter = Z.t @@ -5060,3 +5064,7 @@ module Fees : sig val check_storage_limit : context -> storage_limit:Z.t -> unit tzresult end + +module Shared_global_counter : sig + val increment_shared_global_counter : context -> context tzresult Lwt.t +end diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 2051f19cbe6..eb2ad874002 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -749,7 +749,15 @@ let apply_manager_operation : (Reveal_result {consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt} : kind successful_manager_operation_result), - [] ) + []) + | Increment_global_counter -> + let open Lwt_tzresult_syntax in + let+ ctxt = Shared_global_counter.increment_shared_global_counter ctxt in + (ctxt, + Increment_global_counter_result {consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt}, + [] + ) + | Transaction {amount; parameters; destination = Implicit pkh; entrypoint} -> Script.force_decode_in_context ~consume_deserialization_gas @@ -1575,7 +1583,7 @@ let burn_manager_storage_fees : ~payer >>=? fun (ctxt, storage_limit, origination_result) -> return (ctxt, storage_limit, Origination_result origination_result) - | Reveal_result _ | Delegation_result _ -> return (ctxt, storage_limit, smopr) + | Reveal_result _ | Delegation_result _ | Increment_global_counter_result _ -> return (ctxt, storage_limit, smopr) | Register_global_constant_result payload -> let consumed = payload.size_of_constant in Fees.burn_storage_fees ctxt ~storage_limit ~payer consumed diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 5ca742c480b..68cbcb80acc 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -193,6 +193,9 @@ type _ successful_manager_operation_result = paid_storage_size_diff : Z.t; } -> Kind.zk_rollup_publish successful_manager_operation_result + | Increment_global_counter_result : { + consumed_gas: Gas.Arith.fp; + } -> Kind.increment_global_counter successful_manager_operation_result let migration_origination_result_to_successful_manager_operation_result ({ @@ -1232,6 +1235,9 @@ 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.Increment_global_counter_manager_kind, Increment_global_counter_manager_kind -> + Some Eq + | Kind.Increment_global_counter_manager_kind, _ -> None module Encoding = struct type 'kind case = @@ -2923,6 +2929,7 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Zk_rollup_origination _; _}, _ -> None + | ( Manager_operation {operation = Zk_rollup_publish _; _}, Manager_operation_result {operation_result = Applied (Zk_rollup_publish_result _); _} ) -> @@ -2949,6 +2956,32 @@ let kind_equal : Some Eq | Manager_operation {operation = Zk_rollup_publish _; _}, _ -> None + | ( Manager_operation {operation = Increment_global_counter; _}, + Manager_operation_result + {operation_result = Applied (Increment_global_counter_result _); _} ) -> + Some Eq + | ( Manager_operation {operation = Increment_global_counter; _}, + Manager_operation_result + {operation_result = Backtracked (Increment_global_counter_result _, _); _} ) -> + Some Eq + | ( Manager_operation {operation = Increment_global_counter; _}, + Manager_operation_result + { + operation_result = + Failed (Alpha_context.Kind.Increment_global_counter_manager_kind, _); + _; + } ) -> + Some Eq + | ( Manager_operation {operation = Increment_global_counter; _}, + Manager_operation_result + { + operation_result = + Skipped Alpha_context.Kind.Increment_global_counter_manager_kind; + _; + } ) -> + Some Eq + | Manager_operation {operation = Increment_global_counter; _}, _ -> None + let rec kind_equal_list : type kind kind2. kind contents_list -> kind2 contents_result_list -> (kind, kind2) eq option diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index b468b0bae19..c1bf4e4ac99 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 + | Increment_global_counter_result : { + consumed_gas: Gas.Arith.fp; + } -> Kind.increment_global_counter successful_manager_operation_result and packed_successful_manager_operation_result = | Successful_manager_result : diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index ccbbfeb2cc9..05b0cde856e 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -134,6 +134,7 @@ Carbonated_map Raw_context_intf Raw_context + Shared_global_counter_storage Storage_costs Storage_sigs Storage_functors diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index a66d09dc635..37976fc94f2 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 increment_global_counter = Increment_global_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 + | Increment_global_counter_manager_kind : increment_global_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 + | Increment_global_counter : Kind.increment_global_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 + | Increment_global_counter -> Kind.Increment_global_counter_manager_kind type packed_manager_operation = | Manager : 'kind manager_operation -> packed_manager_operation @@ -2093,6 +2098,8 @@ let equal_manager_operation_kind : | Zk_rollup_origination _, _ -> None | Zk_rollup_publish _, Zk_rollup_publish _ -> Some Eq | Zk_rollup_publish _, _ -> None + | Increment_global_counter, Increment_global_counter -> Some Eq + | Increment_global_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..ab46e27ba93 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 increment_global_counter = Increment_global_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 + | Increment_global_counter_manager_kind : increment_global_counter manager end type 'a consensus_operation_type = @@ -582,6 +585,8 @@ and _ manager_operation = (* See {!Zk_rollup_apply} *) } -> Kind.zk_rollup_publish manager_operation + | Increment_global_counter : Kind.increment_global_counter manager_operation + (* TODO Why do we need the same declaratioin both ml and mli? *) (** Counters are used as anti-replay protection mechanism in manager operations: each manager account stores a counter and diff --git a/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml b/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml new file mode 100644 index 00000000000..47eae3cf7de --- /dev/null +++ b/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml @@ -0,0 +1,4 @@ +let increment_shared_global_counter ctx = + Storage.Contract.Shared_global_counter.get ctx >>=? + fun shared_global_counter -> + Storage.Contract.Shared_global_counter.update ctx (Z.succ shared_global_counter) \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli b/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli new file mode 100644 index 00000000000..74cc83221f3 --- /dev/null +++ b/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli @@ -0,0 +1,2 @@ + +val increment_shared_global_counter : Raw_context.t -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index b1c5b774c43..603e7311d19 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -176,6 +176,13 @@ module Contract = struct end) (Encoding.Z) + module Shared_global_counter : Simple_single_data_storage with type value = Z.t = + Make_single_data_storage (Registered) (Raw_context) + (struct + let name = ["shared_global_counter"] + end) + (Encoding.Z) + module Indexed_context = Make_indexed_subcontext (Make_subcontext (Registered) (Raw_context) diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index a69a729c0a3..983e2965435 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -59,6 +59,8 @@ module Contract : sig module Global_counter : Simple_single_data_storage with type value = Z.t + module Shared_global_counter : Simple_single_data_storage with type value = Z.t + (** The domain of alive contracts *) val fold : Raw_context.t -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 67943470a34..d1ca6d7d3fc 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -907,7 +907,7 @@ 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 _ | Increment_global_counter_result _-> balance_updates_rev | Transaction_result ( Transaction_to_contract_result {balance_updates; _} @@ -962,7 +962,8 @@ 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 (Increment_global_counter_result _)-> origination_results_rev | Successful_manager_result (Origination_result x) -> Origination_result x :: origination_results_rev) diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml b/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml index 98dedeb3932..75f0fa0fa83 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml @@ -99,7 +99,7 @@ let ensure_kind infos kind = | Sc_rollup_refute _ | Sc_rollup_timeout _ | Sc_rollup_execute_outbox_message _ | Sc_rollup_recover_bond _ | Dal_publish_slot_header _ | Sc_rollup_dal_slot_subscribe _ - | Zk_rollup_origination _ | Zk_rollup_publish _ ), + | Zk_rollup_origination _ | Zk_rollup_publish _ | Increment_global_counter), _ ) -> assert false) | Single _ -> assert false diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index 83414ab0044..0a4e3e9c303 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 + | Increment_global_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 aa03abb6abca57498ae4b8d12c23d5fd6469b193 Mon Sep 17 00:00:00 2001 From: Ilya Peresadin Date: Mon, 31 Oct 2022 15:46:52 +0000 Subject: [PATCH 2/4] Implement client part for increment_global_counter operation --- src/proto_alpha/bin_sc_rollup_node/daemon.ml | 2 +- .../bin_sc_rollup_node/injector.ml | 2 +- .../lib_client/client_proto_context.ml | 36 +++++++++++++++++++ .../lib_client/client_proto_context.mli | 15 ++++++++ .../client_proto_context_commands.ml | 36 +++++++++++++++++++ src/proto_alpha/lib_injector/l1_operation.ml | 2 ++ .../lib_protocol/alpha_context.mli | 4 +++ src/proto_alpha/lib_protocol/apply_results.ml | 33 +++++++++++++++++ .../lib_protocol/contract_storage.ml | 1 + .../lib_protocol/operation_repr.ml | 21 +++++++++++ .../lib_protocol/operation_repr.mli | 4 +++ 11 files changed, 154 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/bin_sc_rollup_node/daemon.ml b/src/proto_alpha/bin_sc_rollup_node/daemon.ml index 5ed53a3a3e5..ef8aba637af 100644 --- a/src/proto_alpha/bin_sc_rollup_node/daemon.ml +++ b/src/proto_alpha/bin_sc_rollup_node/daemon.ml @@ -125,7 +125,7 @@ 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 _ | Increment_global_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..574e467b23d 100644 --- a/src/proto_alpha/bin_sc_rollup_node/injector.ml +++ b/src/proto_alpha/bin_sc_rollup_node/injector.ml @@ -151,7 +151,7 @@ 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 _ | Increment_global_counter -> (* These operations should never be handled by this injector *) assert false diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index a6abe374a7c..45a3726a83d 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -30,6 +30,42 @@ open Tezos_micheline open Client_proto_contracts open Client_keys + +let build_increment_global_counter ?fee ?gas_limit ?storage_limit () = + let operation = Increment_global_counter in + Injection.prepare_manager_operation + ~fee:(Limit.of_option fee) + ~gas_limit:(Limit.of_option gas_limit) + ~storage_limit:(Limit.of_option storage_limit) + operation + +let increment_global_counter (cctxt : #full) ~chain ~block ?confirmations + ?dry_run ?verbose_signing ?simulation ?fee + mgr ~src_pk ~src_sk ~fee_parameter = + let open Lwt_result_syntax in + let op = build_increment_global_counter ?fee () in + let annotated_op = Annotated_manager_operation.Single_manager op in + Injection.inject_manager_operation + cctxt + ~chain + ~block + ?confirmations + ?dry_run + ?verbose_signing + ?simulation + ~source:mgr + ~fee:(Limit.of_option fee) + ~storage_limit:Limit.unknown + ~gas_limit:Limit.unknown + ~src_pk + ~src_sk + ~fee_parameter + annotated_op + >>=? fun (oph, _, op, result) -> + match Apply_results.pack_contents_list op result with + | Apply_results.Single_and_result ((Manager_operation _ as op), result) -> + return (oph, op, result) + let get_balance (rpc : #rpc_context) ~chain ~block contract = Alpha_services.Contract.balance rpc (chain, block) contract diff --git a/src/proto_alpha/lib_client/client_proto_context.mli b/src/proto_alpha/lib_client/client_proto_context.mli index a5bfcd3923f..b67067c8cf3 100644 --- a/src/proto_alpha/lib_client/client_proto_context.mli +++ b/src/proto_alpha/lib_client/client_proto_context.mli @@ -26,6 +26,21 @@ open Protocol open Alpha_context +val increment_global_counter: + #Protocol_client_context.full -> + chain:Shell_services.chain -> + block:Shell_services.block -> + ?confirmations:int -> + ?dry_run:bool -> + ?verbose_signing:bool -> + ?simulation:bool -> + ?fee:Tez.tez -> + public_key_hash -> + src_pk:public_key -> + src_sk:Client_keys.sk_uri -> + fee_parameter:Injection.fee_parameter -> + Kind.increment_global_counter Kind.manager Injection.result tzresult Lwt.t + (** Calls {!Tezos_protocol_alpha.Protocol.Contract_services.list}. *) val list_contract_labels : #Protocol_client_context.full -> 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..14881119853 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 @@ -999,6 +999,42 @@ let commands_rw () = let open Tezos_micheline in let open Clic in [ + command + ~group + ~desc:"Increment global counter" + (args5 + fee_arg + dry_run_switch + verbose_signing_switch + simulate_switch + fee_parameter_args) + (prefixes ["increment"; "global"; "counter"] + @@ ContractAlias.destination_param ~name:"src" ~desc:"source contract" + @@ stop) + (fun (fee, dry_run, verbose_signing, simulation, fee_parameter) contract (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* source = + match contract with + | Originated contract -> Managed_contract.get_contract_manager cctxt contract + | Implicit mgr -> return mgr + in + let* _, src_pk, src_sk = Client_keys.get_key cctxt source in + let* (_ : _ Injection.result) = + increment_global_counter + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + ?confirmations:cctxt#confirmations + ~dry_run + ~verbose_signing + ~simulation + ~fee_parameter + ?fee + source + ~src_pk + ~src_sk + in return_unit + ); command ~group ~desc:"Set the delegate of a contract." diff --git a/src/proto_alpha/lib_injector/l1_operation.ml b/src/proto_alpha/lib_injector/l1_operation.ml index e82d6668f88..6971512e94b 100644 --- a/src/proto_alpha/lib_injector/l1_operation.ml +++ b/src/proto_alpha/lib_injector/l1_operation.ml @@ -70,6 +70,7 @@ module Manager_operation = struct make sc_rollup_execute_outbox_message_case; make sc_rollup_recover_bond_case; make sc_rollup_dal_slot_subscribe_case; + make increment_global_counter_case; ] let get_case : @@ -108,6 +109,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 + | Increment_global_counter -> increment_global_counter_case let pp_kind ppf op = let open Operation.Encoding.Manager_operations in diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index f5da461bfeb..7e71360ab31 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4654,6 +4654,8 @@ module Operation : sig val zk_rollup_publish_case : Kind.zk_rollup_publish Kind.manager case + val increment_global_counter_case : Kind.increment_global_counter Kind.manager case + module Manager_operations : sig type 'b case = | MCase : { @@ -4729,6 +4731,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 increment_global_counter_case : Kind.increment_global_counter case end end diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 68cbcb80acc..eca4c1ed88b 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -1035,6 +1035,26 @@ module Manager_result = struct ~kind:Kind.Sc_rollup_dal_slot_subscribe_manager_kind ~inj:(fun (consumed_gas, slot_index, level) -> Sc_rollup_dal_slot_subscribe_result {consumed_gas; slot_index; level}) + + let increment_global_counter_case = + make + ~op_case: + Operation.Encoding.Manager_operations.increment_global_counter_case + ~encoding: + (obj1 + (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)) + ~select:(function + | Successful_manager_result + (Increment_global_counter_result _ as op) -> + Some op + | _ -> None) + ~proj:(function + | Increment_global_counter_result {consumed_gas} + -> + consumed_gas) + ~kind:Kind.Increment_global_counter_manager_kind + ~inj:(fun consumed_gas -> + Increment_global_counter_result {consumed_gas}) end let successful_manager_operation_result_encoding : @@ -1919,6 +1939,17 @@ module Encoding = struct -> Some (op, res) | _ -> None) + + let increment_global_counter_case = + make_manager_case + Operation.Encoding.increment_global_counter_case + Manager_result.increment_global_counter_case + (function + | Contents_and_result + ((Manager_operation {operation = Increment_global_counter; _} as op), res) + -> + Some (op, res) + | _ -> None) end let contents_result_encoding = @@ -1981,6 +2012,7 @@ let contents_result_encoding = make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make increment_global_counter_case; ] let contents_and_result_encoding = @@ -2048,6 +2080,7 @@ let contents_and_result_encoding = make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make increment_global_counter_case; ] type 'kind contents_result_list = diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index 4f11a9171a4..1cea6ef3877 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -619,6 +619,7 @@ let credit_only_call_from_token c contract amount = let init c = Storage.Contract.Global_counter.init c Z.zero >>=? fun c -> + Storage.Contract.Shared_global_counter.init c Z.zero >>=? fun c -> Lazy_storage_diff.init c let used_storage_space c contract = diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index 37976fc94f2..6ddb051bf39 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -636,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 increment_global_counter_tag = 254 + module Encoding = struct open Data_encoding @@ -1319,6 +1321,19 @@ module Encoding = struct (fun (rollup, slot_index) -> Sc_rollup_dal_slot_subscribe {rollup; slot_index}); } + + let increment_global_counter_case = + MCase + { + tag = increment_global_counter_tag; + name = "increment_global_counter"; + encoding = obj1 (req "increment_global_counter" Data_encoding.unit); + select = + (function + | Manager (Increment_global_counter as op) -> Some op | _ -> None); + proj = (function Increment_global_counter -> ()); + inj = (fun () -> Increment_global_counter); + } end type 'b case = @@ -1780,6 +1795,11 @@ module Encoding = struct make_manager_case zk_rollup_operation_publish_tag Manager_operations.zk_rollup_publish_case + + let increment_global_counter_case = + make_manager_case + increment_global_counter_tag + Manager_operations.increment_global_counter_case let contents_encoding = let make (Case {tag; name; encoding; select; proj; inj}) = @@ -1835,6 +1855,7 @@ module Encoding = struct make sc_rollup_dal_slot_subscribe_case; make zk_rollup_origination_case; make zk_rollup_publish_case; + make increment_global_counter_case; ] let contents_list_encoding = diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index ab46e27ba93..8ec554a7fda 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -844,6 +844,8 @@ module Encoding : sig val zk_rollup_publish_case : Kind.zk_rollup_publish Kind.manager case + val increment_global_counter_case : Kind.increment_global_counter Kind.manager case + module Manager_operations : sig type 'b case = | MCase : { @@ -918,5 +920,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 increment_global_counter_case : Kind.increment_global_counter case end end -- GitLab From 5e514752a43788ef374b99a7c1b3cee40e0f3a8a Mon Sep 17 00:00:00 2001 From: Ilya Peresadin Date: Wed, 2 Nov 2022 14:12:21 +0000 Subject: [PATCH 3/4] Add rpc to fetch shared_global_counter value --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 2 +- .../lib_protocol/alpha_context.mli | 1 + .../lib_protocol/alpha_services.ml | 20 +++++++++++ .../lib_protocol/alpha_services.mli | 5 +++ .../shared_global_counter_storage.ml | 35 ++++++++++++++++--- .../shared_global_counter_storage.mli | 30 +++++++++++++++- 6 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 215104176c3..d7bc9ed8308 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -132,7 +132,7 @@ "Cache_repr", "Zk_rollup_storage", - + "Shared_global_counter_storage", "Contract_delegate_storage", diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 7e71360ab31..3029c7786d7 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -5071,4 +5071,5 @@ end module Shared_global_counter : sig val increment_shared_global_counter : context -> context tzresult Lwt.t + val get_shared_global_counter : context -> Z.t tzresult Lwt.t end diff --git a/src/proto_alpha/lib_protocol/alpha_services.ml b/src/proto_alpha/lib_protocol/alpha_services.ml index 1dfb872f9cc..2b34109cb8a 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.ml +++ b/src/proto_alpha/lib_protocol/alpha_services.ml @@ -241,6 +241,25 @@ module Liquidity_baking = struct RPC_context.make_call0 S.get_cpmm_address ctxt block () () end +module Shared_global_counter = struct + module S = struct + let get_shared_global_counter = + RPC_service.get_service + ~description:"Get shared global counter" + ~query:RPC_query.empty + ~output:Data_encoding.z + RPC_path.(custom_root / "context" / "shared_global_counter") + end + + let register () = + let open Services_registration in + register0 ~chunked:false S.get_shared_global_counter (fun ctxt () () -> + Alpha_context.Shared_global_counter.get_shared_global_counter ctxt) + + let get_shared_global_counter ctxt block = + RPC_context.make_call0 S.get_shared_global_counter ctxt block () () +end + module Cache = struct module S = struct let cached_contracts = @@ -309,5 +328,6 @@ let register () = Voting.register () ; Sapling.register () ; Liquidity_baking.register () ; + Shared_global_counter.register () ; Cache.register () ; Tx_rollup.register () diff --git a/src/proto_alpha/lib_protocol/alpha_services.mli b/src/proto_alpha/lib_protocol/alpha_services.mli index 14235f19a69..4f65a9800b9 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.mli +++ b/src/proto_alpha/lib_protocol/alpha_services.mli @@ -73,6 +73,11 @@ module Liquidity_baking : sig 'a #RPC_context.simple -> 'a -> Contract_hash.t shell_tzresult Lwt.t end +module Shared_global_counter: sig + val get_shared_global_counter : + 'a #RPC_context.simple -> 'a -> Z.t shell_tzresult Lwt.t +end + module Cache : sig val cached_contracts : 'a #RPC_context.simple -> diff --git a/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml b/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml index 47eae3cf7de..847c4c03a9d 100644 --- a/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml +++ b/src/proto_alpha/lib_protocol/shared_global_counter_storage.ml @@ -1,4 +1,31 @@ -let increment_shared_global_counter ctx = - Storage.Contract.Shared_global_counter.get ctx >>=? - fun shared_global_counter -> - Storage.Contract.Shared_global_counter.update ctx (Z.succ shared_global_counter) \ No newline at end of file +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +let increment_shared_global_counter ctx = + Storage.Contract.Shared_global_counter.get ctx >>=? + fun shared_global_counter -> + Storage.Contract.Shared_global_counter.update ctx (Z.succ shared_global_counter) + +let get_shared_global_counter = Storage.Contract.Shared_global_counter.get diff --git a/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli b/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli index 74cc83221f3..7f79909d84f 100644 --- a/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli +++ b/src/proto_alpha/lib_protocol/shared_global_counter_storage.mli @@ -1,2 +1,30 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* *) +(* 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 increment_shared_global_counter : Raw_context.t -> Raw_context.t tzresult Lwt.t +val increment_shared_global_counter : + Raw_context.t -> Raw_context.t tzresult Lwt.t + +val get_shared_global_counter : + Raw_context.t -> Z.t tzresult Lwt.t -- GitLab From 1b7eb9f43840153600c27c709dfe8fdf461362d9 Mon Sep 17 00:00:00 2001 From: Ilya Peresadin Date: Wed, 9 Nov 2022 16:56:52 +0000 Subject: [PATCH 4/4] Add tests --- .../lib_protocol/test/helpers/context.ml | 2 + .../lib_protocol/test/helpers/context.mli | 3 + .../lib_protocol/test/helpers/op.ml | 16 ++++ .../lib_protocol/test/helpers/op.mli | 10 +++ .../test/integration/operations/main.ml | 1 + .../test_increment_global_counter.ml | 73 +++++++++++++++++++ tezt/lib_tezos/client.ml | 12 +++ tezt/lib_tezos/client.mli | 16 +++- tezt/tests/increment_global_counter.ml | 60 +++++++++++++++ tezt/tests/main.ml | 3 +- 10 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/test/integration/operations/test_increment_global_counter.ml create mode 100644 tezt/tests/increment_global_counter.ml diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index e672f75228e..727cacb76f8 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -232,6 +232,8 @@ let get_liquidity_baking_subsidy ctxt = let get_liquidity_baking_cpmm_address ctxt = Alpha_services.Liquidity_baking.get_cpmm_address rpc_ctxt ctxt +let get_shared_global_counter = Alpha_services.Shared_global_counter.get_shared_global_counter rpc_ctxt + (* Voting *) module Vote = struct diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 84c53d2d7e6..00d66bf023e 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -69,6 +69,9 @@ val get_voting_power : val get_total_voting_power : t -> int64 Environment.Error_monad.shell_tzresult Lwt.t +val get_shared_global_counter : + t -> Z.t Environment.Error_monad.shell_tzresult Lwt.t + val get_bakers : ?filter:(Plugin.RPC.Baking_rights.t -> bool) -> ?cycle:Cycle.t -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index 5fbc14b0f8a..8a834e4cf6a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -1044,3 +1044,19 @@ 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 increment_global_counter ?force_reveal ?fee ?gas_limit ?counter ?storage_limit ctxt source = + let iop = Increment_global_counter in + manager_operation + ?force_reveal + ?fee + ?counter + ?gas_limit + ?storage_limit + ~source + ctxt + iop + >>=? fun sop -> + Context.Contract.manager ctxt source >|=? fun account -> + sign account.sk ctxt sop \ No newline at end of file diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index aa869b298ba..07b525c1590 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -780,3 +780,13 @@ 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 increment_global_counter : + ?force_reveal:bool -> + ?fee:Tez.tez -> + ?gas_limit:gas_limit -> + ?counter:Z.t -> + ?storage_limit:Z.t -> + Context.t -> + Contract.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..93ee6ae6e96 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); + ("global shared counter", Test_increment_global_counter.tests); ] |> Lwt_main.run diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_increment_global_counter.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_increment_global_counter.ml new file mode 100644 index 00000000000..f82da66d189 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_increment_global_counter.ml @@ -0,0 +1,73 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* *) +(* 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: Protocol (origination) + Invocation: + dune exec \ + src/proto_alpha/lib_protocol/test/integration/operations/main.exe \ + -- test "^global shared counter$" + Subject: On incrementing shared global counter. +*) + +let bake_next_block_with_single_counter_increment ~loc:_ ~mgr prev_block = + let open Lwt_result_syntax in + let* incr = Incremental.begin_construction prev_block in + let* op = Op.increment_global_counter (I incr) mgr in + let* incr1 = Incremental.add_operation incr op in + Incremental.finalize_block incr1 + +let test_increment_global_counter_by_different_mgrs ~loc:_ ~n_times () = + let open Lwt_result_syntax in + let* (genesis_b, mgrs) = Context.init_n ~consensus_threshold:0 n_times () in + let* b = + List.fold_left_es + (fun pb mgr -> bake_next_block_with_single_counter_increment ~loc:__LOC__ ~mgr pb) + genesis_b + mgrs + in + (* check that after the block has been baked the global counter incremented *) + let* global_counter = Context.get_shared_global_counter (B b) in + Assert.equal + ~loc:__LOC__ + Z.Compare.(=) + (Format.sprintf "Global counter expected to be equal to %d" n_times) + Z.pp_print + (Z.of_int n_times) + global_counter + +(******************************************************) +(* Tests *) +(******************************************************) + +let test_increment_global_counter_once () = test_increment_global_counter_by_different_mgrs ~loc:__LOC__ ~n_times:1 () +let test_increment_global_counter_thrice () = test_increment_global_counter_by_different_mgrs ~loc:__LOC__ ~n_times:3 () + +let tests = + [ + Tztest.tztest "Increment global counter once" `Quick test_increment_global_counter_once; + Tztest.tztest "Increment global counter thrice: an operation per block" `Quick test_increment_global_counter_thrice; + ] \ No newline at end of file diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index b8fb834be8a..e3ce336277e 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -2254,3 +2254,15 @@ let spawn_config_init ?protocol ?bootstrap_accounts ?protocol_constants client = let config_init ?protocol ?bootstrap_accounts ?protocol_constants client = spawn_config_init ?protocol ?bootstrap_accounts ?protocol_constants client |> Process.check + +let increment_global_counter ~src client = + spawn_command + client + ["--wait"; "none"; "increment"; "global"; "counter"; src] + |> Process.check + + +let get_global_counter ?endpoint ?(chain = "main") ?(block = "head") client = + let path = ["chains"; chain; "blocks"; block; "context"; "shared_global_counter"] in + spawn_rpc ?endpoint GET path client |> + Process.check_and_read_stdout \ No newline at end of file diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index faaeca872df..171f2ef0659 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1177,8 +1177,8 @@ val spawn_run_tzip4_view : t -> Process.t -(** Run [tezos-client run tzip4 view .. on contract .. with input .. ] - +(** Run [tezos-client run tzip4 view .. on contract .. with input .. ] + Returns the value returned by a view as a string. Fails if the view or the contract does not exist. If [input] is [None], @@ -1686,3 +1686,15 @@ val spawn_config_init : ?protocol_constants:string -> t -> Process.t + +val increment_global_counter : + src:string -> + t -> + unit Lwt.t + +val get_global_counter : + ?endpoint:endpoint -> + ?chain:string -> + ?block:string -> + t -> + string Lwt.t \ No newline at end of file diff --git a/tezt/tests/increment_global_counter.ml b/tezt/tests/increment_global_counter.ml new file mode 100644 index 00000000000..a81df35fb63 --- /dev/null +++ b/tezt/tests/increment_global_counter.ml @@ -0,0 +1,60 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 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: Shared global counter test + Invocation: + dune exec tezt/tests/main.exe -- --file increment_global_counter.ml + Subject: Tests for testing global counter operations. +*) + +let test_increment_global_counter_twice = + Protocol.register_test + ~__FILE__ + ~title:"Increase global counter twice" + ~tags:["global_counter"; "shared_global_counter"] + ~supports:(Protocol.From_protocol 016) + @@ fun protocol -> + let* _, client = Client.init_with_protocol ~protocol `Client () in + let to_int x = + let x1 = String.trim x in + int_of_string @@ String.sub x1 1 (String.length x1 - 2) + in + let inc_by_one ~src expected_val_after = + let* () = Client.increment_global_counter ~src client in + let* () = Client.bake_for_and_wait client in + let* val1 = Client.get_global_counter client in + Check.(to_int val1 = expected_val_after) + Check.int + ~error_msg: + ("Unexpected shared global counter value. Expected %R. Got %L"); + unit + in + let* () = inc_by_one ~src:"bootstrap1" 1 in + let* () = inc_by_one ~src:"bootstrap2" 2 in + unit + +let register ~protocols = test_increment_global_counter_twice protocols \ No newline at end of file diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 577624899e9..35364edd8d6 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -152,7 +152,8 @@ let register_protocol_tests_that_use_supports_correctly () = Tx_rollup_l2_node.register ~protocols ; Tzip4_view.register ~protocols ; Used_paid_storage_spaces.register ~protocols ; - Vdf_test.register ~protocols + Vdf_test.register ~protocols ; + Increment_global_counter.register ~protocols (* Regression tests are not easy to maintain for multiple protocols because one needs to update and maintain all the expected output files. Some of them, such as -- GitLab