123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* *)(* 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. *)(* *)(*****************************************************************************)moduleS=Saturation_reprmoduleS_syntax=structletlog2x=S.safe_int(1+S.numbitsx)let(+)=S.addlet(*)=S.mullet(lsr)=S.shift_rightendmoduleConstants=struct(* TODO: https://gitlab.com/tezos/tezos/-/issues/2648
Fill in real benchmarked values.
Need to create benchmark and fill in values.
*)letcost_add_message_base=S.safe_int430letcost_add_message_per_byte=S.safe_int15letcost_add_inbox_per_level=S.safe_int15letcost_update_num_and_size_of_messages=S.safe_int15(* equal to Michelson_v1_gas.Cost_of.Unparsing.contract_optimized *)letcost_decoding_contract_optimized=S.safe_int70(* equal to Michelson_v1_gas.Cost_of.Unparsing.key_hash_optimized *)letcost_decoding_key_hash_optimized=S.safe_int50(* Set to the cost of encoding a pkh defined in {!Michelson_v1_gas} divided
by the number of characters of a pkh, i.e. 70/35. To be updated when
benchmarking is completed. *)letcost_encode_string_per_byte=S.safe_int2(* Cost of serializing a state hash. *)letcost_serialize_state_hash=letlen=S.safe_intState_hash.sizeinS_syntax.(cost_encode_string_per_byte*len)(* Cost of serializing a commitment hash. *)letcost_serialize_commitment_hash=letlen=S.safe_intSc_rollup_commitment_repr.Hash.sizeinS_syntax.(cost_encode_string_per_byte*len)(* Cost of serializing a commitment. The cost of serializing the level and
number of ticks (both int32) is negligible. *)letcost_serialize_commitment=S_syntax.(cost_serialize_state_hash+cost_serialize_commitment_hash)(* Cost of serializing an operation hash. *)letcost_serialize_operation_hash=letlen=S.safe_intOperation_hash.sizeinS_syntax.(cost_encode_string_per_byte*len)(* Cost of serializing a nonce. The cost of serializing the index (an int32)
is negligible. *)letcost_serialize_nonce=cost_serialize_operation_hashend(* We assume that the gas cost of adding messages [[ m_1; ... ; m_n]] at level
[l] is linear in the sum of lengths of the messages, and it is logarithmic
in [l]. That is, [cost_add_serialized_messages([m_1; .. ; m_n], l)] =
`n * cost_add_message_base +
cost_add_message_per_bytes * \sum_{i=1}^n length(m_i) +
cost_add_inbox_per_level * l`.
*)letcost_add_serialized_messages~num_messages~total_messages_sizel=letopenS_syntaxinletlog_level=ifInt32.equallInt32.zerothenSaturation_repr.safe_int0elselog2@@S.safe_int(Int32.to_intl)inletlevel_cost=log_level*Constants.cost_add_inbox_per_levelin(S.safe_intnum_messages*Constants.cost_add_message_base)+level_cost+(Constants.cost_add_message_per_byte*S.safe_inttotal_messages_size)(* Reusing model from {!Ticket_costs.has_tickets_of_ty_cost}. *)letis_valid_parameters_ty_cost~ty_size=letfixed_cost=S.safe_int10inletcoeff=S.safe_int6inS.addfixed_cost(S.mulcoeffty_size)letcost_serialize_internal_inbox_messageSc_rollup_inbox_message_repr.{payload;sender=_;source=_}=letlexpr=Script_repr.lazy_exprpayloadinletexpr_cost=Script_repr.force_bytes_costlexprinS_syntax.(expr_cost+Constants.cost_decoding_contract_optimized+Constants.cost_decoding_key_hash_optimized)(** TODO: #3212
Confirm gas cost model.
We here assume that the cost of deserializing an expression of [bytes_len]
is proportional to deserializing a script expression of size [bytes_len].
This may not be the case and in particular, the cost depends on the specific
structure used for the PVM. We may thus need to split the cost function.
*)letcost_deserialize_output_proof~bytes_len=Script_repr.deserialization_cost_estimated_from_bytesbytes_lenletcost_serialize_external_inbox_message~bytes_len=letlen=S.safe_intbytes_leninS_syntax.(Constants.cost_encode_string_per_byte*len)(* Equal to Michelson_v1_gas.Cost_of.Interpreter.blake2b. *)letcost_hash_bytes~bytes_len=letopenS_syntaxinletv0=S.safe_intbytes_leninS.safe_int430+v0+(v0lsr3)