123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021-2022 Nomadic Labs <contact@nomadic-labs.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. *)(* *)(*****************************************************************************)typet=inttypemicheline_size={traversal:t;int_bytes:t;string_bytes:t}(* ------------------------------------------------------------------------- *)(* encoding *)letencoding:tData_encoding.encoding=letopenData_encodinginconv(funi->Int64.of_inti)(funl->Int64.to_intl)int64letmicheline_size_encoding:micheline_sizeData_encoding.encoding=letopenData_encodinginconv(fun{traversal;int_bytes;string_bytes}->(traversal,int_bytes,string_bytes))(fun(traversal,int_bytes,string_bytes)->{traversal;int_bytes;string_bytes})(tup3encodingencodingencoding)(* ------------------------------------------------------------------------- *)letzero=0letadd=(+)letpp=Format.pp_print_intletpp_micheline_sizefmtr{traversal;int_bytes;string_bytes}=Format.fprintffmtr"@[{ traversal = %a;@; int_bytes = %a;@; string_bytes = %a;@,}@]"pptraversalppint_bytesppstring_bytesletto_intx=xletof_intx=xletunit:t=1letinteger(i:'aScript_int.num):t=Z.numbits(Script_int.to_zinti)/8letstring=String.lengthletscript_string=Script_string.lengthletbytes(b:Bytes.t):t=Bytes.lengthbletmutez(_tez:Alpha_context.Tez.tez):t=(* Up to now, mutez are stored on 8 bytes (int64). *)8letbool(_:bool):t=1letsignature(signature:Script_typed_ir.Script_signature.t):t=Script_typed_ir.Script_signature.sizesignatureletkey_hash(_keyhash:Signature.public_key_hash):t=Signature.Public_key_hash.sizeletpublic_key(public_key:Signature.public_key):t=Signature.Public_key.sizepublic_keyletchain_id(_chain_id:Script_typed_ir.Script_chain_id.t):t=Script_typed_ir.Script_chain_id.sizeletaddress(addr:Script_typed_ir.address):t=letentrypoint=addr.entrypointinSignature.Public_key_hash.size+String.length(Alpha_context.Entrypoint.to_stringentrypoint)lettx_rollup_l2_addressx=Tx_rollup_l2_address.Indexable.size@@Indexable.forgetxlettimestamp(tstamp:Script_timestamp.t):t=Z.numbits(Script_timestamp.to_zinttstamp)/8letrecsize_of_comparable_value:typea.aScript_typed_ir.comparable_ty->a->t=fun(typea)(wit:aScript_typed_ir.comparable_ty)(v:a)->matchwitwith|Never_t->(matchvwith_->.)|Unit_t->unit|Int_t->integerv|Nat_t->integerv|String_t->script_stringv|Bytes_t->bytesv|Mutez_t->mutezv|Bool_t->boolv|Key_hash_t->key_hashv|Timestamp_t->timestampv|Address_t->addressv|Tx_rollup_l2_address_t->tx_rollup_l2_addressv|Pair_t(leaf,node,_,YesYes)->letlv,rv=vinletsize=size_of_comparable_valueleaflv+size_of_comparable_valuenodervinsize+1|Union_t(left,right,_,YesYes)->letsize=matchvwith|Lv->size_of_comparable_valueleftv|Rv->size_of_comparable_valuerightvinsize+1|Option_t(ty,_,Yes)->(matchvwithNone->1|Somex->size_of_comparable_valuetyx+1)|Signature_t->signaturev|Key_t->public_keyv|Chain_id_t->chain_idv