From ffb3890f80a7b61a2a04aae4836bf60f07262056 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Fri, 29 Jun 2018 15:14:38 +0200 Subject: [PATCH] Alpha: fix gas accounting in lazy decoding --- .../lib_protocol/src/script_repr.ml | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/script_repr.ml b/src/proto_alpha/lib_protocol/src/script_repr.ml index a4a5d29fb..899d9cf80 100644 --- a/src/proto_alpha/lib_protocol/src/script_repr.ml +++ b/src/proto_alpha/lib_protocol/src/script_repr.ml @@ -138,28 +138,34 @@ let serialized_cost bytes = alloc_mbytes_cost (MBytes.length bytes) let force_decode lexpr = + let account_deserialization_cost = + Data_encoding.apply_lazy + ~fun_value:(fun _ -> false) + ~fun_bytes:(fun _ -> true) + ~fun_combine:(fun _ _ -> false) + lexpr in match Data_encoding.force_decode lexpr with | Some v -> - let deserialize_cost = - Data_encoding.apply_lazy - ~fun_value:(fun _ -> Gas_limit_repr.free) - ~fun_bytes:(fun _ -> deserialized_cost v) - ~fun_combine:(fun c_free _ -> c_free) - lexpr in - ok (v, deserialize_cost) + if account_deserialization_cost then + ok (v, deserialized_cost v) + else + ok (v, Gas_limit_repr.free) | None -> error Lazy_script_decode let force_bytes expr = let open Gas_limit_repr in + let account_serialization_cost = + Data_encoding.apply_lazy + ~fun_value:(fun v -> Some v) + ~fun_bytes:(fun _ -> None) + ~fun_combine:(fun _ _ -> None) + expr in match Data_encoding.force_bytes expr with | bytes -> - let serialize_cost = - Data_encoding.apply_lazy - ~fun_value:(fun v -> traversal_cost v +@ serialized_cost bytes) - ~fun_bytes:(fun _ -> Gas_limit_repr.free) - ~fun_combine:(fun _ c_free -> c_free) - expr in - ok (bytes, serialize_cost) + begin match account_serialization_cost with + | Some v -> ok (bytes, traversal_cost v +@ serialized_cost bytes) + | None -> ok (bytes, Gas_limit_repr.free) + end | exception _ -> error Lazy_script_decode let minimal_deserialize_cost lexpr =