Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file gas_limit_repr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)typet=|Unaccounted|Limitedof{remaining:Z.t}typeinternal_gas=Z.ttypecost={allocations:Z.t;steps:Z.t;reads:Z.t;writes:Z.t;bytes_read:Z.t;bytes_written:Z.t}letencoding=letopenData_encodinginunion[case(Tag0)~title:"Limited"z(functionLimited{remaining}->Someremaining|_->None)(funremaining->Limited{remaining});case(Tag1)~title:"Unaccounted"(constant"unaccounted")(functionUnaccounted->Some()|_->None)(fun()->Unaccounted)]letppppf=function|Unaccounted->Format.fprintfppf"unaccounted"|Limited{remaining}->Format.fprintfppf"%s units remaining"(Z.to_stringremaining)letcost_encoding=letopenData_encodinginconv(fun{allocations;steps;reads;writes;bytes_read;bytes_written}->(allocations,steps,reads,writes,bytes_read,bytes_written))(fun(allocations,steps,reads,writes,bytes_read,bytes_written)->{allocations;steps;reads;writes;bytes_read;bytes_written})(obj6(req"allocations"z)(req"steps"z)(req"reads"z)(req"writes"z)(req"bytes_read"z)(req"bytes_written"z))letpp_costppf{allocations;steps;reads;writes;bytes_read;bytes_written}=Format.fprintfppf"(steps: %s, allocs: %s, reads: %s (%s bytes), writes: %s (%s bytes))"(Z.to_stringsteps)(Z.to_stringallocations)(Z.to_stringreads)(Z.to_stringbytes_read)(Z.to_stringwrites)(Z.to_stringbytes_written)typeerror+=Block_quota_exceeded(* `Temporary *)typeerror+=Operation_quota_exceeded(* `Temporary *)letallocation_weight=Z.of_int2letstep_weight=Z.of_int1letread_base_weight=Z.of_int100letwrite_base_weight=Z.of_int160letbyte_read_weight=Z.of_int10letbyte_written_weight=Z.of_int15letrescaling_bits=7letrescaling_mask=Z.sub(Z.shift_leftZ.onerescaling_bits)Z.oneletscale(z:Z.t)=Z.shift_leftzrescaling_bitsletrescale(z:Z.t)=Z.shift_rightzrescaling_bitsletcost_to_internal_gas(cost:cost):internal_gas=Z.add(Z.add(Z.mulcost.allocationsallocation_weight)(Z.mulcost.stepsstep_weight))(Z.add(Z.add(Z.mulcost.readsread_base_weight)(Z.mulcost.writeswrite_base_weight))(Z.add(Z.mulcost.bytes_readbyte_read_weight)(Z.mulcost.bytes_writtenbyte_written_weight)))letinternal_gas_to_gasinternal_gas:Z.t*internal_gas=letgas=rescaleinternal_gasinletrest=Z.logandinternal_gasrescaling_maskin(gas,rest)letconsumeblock_gasoperation_gasinternal_gascost=matchoperation_gaswith|Unaccounted->ok(block_gas,Unaccounted,internal_gas)|Limited{remaining}->letcost_internal_gas=cost_to_internal_gascostinlettotal_internal_gas=Z.addcost_internal_gasinternal_gasinletgas,rest=internal_gas_to_gastotal_internal_gasinifCompare.Z.(gas>Z.zero)thenletremaining=Z.subremaininggasinletblock_remaining=Z.subblock_gasgasinifCompare.Z.(remaining<Z.zero)thenerrorOperation_quota_exceededelseifCompare.Z.(block_remaining<Z.zero)thenerrorBlock_quota_exceededelseok(block_remaining,Limited{remaining},rest)elseok(block_gas,operation_gas,total_internal_gas)letcheck_enoughblock_gasoperation_gasinternal_gascost=consumeblock_gasoperation_gasinternal_gascost>|?fun(_block_remainig,_remaining,_internal_gas)->()letinternal_gas_zero:internal_gas=Z.zeroletalloc_costn={allocations=scale(Z.of_int(n+1));steps=Z.zero;reads=Z.zero;writes=Z.zero;bytes_read=Z.zero;bytes_written=Z.zero}letalloc_bytes_costn=alloc_cost((n+7)/8)letalloc_bits_costn=alloc_cost((n+63)/64)letatomic_step_costn={allocations=Z.zero;steps=Z.of_int(2*n);reads=Z.zero;writes=Z.zero;bytes_read=Z.zero;bytes_written=Z.zero}letstep_costn={allocations=Z.zero;steps=scale(Z.of_intn);reads=Z.zero;writes=Z.zero;bytes_read=Z.zero;bytes_written=Z.zero}letfree={allocations=Z.zero;steps=Z.zero;reads=Z.zero;writes=Z.zero;bytes_read=Z.zero;bytes_written=Z.zero}letread_bytes_costn={allocations=Z.zero;steps=Z.zero;reads=scaleZ.one;writes=Z.zero;bytes_read=scalen;bytes_written=Z.zero}letwrite_bytes_costn={allocations=Z.zero;steps=Z.zero;reads=Z.zero;writes=Z.one;bytes_read=Z.zero;bytes_written=scalen}let(+@)xy={allocations=Z.addx.allocationsy.allocations;steps=Z.addx.stepsy.steps;reads=Z.addx.readsy.reads;writes=Z.addx.writesy.writes;bytes_read=Z.addx.bytes_ready.bytes_read;bytes_written=Z.addx.bytes_writteny.bytes_written}let(*@)xy={allocations=Z.mul(Z.of_intx)y.allocations;steps=Z.mul(Z.of_intx)y.steps;reads=Z.mul(Z.of_intx)y.reads;writes=Z.mul(Z.of_intx)y.writes;bytes_read=Z.mul(Z.of_intx)y.bytes_read;bytes_written=Z.mul(Z.of_intx)y.bytes_written}letalloc_mbytes_costn=alloc_cost12+@alloc_bytes_costnlet()=letopenData_encodinginregister_error_kind`Temporary~id:"gas_exhausted.operation"~title:"Gas quota exceeded for the operation"~description:"A script or one of its callee took more \
time than the operation said it would"empty(functionOperation_quota_exceeded->Some()|_->None)(fun()->Operation_quota_exceeded);register_error_kind`Temporary~id:"gas_exhausted.block"~title:"Gas quota exceeded for the block"~description:"The sum of gas consumed by all the operations in the block \
exceeds the hard gas limit per block"empty(functionBlock_quota_exceeded->Some()|_->None)(fun()->Block_quota_exceeded);