From 8648555f019a7abea966fcd040b80bfb8d5b56ae Mon Sep 17 00:00:00 2001 From: Alain Mebsout Date: Thu, 28 Jun 2018 01:27:39 +0200 Subject: [PATCH] Alpha: raise permanent error on gas exhaustion in precheck --- .../lib_protocol/src/alpha_context.ml | 6 --- .../lib_protocol/src/alpha_context.mli | 3 -- src/proto_alpha/lib_protocol/src/apply.ml | 39 +++++++++++-------- .../lib_protocol/src/script_repr.ml | 32 +-------------- .../lib_protocol/src/script_repr.mli | 3 -- src/proto_alpha/lib_protocol/src/storage.ml | 18 +++------ 6 files changed, 29 insertions(+), 72 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.ml b/src/proto_alpha/lib_protocol/src/alpha_context.ml index 037c54e20..fd7aec34d 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/src/alpha_context.ml @@ -70,12 +70,6 @@ module Voting_period = Voting_period_repr module Gas = struct include Gas_limit_repr type error += Gas_limit_too_high = Raw_context.Gas_limit_too_high - type error += Not_enough_gas_minimal_deserialize_parameters = - Script_repr.Not_enough_gas_minimal_deserialize_parameters - type error += Not_enough_gas_minimal_deserialize_storage = - Script_repr.Not_enough_gas_minimal_deserialize_storage - type error += Not_enough_gas_minimal_serialize_storage = - Script_repr.Not_enough_gas_minimal_deserialize_storage let check_limit = Raw_context.check_gas_limit let set_limit = Raw_context.set_gas_limit let set_unlimited = Raw_context.set_gas_unlimited diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 13fd8aa16..017e73adf 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -124,9 +124,6 @@ module Gas : sig type error += Block_quota_exceeded (* `Temporary *) type error += Operation_quota_exceeded (* `Temporary *) type error += Gas_limit_too_high (* `Permanent *) - type error += Not_enough_gas_minimal_deserialize_parameters (* `Permanent *) - type error += Not_enough_gas_minimal_deserialize_storage (* `Temporary *) - type error += Not_enough_gas_minimal_serialize_storage (* `Temporary *) val free : cost val step_cost : int -> cost diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 046f6b4ea..4a7cd8b6a 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -41,6 +41,7 @@ type error += Outdated_double_baking_evidence of { level: Raw_level.t ; last: Raw_level.t } (* `Permanent *) type error += Invalid_activation of { pkh : Ed25519.Public_key_hash.t } type error += Multiple_revelation +type error += Gas_quota_exceeded_init_deserialize (* Permanent *) let () = register_error_kind @@ -318,7 +319,16 @@ let () = "Multiple revelations were included in a manager operation") Data_encoding.empty (function Multiple_revelation -> Some () | _ -> None) - (fun () -> Multiple_revelation) + (fun () -> Multiple_revelation) ; + register_error_kind + `Permanent + ~id:"gas_exhausted.init_deserialize" + ~title:"Not enough gas for initial deserialization of parameters" + ~description:"Gas quota was not enough to deserialize the transaction \ + parameters in precheck" + Data_encoding.empty + (function Gas_quota_exceeded_init_deserialize -> Some () | _ -> None) + (fun () -> Gas_quota_exceeded_init_deserialize) open Apply_results @@ -502,26 +512,23 @@ let precheck_manager_contents Contract.reveal_manager_key ctxt source pk | Transaction { parameters = Some arg ; _ } -> (* Fail if not enough gas for minimal deserialization cost *) - begin match Gas.consume ctxt (Script.minimal_deserialize_cost arg) with - | Ok _ -> return ctxt - | Error _ -> fail Gas.Not_enough_gas_minimal_deserialize_parameters - end >>=? fun ctxt -> + Lwt.return @@ record_trace Gas_quota_exceeded_init_deserialize @@ + Gas.consume ctxt (Script.minimal_deserialize_cost arg) >>=? fun _ -> Lwt.return @@ Script.force_decode arg >>=? fun (_arg, cost_arg) -> - Lwt.return @@ Gas.consume ctxt cost_arg + Lwt.return @@ record_trace Gas_quota_exceeded_init_deserialize @@ + Gas.consume ctxt cost_arg | Origination { script = Some script ; _ } -> (* Fail if not enough gas for minimal deserialization cost *) - begin - match - Gas.consume ctxt (Script.minimal_deserialize_cost script.code) >>? fun ctxt -> - Gas.consume ctxt (Script.minimal_deserialize_cost script.storage) - with - | Ok _ -> return ctxt - | Error _ -> fail Gas.Not_enough_gas_minimal_deserialize_parameters - end >>=? fun ctxt -> + Lwt.return @@ record_trace Gas_quota_exceeded_init_deserialize @@ + (Gas.consume ctxt (Script.minimal_deserialize_cost script.code) >>? fun ctxt -> + Gas.consume ctxt (Script.minimal_deserialize_cost script.storage)) + >>=? fun _ -> Lwt.return @@ Script.force_decode script.code >>=? fun (_code, cost_code) -> - Lwt.return @@ Gas.consume ctxt cost_code >>=? fun ctxt -> + Lwt.return @@ record_trace Gas_quota_exceeded_init_deserialize @@ + Gas.consume ctxt cost_code >>=? fun ctxt -> Lwt.return @@ Script.force_decode script.storage >>=? fun (_storage, cost_storage) -> - Lwt.return @@ Gas.consume ctxt cost_storage + Lwt.return @@ record_trace Gas_quota_exceeded_init_deserialize @@ + Gas.consume ctxt cost_storage | _ -> return ctxt end >>=? fun ctxt -> Contract.get_manager_key ctxt source >>=? fun public_key -> diff --git a/src/proto_alpha/lib_protocol/src/script_repr.ml b/src/proto_alpha/lib_protocol/src/script_repr.ml index 7d8d8b5ab..bdad89873 100644 --- a/src/proto_alpha/lib_protocol/src/script_repr.ml +++ b/src/proto_alpha/lib_protocol/src/script_repr.ml @@ -25,9 +25,6 @@ let expr_encoding = Michelson_v1_primitives.prim_encoding type error += Lazy_script_decode (* `Permanent *) -type error += Not_enough_gas_minimal_deserialize_parameters (* `Permanent *) -type error += Not_enough_gas_minimal_deserialize_storage (* `Temporary *) -type error += Not_enough_gas_minimal_serialize_storage (* `Temporary *) let () = register_error_kind `Permanent @@ -37,34 +34,7 @@ let () = from its binary representation" Data_encoding.empty (function Lazy_script_decode -> Some () | _ -> None) - (fun () -> Lazy_script_decode) ; - register_error_kind - `Permanent - ~id:"gas_exhausted.minimal_deserialization_parameters" - ~title:"Not enough gas for minimal deserialization of transaction parameters" - ~description:"Gas quota is not enough for deserializing the transaction \ - parameters, even for the cheapest case" - Data_encoding.empty - (function Not_enough_gas_minimal_deserialize_parameters -> Some () | _ -> None) - (fun () -> Not_enough_gas_minimal_deserialize_parameters) ; - register_error_kind - `Temporary - ~id:"gas_exhausted.minimal_deserialization_storage" - ~title:"Not enough gas for minimal deserialization of contract storage" - ~description:"Gas quota is not enough for deserializing the contract \ - storage or code, even for the cheapest case" - Data_encoding.empty - (function Not_enough_gas_minimal_deserialize_storage -> Some () | _ -> None) - (fun () -> Not_enough_gas_minimal_deserialize_storage) ; - register_error_kind - `Temporary - ~id:"gas_exhausted.minimal_serialization_storage" - ~title:"Not enough gas for minimal serialization of contract storage" - ~description:"Gas quota is not enough for serializing the contract \ - storage or code, even for the cheapest case" - Data_encoding.empty - (function Not_enough_gas_minimal_deserialize_storage -> Some () | _ -> None) - (fun () -> Not_enough_gas_minimal_deserialize_storage) + (fun () -> Lazy_script_decode) let lazy_expr_encoding = Data_encoding.lazy_encoding expr_encoding diff --git a/src/proto_alpha/lib_protocol/src/script_repr.mli b/src/proto_alpha/lib_protocol/src/script_repr.mli index b8ffb1798..8299c38ff 100644 --- a/src/proto_alpha/lib_protocol/src/script_repr.mli +++ b/src/proto_alpha/lib_protocol/src/script_repr.mli @@ -14,9 +14,6 @@ type annot = Micheline.annot type expr = Michelson_v1_primitives.prim Micheline.canonical type error += Lazy_script_decode (* `Permanent *) -type error += Not_enough_gas_minimal_deserialize_parameters (* `Permanent *) -type error += Not_enough_gas_minimal_deserialize_storage (* `Temporary *) -type error += Not_enough_gas_minimal_serialize_storage (* `Temporary *) type lazy_expr = expr Data_encoding.lazy_t diff --git a/src/proto_alpha/lib_protocol/src/storage.ml b/src/proto_alpha/lib_protocol/src/storage.ml index e31506cb1..f425e4e01 100644 --- a/src/proto_alpha/lib_protocol/src/storage.ml +++ b/src/proto_alpha/lib_protocol/src/storage.ml @@ -152,24 +152,16 @@ module Contract = struct end) let consume_deserialize_gas ctxt value = - begin match Raw_context.consume_gas ctxt (Script_repr.minimal_deserialize_cost value) with - | Ok _ -> return ctxt - | Error _ -> - fail Script_repr.Not_enough_gas_minimal_deserialize_storage - end >>=? fun ctxt -> Lwt.return @@ - (Script_repr.force_decode value >>? fun (_value, value_cost) -> + (Raw_context.consume_gas ctxt (Script_repr.minimal_deserialize_cost value) >>? fun _ -> + Script_repr.force_decode value >>? fun (_value, value_cost) -> Raw_context.consume_gas ctxt value_cost) let consume_serialize_gas ctxt value = - begin match Raw_context.consume_gas ctxt (Script_repr.minimal_serialize_cost value) with - | Ok _ -> return ctxt - | Error _ -> - fail Script_repr.Not_enough_gas_minimal_serialize_storage - end >>=? fun ctxt -> Lwt.return @@ - (Script_repr.force_bytes value >>? fun (_value, value_cost) -> - Raw_context.consume_gas ctxt value_cost) + ( Raw_context.consume_gas ctxt (Script_repr.minimal_serialize_cost value) >>? fun _ -> + Script_repr.force_bytes value >>? fun (_value, value_cost) -> + Raw_context.consume_gas ctxt value_cost) let get ctxt contract = get ctxt contract >>=? fun (ctxt, value) ->