From fa4b33aa475f633417cad1abb94095353b628cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 23 Nov 2018 20:24:11 +0100 Subject: [PATCH] Alpha: fix gas consumption for manager operations --- src/bin_client/test/test_lib.inc.sh | 2 +- .../lib_client/client_proto_context.ml | 8 ++--- src/proto_alpha/lib_client/injection.ml | 14 ++++---- .../lib_client/operation_result.ml | 18 ++++++---- src/proto_alpha/lib_protocol/src/apply.ml | 9 ++--- .../lib_protocol/src/apply_results.ml | 33 +++++++++++-------- .../lib_protocol/src/apply_results.mli | 8 +++-- .../lib_protocol/src/michelson_v1_gas.ml | 2 ++ .../lib_protocol/src/michelson_v1_gas.mli | 2 ++ .../lib_protocol/test/helpers/op.ml | 6 ++-- 10 files changed, 61 insertions(+), 41 deletions(-) diff --git a/src/bin_client/test/test_lib.inc.sh b/src/bin_client/test/test_lib.inc.sh index 2ce5d4ba3..e4f75f35a 100755 --- a/src/bin_client/test/test_lib.inc.sh +++ b/src/bin_client/test/test_lib.inc.sh @@ -132,7 +132,7 @@ init_contract_from_file () { } bake () { - $client bake for bootstrap1 --max-priority 512 --minimal-timestamp --minimal-fees 0 --minimal-fees-per-byte 0 --minimal-fees-per-gas-unit 0 + $client bake for bootstrap1 --max-priority 512 --minimal-timestamp --minimal-fees 0 --minimal-picotez-per-byte 0 --minimal-picotez-per-gas-unit 0 } bake_after () { diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index 2768eb020..34d6c0aad 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -84,7 +84,7 @@ let reveal cctxt let contents = Single (Manager_operation { source ; fee ; counter ; - gas_limit = Z.zero ; storage_limit = Z.zero ; + gas_limit = Z.of_int 10_000 ; storage_limit = Z.zero ; operation = Reveal src_pk }) in Injection.inject_operation cctxt ~chain ~block ?confirmations ?dry_run @@ -129,7 +129,7 @@ let originate_account originate cctxt ~chain ~block ?confirmations ?dry_run - ?branch ~source ~gas_limit:Z.zero ~src_pk ~src_sk ~fee origination + ?branch ~source ~gas_limit:(Z.of_int 10_000) ~src_pk ~src_sk ~fee origination let delegate_contract cctxt ~chain ~block ?branch ?confirmations @@ -140,7 +140,7 @@ let delegate_contract cctxt Injection.inject_manager_operation cctxt ~chain ~block ?confirmations ?dry_run - ?branch ~source ~fee ~gas_limit:Z.zero ~storage_limit:Z.zero + ?branch ~source ~fee ~gas_limit:(Z.of_int 10_000) ~storage_limit:Z.zero ~src_pk ~src_sk operation >>=? fun res -> return res @@ -425,4 +425,4 @@ let display_receipt_for_operation return_unit | Some op -> cctxt#message "%a" pp_operation op >>= fun () -> - return_unit \ No newline at end of file + return_unit diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index be524811a..50c63f6dc 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -117,8 +117,8 @@ let estimated_gas_single match result with | Applied (Transaction_result { consumed_gas }) -> Ok consumed_gas | Applied (Origination_result { consumed_gas }) -> Ok consumed_gas - | Applied Reveal_result -> Ok Z.zero - | Applied Delegation_result -> Ok Z.zero + | Applied (Reveal_result { consumed_gas }) -> Ok consumed_gas + | Applied (Delegation_result { consumed_gas }) -> Ok consumed_gas | Skipped _ -> assert false | Backtracked (_, None) -> Ok Z.zero (* there must be another error for this to happen *) | Backtracked (_, Some errs) -> Alpha_environment.wrap_error (Error errs) @@ -154,8 +154,8 @@ let estimated_storage_single Ok paid_storage_size_diff | Applied (Origination_result { paid_storage_size_diff }) -> Ok (Z.add paid_storage_size_diff origination_size) - | Applied Reveal_result -> Ok Z.zero - | Applied Delegation_result -> Ok Z.zero + | Applied (Reveal_result _)-> Ok Z.zero + | Applied (Delegation_result _) -> Ok Z.zero | Skipped _ -> assert false | Backtracked (_, None) -> Ok Z.zero (* there must be another error for this to happen *) | Backtracked (_, Some errs) -> Alpha_environment.wrap_error (Error errs) @@ -188,8 +188,8 @@ let originated_contracts_single match result with | Applied (Transaction_result { originated_contracts }) -> Ok originated_contracts | Applied (Origination_result { originated_contracts }) -> Ok originated_contracts - | Applied Reveal_result -> Ok [] - | Applied Delegation_result -> Ok [] + | Applied (Reveal_result _) -> Ok [] + | Applied (Delegation_result _) -> Ok [] | Skipped _ -> assert false | Backtracked (_, None) -> Ok [] (* there must be another error for this to happen *) | Backtracked (_, Some errs) -> Alpha_environment.wrap_error (Error errs) @@ -478,7 +478,7 @@ let inject_manager_operation let contents = Cons (Manager_operation { source ; fee = Tez.zero ; counter ; - gas_limit = Z.zero ; storage_limit = Z.zero ; + gas_limit = Z.of_int 10_000 ; storage_limit = Z.zero ; operation = Reveal src_pk }, Single (Manager_operation { source ; fee ; counter = Z.succ counter ; gas_limit ; storage_limit ; operation })) in diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 429645702..29b733106 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -235,17 +235,23 @@ let pp_manager_operation_contents_and_result ppf | Failed (_, _errs) -> Format.fprintf ppf "This operation FAILED." - | Applied Reveal_result -> + | Applied (Reveal_result { consumed_gas }) -> Format.fprintf ppf - "This revelation was successfully applied" - | Backtracked (Reveal_result, _) -> + "This revelation was successfully applied" ; + Format.fprintf ppf + "@,Consumed gas: %s" + (Z.to_string consumed_gas) + | Backtracked (Reveal_result _, _) -> Format.fprintf ppf "@[This revelation was BACKTRACKED, \ its expected effects were NOT applied.@]" ; - | Applied Delegation_result -> + | Applied (Delegation_result { consumed_gas })-> Format.fprintf ppf - "This delegation was successfully applied" - | Backtracked (Delegation_result, _) -> + "This delegation was successfully applied" ; + Format.fprintf ppf + "@,Consumed gas: %s" + (Z.to_string consumed_gas) + | Backtracked (Delegation_result _, _) -> Format.fprintf ppf "@[This delegation was BACKTRACKED, \ its expected effects were NOT applied.@]" ; diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 307025d77..1dc701d3a 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -367,10 +367,11 @@ let apply_manager_operation_content : let set_delegate = (* Ignore the delegatable flag for smart contracts. *) if internal then Delegate.set_from_script else Delegate.set in + Lwt.return (Gas.consume ctxt Michelson_v1_gas.Cost_of.manager_operation) >>=? fun ctxt -> match operation with | Reveal _ -> return (* No-op: action already performed by `precheck_manager_contents`. *) - (ctxt, (Reveal_result : kind successful_manager_operation_result), []) + (ctxt, (Reveal_result { consumed_gas = Gas.consumed ~since:before_operation ~until:ctxt } : kind successful_manager_operation_result), []) | Transaction { amount ; parameters ; destination } -> begin spend ctxt source amount >>=? fun ctxt -> begin match Contract.is_implicit destination with @@ -501,7 +502,7 @@ let apply_manager_operation_content : return (ctxt, result, []) | Delegation delegate -> set_delegate ctxt source delegate >>=? fun ctxt -> - return (ctxt, Delegation_result, []) + return (ctxt, Delegation_result { consumed_gas = Gas.consumed ~since:before_operation ~until:ctxt }, []) let apply_internal_manager_operations ctxt mode ~payer ops = let rec apply ctxt applied worklist = @@ -612,7 +613,7 @@ let skipped_operation_result = function operation -> match operation with | Reveal _ -> - Applied ( Reveal_result : kind successful_manager_operation_result ) + Applied ( Reveal_result { consumed_gas = Z.zero } : kind successful_manager_operation_result ) | _ -> Skipped (manager_kind operation) let rec mark_skipped @@ -727,7 +728,7 @@ let mark_backtracked results = : type kind. kind manager_operation_result -> kind manager_operation_result = function | Failed _ | Skipped _ | Backtracked _ as result -> result - | Applied Reveal_result as result -> result + | Applied (Reveal_result _) as result -> result | Applied result -> Backtracked (result, None) in mark_contents_list results diff --git a/src/proto_alpha/lib_protocol/src/apply_results.ml b/src/proto_alpha/lib_protocol/src/apply_results.ml index 3961608ab..0ef56ef6e 100644 --- a/src/proto_alpha/lib_protocol/src/apply_results.ml +++ b/src/proto_alpha/lib_protocol/src/apply_results.ml @@ -42,7 +42,9 @@ let error_encoding = ~binary:Error_monad.error_encoding type _ successful_manager_operation_result = - | Reveal_result : Kind.reveal successful_manager_operation_result + | Reveal_result : + { consumed_gas : Z.t + } -> Kind.reveal successful_manager_operation_result | Transaction_result : { storage : Script.expr option ; big_map_diff : Contract.big_map_diff option ; @@ -60,7 +62,9 @@ type _ successful_manager_operation_result = storage_size : Z.t ; paid_storage_size_diff : Z.t ; } -> Kind.origination successful_manager_operation_result - | Delegation_result : Kind.delegation successful_manager_operation_result + | Delegation_result : + { consumed_gas : Z.t + } -> Kind.delegation successful_manager_operation_result type packed_successful_manager_operation_result = | Successful_manager_result : @@ -146,7 +150,8 @@ module Manager_result = struct let reveal_case = make ~op_case: Operation.Encoding.Manager_operations.reveal_case - ~encoding: Data_encoding.empty + ~encoding: Data_encoding.(obj1 (dft "consumed_gas" z Z.zero)) + ~iselect: (function | Internal_operation_result @@ -155,11 +160,11 @@ module Manager_result = struct | _ -> None) ~select: (function - | Successful_manager_result (Reveal_result as op) -> Some op + | Successful_manager_result (Reveal_result _ as op) -> Some op | _ -> None) ~kind: Kind.Reveal_manager_kind - ~proj: (function Reveal_result -> ()) - ~inj: (fun () -> Reveal_result) + ~proj: (function Reveal_result { consumed_gas } -> consumed_gas) + ~inj: (fun consumed_gas -> Reveal_result { consumed_gas }) let transaction_case = make @@ -248,7 +253,7 @@ module Manager_result = struct let delegation_case = make ~op_case: Operation.Encoding.Manager_operations.delegation_case - ~encoding: Data_encoding.empty + ~encoding: Data_encoding.(obj1 (dft "consumed_gas" z Z.zero)) ~iselect: (function | Internal_operation_result @@ -257,11 +262,11 @@ module Manager_result = struct | _ -> None) ~select: (function - | Successful_manager_result (Delegation_result as op) -> Some op + | Successful_manager_result (Delegation_result _ as op) -> Some op | _ -> None) ~kind: Kind.Delegation_manager_kind - ~proj: (function Delegation_result -> ()) - ~inj: (fun () -> Delegation_result) + ~proj: (function Delegation_result { consumed_gas } -> consumed_gas) + ~inj: (fun consumed_gas -> Delegation_result { consumed_gas }) end @@ -770,11 +775,11 @@ let kind_equal | Manager_operation { operation = Reveal _ ; _ }, Manager_operation_result - { operation_result = Applied Reveal_result ; _ } -> Some Eq + { operation_result = Applied (Reveal_result _); _ } -> Some Eq | Manager_operation { operation = Reveal _ ; _ }, Manager_operation_result - { operation_result = Backtracked (Reveal_result, _) ; _ } -> Some Eq + { operation_result = Backtracked (Reveal_result _, _) ; _ } -> Some Eq | Manager_operation { operation = Reveal _ ; _ }, Manager_operation_result @@ -827,11 +832,11 @@ let kind_equal | Manager_operation { operation = Delegation _ ; _ }, Manager_operation_result - { operation_result = Applied Delegation_result ; _ } -> Some Eq + { operation_result = Applied (Delegation_result _) ; _ } -> Some Eq | Manager_operation { operation = Delegation _ ; _ }, Manager_operation_result - { operation_result = Backtracked (Delegation_result, _) ; _ } -> Some Eq + { operation_result = Backtracked (Delegation_result _, _) ; _ } -> Some Eq | Manager_operation { operation = Delegation _ ; _ }, Manager_operation_result diff --git a/src/proto_alpha/lib_protocol/src/apply_results.mli b/src/proto_alpha/lib_protocol/src/apply_results.mli index fbe5186b9..b4505f502 100644 --- a/src/proto_alpha/lib_protocol/src/apply_results.mli +++ b/src/proto_alpha/lib_protocol/src/apply_results.mli @@ -86,7 +86,9 @@ and 'kind manager_operation_result = (** Result of applying a {!manager_operation_content}, either internal or external. *) and _ successful_manager_operation_result = - | Reveal_result : Kind.reveal successful_manager_operation_result + | Reveal_result : + { consumed_gas : Z.t + } -> Kind.reveal successful_manager_operation_result | Transaction_result : { storage : Script.expr option ; big_map_diff : Contract.big_map_diff option ; @@ -104,7 +106,9 @@ and _ successful_manager_operation_result = storage_size : Z.t ; paid_storage_size_diff : Z.t ; } -> Kind.origination successful_manager_operation_result - | Delegation_result : Kind.delegation successful_manager_operation_result + | Delegation_result : + { consumed_gas : Z.t + } -> Kind.delegation successful_manager_operation_result and packed_successful_manager_operation_result = | Successful_manager_result : diff --git a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml index b6c1da42d..0e7e45617 100644 --- a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml @@ -235,6 +235,8 @@ module Cost_of = struct let compare_timestamp t1 t2 = compare_zint (Script_timestamp.to_zint t1) (Script_timestamp.to_zint t2) let compare_address _ _ = step_cost 20 + let manager_operation = step_cost 10_000 + module Typechecking = struct let cycle = step_cost 1 let bool = free diff --git a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli index 89c747220..cfb121cf9 100644 --- a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli +++ b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli @@ -111,6 +111,8 @@ module Cost_of : sig val compare_timestamp : Script_timestamp.t -> Script_timestamp.t -> Gas.cost val compare_address : Contract.t -> Contract.t -> Gas.cost + val manager_operation : Gas.cost + module Typechecking : sig val cycle : Gas.cost val unit : Gas.cost diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index 1c58dc6e6..fc93802dc 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -98,7 +98,7 @@ let combine_operations fee = Tez.zero ; counter ; operation = Reveal public_key ; - gas_limit = Z.of_int 20 ; + gas_limit = Z.of_int 10000 ; storage_limit = Z.zero ; } in return (Some (Contents reveal_op), Z.succ counter) @@ -153,7 +153,7 @@ let manager_operation fee = Tez.zero ; counter ; operation = Reveal public_key ; - gas_limit = Z.of_int 20 ; + gas_limit = Z.of_int 10000 ; storage_limit = Z.zero ; } in let op = @@ -181,7 +181,7 @@ let revelation ctxt public_key = fee = Tez.zero ; counter ; operation = Reveal public_key ; - gas_limit = Z.of_int 20 ; + gas_limit = Z.of_int 10000 ; storage_limit = Z.zero ; })) in return @@ sign account.sk ctxt sop