123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Marigold <contact@marigold.dev> *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Oxhead Alpha <info@oxhead-alpha.com> *)(* *)(* 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. *)(* *)(*****************************************************************************)typeerror+=Tx_rollup_negative_input_sizemoduleS=Saturation_repr(** The model in {!Michelson_v1_gas.N_IBlake2b}, plus the allocation
of bytes from {!Storage_functor}. *)lethash_costinput_size=error_unlessCompare.Int.(0<=input_size)Tx_rollup_negative_input_size>>?fun()->let(+)=S.addinletcost_serialization=Gas_limit_repr.alloc_mbytes_costinput_sizeinletv0=Saturation_repr.safe_intinput_sizeinletcost_N_IBlake2b=S.safe_int430+v0+S.shift_rightv03inletcost_blake2b=Gas_limit_repr.atomic_step_costcost_N_IBlake2binok@@(cost_serialization+cost_blake2b)(** Model from {!Ticket_costs.Constants.cost_compare_ticket_hash} since they are
Blake2B hashes too. *)letcompare_blake2b_hash=S.safe_int10letcheck_path_costelement_sizepath_depth=let(+)=S.addinerror_unlessCompare.Int.(0<=path_depth)Tx_rollup_negative_input_size>>?fun()->(* We hash the element *)hash_costelement_size>>?funelement_hash_cost->(* At each step of the way, we hash 2 hashes together *)hash_cost64>>?funhash_cost->letrecacc_hash_costacci=ifCompare.Int.(i<=0)thenaccelseacc_hash_cost(hash_cost+acc)(i-1)inok(element_hash_cost+acc_hash_costcompare_blake2b_hashpath_depth)letconsume_check_path_inbox_costctxt=letcount_limit=Constants_storage.tx_rollup_max_messages_per_inboxctxtinletmax_depth=Merkle_list.max_depth~count_limitincheck_path_costTx_rollup_prefixes.message_hash.hash_sizemax_depth>>?funcost->Raw_context.consume_gasctxtcostletconsume_check_path_commitment_costctxt=letcount_limit=Constants_storage.tx_rollup_max_messages_per_inboxctxtinletmax_depth=Merkle_list.max_depth~count_limitincheck_path_costTx_rollup_prefixes.message_result_hash.hash_sizemax_depth>>?funcost->Raw_context.consume_gasctxtcost(** As generated by the model [inbox_add_message_codegen] in
[lib_benchmarks_proto/tx_rollup_benchmarks.ml]. *)letmodel_inbox_add_message_codegeninbox_length=(* We assume that the Merkle_tree implementation computes a tree
whose depth is logarithmic in the number of leaves. The cost of
inserting an element in this structure, in the worst case, is the
cost of hashing the element and hashing the concatenation of two
elements at each level in the tree, that is:
cost_of_hashing(32 bytes) + log2(inbox_length) *
inbox_max_length * cost_of_hashing(32 bytes + 32 bytes)
This cost is captured by the following cost function which is
inferred emperically through the benchmark & model mentioned
above. *)letlog2n=S.safe_int(1+S.numbitsn)inS.mul(S.safe_int445)(log2(S.safe_intinbox_length))letconsume_add_message_costctxt=letmax_messages_per_inbox=Constants_storage.tx_rollup_max_messages_per_inboxctxtin(* as a safe, constant, over-approximation, suppose the inbox is
the maximum size allowed *)letcost=model_inbox_add_message_codegenmax_messages_per_inboxinRaw_context.consume_gasctxtcost(** As generated by the model [model_commitment_full_compact] in
[lib_benchmarks_proto/tx_rollup_benchmarks.ml]. We add one to
[inbox_length] so that the cost is never zero. *)letmodel_commitment_full_compactinbox_length=S.mul(S.safe_int915)(S.safe_int(1+inbox_length))letconsume_compact_commitment_costctxtinbox_length=letcost=model_commitment_full_compactinbox_lengthinRaw_context.consume_gasctxtcostlethash~hash_fctxtencodinginput=matchData_encoding.Binary.to_bytes_optencodinginputwith|Somebuffer->letlen=Bytes.lengthbufferinhash_costlen>>?funcost->Raw_context.consume_gasctxtcost>>?functxt->ok(ctxt,hash_f[buffer])|None->error(Tx_rollup_errors_repr.Internal_error"Cannot serialize input to hash function")let()=letopenData_encodingin(* Tx_rollup_negative_message_size *)register_error_kind`Permanent~id:"tx_rollup_negative_input_size"~title:"The protocol has computed a negative size for the input of a hash \
function"~description:"The protocol has computed a negative size for the input of a hash \
function. This is an internal error, and denotes a bug in the protocol \
implementation."unit(functionTx_rollup_negative_input_size->Some()|_->None)(fun()->Tx_rollup_negative_input_size)