Alpha: fix gas accounting in lazy decoding

This commit is contained in:
Benjamin Canou 2018-06-29 15:14:38 +02:00
parent 8db9fb328f
commit ffb3890f80

View File

@ -138,28 +138,34 @@ let serialized_cost bytes =
alloc_mbytes_cost (MBytes.length bytes) alloc_mbytes_cost (MBytes.length bytes)
let force_decode lexpr = 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 match Data_encoding.force_decode lexpr with
| Some v -> | Some v ->
let deserialize_cost = if account_deserialization_cost then
Data_encoding.apply_lazy ok (v, deserialized_cost v)
~fun_value:(fun _ -> Gas_limit_repr.free) else
~fun_bytes:(fun _ -> deserialized_cost v) ok (v, Gas_limit_repr.free)
~fun_combine:(fun c_free _ -> c_free)
lexpr in
ok (v, deserialize_cost)
| None -> error Lazy_script_decode | None -> error Lazy_script_decode
let force_bytes expr = let force_bytes expr =
let open Gas_limit_repr in 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 match Data_encoding.force_bytes expr with
| bytes -> | bytes ->
let serialize_cost = begin match account_serialization_cost with
Data_encoding.apply_lazy | Some v -> ok (bytes, traversal_cost v +@ serialized_cost bytes)
~fun_value:(fun v -> traversal_cost v +@ serialized_cost bytes) | None -> ok (bytes, Gas_limit_repr.free)
~fun_bytes:(fun _ -> Gas_limit_repr.free) end
~fun_combine:(fun _ c_free -> c_free)
expr in
ok (bytes, serialize_cost)
| exception _ -> error Lazy_script_decode | exception _ -> error Lazy_script_decode
let minimal_deserialize_cost lexpr = let minimal_deserialize_cost lexpr =