Source file tx_rollup_commitment_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
open Tx_rollup_commitment_repr
open Tx_rollup_errors_repr
let check_message_result ctxt {messages; _} result ~path ~index =
(match result with
| `Hash hash -> ok (ctxt, hash)
| `Result result -> Tx_rollup_hash_builder.message_result ctxt result)
>>? fun (ctxt, computed) ->
Tx_rollup_gas.consume_check_path_commitment_cost ctxt >>? fun ctxt ->
let cond =
match
Merkle.check_path
path
index
computed
messages.Tx_rollup_commitment_repr.Compact.root
with
| Ok x -> x
| Error _ -> false
in
error_unless
cond
Tx_rollup_errors_repr.(
Wrong_rejection_hash
{provided = computed; expected = `Valid_path (messages.root, index)})
>>? fun () -> ok ctxt
let adjust_commitments_count ctxt tx_rollup pkh ~(dir : [`Incr | `Decr]) =
let delta = match dir with `Incr -> 1 | `Decr -> -1 in
Storage.Tx_rollup.Commitment_bond.find (ctxt, tx_rollup) pkh
>>=? fun (ctxt, commitment) ->
let count =
match commitment with Some count -> count + delta | None -> delta
in
fail_when Compare.Int.(count < 0) (Commitment_bond_negative count)
>>=? fun () ->
Storage.Tx_rollup.Commitment_bond.add (ctxt, tx_rollup) pkh count
>>=? fun (ctxt, _, _) -> return ctxt
let remove_bond :
Raw_context.t ->
Tx_rollup_repr.t ->
Signature.public_key_hash ->
Raw_context.t tzresult Lwt.t =
fun ctxt tx_rollup contract ->
Storage.Tx_rollup.Commitment_bond.find (ctxt, tx_rollup) contract
>>=? fun (ctxt, bond) ->
match bond with
| None -> fail (Bond_does_not_exist contract)
| Some 0 ->
Storage.Tx_rollup.Commitment_bond.remove (ctxt, tx_rollup) contract
>>=? fun (ctxt, _, _) -> return ctxt
| Some _ -> fail (Bond_in_use contract)
let slash_bond ctxt tx_rollup contract =
Storage.Tx_rollup.Commitment_bond.find (ctxt, tx_rollup) contract
>>=? fun (ctxt, bond_counter) ->
match bond_counter with
| None -> return (ctxt, false)
| Some c ->
Storage.Tx_rollup.Commitment_bond.remove (ctxt, tx_rollup) contract
>>=? fun (ctxt, _, _) -> return (ctxt, Compare.Int.(0 < c))
let find :
Raw_context.t ->
Tx_rollup_repr.t ->
Tx_rollup_state_repr.t ->
Tx_rollup_level_repr.t ->
(Raw_context.t * Submitted_commitment.t option) tzresult Lwt.t =
fun ctxt tx_rollup state level ->
if Tx_rollup_state_repr.has_valid_commitment_at state level then
Storage.Tx_rollup.Commitment.find (ctxt, tx_rollup) level
>>=? fun (ctxt, commitment) ->
match commitment with
| None ->
Tx_rollup_state_storage.assert_exist ctxt tx_rollup >>=? fun ctxt ->
return (ctxt, None)
| Some res -> return (ctxt, Some res)
else return (ctxt, None)
let get :
Raw_context.t ->
Tx_rollup_repr.t ->
Tx_rollup_state_repr.t ->
Tx_rollup_level_repr.t ->
(Raw_context.t * Submitted_commitment.t) tzresult Lwt.t =
fun ctxt tx_rollup state level ->
find ctxt tx_rollup state level >>=? fun (ctxt, commitment) ->
match commitment with
| None -> fail @@ Tx_rollup_errors_repr.Commitment_does_not_exist level
| Some commitment -> return (ctxt, commitment)
let get_finalized :
Raw_context.t ->
Tx_rollup_repr.t ->
Tx_rollup_state_repr.t ->
Tx_rollup_level_repr.t ->
(Raw_context.t * Submitted_commitment.t) tzresult Lwt.t =
fun ctxt tx_rollup state level ->
let window = Tx_rollup_state_repr.finalized_commitments_range state in
(match window with
| Some (first, last) ->
error_unless
Tx_rollup_level_repr.(first <= level && level <= last)
(Tx_rollup_errors_repr.No_finalized_commitment_for_level {level; window})
| None ->
error
(Tx_rollup_errors_repr.No_finalized_commitment_for_level {level; window}))
>>?= fun () ->
Storage.Tx_rollup.Commitment.find (ctxt, tx_rollup) level
>>=? fun (ctxt, commitment) ->
match commitment with
| None -> fail @@ Tx_rollup_errors_repr.Commitment_does_not_exist level
| Some commitment -> return (ctxt, commitment)
let check_commitment_level current_level state commitment =
Tx_rollup_state_repr.next_commitment_level state current_level
>>? fun expected_level ->
error_when
Tx_rollup_level_repr.(commitment.level < expected_level)
(Level_already_has_commitment commitment.level)
>>? fun () ->
error_when
Tx_rollup_level_repr.(expected_level < commitment.level)
(Commitment_too_early
{provided = commitment.level; expected = expected_level})
(** [check_commitment_predecessor ctxt tx_rollup state commitment]
will raise an error if the [predecessor] field of [commitment] is
not consistent with the context, assuming its [level] field is
correct. *)
let check_commitment_predecessor ctxt state commitment =
match
( commitment.predecessor,
Tx_rollup_state_repr.next_commitment_predecessor state )
with
| (Some pred_hash, Some expected_hash) when Hash.(pred_hash = expected_hash)
->
return ctxt
| (None, None) -> return ctxt
| (provided, expected) -> fail (Wrong_predecessor_hash {provided; expected})
let check_commitment_batches_and_merkle_root ctxt state inbox commitment =
let Tx_rollup_inbox_repr.{inbox_length; merkle_root; _} = inbox in
fail_unless
Compare.List_length_with.(commitment.messages = inbox_length)
Wrong_batch_count
>>=? fun () ->
fail_unless
Tx_rollup_inbox_repr.Merkle.(commitment.inbox_merkle_root = merkle_root)
Wrong_inbox_hash
>>=? fun () -> return (ctxt, state)
let add_commitment ctxt tx_rollup state pkh commitment =
let commitment_limit =
Constants_storage.tx_rollup_max_commitments_count ctxt
in
fail_when
Compare.Int.(
Tx_rollup_state_repr.commitments_count state >= commitment_limit)
Too_many_commitments
>>=? fun () ->
let current_level = (Raw_context.current_level ctxt).level in
check_commitment_level current_level state commitment >>?= fun () ->
check_commitment_predecessor ctxt state commitment >>=? fun ctxt ->
Tx_rollup_inbox_storage.get ctxt commitment.level tx_rollup
>>=? fun (ctxt, inbox) ->
check_commitment_batches_and_merkle_root ctxt state inbox commitment
>>=? fun (ctxt, state) ->
Storage.Tx_rollup.Commitment.find (ctxt, tx_rollup) commitment.level
>>=? fun (ctxt, invalid_commitment) ->
Option.map_e
(fun x ->
let to_slash = x.Submitted_commitment.committer in
error_when Signature.Public_key_hash.(pkh = to_slash) Invalid_committer
>>? fun () -> ok to_slash)
invalid_commitment
>>?= fun to_slash ->
Tx_rollup_gas.consume_compact_commitment_cost ctxt inbox.inbox_length
>>?= fun ctxt ->
let commitment = Tx_rollup_commitment_repr.Full.compact commitment in
Tx_rollup_hash_builder.compact_commitment ctxt commitment
>>?= fun (ctxt, commitment_hash) ->
let submitted : Tx_rollup_commitment_repr.Submitted_commitment.t =
{
commitment;
commitment_hash;
committer = pkh;
submitted_at = current_level;
finalized_at = None;
}
in
Storage.Tx_rollup.Commitment.add (ctxt, tx_rollup) commitment.level submitted
>>=? fun (ctxt, _commitment_size_alloc, _) ->
Tx_rollup_state_repr.record_commitment_creation
state
commitment.level
commitment_hash
>>?= fun state ->
adjust_commitments_count ctxt tx_rollup pkh ~dir:`Incr >>=? fun ctxt ->
return (ctxt, state, to_slash)
let pending_bonded_commitments :
Raw_context.t ->
Tx_rollup_repr.t ->
Signature.public_key_hash ->
(Raw_context.t * int) tzresult Lwt.t =
fun ctxt tx_rollup pkh ->
Storage.Tx_rollup.Commitment_bond.find (ctxt, tx_rollup) pkh
>|=? fun (ctxt, pending) -> (ctxt, Option.value ~default:0 pending)
let has_bond :
Raw_context.t ->
Tx_rollup_repr.t ->
Signature.public_key_hash ->
(Raw_context.t * bool) tzresult Lwt.t =
fun ctxt tx_rollup pkh ->
Storage.Tx_rollup.Commitment_bond.find (ctxt, tx_rollup) pkh
>|=? fun (ctxt, pending) -> (ctxt, Option.is_some pending)
let finalize_commitment ctxt rollup state =
match Tx_rollup_state_repr.next_commitment_to_finalize state with
| Some oldest_inbox_level ->
get ctxt rollup state oldest_inbox_level >>=? fun (ctxt, commitment) ->
let finality_period = Constants_storage.tx_rollup_finality_period ctxt in
let current_level = (Raw_context.current_level ctxt).level in
fail_when
Raw_level_repr.(
current_level < add commitment.submitted_at finality_period)
No_commitment_to_finalize
>>=? fun () ->
Tx_rollup_inbox_storage.remove ctxt oldest_inbox_level rollup
>>=? fun ctxt ->
Storage.Tx_rollup.Commitment.update
(ctxt, rollup)
oldest_inbox_level
{commitment with finalized_at = Some current_level}
>>=? fun (ctxt, _commitment_size_alloc) ->
Tx_rollup_state_repr.record_inbox_deletion state oldest_inbox_level
>>?= fun state -> return (ctxt, state, oldest_inbox_level)
| None -> fail No_commitment_to_finalize
let remove_commitment ctxt rollup state =
match Tx_rollup_state_repr.next_commitment_to_remove state with
| Some tail ->
get ctxt rollup state tail >>=? fun (ctxt, commitment) ->
(match commitment.finalized_at with
| Some finalized_at ->
let withdraw_period =
Constants_storage.tx_rollup_withdraw_period ctxt
in
let current_level = (Raw_context.current_level ctxt).level in
fail_when
Raw_level_repr.(current_level < add finalized_at withdraw_period)
No_commitment_to_remove
| None ->
fail (Internal_error "Missing finalized_at field"))
>>=? fun () ->
adjust_commitments_count ctxt rollup commitment.committer ~dir:`Decr
>>=? fun ctxt ->
Storage.Tx_rollup.Commitment.remove (ctxt, rollup) tail
>>=? fun (ctxt, _freed_size, _existed) ->
Tx_rollup_reveal_storage.remove ctxt rollup tail >>=? fun ctxt ->
let msg_hash = commitment.commitment.messages.last_result_message_hash in
Tx_rollup_state_repr.record_commitment_deletion
state
tail
commitment.commitment_hash
msg_hash
>>?= fun state -> return (ctxt, state, tail)
| None -> fail No_commitment_to_remove
let check_agreed_and_disputed_results ctxt tx_rollup state
(submitted_commitment : Submitted_commitment.t) ~agreed_result
~agreed_result_path ~disputed_result ~disputed_position
~disputed_result_path =
let commitment = submitted_commitment.commitment in
Tx_rollup_state_repr.check_level_can_be_rejected state commitment.level
>>?= fun () ->
check_message_result
ctxt
commitment
(`Hash disputed_result)
~path:disputed_result_path
~index:disputed_position
>>?= fun ctxt ->
if Compare.Int.(disputed_position = 0) then
Tx_rollup_hash_builder.message_result ctxt agreed_result
>>?= fun (ctxt, agreed) ->
match Tx_rollup_level_repr.pred commitment.level with
| None ->
let expected = Tx_rollup_message_result_hash_repr.init in
fail_unless
Tx_rollup_message_result_hash_repr.(agreed = expected)
(Wrong_rejection_hash {provided = agreed; expected = `Hash expected})
>>=? fun () -> return ctxt
| Some pred_level -> (
Storage.Tx_rollup.Commitment.find (ctxt, tx_rollup) pred_level
>>=? fun (ctxt, candidate) ->
match candidate with
| Some commitment ->
let expected =
commitment.commitment.messages.last_result_message_hash
in
fail_unless
Tx_rollup_message_result_hash_repr.(agreed = expected)
(Wrong_rejection_hash
{provided = agreed; expected = `Hash expected})
>>=? fun () -> return ctxt
| None -> (
match Tx_rollup_state_repr.last_removed_commitment_hashes state with
| Some (last_hash, _) ->
fail_unless
Tx_rollup_message_result_hash_repr.(agreed = last_hash)
(Wrong_rejection_hash
{provided = agreed; expected = `Hash last_hash})
>>=? fun () -> return ctxt
| None -> fail (Internal_error "Missing commitment predecessor")))
else
check_message_result
ctxt
commitment
(`Result agreed_result)
~path:agreed_result_path
~index:(disputed_position - 1)
>>?= fun ctxt -> return ctxt
let reject_commitment ctxt rollup state level =
Tx_rollup_state_repr.check_level_can_be_rejected state level >>?= fun () ->
(match Tx_rollup_level_repr.pred level with
| Some pred_level ->
find ctxt rollup state pred_level >>=? fun (ctxt, pred_commitment) ->
let pred_hash =
Option.map
(fun (x : Submitted_commitment.t) -> x.commitment_hash)
pred_commitment
in
return (ctxt, pred_hash)
| None -> return (ctxt, None))
>>=? fun (ctxt, pred_hash) ->
Tx_rollup_state_repr.record_commitment_rejection state level pred_hash
>>?= fun state -> return (ctxt, state)