Module Alpha_context.Gas

This module implements the gas subsystem of the context.

Gas reflects the computational cost of each operation to limit the cost of operations and, by extension, the cost of blocks.

There are two gas quotas: one for operation and one for block. For this reason, we maintain two gas levels -- one for operations and another one for blocks -- that correspond to the remaining amounts of gas, initialized with the quota limits and decreased each time gas is consumed.

type t = private
  1. | Unaccounted
  2. | Limited of {
    1. remaining : Arith.fp;
    }

For maintenance operations or for testing, gas can be Unaccounted. Otherwise, the computation is Limited by the remaining gas in the context.

val set_limit : context -> 'a Arith.t -> context

set_limit ctxt limit returns a context with a given limit level of gas allocated for an operation.

val set_unlimited : context -> context

set_unlimited allows unlimited gas consumption.

val remaining_operation_gas : context -> Arith.fp

remaining_operation_gas ctxt returns the current gas level in the context ctxt for the current operation. If gas is Unaccounted, an arbitrary value will be returned.

val reset_block_gas : context -> context

reset_block_gas ctxt returns a context where the remaining gas in the block is reset to the constant hard_gas_limit_per_block, i.e., as if no operations have been included in the block.

/!\ Do not call this function unless you want to validate operations on their own (like in the mempool).

val level : context -> t

level ctxt is the current gas level in ctxt for the current operation.

val update_remaining_operation_gas : context -> Arith.fp -> context

update_remaining_operation_gas ctxt remaining sets the current gas level for operations to remaining.

val consumed : since:context -> until:context -> Arith.fp

consumed since until is the operation gas level difference between context since and context until. This function returns Arith.zero if any of the two contexts allows for an unlimited gas consumption. This function also returns Arith.zero if since has less gas than until.

val block_level : context -> Arith.fp

block_level ctxt returns the block gas level in context ctxt.

Costs are computed using a saturating arithmetic. See Saturation_repr.

consume ctxt cost subtracts cost to the current operation gas level in ctxt. This operation may fail with Operation_quota_exceeded if the operation gas level would go below zero.

consume_from available_gas cost subtracts cost from available_gas and returns the remaining gas.

  • returns

    Error Operation_quota_exceeded if the remaining gas would fall below 0.

val check_gas_limit : hard_gas_limit_per_operation:Arith.integral -> gas_limit:Arith.integral -> unit Mavryk_protocol_environment_001_PtAtLas.Error_monad.tzresult

Check that gas_limit is a valid operation gas limit: at most hard_gas_limit_per_operation and nonnegative.

  • returns

    Error Gas_limit_too_high if gas_limit is greater than hard_gas_limit_per_operation or negative.

val free : cost

The cost of free operation is 0.

val cost_of_gas : 'a Arith.t -> cost

Convert a fixed-point amount of gas to a cost.

val fp_of_milligas_int : int -> Arith.fp

Convert an amount of milligas expressed as an int to Arith.fp.

val atomic_step_cost : _ Saturation_repr.t -> cost

atomic_step_cost x corresponds to x milliunit of gas.

val step_cost : _ Saturation_repr.t -> cost

step_cost x corresponds to x units of gas.

val alloc_cost : _ Saturation_repr.t -> cost

Cost of allocating qwords of storage. alloc_cost n estimates the cost of allocating n qwords of storage.

val alloc_bytes_cost : int -> cost

Cost of allocating bytes in the storage. alloc_bytes_cost b estimates the cost of allocating b bytes of storage.

val alloc_mbytes_cost : int -> cost

Cost of allocating bytes in the storage.

alloc_mbytes_cost b estimates the cost of allocating b bytes of storage and the cost of an header to describe these bytes.

val read_bytes_cost : int -> cost

Cost of reading the storage. read_bytes_cost n estimates the cost of reading n bytes of storage.

val write_bytes_cost : int -> cost

Cost of writing to storage. write_bytes_const n estimates the cost of writing n bytes to the storage.

val (*@) : _ Saturation_repr.t -> cost -> cost

Multiply a cost by a factor. Both arguments are saturated arithmetic values, so no negative numbers are involved.

val (+@) : cost -> cost -> cost

Add two costs together.

val cost_of_repr : Gas_limit_repr.cost -> cost

cost_of_repr is an internal operation needed to inject costs for Storage_costs into Gas.cost.