Skip to content

protocol: Use saturated arithmetic to represent gas

Since the gas limit per operation is 1040000000 mgas, the gas_counter can be represented using an OCaml int (even on 32-bit architectures since 2 ^ 30 = 1073741824 > 1040000000). Given the recent decision to only support 64-bit architectures, this saturated arithmetic maintains integers between 0 and 2^62 - 1, which seems largely sufficient for future evolutions.

The cost model can produce values outside of this range but since they are ultimately subtracted to gas_counter, we can simply use saturated arithmetic over int instead of the arbitrary precision arithmetic from ZArith.t to correctly detect gas exhaustion.

This optimization is part of a sequence to optimize gas update and check for gas exhaustion documented here: https://hackmd.io/@yrg/rkLwB17wD

Edited by Yann Regis-Gianas

Merge request reports