123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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+=Cannot_pay_storage_fee(* `Temporary *)typeerror+=Operation_quota_exceeded(* `Temporary *)typeerror+=Storage_limit_too_high(* `Permanent *)let()=letopenData_encodinginregister_error_kind`Temporary~id:"contract.cannot_pay_storage_fee"~title:"Cannot pay storage fee"~description:"The storage fee is higher than the contract balance"~pp:(funppf()->Format.fprintfppf"Cannot pay storage storage fee")Data_encoding.empty(functionCannot_pay_storage_fee->Some()|_->None)(fun()->Cannot_pay_storage_fee);register_error_kind`Temporary~id:"storage_exhausted.operation"~title:"Storage quota exceeded for the operation"~description:"A script or one of its callee wrote more bytes than the operation said \
it would"Data_encoding.empty(functionOperation_quota_exceeded->Some()|_->None)(fun()->Operation_quota_exceeded);register_error_kind`Permanent~id:"storage_limit_too_high"~title:"Storage limit out of protocol hard bounds"~description:"A transaction tried to exceed the hard limit on storage"empty(functionStorage_limit_too_high->Some()|_->None)(fun()->Storage_limit_too_high)letorigination_burnc=letorigination_size=Constants_storage.origination_sizecinletcost_per_byte=Constants_storage.cost_per_bytecin(* the origination burn, measured in bytes *)Tez_repr.(cost_per_byte*?Int64.of_intorigination_size)>|?funto_be_paid->(Raw_context.update_allocated_contracts_countc,to_be_paid)letstart_counting_storage_feesc=Raw_context.init_storage_space_to_payc(* TODO: https://gitlab.com/tezos/tezos/-/issues/1615
Refactor other functions in module to use this one.
This function was added when adding the table
of globals feature. In principle other parts of this module
could be refactored to use this function. *)letcost_of_bytescn=letcost_per_byte=Constants_storage.cost_per_bytecinTez_repr.(cost_per_byte*?Z.to_int64n)letrecord_paid_storage_spaceccontract=Contract_storage.used_storage_spaceccontract>>=?funsize->Contract_storage.set_paid_storage_space_and_return_fees_to_payccontractsize>>=?fun(to_be_paid,c)->letc=Raw_context.update_storage_space_to_paycto_be_paidinletcost_per_byte=Constants_storage.cost_per_bytecinLwt.return(Tez_repr.(cost_per_byte*?Z.to_int64to_be_paid)>|?funto_burn->(c,size,to_be_paid,to_burn))letrecord_global_constant_storage_spacecontextsize=(* Following the precedent of big_map, a key in the
global table of constants costs 65 bytes (see
[Lazy_storage_diff.Big_map.bytes_size_for_big_map_key])*)letcost_of_key=Z.of_int65inletto_be_paid=Z.addsizecost_of_keyin(Raw_context.update_storage_space_to_paycontextto_be_paid,to_be_paid)letrecord_paid_storage_space_subsidyccontract=letc=start_counting_storage_feescinrecord_paid_storage_spaceccontract>>=?fun(c,size,to_be_paid,_)->let(c,_,_)=Raw_context.clear_storage_space_to_paycinreturn(c,size,to_be_paid)letburn_storage_feesc~storage_limit~payer=letorigination_size=Constants_storage.origination_sizecinlet(c,storage_space_to_pay,allocated_contracts)=Raw_context.clear_storage_space_to_paycinletstorage_space_for_allocated_contracts=Z.mul(Z.of_intallocated_contracts)(Z.of_intorigination_size)inletconsumed=Z.addstorage_space_to_paystorage_space_for_allocated_contractsinletremaining=Z.substorage_limitconsumedinifCompare.Z.(remaining<Z.zero)thenfailOperation_quota_exceededelseletcost_per_byte=Constants_storage.cost_per_bytecinTez_repr.(cost_per_byte*?Z.to_int64consumed)>>?=funto_burn->(* Burning the fees... *)ifTez_repr.(to_burn=Tez_repr.zero)then(* If the payer was was deleted by transferring all its balance, and no space was used,
burning zero would fail *)returncelsetraceCannot_pay_storage_fee(Contract_storage.must_existcpayer>>=?fun()->Contract_storage.spendcpayerto_burn)letcheck_storage_limitc~storage_limit=ifCompare.Z.(storage_limit>(Raw_context.constantsc).hard_storage_limit_per_operation)||Compare.Z.(storage_limit<Z.zero)thenerrorStorage_limit_too_highelseok_unit