package tezos-protocol-alpha

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file michelson_v1_gas_costs.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-2022 Nomadic Labs <contact@nomadic-labs.com>           *)
(* Copyright (c) 2020 Metastate AG <hello@metastate.dev>                     *)
(* Copyright (c) 2022-2023 DaiLambda, Inc. <contact@dailambda.jp>            *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

include Michelson_v1_gas_costs_generated
module S = Saturation_repr

(** Hand-edited/written cost functions *)

(* Functions to be replaced by the generated code.

   The codegen cannot generate exactly the same code here. They have to
   be replaced by the generated versions.
*)

(* generated code is not usable: the const is not on a grid point *)
(* model N_ILsl_nat *)
(* Allocates at most [size + 256] bytes *)
let cost_N_ILsl_nat size =
  let open S.Syntax in
  let v0 = S.safe_int size in
  S.safe_int 128 + (v0 lsr 1)

(* generated code is not usable: the actual code and the model differ *)
(* model N_ILsl_bytes *)
(* Allocates [size + shift / 8] bytes *)
(* fun size1 -> fun size2 -> ((63.0681507316 + (0.667539714647 * size1)) + (0. * size2)) *)
let cost_N_ILsl_bytes size shift =
  let open S.Syntax in
  let v1 = S.safe_int size in
  let v0 = S.safe_int shift in
  S.safe_int 65 + (v1 lsr 1) + (v1 lsr 2) + (v0 lsr 4)

(* ------------------------------------------------------------------------ *)

(* N_ISapling_verify_update_with_blake2b
   This function depends on another cost function cost_N_IBlake2b.
   Such code can't be generated by the current Snoop. *)
let cost_N_ISapling_verify_update_with_blake2b size1 size2 bound_data =
  let open S.Syntax in
  cost_N_IBlake2b bound_data + cost_N_ISapling_verify_update size1 size2

(* N_IApply
   The current generated model receives int as a flag,
   but it should receive bool. *)
(* model N_IApply *)
(* fun size -> if (size = 0) then 140 else 220 *)
let cost_N_IApply rec_flag = if rec_flag then S.safe_int 220 else S.safe_int 140

(* N_KMap_enter_body
   Removed conversion of [size] for optimization *)
(* model N_KMap_enter_body *)
let cost_N_KMap_enter_body size =
  if Compare.Int.(size = 0) then S.safe_int 10 else S.safe_int 80

(* N_KList_enter_body
   The generated model receives the length of `xs` as the first argument
   and branches on whether it is 0 or not.
   However, calculating the length makes the performance worse.
   The model should be changed to receive `xs_is_nil` as the first argument. *)
(* model N_KList_enter_body *)
(* Approximating 1.797068 x term *)
let cost_N_KList_enter_body xs size_ys =
  match xs with
  | [] ->
      let open S.Syntax in
      let v0 = S.safe_int size_ys in
      S.safe_int 30 + (v0 + (v0 lsr 1) + (v0 lsr 2) + (v0 lsr 4))
  | _ :: _ -> S.safe_int 30

(* model PARSE_TYPE
   This is the cost of one iteration of parse_ty, extracted by hand from the
   parameter fit for the PARSE_TYPE benchmark. *)
let cost_PARSE_TYPE1 = cost_PARSE_TYPE 1

(* model TYPECHECKING_CODE
   This is the cost of one iteration of parse_instr, extracted by hand from the
   parameter fit for the TYPECHECKING_CODE benchmark. *)
let cost_TYPECHECKING_CODE = S.safe_int 220

(* model UNPARSING_CODE
   This is the cost of one iteration of unparse_instr, extracted by hand from the
   parameter fit for the UNPARSING_CODE benchmark. *)
let cost_UNPARSING_CODE = S.safe_int 115

(* model TYPECHECKING_DATA
   This is the cost of one iteration of parse_data, extracted by hand from the
   parameter fit for the TYPECHECKING_DATA benchmark. *)
let cost_TYPECHECKING_DATA = S.safe_int 100

(* model UNPARSING_DATA
   This is the cost of one iteration of unparse_data, extracted by hand from the
   parameter fit for the UNPARSING_DATA benchmark. *)
let cost_UNPARSING_DATA = S.safe_int 65

(* TODO: https://gitlab.com/tezos/tezos/-/issues/2264
   Benchmark.
   Currently approximated by 2 comparisons of the longest entrypoint. *)
let cost_FIND_ENTRYPOINT = cost_N_ICompare 31 31

(* ------------------------------------------------------------------------ *)

(* These functions lack the corresponding models. *)

(* model SAPLING_TRANSACTION_ENCODING *)
let cost_SAPLING_TRANSACTION_ENCODING ~inputs ~outputs ~bound_data =
  S.safe_int (1500 + (inputs * 160) + (outputs * 320) + (bound_data lsr 3))

(* model SAPLING_DIFF_ENCODING *)
let cost_SAPLING_DIFF_ENCODING ~nfs ~cms = S.safe_int ((nfs * 22) + (cms * 215))

(* ------------------------------------------------------------------------ *)

(* IDropN and IDupN use non affine models with multiple cases. The inferred
   cost functions are more complex than the following affine functions. *)

(* model N_IDropN *)
(* Approximating 2.713108 x term *)
let cost_N_IDropN size =
  let open S.Syntax in
  let v0 = S.safe_int size in
  S.safe_int 30 + (S.safe_int 2 * v0) + (v0 lsr 1) + (v0 lsr 3)

(* model N_IDupN *)
(* Approximating 1.222263 x term *)
let cost_N_IDupN size =
  let open S.Syntax in
  let v0 = S.safe_int size in
  S.safe_int 20 + v0 + (v0 lsr 2)

(* ------------------------------------------------------------------------ *)

(* Following functions are partially carbonated: they charge some gas
   by themselves.  Their inferred gas parameters cannot be directly
   used since they should contain the partial carbonation.
*)

(* model N_IContract *)
(* Inferred value: 703.26072741 *)
(* Most computation happens in [parse_contract_for_script], which is
   carbonated. *)
let cost_N_IContract = S.safe_int 30

(* model N_ICreate_contract *)
(* Inferred value: 814.154060743 *)
(* Most computation happens in [create_contract], which is carbonated. *)
let cost_N_ICreate_contract = S.safe_int 60

(* model N_ITransfer_tokens *)
(* Inferred value: 230.707394077 *)
(* Most computation happens in [transfer], which is carbonated. *)
let cost_N_ITransfer_tokens = S.safe_int 60

(* model IEmit *)
(* Inferred value: 244.687394077 *)
(* Most computation happens in [emit_event], which is carbonated. *)
let cost_N_IEmit = S.safe_int 30

(* --------------------------------------------------------------------- *)

(* The cost functions below where not benchmarked, a cost model was derived
    from looking at similar instructions. *)
(* Cost for Concat_string is paid in two steps: when entering the interpreter,
    the user pays for the cost of computing the information necessary to compute
    the actual gas (so it's meta-gas): indeed, one needs to run through the
    list of strings to compute the total allocated cost.
    [concat_string_precheck] corresponds to the meta-gas cost of this computation.
*)

let cost_N_IConcat_string_precheck length =
  (* we set the precheck to be slightly more expensive than cost_N_IList_iter *)
  let open S.Syntax in
  let length = S.safe_int length in
  length * S.safe_int 10

(* This is the cost of allocating a string and blitting existing ones into it. *)
let cost_N_IConcat_string total_bytes =
  let open S.Syntax in
  S.safe_int 100 + (total_bytes lsr 1)

(* Same story as Concat_string. *)
let cost_N_IConcat_bytes total_bytes =
  let open S.Syntax in
  S.safe_int 100 + (total_bytes lsr 1)

(* A partially carbonated instruction,
   so its model does not correspond to this function *)
(* Cost of Unpack pays two integer comparisons, and a Bytes slice *)
let cost_N_IUnpack total_bytes =
  let open S.Syntax in
  let total_bytes = S.safe_int total_bytes in
  S.safe_int 260 + (total_bytes lsr 1)
OCaml

Innovation. Community. Security.