From 24322dd7d8aad6017032a2afbf5ed832733f7a14 Mon Sep 17 00:00:00 2001 From: Diana Savatina Date: Wed, 27 Dec 2023 16:15:21 +0000 Subject: [PATCH 1/3] Cnt: adding a counter 1. Add a new module to TEZOS_PROTOCOL 2. Generate dune file: > make -C manifest 3. make adding client commands adding Push_cnt operation and its handling Check in mockup mode ==================== alias mockup-client='./octez-client --mode mockup --base-dir /tmp/mockup' rm -rf /tmp/mockup mockup-client create mockup --asynchronous mockup-client list known addresses mockup-client increase counter from bootstrap3 Check in sandbox mode ===================== TEZOS_LOG="*->debug" ./src/bin_node/octez-sandboxed-node.sh 1 --connections 1 TEZOS_LOG="*->debug" ./src/bin_node/octez-sandboxed-node.sh 9 --connections 1 eval `./src/bin_client/octez-init-sandboxed-client.sh 1` octez-autocomplete octez-activate-alpha octez-client increase counter from bootstrap4 & octez-client bake for --minimal-timestamp Updated counter: 1 octez-client increase counter from bootstrap4 & octez-client bake for --minimal-timestamp Updated counter: 2 --- cnt.tz | 4 ++ .../lib_client/client_proto_context.ml | 34 ++++++++++++ .../lib_client/client_proto_context.mli | 11 ++++ src/proto_alpha/lib_client/injection.ml | 6 +- .../lib_client/operation_result.ml | 9 +++ .../client_proto_context_commands.ml | 34 ++++++++++++ src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 2 + src/proto_alpha/lib_protocol/alpha_context.ml | 9 +++ .../lib_protocol/alpha_context.mli | 25 +++++++++ src/proto_alpha/lib_protocol/apply.ml | 14 ++++- src/proto_alpha/lib_protocol/apply_results.ml | 55 +++++++++++++++++++ .../lib_protocol/apply_results.mli | 5 ++ src/proto_alpha/lib_protocol/cnt_repr.ml | 33 +++++++++++ src/proto_alpha/lib_protocol/cnt_repr.mli | 30 ++++++++++ src/proto_alpha/lib_protocol/cnt_storage.ml | 34 ++++++++++++ src/proto_alpha/lib_protocol/cnt_storage.mli | 29 ++++++++++ src/proto_alpha/lib_protocol/dune | 8 +++ .../lib_protocol/gas_parameters.json | 2 + src/proto_alpha/lib_protocol/init_storage.ml | 1 + .../lib_protocol/operation_repr.ml | 26 +++++++++ .../lib_protocol/operation_repr.mli | 8 +++ src/proto_alpha/lib_protocol/storage.ml | 7 +++ src/proto_alpha/lib_protocol/storage.mli | 2 + .../lib_protocol/test/helpers/block.ml | 3 +- .../lib_protocol/test/helpers/op.ml | 19 +++++++ .../lib_protocol/test/helpers/op.mli | 12 ++++ .../test/helpers/operation_generator.ml | 6 ++ .../validate/manager_operation_helpers.ml | 20 ++++++- .../test/integration/validate/test_sanity.ml | 16 +++--- src/proto_alpha/lib_protocol/validate.ml | 1 + .../lib_sc_rollup_node/daemon_helpers.ml | 7 ++- 31 files changed, 455 insertions(+), 17 deletions(-) create mode 100644 cnt.tz create mode 100644 src/proto_alpha/lib_protocol/cnt_repr.ml create mode 100644 src/proto_alpha/lib_protocol/cnt_repr.mli create mode 100644 src/proto_alpha/lib_protocol/cnt_storage.ml create mode 100644 src/proto_alpha/lib_protocol/cnt_storage.mli diff --git a/cnt.tz b/cnt.tz new file mode 100644 index 000000000000..42aed526e2de --- /dev/null +++ b/cnt.tz @@ -0,0 +1,4 @@ +parameter unit ; +storage int ; +code { # This contract pushes counter and the counter is increased and pushed to the stack + DROP ; CNT ; NIL operation ; PAIR} ; diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index 9df05639dcd6..a8355b60a37b 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -650,6 +650,40 @@ let register_global_constant (cctxt : #full) ~chain ~block ?confirmations | Apply_results.Single_and_result ((Manager_operation _ as op), result) -> return (oph, op, result) +let push_cnt (cctxt : #full) ?fee ?confirmations ~source ~src_pk ~src_sk + ~fee_parameter () = + let open Lwt_result_syntax in + let*! () = + cctxt#message "==> client_proto_context.ml: about to inject...\n" + in + let operation = Push_cnt in + let operation = + Annotated_manager_operation.Single_manager + (Injection.prepare_manager_operation + ~fee:(Limit.of_option fee) + ~gas_limit:Limit.unknown + ~storage_limit:Limit.unknown + operation) + in + let* oph, _, op, result = + Injection.inject_manager_operation + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + ?confirmations + ~source + ~fee:(Limit.of_option fee) + ~storage_limit:Limit.unknown + ~gas_limit:Limit.unknown + ~src_pk + ~src_sk + ~fee_parameter + operation + in + match Apply_results.pack_contents_list op result with + | Apply_results.Single_and_result ((Manager_operation _ as op), result) -> + return (oph, op, result) + type activation_key = { pkh : Signature.Ed25519.Public_key_hash.t; amount : Tez.t; diff --git a/src/proto_alpha/lib_client/client_proto_context.mli b/src/proto_alpha/lib_client/client_proto_context.mli index 08a8c1008b2f..fe4f581f2058 100644 --- a/src/proto_alpha/lib_client/client_proto_context.mli +++ b/src/proto_alpha/lib_client/client_proto_context.mli @@ -90,6 +90,17 @@ val register_global_constant : (Kind.register_global_constant Kind.manager Injection.result, tztrace) result Lwt.t +val push_cnt : + #Protocol_client_context.full -> + ?fee:Tez.t -> + ?confirmations:int -> + source:Signature.public_key_hash -> + src_pk:Signature.public_key -> + src_sk:Client_keys.sk_uri -> + fee_parameter:Injection.fee_parameter -> + unit -> + (Kind.push_cnt Kind.manager Injection.result, tztrace) result Lwt.t + (** Calls {!Tezos_protocol_plugin_alpha.Plugin.RPC.Big_map.big_map_get_normalized}. *) val get_big_map_value : #Protocol_client_context.rpc_context -> diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index 0fac51be112a..b38ccff4e54d 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -345,6 +345,7 @@ let estimated_gas_single (type kind) | Reveal_result {consumed_gas} | Delegation_result {consumed_gas; _} | Register_global_constant_result {consumed_gas; _} + | Push_cnt_result {consumed_gas; _} | Set_deposits_limit_result {consumed_gas} | Update_consensus_key_result {consumed_gas; _} | Increase_paid_storage_result {consumed_gas; _} @@ -411,6 +412,7 @@ let estimated_storage_single (type kind) ~origination_size Ok (Z.add paid_storage_size_diff origination_size) | Register_global_constant_result {size_of_constant; _} -> Ok size_of_constant + | Push_cnt_result _ -> Ok Z.zero | Update_consensus_key_result _ -> Ok Z.zero | Sc_rollup_execute_outbox_message_result {paid_storage_size_diff; _} | Transfer_ticket_result {paid_storage_size_diff; _} @@ -503,8 +505,8 @@ let originated_contracts_single (type kind) | Transaction_result ( Transaction_to_sc_rollup_result _ | Transaction_to_zk_rollup_result _ ) - | Register_global_constant_result _ | Reveal_result _ - | Delegation_result _ | Set_deposits_limit_result _ + | Register_global_constant_result _ | Push_cnt_result _ + | Reveal_result _ | Delegation_result _ | Set_deposits_limit_result _ | Update_consensus_key_result _ | Increase_paid_storage_result _ | Transfer_ticket_result _ | Dal_publish_slot_header_result _ | Sc_rollup_originate_result _ | Sc_rollup_add_messages_result _ diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 3286b70a9f3e..b42d8435fb6d 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -214,6 +214,7 @@ let pp_manager_operation_content (type kind) source ppf "Register Global:@,Value: %a" pp_micheline_from_lazy_expr value + | Push_cnt -> Format.fprintf ppf "Pushing the counter +1" | Set_deposits_limit limit_opt -> ( Format.fprintf ppf @@ -555,6 +556,8 @@ let pp_slot_header ppf slot_header = let pp_consumed_gas ppf consumed_gas = Format.fprintf ppf "@,Consumed gas: %a" Gas.Arith.pp consumed_gas +let pp_cnt ppf cnt = Format.fprintf ppf "@,Updated counter: %a" Cnt.pp cnt + let pp_paid_storage_size_diff ppf paid_storage_size_diff = if paid_storage_size_diff <> Z.zero then Format.fprintf @@ -680,6 +683,10 @@ let pp_manager_operation_contents_result ppf op_result = pp_storage_size ppf size_of_constant ; Format.fprintf ppf "@,Global address: %a" Script_expr_hash.pp global_address in + let pp_push_cnt_result (Push_cnt_result {consumed_gas; cnt}) = + pp_consumed_gas ppf consumed_gas ; + pp_cnt ppf cnt + in let pp_increase_paid_storage_result (Increase_paid_storage_result {consumed_gas; balance_updates}) = pp_balance_updates ppf balance_updates ; @@ -815,6 +822,7 @@ let pp_manager_operation_contents_result ppf op_result = | Origination_result _ -> "origination" | Delegation_result _ -> "delegation" | Register_global_constant_result _ -> "global constant registration" + | Push_cnt_result _ -> "pushing cnt" | Set_deposits_limit_result _ -> "deposits limit modification" | Update_consensus_key_result _ -> "consensus key update" | Increase_paid_storage_result _ -> "paid storage increase" @@ -849,6 +857,7 @@ let pp_manager_operation_contents_result ppf op_result = | Origination_result op_res -> pp_origination_result ppf op_res | Register_global_constant_result _ as op -> pp_register_global_constant_result op + | Push_cnt_result _ as op -> pp_push_cnt_result op | Increase_paid_storage_result _ as op -> pp_increase_paid_storage_result op | Transfer_ticket_result _ as op -> pp_transfer_ticket_result op | Sc_rollup_originate_result _ as op -> pp_sc_rollup_originate_result op 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 4ea45fbc1bcd..c8ae64a70794 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 @@ -1849,6 +1849,40 @@ let commands_rw () = errors in return_unit); + command + ~group + ~desc:"Increase counter: inject the manager operation" + (args2 fee_arg fee_parameter_args) + (prefixes ["increase"; "counter"] + @@ prefix "from" + @@ Client_keys.Public_key_hash.source_param + ~name:"src" + ~desc:"the delagate key for account pushing the counter" + @@ stop) + (fun (fee, fee_parameter) src_pkh (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* _, src_pk, src_sk = Client_keys.get_key cctxt src_pkh in + let*! () = cctxt#message "==> Increasing counter started...\n" in + let*! errors = + push_cnt + cctxt + ?fee + ?confirmations:cctxt#confirmations + ~source:src_pkh + ~src_pk + ~src_sk + ~fee_parameter + () + in + let*! (_ : _ Injection.result option) = + report_michelson_errors + ~no_print_source:false + ~msg:"pushing the counter simulation failed" + cctxt + errors + in + let*! () = cctxt#message "==> client_proto_commands.ml: exit\n" in + return_unit); command ~group ~desc:"Call a smart contract (same as 'transfer 0')." diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index 11216be38d1c..86cd97ee56e5 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -113,6 +113,7 @@ "Destination_repr", "Script_int", "Ticket_amount", + "Cnt_repr", "Operation_repr", "Manager_repr", "Commitment_repr", @@ -145,6 +146,7 @@ "Ticket_hash_builder", "Constants_storage", "Level_storage", + "Cnt_storage", "Nonce_storage", "Seed_storage", "Contract_manager_storage", diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index f8383487b87c..dfbc3ee171e8 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -271,6 +271,15 @@ module Round = struct let get ctxt = Storage.Block_round.get ctxt end +module Cnt = struct + include Cnt_repr + include Cnt_storage + + let get ctxt = Storage.Cnt.get ctxt + + let update ctxt cnt = Storage.Cnt.update ctxt cnt +end + module Gas = struct include Gas_limit_repr diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 577910cfdf18..6778601d9ad7 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -377,6 +377,23 @@ module Round : sig val update : context -> t -> context tzresult Lwt.t end +(** This module re-exports definitions from {!Round_repr}. *) +module Cnt : sig + type t = int32 + + val encoding : int32 Data_encoding.t + + val pp : Format.formatter -> int32 -> unit + + val current : context -> int32 tzresult Lwt.t + + val increase : context -> (context, error trace) result Lwt.t + + val get : context -> t tzresult Lwt.t + + val update : context -> t -> context tzresult Lwt.t +end + module Gas : sig (** This module implements the gas subsystem of the context. @@ -4351,6 +4368,8 @@ module Kind : sig type register_global_constant = Register_global_constant_kind + type push_cnt = Push_cnt_kind + type transfer_ticket = Transfer_ticket_kind type dal_publish_slot_header = Dal_publish_slot_header_kind @@ -4385,6 +4404,7 @@ module Kind : sig | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Push_cnt_manager_kind : push_cnt manager | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager @@ -4523,6 +4543,7 @@ and _ manager_operation = value : Script.lazy_expr; } -> Kind.register_global_constant manager_operation + | Push_cnt : Kind.push_cnt manager_operation | Set_deposits_limit : Tez.t option -> Kind.set_deposits_limit manager_operation @@ -4776,6 +4797,8 @@ module Operation : sig val register_global_constant_case : Kind.register_global_constant Kind.manager case + val push_cnt_case : Kind.push_cnt Kind.manager case + val set_deposits_limit_case : Kind.set_deposits_limit Kind.manager case val increase_paid_storage_case : @@ -4833,6 +4856,8 @@ module Operation : sig val register_global_constant_case : Kind.register_global_constant case + val push_cnt_case : Kind.push_cnt case + val set_deposits_limit_case : Kind.set_deposits_limit case val increase_paid_storage_case : Kind.increase_paid_storage case diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 857160c3592c..1eef2d7bf288 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1393,6 +1393,17 @@ let apply_manager_operation : } in return (ctxt, result, []) + | Push_cnt -> + let* ctxt = Cnt.increase ctxt in + let* new_value = Cnt.get ctxt in + let result = + Push_cnt_result + { + consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt; + cnt = new_value; + } + in + return (ctxt, result, []) | Set_deposits_limit limit -> let*! is_registered = Delegate.registered ctxt source in let*? () = @@ -1771,7 +1782,8 @@ let burn_manager_storage_fees : size_of_constant = payload.size_of_constant; global_address = payload.global_address; } ) - | Set_deposits_limit_result _ | Update_consensus_key_result _ -> + | Push_cnt_result _ | Set_deposits_limit_result _ + | Update_consensus_key_result _ -> return (ctxt, storage_limit, smopr) | Increase_paid_storage_result _ -> return (ctxt, storage_limit, smopr) | Transfer_ticket_result payload -> diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index f2aa020fb079..29d98e283c78 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -58,6 +58,11 @@ type _ successful_manager_operation_result = global_address : Script_expr_hash.t; } -> Kind.register_global_constant successful_manager_operation_result + | Push_cnt_result : { + consumed_gas : Gas.Arith.fp; + cnt : int32; + } + -> Kind.push_cnt successful_manager_operation_result | Set_deposits_limit_result : { consumed_gas : Gas.Arith.fp; } @@ -453,6 +458,22 @@ module Manager_result = struct Register_global_constant_result {balance_updates; consumed_gas; size_of_constant; global_address}) + let push_cnt_case = + make + ~op_case:Operation.Encoding.Manager_operations.push_cnt_case + ~encoding: + Data_encoding.( + obj2 + (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero) + (req "cnt" Cnt_repr.encoding)) + ~select:(function + | Successful_manager_result (Push_cnt_result _ as op) -> Some op + | _ -> None) + ~proj:(function + | Push_cnt_result {consumed_gas; cnt} -> (consumed_gas, cnt)) + ~kind:Kind.Push_cnt_manager_kind + ~inj:(fun (consumed_gas, cnt) -> Push_cnt_result {consumed_gas; cnt}) + let delegation_case = make ~op_case:Operation.Encoding.Manager_operations.delegation_case @@ -999,6 +1020,8 @@ let equal_manager_kind : | Kind.Event_manager_kind, Kind.Event_manager_kind -> Some Eq | Kind.Event_manager_kind, _ -> None | Kind.Register_global_constant_manager_kind, _ -> None + | Kind.Push_cnt_manager_kind, Kind.Push_cnt_manager_kind -> Some Eq + | Kind.Push_cnt_manager_kind, _ -> None | Kind.Set_deposits_limit_manager_kind, Kind.Set_deposits_limit_manager_kind -> Some Eq @@ -1640,6 +1663,16 @@ module Encoding = struct Some (op, res) | _ -> None) + let push_cnt_case = + make_manager_case + Operation.Encoding.push_cnt_case + Manager_result.push_cnt_case + (function + | Contents_and_result + ((Manager_operation {operation = Push_cnt; _} as op), res) -> + Some (op, res) + | _ -> None) + let set_deposits_limit_case = make_manager_case Operation.Encoding.set_deposits_limit_case @@ -1824,6 +1857,7 @@ let common_cases = origination_case; delegation_case; register_global_constant_case; + push_cnt_case; set_deposits_limit_case; increase_paid_storage_case; update_consensus_key_case; @@ -2228,6 +2262,27 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Register_global_constant _; _}, _ -> None + | ( Manager_operation {operation = Push_cnt; _}, + Manager_operation_result + {operation_result = Applied (Push_cnt_result _); _} ) -> + Some Eq + | ( Manager_operation {operation = Push_cnt; _}, + Manager_operation_result + {operation_result = Backtracked (Push_cnt_result _, _); _} ) -> + Some Eq + | ( Manager_operation {operation = Push_cnt; _}, + Manager_operation_result + { + operation_result = Failed (Alpha_context.Kind.Push_cnt_manager_kind, _); + _; + } ) -> + Some Eq + | ( Manager_operation {operation = Push_cnt; _}, + Manager_operation_result + {operation_result = Skipped Alpha_context.Kind.Push_cnt_manager_kind; _} + ) -> + Some Eq + | Manager_operation {operation = Push_cnt; _}, _ -> None | ( Manager_operation {operation = Set_deposits_limit _; _}, Manager_operation_result {operation_result = Applied (Set_deposits_limit_result _); _} ) -> diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index d62d61fcf29a..dbd77783433e 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -167,6 +167,11 @@ and _ successful_manager_operation_result = global_address : Script_expr_hash.t; } -> Kind.register_global_constant successful_manager_operation_result + | Push_cnt_result : { + consumed_gas : Gas.Arith.fp; + cnt : int32; + } + -> Kind.push_cnt successful_manager_operation_result | Set_deposits_limit_result : { consumed_gas : Gas.Arith.fp; } diff --git a/src/proto_alpha/lib_protocol/cnt_repr.ml b/src/proto_alpha/lib_protocol/cnt_repr.ml new file mode 100644 index 000000000000..267f7f50e97a --- /dev/null +++ b/src/proto_alpha/lib_protocol/cnt_repr.ml @@ -0,0 +1,33 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(* used cycle_repr.ml as an example *) +(* used raw_level_repr.ml as an example *) + +type t = int32 + +let encoding = Data_encoding.int32 + +let pp ppf cnt = Format.fprintf ppf "%ld" cnt diff --git a/src/proto_alpha/lib_protocol/cnt_repr.mli b/src/proto_alpha/lib_protocol/cnt_repr.mli new file mode 100644 index 000000000000..ca354bff7430 --- /dev/null +++ b/src/proto_alpha/lib_protocol/cnt_repr.mli @@ -0,0 +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. *) +(* *) +(*****************************************************************************) + +type t = int32 + +val encoding : int32 Data_encoding.t + +val pp : Format.formatter -> int32 -> unit diff --git a/src/proto_alpha/lib_protocol/cnt_storage.ml b/src/proto_alpha/lib_protocol/cnt_storage.ml new file mode 100644 index 000000000000..27a4d5a8548e --- /dev/null +++ b/src/proto_alpha/lib_protocol/cnt_storage.ml @@ -0,0 +1,34 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(* example: src/proto_alpha/lib_protocol/contract_delegate_storage.ml *) + +let current ctxt = Storage.Cnt.get ctxt + +let increase ctxt = + let open Lwt_result_syntax in + let* cnt_value = current ctxt in + let updated_cnt = Int32.add cnt_value 1l in + Storage.Cnt.update ctxt updated_cnt diff --git a/src/proto_alpha/lib_protocol/cnt_storage.mli b/src/proto_alpha/lib_protocol/cnt_storage.mli new file mode 100644 index 000000000000..148d8e14d2f6 --- /dev/null +++ b/src/proto_alpha/lib_protocol/cnt_storage.mli @@ -0,0 +1,29 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(* val current : Raw_context.t -> Cnt_repr.t *) +val current : Raw_context.t -> int32 tzresult Lwt.t + +val increase : Raw_context.t -> (Raw_context.t, error trace) result Lwt.t diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index 5c1c4e88d14b..6bc843bd2524 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -135,6 +135,7 @@ Destination_repr Script_int Ticket_amount + Cnt_repr Operation_repr Manager_repr Commitment_repr @@ -164,6 +165,7 @@ Ticket_hash_builder Constants_storage Level_storage + Cnt_storage Nonce_storage Seed_storage Contract_manager_storage @@ -431,6 +433,7 @@ destination_repr.ml destination_repr.mli script_int.ml script_int.mli ticket_amount.ml ticket_amount.mli + cnt_repr.ml cnt_repr.mli operation_repr.ml operation_repr.mli manager_repr.ml manager_repr.mli commitment_repr.ml commitment_repr.mli @@ -460,6 +463,7 @@ ticket_hash_builder.ml ticket_hash_builder.mli constants_storage.ml constants_storage.mli level_storage.ml level_storage.mli + cnt_storage.ml cnt_storage.mli nonce_storage.ml nonce_storage.mli seed_storage.ml seed_storage.mli contract_manager_storage.ml contract_manager_storage.mli @@ -729,6 +733,7 @@ destination_repr.ml destination_repr.mli script_int.ml script_int.mli ticket_amount.ml ticket_amount.mli + cnt_repr.ml cnt_repr.mli operation_repr.ml operation_repr.mli manager_repr.ml manager_repr.mli commitment_repr.ml commitment_repr.mli @@ -758,6 +763,7 @@ ticket_hash_builder.ml ticket_hash_builder.mli constants_storage.ml constants_storage.mli level_storage.ml level_storage.mli + cnt_storage.ml cnt_storage.mli nonce_storage.ml nonce_storage.mli seed_storage.ml seed_storage.mli contract_manager_storage.ml contract_manager_storage.mli @@ -1011,6 +1017,7 @@ destination_repr.ml destination_repr.mli script_int.ml script_int.mli ticket_amount.ml ticket_amount.mli + cnt_repr.ml cnt_repr.mli operation_repr.ml operation_repr.mli manager_repr.ml manager_repr.mli commitment_repr.ml commitment_repr.mli @@ -1040,6 +1047,7 @@ ticket_hash_builder.ml ticket_hash_builder.mli constants_storage.ml constants_storage.mli level_storage.ml level_storage.mli + cnt_storage.ml cnt_storage.mli nonce_storage.ml nonce_storage.mli seed_storage.ml seed_storage.mli contract_manager_storage.ml contract_manager_storage.mli diff --git a/src/proto_alpha/lib_protocol/gas_parameters.json b/src/proto_alpha/lib_protocol/gas_parameters.json index 958030614a1b..b78d40fe33aa 100644 --- a/src/proto_alpha/lib_protocol/gas_parameters.json +++ b/src/proto_alpha/lib_protocol/gas_parameters.json @@ -227,6 +227,8 @@ "interpreter/N_ICheck_signature_secp256k1_alloc_const": 0, "interpreter/N_ICheck_signature_secp256k1_coeff": 1.125, "interpreter/N_ICheck_signature_secp256k1_const": 51600, + "interpreter/N_ICnt_alloc_const": 12, + "interpreter/N_ICnt_const": 10, "interpreter/N_IComb_alloc_coeff": 11.98235521489165, "interpreter/N_IComb_alloc_const": 5.609495538132251, "interpreter/N_IComb_coeff": 3.25, diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index e5958cf63513..7978e1b4abc6 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -135,6 +135,7 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract let* ctxt = Forbidden_delegates_storage.init_for_genesis ctxt in let*! ctxt = Storage.Contract.Total_supply.add ctxt Tez_repr.zero in let* ctxt = Storage.Block_round.init ctxt Round_repr.zero in + let* ctxt = Storage.Cnt.init ctxt 0l in let init_commitment (ctxt, balance_updates) Commitment_repr.{blinded_public_key_hash; amount} = let* ctxt, new_balance_updates = diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index c88d5beb62c5..226d59a8a97f 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -84,6 +84,8 @@ module Kind = struct type register_global_constant = Register_global_constant_kind + type push_cnt = Push_cnt_kind + type transfer_ticket = Transfer_ticket_kind type dal_publish_slot_header = Dal_publish_slot_header_kind @@ -118,6 +120,7 @@ module Kind = struct | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Push_cnt_manager_kind : push_cnt manager | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager @@ -323,6 +326,7 @@ and _ manager_operation = value : Script_repr.lazy_expr; } -> Kind.register_global_constant manager_operation + | Push_cnt : Kind.push_cnt manager_operation | Set_deposits_limit : Tez_repr.t option -> Kind.set_deposits_limit manager_operation @@ -413,6 +417,7 @@ let manager_kind : type kind. kind manager_operation -> kind Kind.manager = | Origination _ -> Kind.Origination_manager_kind | Delegation _ -> Kind.Delegation_manager_kind | Register_global_constant _ -> Kind.Register_global_constant_manager_kind + | Push_cnt -> Kind.Push_cnt_manager_kind | Set_deposits_limit _ -> Kind.Set_deposits_limit_manager_kind | Increase_paid_storage _ -> Kind.Increase_paid_storage_manager_kind | Update_consensus_key _ -> Kind.Update_consensus_key_manager_kind @@ -552,6 +557,10 @@ let zk_rollup_operation_publish_tag = zk_rollup_operation_tag_offset + 1 let zk_rollup_operation_update_tag = zk_rollup_operation_tag_offset + 2 +let cnt_offset = 235 + +let push_cnt_tag = cnt_offset + 0 + module Encoding = struct open Data_encoding @@ -682,6 +691,17 @@ module Encoding = struct inj = (fun value -> Register_global_constant {value}); } + let push_cnt_case = + MCase + { + tag = push_cnt_tag; + name = "push_cnt"; + encoding = Data_encoding.empty; + select = (function Manager (Push_cnt as op) -> Some op | _ -> None); + proj = (function Push_cnt -> ()); + inj = (fun () -> Push_cnt); + } + let set_deposits_limit_case = MCase { @@ -1464,6 +1484,9 @@ module Encoding = struct let register_global_constant_case = make_manager_case 111 Manager_operations.register_global_constant_case + let push_cnt_case = + make_manager_case push_cnt_tag Manager_operations.push_cnt_case + let set_deposits_limit_case = make_manager_case 112 Manager_operations.set_deposits_limit_case @@ -1559,6 +1582,7 @@ module Encoding = struct PCase drain_delegate_case; PCase failing_noop_case; PCase register_global_constant_case; + PCase push_cnt_case; PCase transfer_ticket_case; PCase dal_publish_slot_header_case; PCase sc_rollup_originate_case; @@ -2008,6 +2032,8 @@ let equal_manager_operation_kind : | Delegation _, _ -> None | Register_global_constant _, Register_global_constant _ -> Some Eq | Register_global_constant _, _ -> None + | Push_cnt, Push_cnt -> Some Eq + | Push_cnt, _ -> None | Set_deposits_limit _, Set_deposits_limit _ -> Some Eq | Set_deposits_limit _, _ -> None | Increase_paid_storage _, Increase_paid_storage _ -> Some Eq diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index fb56832141b2..79b5a6ba8f75 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -118,6 +118,8 @@ module Kind : sig type register_global_constant = Register_global_constant_kind + type push_cnt = Push_cnt_kind + type transfer_ticket = Transfer_ticket_kind type dal_publish_slot_header = Dal_publish_slot_header_kind @@ -152,6 +154,7 @@ module Kind : sig | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Push_cnt_manager_kind : push_cnt manager | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager @@ -370,6 +373,7 @@ and _ manager_operation = value : Script_repr.lazy_expr; } -> Kind.register_global_constant manager_operation + | Push_cnt : Kind.push_cnt manager_operation (* [Set_deposits_limit] sets an optional limit for frozen deposits of a contract at a lower value than the maximum limit. When None, the limit in unset back to the default maximum limit. *) @@ -728,6 +732,8 @@ module Encoding : sig val register_global_constant_case : Kind.register_global_constant Kind.manager case + val push_cnt_case : Kind.push_cnt Kind.manager case + val set_deposits_limit_case : Kind.set_deposits_limit Kind.manager case val increase_paid_storage_case : Kind.increase_paid_storage Kind.manager case @@ -788,6 +794,8 @@ module Encoding : sig val register_global_constant_case : Kind.register_global_constant case + val push_cnt_case : Kind.push_cnt case + val set_deposits_limit_case : Kind.set_deposits_limit case val increase_paid_storage_case : Kind.increase_paid_storage case diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 1f383877fa40..2ea5a07d648a 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -105,6 +105,13 @@ module Block_round : Simple_single_data_storage with type value = Round_repr.t = end) (Round_repr) +module Cnt : Simple_single_data_storage with type value = Cnt_repr.t = + Make_single_data_storage (Registered) (Raw_context) + (struct + let name = ["cnt"] + end) + (Cnt_repr) + module Tenderbake = struct module First_level_of_protocol = Make_single_data_storage (Registered) (Raw_context) diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 54319f6a6d59..73677ac4966a 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -49,6 +49,8 @@ end module Block_round : Simple_single_data_storage with type value = Round_repr.t +module Cnt : Simple_single_data_storage with type value = Cnt_repr.t + type missed_attestations_info = {remaining_slots : int; missed_levels : int} module Slashed_deposits_history : sig diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index b5bd8cc1e05e..88473315b221 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -1078,7 +1078,7 @@ let balance_update_of_operation_result : match siopr with | Protocol.Apply_results.Transaction_result (Transaction_to_sc_rollup_result _) - | Reveal_result _ | Update_consensus_key_result _ + | Reveal_result _ | Update_consensus_key_result _ | Push_cnt_result _ | Set_deposits_limit_result _ | Transfer_ticket_result _ | Dal_publish_slot_header_result _ | Sc_rollup_originate_result _ | Sc_rollup_add_messages_result _ | Sc_rollup_cement_result _ @@ -1191,6 +1191,7 @@ let bake_n_with_origination_results ?baking_mode ?policy n b = | Successful_manager_result (Update_consensus_key_result _) | Successful_manager_result (Transaction_result _) | Successful_manager_result (Register_global_constant_result _) + | Successful_manager_result (Push_cnt_result _) | Successful_manager_result (Set_deposits_limit_result _) | Successful_manager_result (Increase_paid_storage_result _) | Successful_manager_result (Transfer_ticket_result _) diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index c98b8379282d..b18f521fe28d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -620,6 +620,25 @@ let register_global_constant ?force_reveal ?counter ?public_key ?fee ?gas_limit in sign account.sk (Context.branch ctxt) sop +let push_cnt ?force_reveal ?counter ?public_key ?fee ?gas_limit ?storage_limit + ctxt ~source = + let open Lwt_result_syntax in + let* account = Context.Contract.manager ctxt source in + let operation = Push_cnt in + let+ sop = + manager_operation + ?force_reveal + ?counter + ?public_key + ?fee + ?gas_limit + ?storage_limit + ~source + ctxt + operation + in + sign account.sk (Context.branch ctxt) sop + let unsafe_transaction ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ?(parameters = Script.unit_parameter) ?(entrypoint = Entrypoint.default) ctxt (src : Contract.t) (destination : Contract.t) (amount : Tez.t) = diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index adea59f7c646..af25a094213d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -305,6 +305,18 @@ val register_global_constant : value:Protocol.Alpha_context.Script.lazy_expr -> (Protocol.operation, tztrace) result Lwt.t +val push_cnt : + ?force_reveal:bool -> + ?counter:Manager_counter.t -> + ?public_key:Signature.public_key -> + ?fee:Tez.t -> + ?gas_limit:gas_limit -> + ?storage_limit:Z.t -> + Context.t -> + (* Account doing the registration *) + source:Contract.t -> + (Protocol.operation, tztrace) result Lwt.t + val double_attestation : Context.t -> Kind.attestation Operation.t -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml b/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml index 59f46b49d293..b232411e6435 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml @@ -73,6 +73,7 @@ let manager_kinds = `KSet_deposits_limit; `KIncrease_paid_storage; `KRegister_global_constant; + `KPush_cnt; `KTransfer_ticket; `KDal_publish_slot_header; `KSc_rollup_originate; @@ -512,6 +513,10 @@ let generate_register_global_constant = let value = Script_repr.lazy_expr (Expr.from_string "Pair 1 2") in QCheck2.Gen.pure (Register_global_constant {value}) +let generate_push_cnt = + let open QCheck2.Gen in + return Push_cnt + let generate_transfer_ticket = let open QCheck2.Gen in let* ticketer = random_contract in @@ -614,6 +619,7 @@ let generator_of ?source = function | `KDelegation -> generate_manager_operation ?source generate_delegation | `KRegister_global_constant -> generate_manager_operation ?source generate_register_global_constant + | `KPush_cnt -> generate_manager_operation ?source generate_push_cnt | `KTransfer_ticket -> generate_manager_operation ?source generate_transfer_ticket | `KDal_publish_slot_header -> diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml b/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml index d9d4f2f9bb3a..dd0c87c83e76 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml @@ -77,6 +77,7 @@ type manager_operation_kind = | K_Transaction | K_Origination | K_Register_global_constant + | K_Push_cnt | K_Delegation | K_Undelegation | K_Self_delegation @@ -173,6 +174,7 @@ let kind_to_string = function | K_Update_consensus_key -> "Update consensus key" | K_Origination -> "Origination" | K_Register_global_constant -> "Register global constant" + | K_Push_cnt -> "Push cnt" | K_Increase_paid_storage -> "Increase paid storage" | K_Reveal -> "Revelation" | K_Transfer_ticket -> "Transfer_ticket" @@ -667,6 +669,16 @@ let mk_register_global_constant (oinfos : operation_req) (infos : infos) = ~source:(contract_of (get_source infos)) ~value:(Script_repr.lazy_expr (Expr.from_string "Pair 1 2")) +let mk_push_cnt (oinfos : operation_req) (infos : infos) = + Op.push_cnt + ?force_reveal:oinfos.force_reveal + ?counter:oinfos.counter + ?fee:oinfos.fee + ?gas_limit:oinfos.gas_limit + ?storage_limit:oinfos.storage_limit + (B infos.ctxt.block) + ~source:(contract_of (get_source infos)) + let mk_set_deposits_limit (oinfos : operation_req) (infos : infos) = Op.set_deposits_limit ?force_reveal:oinfos.force_reveal @@ -987,6 +999,7 @@ let select_op (op_req : operation_req) (infos : infos) = | K_Transaction -> mk_transaction | K_Origination -> mk_origination | K_Register_global_constant -> mk_register_global_constant + | K_Push_cnt -> mk_push_cnt | K_Delegation -> mk_delegation | K_Undelegation -> mk_undelegation | K_Self_delegation -> mk_self_delegation @@ -1353,6 +1366,7 @@ let subjects = K_Transaction; K_Origination; K_Register_global_constant; + K_Push_cnt; K_Delegation; K_Undelegation; K_Self_delegation; @@ -1384,7 +1398,7 @@ let is_consumer = function | K_Dal_publish_slot_header | K_Zk_rollup_origination | K_Zk_rollup_publish | K_Zk_rollup_update -> false - | K_Transaction | K_Origination | K_Register_global_constant + | K_Transaction | K_Origination | K_Register_global_constant | K_Push_cnt | K_Transfer_ticket -> true @@ -1395,8 +1409,8 @@ let revealed_subjects = List.filter (function K_Reveal -> false | _ -> true) subjects let is_disabled flags = function - | K_Transaction | K_Origination | K_Register_global_constant | K_Delegation - | K_Undelegation | K_Self_delegation | K_Set_deposits_limit + | K_Transaction | K_Origination | K_Register_global_constant | K_Push_cnt + | K_Delegation | K_Undelegation | K_Self_delegation | K_Set_deposits_limit | K_Update_consensus_key | K_Increase_paid_storage | K_Reveal | K_Transfer_ticket -> false 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 98b47f13288f..54570830cb06 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 @@ -63,6 +63,7 @@ let ensure_kind infos kind = | Delegation _, K_Undelegation | Delegation _, K_Self_delegation | Register_global_constant _, K_Register_global_constant + | Push_cnt, K_Push_cnt | Set_deposits_limit _, K_Set_deposits_limit | Update_consensus_key _, K_Update_consensus_key | Increase_paid_storage _, K_Increase_paid_storage @@ -81,13 +82,14 @@ let ensure_kind infos kind = | Zk_rollup_update _, K_Zk_rollup_update -> return_unit | ( ( Transaction _ | Origination _ | Register_global_constant _ - | Delegation _ | Set_deposits_limit _ | Update_consensus_key _ - | Increase_paid_storage _ | Reveal _ | Transfer_ticket _ - | Sc_rollup_originate _ | Sc_rollup_publish _ | Sc_rollup_cement _ - | Sc_rollup_add_messages _ | Sc_rollup_refute _ | Sc_rollup_timeout _ - | Sc_rollup_execute_outbox_message _ | Sc_rollup_recover_bond _ - | Dal_publish_slot_header _ | Zk_rollup_origination _ - | Zk_rollup_publish _ | Zk_rollup_update _ ), + | Push_cnt | Delegation _ | Set_deposits_limit _ + | Update_consensus_key _ | Increase_paid_storage _ | Reveal _ + | Transfer_ticket _ | Sc_rollup_originate _ | Sc_rollup_publish _ + | Sc_rollup_cement _ | Sc_rollup_add_messages _ | Sc_rollup_refute _ + | Sc_rollup_timeout _ | Sc_rollup_execute_outbox_message _ + | Sc_rollup_recover_bond _ | Dal_publish_slot_header _ + | Zk_rollup_origination _ | Zk_rollup_publish _ | Zk_rollup_update _ + ), _ ) -> assert false) | Single _ -> assert false diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index b5b9eda7e4fb..284f1fcece00 100644 --- a/src/proto_alpha/lib_protocol/validate.ml +++ b/src/proto_alpha/lib_protocol/validate.ml @@ -2100,6 +2100,7 @@ module Manager = struct | Register_global_constant {value} -> let* (_ : Gas.Arith.fp) = consume_decoding_gas remaining_gas value in return_unit + | Push_cnt -> return_unit | Delegation (Some pkh) -> Delegate.check_not_tz4 pkh | Update_consensus_key pk -> Delegate.Consensus_key.check_not_tz4 pk | Delegation None | Set_deposits_limit _ | Increase_paid_storage _ -> diff --git a/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml b/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml index a1106f5a0b6e..34a3af164d0d 100644 --- a/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml +++ b/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml @@ -323,9 +323,10 @@ let process_l1_operation (type kind) ~catching_up node_ctxt rollup = node_ctxt.Node_context.config.sc_rollup_address) | Dal_publish_slot_header _ -> true | Reveal _ | Transaction _ | Origination _ | Delegation _ - | Update_consensus_key _ | Register_global_constant _ | Set_deposits_limit _ - | Increase_paid_storage _ | Transfer_ticket _ | Sc_rollup_originate _ - | Zk_rollup_origination _ | Zk_rollup_publish _ | Zk_rollup_update _ -> + | Update_consensus_key _ | Register_global_constant _ | Push_cnt + | Set_deposits_limit _ | Increase_paid_storage _ | Transfer_ticket _ + | Sc_rollup_originate _ | Zk_rollup_origination _ | Zk_rollup_publish _ + | Zk_rollup_update _ -> false in (* Only look at operations that are for the node's rollup *) -- GitLab From 0fecb90e676f465fa47752c0d6b18a509e8578d0 Mon Sep 17 00:00:00 2001 From: Diana Savatina Date: Tue, 16 Jan 2024 17:40:05 +0000 Subject: [PATCH 2/3] cnt: added michelson and cnt.tz mockup-client -l --protocol ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK typecheck script cnt.tz Well typed (Gas remaining: 1039998.017 units remaining) mockup-client -l --protocol ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK run script cnt.tz on storage '1' and input 'Unit' Sandbox mode: ------------- octez-client originate contract mycnt1 transferring 10 from bootstrap1 running cnt.tz --init 1 --burn-cap 0.07425 & octez-client -l bake for --minimal-timestamp ... Script: { parameter unit ; storage int ; code { DROP ; CNT ; NIL operation ; PAIR } } Initial storage: 1 No delegate for this contract This origination was successfully applied octez-client transfer 0 from bootstrap1 to mycnt1 & ... Updated storage: 1 octez-client transfer 0 from bootstrap1 to mycnt1 & octez-client bake for --minimal-timestamp Updated storage: 2 octez-client transfer 0 from bootstrap1 to mycnt1 & octez-client bake for --minimal-timestamp Updated storage: 3 octez-client transfer 0 from bootstrap1 to mycnt1 & octez-client bake for --minimal-timestamp Updated storage: 4 octez-client increase counter from bootstrap2 & octez-client bake for --minimal-timestamp Updated counter: 5 octez-client transfer 0 from bootstrap1 to mycnt1 & octez-client bake for --minimal-timestamp Updated storage: 6 octez-client transfer 0 from bootstrap1 to mycnt1 & octez-client increase counter from bootstrap2 & octez-client bake for --minimal-timestamp Updated storage: 8 Updated storage: 7 octez-client increase counter from bootstrap2 & octez-client bake for --minimal-timestamp Updated counter: 9 Fixed activation issue: > octez-activate-alpha Error: { "id": "proto.alpha.michelson_v1.ill_formed_type", "description": "The toplevel error thrown when trying to parse a type expression (always followed by more precise errors).", ... { "id": "proto.alpha.michelson_v1.invalid_arity", "description": "In a script or data expression, a primitive was applied to an unsupported number of arguments.", "data": { "location": 2, "primitive_name": "option", "expected_arity": 1, "wrong_arity": 2 } } --- cnt.tz | 3 +-- .../lib_benchmarks_proto/interpreter_benchmarks.ml | 7 +++++++ .../lib_benchmarks_proto/interpreter_model.ml | 2 +- .../lib_benchmarks_proto/interpreter_workload.ml | 6 ++++++ src/proto_alpha/lib_plugin/RPC.ml | 1 + .../lib_plugin/script_interpreter_logging.ml | 9 +++++++++ src/proto_alpha/lib_protocol/alpha_context.mli | 1 + src/proto_alpha/lib_protocol/michelson_v1_gas.ml | 2 ++ src/proto_alpha/lib_protocol/michelson_v1_gas.mli | 2 ++ .../michelson_v1_gas_costs_generated.ml | 12 ++++++++++++ .../lib_protocol/michelson_v1_primitives.ml | 8 ++++++-- .../lib_protocol/michelson_v1_primitives.mli | 1 + src/proto_alpha/lib_protocol/script_interpreter.ml | 13 +++++++++++++ .../lib_protocol/script_interpreter_defs.ml | 1 + .../lib_protocol/script_ir_translator.ml | 8 +++++++- src/proto_alpha/lib_protocol/script_typed_ir.ml | 5 +++++ src/proto_alpha/lib_protocol/script_typed_ir.mli | 3 +++ .../lib_protocol/script_typed_ir_size.ml | 1 + 18 files changed, 79 insertions(+), 6 deletions(-) diff --git a/cnt.tz b/cnt.tz index 42aed526e2de..6f74bd66b551 100644 --- a/cnt.tz +++ b/cnt.tz @@ -1,4 +1,3 @@ parameter unit ; storage int ; -code { # This contract pushes counter and the counter is increased and pushed to the stack - DROP ; CNT ; NIL operation ; PAIR} ; +code { DROP ; CNT ; NIL operation ; PAIR} ; diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml index ed15db1546c9..05451bfb72cf 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml @@ -3283,6 +3283,13 @@ module Registration_section = struct ~kinstr:(IAmount (dummy_loc, halt)) () + let () = + simple_time_alloc_benchmark + ~name:Interpreter_workload.N_ICnt + ~stack_type:(unit @$ bot) + ~kinstr:(ICnt (dummy_loc, halt)) + () + let () = simple_time_alloc_benchmark ~name:Interpreter_workload.N_IChainId diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml index 0100cd5898be..8de57f4a9f34 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml @@ -643,7 +643,7 @@ let ir_model instr_or_cont = | N_IDip | N_IExec | N_IView | N_IFailwith | N_IAddress | N_ICreate_contract | N_ISet_delegate | N_INow | N_IMin_block_time | N_IBalance | N_IHash_key | N_IUnpack | N_ISource | N_ISender | N_ISelf - | N_IAmount | N_IChainId | N_ILevel | N_ISelf_address | N_INever + | N_IAmount | N_ICnt | N_IChainId | N_ILevel | N_ISelf_address | N_INever | N_IUnpair | N_IVoting_power | N_ITotal_voting_power | N_IList_size | N_ISet_size | N_IMap_size | N_ISapling_empty_state -> (const1_model, const1_model) |> m2 name diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_workload.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_workload.ml index c7c16d355f91..03f51f9013dd 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_workload.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_workload.ml @@ -198,6 +198,7 @@ type instruction_name = | N_ISelf | N_ISelf_address | N_IAmount + | N_ICnt | N_ISapling_empty_state | N_ISapling_verify_update | N_IDig @@ -401,6 +402,7 @@ let string_of_instruction_name : instruction_name -> string = | N_ISender -> "N_ISender" | N_ISelf -> "N_ISelf" | N_IAmount -> "N_IAmount" + | N_ICnt -> "N_ICnt" | N_IDig -> "N_IDig" | N_IDug -> "N_IDug" | N_IDipN -> "N_IDipN" @@ -684,6 +686,7 @@ let all_instructions = N_IXor_bytes; N_INot_bytes; N_IUnit; + N_ICnt; ] (* Changing the ordering breaks the workload file compatibility *) @@ -1084,6 +1087,8 @@ module Instructions = struct let amount = ir_sized_step N_IAmount nullary + let cnt = ir_sized_step N_ICnt nullary + let dig depth = ir_sized_step N_IDig (unary "depth" depth) let dug depth = ir_sized_step N_IDug (unary "depth" depth) @@ -1482,6 +1487,7 @@ let extract_ir_sized_step : | ISelf (_, _, _, _), _ -> Instructions.self | ISelf_address (_, _), _ -> Instructions.self_address | IAmount (_, _), _ -> Instructions.amount + | ICnt (_, _), _ -> Instructions.cnt | ISapling_empty_state (_, _, _), _ -> Instructions.sapling_empty_state | ISapling_verify_update (_, _), (transaction, (_state, _)) -> let inputs = Size.sapling_transaction_inputs transaction in diff --git a/src/proto_alpha/lib_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index b50dc53262d2..717cc1e0fda1 100644 --- a/src/proto_alpha/lib_plugin/RPC.ml +++ b/src/proto_alpha/lib_plugin/RPC.ml @@ -1073,6 +1073,7 @@ module Scripts = struct | ISelf _ -> pp_print_string fmt "SELF" | ISelf_address _ -> pp_print_string fmt "SELF_ADDRESS" | IAmount _ -> pp_print_string fmt "AMOUNT" + | ICnt _ -> pp_print_string fmt "CNT" | ISapling_empty_state _ -> pp_print_string fmt "SAPLING_EMPTY_STATE" | ISapling_verify_update _ | ISapling_verify_update_deprecated _ -> pp_print_string fmt "SAPLING_VERIFY_UPDATE" diff --git a/src/proto_alpha/lib_plugin/script_interpreter_logging.ml b/src/proto_alpha/lib_plugin/script_interpreter_logging.ml index a6fa1d80bce2..0327d0ee6592 100644 --- a/src/proto_alpha/lib_plugin/script_interpreter_logging.ml +++ b/src/proto_alpha/lib_plugin/script_interpreter_logging.ml @@ -1325,6 +1325,15 @@ module Stack_utils = struct continuation = k; reconstruct = (fun k -> IAmount (loc, k)); } + | ICnt (loc, k), s -> + let s = Item_t (int_t, s) in + return + @@ Ex_split_kinstr + { + cont_init_stack = s; + continuation = k; + reconstruct = (fun k -> ICnt (loc, k)); + } | ISapling_empty_state (loc, memo_size, k), s -> return @@ Ex_split_kinstr diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 6778601d9ad7..09a59854d57d 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -588,6 +588,7 @@ module Script : sig | I_CDR | I_CHAIN_ID | I_CHECK_SIGNATURE + | I_CNT | I_COMPARE | I_CONCAT | I_CONS diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml index 681337fa61c1..8069fa07bd78 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml @@ -453,6 +453,8 @@ module Cost_of = struct let chain_id = atomic_step_cost cost_N_IChainId + let cnt = atomic_step_cost cost_N_ICnt + let ticket = atomic_step_cost cost_N_ITicket let read_ticket = atomic_step_cost cost_N_IRead_ticket diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli index 8f64123e310c..ee6cdfa54b14 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli @@ -371,6 +371,8 @@ module Cost_of : sig val amount : Gas.cost + val cnt : Gas.cost + val chain_id : Gas.cost val unpack : bytes -> Gas.cost diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml index a64681951e44..8949a0b4d9db 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml @@ -500,6 +500,18 @@ let cost_N_IAmount_alloc = S.safe_int 15 (* let time = 10. in let alloc = 12. in max 10 (max time alloc) *) let cost_N_IAmount_synthesized = S.safe_int 15 +(* model interpreter/N_ICnt *) +(* max 10 10. *) +let cost_N_ICnt = S.safe_int 10 + +(* model interpreter/N_ICnt_alloc *) +(* max 10 12. *) +let cost_N_ICnt_alloc = S.safe_int 15 + +(* model interpreter/N_ICnt_synthesized *) +(* let time = 10. in let alloc = 12. in max 10 (max time alloc) *) +let cost_N_ICnt_synthesized = S.safe_int 15 + (* model interpreter/N_IAnd *) (* max 10 10. *) let cost_N_IAnd = S.safe_int 10 diff --git a/src/proto_alpha/lib_protocol/michelson_v1_primitives.ml b/src/proto_alpha/lib_protocol/michelson_v1_primitives.ml index 8c198be16e91..7284011f6b46 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_primitives.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_primitives.ml @@ -64,6 +64,7 @@ type prim = | I_CDR | I_CHAIN_ID | I_CHECK_SIGNATURE + | I_CNT | I_COMPARE | I_CONCAT | I_CONS @@ -208,7 +209,7 @@ let namespace = function | D_Elt | D_False | D_Left | D_None | D_Pair | D_Right | D_Some | D_True | D_Unit | D_Lambda_rec -> Constant_namespace - | I_ABS | I_ADD | I_ADDRESS | I_AMOUNT | I_AND | I_APPLY | I_BALANCE + | I_ABS | I_ADD | I_ADDRESS | I_AMOUNT | I_CNT | I_AND | I_APPLY | I_BALANCE | I_BLAKE2B | I_CAR | I_CAST | I_CDR | I_CHAIN_ID | I_CHECK_SIGNATURE | I_COMPARE | I_CONCAT | I_CONS | I_CONTRACT | I_CREATE_ACCOUNT | I_CREATE_CONTRACT | I_DIG | I_DIP | I_DROP | I_DUG | I_DUP | I_VIEW | I_EDIV @@ -277,6 +278,7 @@ let string_of_prim = function | I_CDR -> "CDR" | I_CHAIN_ID -> "CHAIN_ID" | I_CHECK_SIGNATURE -> "CHECK_SIGNATURE" + | I_CNT -> "CNT" | I_COMPARE -> "COMPARE" | I_CONCAT -> "CONCAT" | I_CONS -> "CONS" @@ -441,6 +443,7 @@ let prim_of_string = | "COMPARE" -> return I_COMPARE | "CONCAT" -> return I_CONCAT | "CONS" -> return I_CONS + | "CNT" -> return I_CNT | "CREATE_ACCOUNT" -> return I_CREATE_ACCOUNT | "CREATE_CONTRACT" -> return I_CREATE_CONTRACT | "IMPLICIT_ACCOUNT" -> return I_IMPLICIT_ACCOUNT @@ -792,7 +795,8 @@ let prim_encoding = ("LAMBDA_REC", I_LAMBDA_REC); ("TICKET", I_TICKET); ("BYTES", I_BYTES); - ("NAT", I_NAT) + ("NAT", I_NAT); + ("CNT", I_CNT) (* New instructions must be added here, for backward compatibility of the encoding. *) (* Keep the comment above at the end of the list *); ] diff --git a/src/proto_alpha/lib_protocol/michelson_v1_primitives.mli b/src/proto_alpha/lib_protocol/michelson_v1_primitives.mli index 761631190aba..be4fcf2c0b17 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_primitives.mli +++ b/src/proto_alpha/lib_protocol/michelson_v1_primitives.mli @@ -76,6 +76,7 @@ type prim = | I_CDR | I_CHAIN_ID | I_CHECK_SIGNATURE + | I_CNT | I_COMPARE | I_CONCAT | I_CONS diff --git a/src/proto_alpha/lib_protocol/script_interpreter.ml b/src/proto_alpha/lib_protocol/script_interpreter.ml index 02f00a6bf3f5..37ecb6aa87b2 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter.ml @@ -1368,6 +1368,19 @@ module Raw = struct | IAmount (_, k) -> let accu = sc.amount and stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks accu stack + | ICnt (_, k) -> + let ctxt = update_context gas ctxt in + let* ctxt = Cnt.increase ctxt in + let* new_counter_int = Cnt.get ctxt in + let new_counter = Script_int.of_int32 new_counter_int in + let gas, ctxt = local_gas_counter_and_outdated_context ctxt in + (step [@ocaml.tailcall]) + (ctxt, sc) + gas + k + ks + new_counter + (accu, stack) | IDig (_, _n, n', k) -> let (accu, stack), x = interp_stack_prefix_preserving_operation diff --git a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml index cfdad3f2c2a8..068761f8f234 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml @@ -366,6 +366,7 @@ let cost_of_instr : type a s r f. (a, s, r, f) kinstr -> a -> s -> Gas.cost = | ISelf _ -> Interp_costs.self | ISelf_address _ -> Interp_costs.self_address | IAmount _ -> Interp_costs.amount + | ICnt _ -> Interp_costs.cnt | IDig (_, n, _, _) -> Interp_costs.dign n | IDug (_, n, _, _) -> Interp_costs.dugn n | IDipn (_, n, _, _, _) -> Interp_costs.dipn n diff --git a/src/proto_alpha/lib_protocol/script_ir_translator.ml b/src/proto_alpha/lib_protocol/script_ir_translator.ml index 91fcfc55f07c..cd258395d369 100644 --- a/src/proto_alpha/lib_protocol/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/script_ir_translator.ml @@ -4361,6 +4361,11 @@ and parse_instr : let instr = {apply = (fun k -> IAmount (loc, k))} in let stack = Item_t (mutez_t, stack) in typed ctxt loc instr stack + | Prim (loc, I_CNT, [], annot), stack -> + let*? () = check_var_annot loc annot in + let instr = {apply = (fun k -> ICnt (loc, k))} in + let stack = Item_t (Int_t, stack) in + typed ctxt loc instr stack | Prim (loc, I_CHAIN_ID, [], annot), stack -> let*? () = check_var_annot loc annot in let instr = {apply = (fun k -> IChainId (loc, k))} in @@ -4639,7 +4644,7 @@ and parse_instr : | I_EDIV | I_OR | I_AND | I_XOR | I_NOT | I_ABS | I_NEG | I_LSL | I_LSR | I_COMPARE | I_EQ | I_NEQ | I_LT | I_GT | I_LE | I_GE | I_TRANSFER_TOKENS | I_SET_DELEGATE | I_NOW | I_MIN_BLOCK_TIME - | I_IMPLICIT_ACCOUNT | I_AMOUNT | I_BALANCE | I_LEVEL + | I_IMPLICIT_ACCOUNT | I_AMOUNT | I_CNT | I_BALANCE | I_LEVEL | I_CHECK_SIGNATURE | I_HASH_KEY | I_SOURCE | I_SENDER | I_BLAKE2B | I_SHA256 | I_SHA512 | I_ADDRESS | I_RENAME | I_PACK | I_ISNAT | I_INT | I_SELF | I_CHAIN_ID | I_NEVER | I_VOTING_POWER @@ -4745,6 +4750,7 @@ and parse_instr : I_CAR; I_CDR; I_CHECK_SIGNATURE; + I_CNT; I_COMPARE; I_CONCAT; I_CONS; diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.ml b/src/proto_alpha/lib_protocol/script_typed_ir.ml index 45d9c4993216..4029ee596369 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.ml +++ b/src/proto_alpha/lib_protocol/script_typed_ir.ml @@ -960,6 +960,9 @@ and ('before_top, 'before, 'result_top, 'result) kinstr = | IAmount : Script.location * (Tez.t, 'a * 'S, 'r, 'F) kinstr -> ('a, 'S, 'r, 'F) kinstr + | ICnt : + Script.location * (z num, 'a * 'S, 'r, 'F) kinstr + -> ('a, 'S, 'r, 'F) kinstr | ISapling_empty_state : Script.location * Sapling.Memo_size.t @@ -1669,6 +1672,7 @@ let kinstr_location : type a s b f. (a, s, b, f) kinstr -> Script.location = | ISelf (loc, _, _, _) -> loc | ISelf_address (loc, _) -> loc | IAmount (loc, _) -> loc + | ICnt (loc, _) -> loc | ISapling_empty_state (loc, _, _) -> loc | ISapling_verify_update (loc, _) -> loc | ISapling_verify_update_deprecated (loc, _) -> loc @@ -2248,6 +2252,7 @@ let kinstr_traverse i init f = | ISelf (_, _, _, k) -> (next [@ocaml.tailcall]) k | ISelf_address (_, k) -> (next [@ocaml.tailcall]) k | IAmount (_, k) -> (next [@ocaml.tailcall]) k + | ICnt (_, k) -> (next [@ocaml.tailcall]) k | ISapling_empty_state (_, _, k) -> (next [@ocaml.tailcall]) k | ISapling_verify_update (_, k) -> (next [@ocaml.tailcall]) k | ISapling_verify_update_deprecated (_, k) -> (next [@ocaml.tailcall]) k diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.mli b/src/proto_alpha/lib_protocol/script_typed_ir.mli index 679f1f4a0051..d7919ae0ab59 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.mli +++ b/src/proto_alpha/lib_protocol/script_typed_ir.mli @@ -947,6 +947,9 @@ and ('before_top, 'before, 'result_top, 'result) kinstr = | IAmount : Script.location * (Tez.t, 'a * 'S, 'r, 'F) kinstr -> ('a, 'S, 'r, 'F) kinstr + | ICnt : + Script.location * (z num, 'a * 'S, 'r, 'F) kinstr + -> ('a, 'S, 'r, 'F) kinstr | ISapling_empty_state : Script.location * Sapling.Memo_size.t diff --git a/src/proto_alpha/lib_protocol/script_typed_ir_size.ml b/src/proto_alpha/lib_protocol/script_typed_ir_size.ml index e3ade406d01d..45499f8836d4 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir_size.ml +++ b/src/proto_alpha/lib_protocol/script_typed_ir_size.ml @@ -604,6 +604,7 @@ and kinstr_size : (base1 loc k +! (word_size *? 2) +! Entrypoint.in_memory_size s) | ISelf_address (loc, k) -> ret_succ_adding accu (base1 loc k) | IAmount (loc, k) -> ret_succ_adding accu (base1 loc k) + | ICnt (loc, k) -> ret_succ_adding accu (base1 loc k) | ISapling_empty_state (loc, m, k) -> ret_succ_adding accu -- GitLab From e376a19bbbfb5a48cb7dafec711507bbd4be9292 Mon Sep 17 00:00:00 2001 From: Diana Savatina Date: Wed, 24 Jan 2024 18:11:54 +0000 Subject: [PATCH 3/3] added unit tests and integration tests > dune exec src/proto_alpha/lib_protocol/test/unit/main.exe \ -- --file test_cnt_repr.ml qcheck random seed: 305030579 [13:35:42.078] [SUCCESS] (1/2) alpha: Cnt_storage.ml (Test counter init for internal count) [13:35:42.080] [SUCCESS] (2/2) alpha: Cnt_storage.ml (Test counter increase for internal count) > dune exec tezt/tests/main.exe -- --file cnt.ml qcheck random seed: 412981918 [13:26:18.749] [SUCCESS] (1/1) Alpha: Increasing intrernal counter with RPC --- manifest/main.ml | 1 + src/proto_alpha/lib_protocol/cnt_storage.mli | 4 +- src/proto_alpha/lib_protocol/test/unit/dune | 3 +- .../lib_protocol/test/unit/test_cnt_repr.ml | 78 +++++++++++++++++++ tezt/lib_tezos/client.ml | 18 +++++ tezt/lib_tezos/client.mli | 4 + tezt/tests/cnt.ml | 66 ++++++++++++++++ tezt/tests/main.ml | 1 + 8 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/test/unit/test_cnt_repr.ml create mode 100644 tezt/tests/cnt.ml diff --git a/manifest/main.ml b/manifest/main.ml index c21f3199d8a8..2b22ad6268b1 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -5531,6 +5531,7 @@ end = struct ("test_adaptive_issuance", N.(number >= 018)); ("test_adaptive_issuance_ema", N.(number >= 018)); ("test_percentage", N.(number >= 019)); + ("test_cnt_repr", N.(number >= 019)); ] |> conditional_list in diff --git a/src/proto_alpha/lib_protocol/cnt_storage.mli b/src/proto_alpha/lib_protocol/cnt_storage.mli index 148d8e14d2f6..cfd72e1acbb0 100644 --- a/src/proto_alpha/lib_protocol/cnt_storage.mli +++ b/src/proto_alpha/lib_protocol/cnt_storage.mli @@ -26,4 +26,6 @@ (* val current : Raw_context.t -> Cnt_repr.t *) val current : Raw_context.t -> int32 tzresult Lwt.t -val increase : Raw_context.t -> (Raw_context.t, error trace) result Lwt.t +val increase : Raw_context.t -> Raw_context.t tzresult Lwt.t + +(* val increase : Raw_context.t -> (Raw_context.t, error trace) result Lwt.t *) diff --git a/src/proto_alpha/lib_protocol/test/unit/dune b/src/proto_alpha/lib_protocol/test/unit/dune index 26a36e23fdb2..25a09b6fa54b 100644 --- a/src/proto_alpha/lib_protocol/test/unit/dune +++ b/src/proto_alpha/lib_protocol/test/unit/dune @@ -74,7 +74,8 @@ test_dal_slot_proof test_adaptive_issuance test_adaptive_issuance_ema - test_percentage)) + test_percentage + test_cnt_repr)) (executable (name main) diff --git a/src/proto_alpha/lib_protocol/test/unit/test_cnt_repr.ml b/src/proto_alpha/lib_protocol/test/unit/test_cnt_repr.ml new file mode 100644 index 000000000000..27681f0d74c7 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/unit/test_cnt_repr.ml @@ -0,0 +1,78 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2021 Marigold *) +(* *) +(* 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: Contract_repr + Invocation: dune exec src/proto_alpha/lib_protocol/test/unit/main.exe \ + -- --file test_cnt_repr.ml + Dependencies: contract_hash.ml + Subject: Testing the internal protocol counter. +*) + +open Protocol + +(* open Storage_functors *) +module A = Alpha_context + +let create () = + let open Lwt_result_syntax in + let account = Account.new_account () in + let bootstrap_account = Account.make_bootstrap_account account in + let* alpha_ctxt = Block.alpha_context [bootstrap_account] in + return @@ A.Internal_for_tests.to_raw alpha_ctxt + +let test_cnt_init () = + let open Lwt_result_wrap_syntax in + let* ctxt = create () in + + let*@ counter = Cnt_storage.current ctxt in + Assert.equal_int32 ~loc:__LOC__ counter 0l + +let test_cnt_increase () = + let open Lwt_result_wrap_syntax in + let* ctxt = create () in + let*@ ctxt = Cnt_storage.increase ctxt in + let*@ counter = Cnt_storage.current ctxt in + let* () = Assert.equal_int32 ~loc:__LOC__ counter 1l in + let*@ ctxt = Cnt_storage.increase ctxt in + let*@ counter = Cnt_storage.current ctxt in + let* () = Assert.equal_int32 ~loc:__LOC__ counter 2l in + let*@ ctxt = Cnt_storage.increase ctxt in + let*@ counter = Cnt_storage.current ctxt in + Assert.equal_int32 ~loc:__LOC__ counter 3l + +let tests = + [ + Tztest.tztest "Test counter init for internal count" `Quick test_cnt_init; + Tztest.tztest + "Test counter increase for internal count" + `Quick + test_cnt_increase; + ] + +let () = + Alcotest_lwt.run ~__FILE__ Protocol.name [("Cnt_storage.ml", tests)] + |> Lwt_main.run diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 31271bae5806..96bf8dc84718 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -2014,6 +2014,24 @@ let register_global_constant ?wait ?burn_cap ~src ~value client = client_output | Some hash -> return hash +let spawn_cnt ?(wait = "none") ?burn_cap ~src client = + spawn_command + client + (["--wait"; wait] + @ ["increase"; "counter"; "from"; src] + @ optional_arg "burn-cap" Tez.to_string burn_cap) + +let cnt ?wait ?burn_cap ~src client = + let* client_output = + spawn_cnt ?wait ?burn_cap ~src client |> Process.check_and_read_stdout + in + match client_output =~* rex "Updated counter: (\\d+)" with + | None -> + Test.fail + "Cannot extract updated counter from client_output: %s" + client_output + | Some new_counter_value -> return (Int32.of_string new_counter_value) + type hash_script_format = TSV | CSV let show_hash_script_format = function TSV -> "tsv" | CSV -> "csv" diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 739d0ad5822d..113de6f42599 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1516,6 +1516,10 @@ val spawn_register_global_constant : t -> Process.t +val cnt : ?wait:string -> ?burn_cap:Tez.t -> src:string -> t -> int32 Lwt.t + +val spawn_cnt : ?wait:string -> ?burn_cap:Tez.t -> src:string -> t -> Process.t + (** Represents the result of a [octez-client hash data .. of type ...] call. Given the output: diff --git a/tezt/tests/cnt.ml b/tezt/tests/cnt.ml new file mode 100644 index 000000000000..e2a948911a5a --- /dev/null +++ b/tezt/tests/cnt.ml @@ -0,0 +1,66 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2023 Trilitech *) +(* *) +(* 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 + Invocation: dune exec tezt/tests/main.exe -- --file cnt.ml + Subject: Test the internal counter with baking +*) +let test_cnt = + Protocol.register_test + ~__FILE__ + ~title:"Increasing intrernal counter with RPC" + ~tags:["cnt"; "rpc"] + ~supports:(Protocol.From_protocol 019) + @@ fun protocol -> + let* _, client = + Client.init_with_protocol + ~nodes_args:[Synchronisation_threshold 0] + ~protocol + `Client + () + in + let* new_counter = + Client.cnt ~src:"bootstrap1" ?burn_cap:(Some (Tez.of_int 100)) client + in + Check.( + (new_counter = 1l) + int32 + ~__LOC__ + ~error_msg:"Expected the counter %R instead of %L") ; + let* () = Client.bake_for_and_wait client in + let* new_counter = + Client.cnt ~src:"bootstrap1" ?burn_cap:(Some (Tez.of_int 100)) client + in + (* updating the counter again *) + Check.( + (new_counter = 2l) + int32 + ~__LOC__ + ~error_msg:"Expected the counter %R instead of %L") ; + let* () = Client.bake_for_and_wait client in + unit + +let register ~protocols = test_cnt protocols diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index ec4e8aeec178..6eab5b8cbb9a 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -124,6 +124,7 @@ let register_protocol_tests_that_use_supports_correctly () = Client_keys.register ~protocols ; Client_run_view.register ~protocols ; Client_simulation_flag.register ~protocols ; + Cnt.register ~protocols ; Comparable_datatype.register ~protocols ; Consensus_key.register ~protocols ; Contract_baker.register ~protocols ; -- GitLab