Proto: Use saturated arithmetic to represent gas
This MR replaces metastatedev/tezos!368 (closed).
Context
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
Related: #1013 (closed)
Depends on: !2328 (merged)
Fixes: https://gitlab.com/nomadic-labs/tezos/-/issues/289
Manually testing the MR
dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "saturation arithmetic"
Checklist
-
Document the interface of any function added or modified (see the coding guidelines) -
Provide automatic testing (see the testing guide). [ ] Add item in theDevelopment Version
section ofCHANGES.md
(only for new features and bug fixes).