Source file global_constants_storage.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
open Micheline
open Michelson_v1_primitives
let bottom_up_fold_cps initial_accumulator node initial_k f =
let rec traverse_node accu node k =
f accu node @@ fun accu node ->
match node with
| String _ | Int _ | Bytes _ -> k accu node
| Prim (loc, prim, args, annot) ->
(traverse_nodes [@ocaml.tailcall]) accu args @@ fun accu args ->
f accu (Prim (loc, prim, args, annot)) k
| Seq (loc, elts) ->
(traverse_nodes [@ocaml.tailcall]) accu elts @@ fun accu elts ->
f accu (Seq (loc, elts)) k
and traverse_nodes accu nodes k =
match nodes with
| [] -> k accu []
| node :: nodes ->
(traverse_node [@ocaml.tailcall]) accu node @@ fun accu node ->
(traverse_nodes [@ocaml.tailcall]) accu nodes @@ fun accu nodes ->
k accu (node :: nodes)
in
traverse_node initial_accumulator node initial_k
[@@coq_axiom_with_reason "local mutually recursive definition not handled"]
module Gas_costs = Global_constants_costs
module Expr_hash_map = Map.Make (Script_expr_hash)
type error += Expression_too_deep
type error += Expression_already_registered
type error += Badly_formed_constant_expression
type error += Nonexistent_global
type error += Expression_too_large
let () =
let description =
"Attempted to register an expression that, after fully expanding all \
referenced global constants, would result in too many levels of nesting."
in
register_error_kind
`Branch
~id:"Expression_too_deep"
~title:"Expression too deep"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Expression_too_deep -> Some () | _ -> None)
(fun () -> Expression_too_deep) ;
let description =
"Attempted to register an expression as global constant that has already \
been registered."
in
register_error_kind
`Branch
~id:"Expression_already_registered"
~title:"Expression already registered"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Expression_already_registered -> Some () | _ -> None)
(fun () -> Expression_already_registered) ;
let description =
"Found a badly formed constant expression. The 'constant' primitive must \
always be followed by a string of the hash of the expression it points \
to."
in
register_error_kind
`Branch
~id:"Badly_formed_constant_expression"
~title:"Badly formed constant expression"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Badly_formed_constant_expression -> Some () | _ -> None)
(fun () -> Badly_formed_constant_expression) ;
let description =
"No registered global was found at the given hash in storage."
in
register_error_kind
`Branch
~id:"Nonexistent_global"
~title:"Tried to look up nonexistent global"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Nonexistent_global -> Some () | _ -> None)
(fun () -> Nonexistent_global) ;
let description =
"Encountered an expression that, after expanding all constants, is larger \
than the expression size limit."
in
register_error_kind
`Branch
~id:"Expression_too_large"
~title:"Expression too large"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Expression_too_large -> Some () | _ -> None)
(fun () -> Expression_too_large)
let get context hash =
Storage.Global_constants.Map.find context hash >>=? fun (context, value) ->
match value with
| None -> fail Nonexistent_global
| Some value -> return (context, value)
let expr_to_address_in_context context expr =
let lexpr = Script_repr.lazy_expr expr in
Raw_context.consume_gas context @@ Script_repr.force_bytes_cost lexpr
>>? fun context ->
Script_repr.force_bytes lexpr >>? fun b ->
Raw_context.consume_gas context @@ Gas_costs.expr_to_address_in_context_cost b
>|? fun context -> (context, Script_expr_hash.hash_bytes [b])
let node_too_large node =
let node_size = Script_repr.Micheline_size.of_node node in
let nodes = Saturation_repr.to_int node_size.nodes in
let string_bytes = Saturation_repr.to_int node_size.string_bytes in
let z_bytes = Saturation_repr.to_int node_size.z_bytes in
Compare.Int.(
nodes > Constants_repr.max_micheline_node_count
|| string_bytes + z_bytes > Constants_repr.max_micheline_bytes_limit)
let expand_node context node =
Raw_context.consume_gas
context
(Gas_costs.expand_no_constants_branch_cost node)
>>?= fun context ->
bottom_up_fold_cps
(context, Expr_hash_map.empty, false)
node
(fun (context, _, did_expansion) node ->
return (context, node, did_expansion))
(fun (context, map, did_expansion) node k ->
match node with
| Prim (_, H_constant, args, annot) -> (
Raw_context.consume_gas context Gas_costs.expand_constants_branch_cost
>>?= fun context ->
match (args, annot) with
| ([String (_, address)], []) -> (
match Script_expr_hash.of_b58check_opt address with
| None -> fail Badly_formed_constant_expression
| Some hash -> (
match Expr_hash_map.find hash map with
| Some node ->
Raw_context.consume_gas
context
(Gas_costs.expand_no_constants_branch_cost node)
>>?= fun context -> k (context, map, true) node
| None ->
get context hash >>=? fun (context, expr) ->
let node = root expr in
Raw_context.consume_gas
context
(Gas_costs.expand_no_constants_branch_cost node)
>>?= fun context ->
k (context, Expr_hash_map.add hash node map, true) node))
| _ -> fail Badly_formed_constant_expression)
| Int _ | String _ | Bytes _ | Prim _ | Seq _ ->
k (context, map, did_expansion) node)
>>=? fun (context, node, did_expansion) ->
if did_expansion then
if node_too_large node then fail Expression_too_large
else return (context, node)
else return (context, node)
let expand context expr =
expand_node context (root expr) >|=? fun (context, node) ->
(context, strip_locations node)
(** Computes the maximum depth of a Micheline node. Fails
with [Expression_too_deep] if greater than
[max_allowed_global_constant_depth].*)
let check_depth node =
let rec advance node depth k =
if Compare.Int.(depth > Constants_repr.max_allowed_global_constant_depth)
then error Expression_too_deep
else
match node with
| Int _ | String _ | Bytes _ | Prim (_, _, [], _) | Seq (_, []) ->
(k [@tailcall]) (depth + 1)
| Prim (loc, _, hd :: tl, _) | Seq (loc, hd :: tl) ->
(advance [@tailcall]) hd (depth + 1) (fun dhd ->
(advance [@tailcall])
(Seq (loc, tl))
depth
(fun dtl -> (k [@tailcall]) (Compare.Int.max dhd dtl)))
in
advance node 0 (fun x -> Ok x)
let register context value =
expand_node context (root value) >>=? fun (context, node) ->
check_depth node >>?= fun (_depth : int) ->
expr_to_address_in_context context value >>?= fun (context, key) ->
trace Expression_already_registered
@@ Storage.Global_constants.Map.init context key value
>|=? fun (context, size) -> (context, key, Z.of_int size)
module Internal_for_tests = struct
let node_too_large = node_too_large
let bottom_up_fold_cps = bottom_up_fold_cps
let expr_to_address_in_context = expr_to_address_in_context
end