Source file sc_rollup_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
open Sc_rollup_errors
module Store = Storage.Sc_rollup
module Commitment = Sc_rollup_commitment_repr
module Commitment_hash = Commitment.Hash
let originate ctxt ~kind ~boot_sector ~parameters_ty =
Raw_context.increment_origination_nonce ctxt >>?= fun (ctxt, nonce) ->
let level = Raw_context.current_level ctxt in
Sc_rollup_repr.Address.from_nonce nonce >>?= fun address ->
Store.PVM_kind.add ctxt address kind >>= fun ctxt ->
Store.Initial_level.add ctxt address level.level >>= fun ctxt ->
Store.Boot_sector.add ctxt address boot_sector >>= fun ctxt ->
Store.Parameters_type.add ctxt address parameters_ty
>>=? fun (ctxt, param_ty_size_diff, _added) ->
let inbox = Sc_rollup_inbox_repr.empty address level.level in
Store.Inbox.init ctxt address inbox >>=? fun (ctxt, inbox_size_diff) ->
Store.Last_cemented_commitment.init ctxt address Commitment_hash.zero
>>=? fun (ctxt, lcc_size_diff) ->
Store.Staker_count.init ctxt address 0l >>=? fun (ctxt, stakers_size_diff) ->
let addresses_size = 2 * Sc_rollup_repr.Address.size in
let stored_kind_size = 2 in
let boot_sector_size =
Data_encoding.Binary.length Data_encoding.string boot_sector
in
let origination_size = Constants_storage.sc_rollup_origination_size ctxt in
let size =
Z.of_int
(origination_size + stored_kind_size + boot_sector_size + addresses_size
+ inbox_size_diff + lcc_size_diff + stakers_size_diff + param_ty_size_diff
)
in
return (address, size, ctxt)
let kind ctxt address = Store.PVM_kind.find ctxt address
let list ctxt = Store.PVM_kind.keys ctxt >|= Result.return
let initial_level ctxt rollup =
let open Lwt_tzresult_syntax in
let* level = Store.Initial_level.find ctxt rollup in
match level with
| None -> fail (Sc_rollup_does_not_exist rollup)
| Some level -> return level
let get_boot_sector ctxt rollup =
let open Lwt_tzresult_syntax in
let* boot_sector = Storage.Sc_rollup.Boot_sector.find ctxt rollup in
match boot_sector with
| None -> fail (Sc_rollup_does_not_exist rollup)
| Some boot_sector -> return boot_sector
let parameters_type ctxt rollup =
let open Lwt_result_syntax in
let+ ctxt, res = Store.Parameters_type.find ctxt rollup in
(res, ctxt)
module Outbox = struct
let level_index ctxt level =
let max_active_levels =
Constants_storage.sc_rollup_max_active_outbox_levels ctxt
in
Int32.rem (Raw_level_repr.to_int32 level) max_active_levels
let record_applied_message ctxt rollup level ~message_index =
let open Lwt_tzresult_syntax in
let*? () =
let max_outbox_messages_per_level =
Constants_storage.sc_rollup_max_outbox_messages_per_level ctxt
in
error_unless
Compare.Int.(
0 <= message_index && message_index < max_outbox_messages_per_level)
Sc_rollup_invalid_outbox_message_index
in
let level_index = level_index ctxt level in
let* ctxt, level_and_bitset_opt =
Store.Applied_outbox_messages.find (ctxt, rollup) level_index
in
let*? bitset, ctxt =
let open Tzresult_syntax in
let* bitset, ctxt =
match level_and_bitset_opt with
| Some (existing_level, bitset)
when Raw_level_repr.(existing_level = level) ->
let* already_applied = Bitset.mem bitset message_index in
let* () =
error_when
already_applied
Sc_rollup_outbox_message_already_applied
in
return (bitset, ctxt)
| Some (existing_level, _bitset)
when Raw_level_repr.(level < existing_level) ->
fail Sc_rollup_outbox_level_expired
| Some _ | None ->
return (Bitset.empty, ctxt)
in
let* bitset = Bitset.add bitset message_index in
return (bitset, ctxt)
in
let+ ctxt, size_diff, _is_new =
Store.Applied_outbox_messages.add
(ctxt, rollup)
level_index
(level, bitset)
in
(Z.of_int size_diff, ctxt)
end
module Dal_slot = struct
let slot_of_int_e n =
let open Tzresult_syntax in
match Dal_slot_repr.Index.of_int n with
| None -> fail Dal_errors_repr.Dal_slot_index_above_hard_limit
| Some slot_index -> return slot_index
let fail_if_slot_index_invalid ctxt slot_index =
let open Lwt_tzresult_syntax in
let*? max_slot_index =
slot_of_int_e @@ ((Raw_context.constants ctxt).dal.number_of_slots - 1)
in
if
Compare.Int.(
Dal_slot_repr.Index.compare slot_index max_slot_index > 0
|| Dal_slot_repr.Index.compare slot_index Dal_slot_repr.Index.zero < 0)
then
fail
Dal_errors_repr.(
Dal_subscribe_rollup_invalid_slot_index
{given = slot_index; maximum = max_slot_index})
else return slot_index
let all_indexes ctxt =
let max_slot_index = (Raw_context.constants ctxt).dal.number_of_slots - 1 in
Misc.(0 --> max_slot_index) |> List.map slot_of_int_e |> all_e
let subscribed_slots_at_level ctxt rollup level =
let open Lwt_tzresult_syntax in
let current_level = (Raw_context.current_level ctxt).level in
if Raw_level_repr.(level > current_level) then
fail
(Sc_rollup_requested_dal_slot_subscriptions_of_future_level
(current_level, level))
else
let*! subscription_levels =
Store.Slot_subscriptions.keys (ctxt, rollup)
in
let relevant_subscription_levels =
subscription_levels
|> List.filter (fun subscription_level ->
Raw_level_repr.(subscription_level <= level))
in
let last_subscription_level_opt =
List.fold_left
(fun max_level level ->
match max_level with
| None -> Some level
| Some max_level ->
Some
(if Raw_level_repr.(max_level > level) then max_level
else level))
None
relevant_subscription_levels
in
match last_subscription_level_opt with
| None -> return Bitset.empty
| Some subscription_level ->
Store.Slot_subscriptions.get (ctxt, rollup) subscription_level
let subscribe ctxt rollup ~slot_index =
let open Lwt_tzresult_syntax in
let* _slot_index = fail_if_slot_index_invalid ctxt slot_index in
let* _initial_level = initial_level ctxt rollup in
let {Level_repr.level; _} = Raw_context.current_level ctxt in
let* subscribed_slots = subscribed_slots_at_level ctxt rollup level in
let*? slot_already_subscribed =
Bitset.mem subscribed_slots (Dal_slot_repr.Index.to_int slot_index)
in
if slot_already_subscribed then
fail (Sc_rollup_dal_slot_already_registered (rollup, slot_index))
else
let*? subscribed_slots =
Bitset.add subscribed_slots (Dal_slot_repr.Index.to_int slot_index)
in
let*! ctxt =
Store.Slot_subscriptions.add (ctxt, rollup) level subscribed_slots
in
return (slot_index, level, ctxt)
let subscribed_slot_indices ctxt rollup level =
let all_indexes = all_indexes ctxt in
let to_dal_slot_index_list bitset =
let open Result_syntax in
let* all_indexes = all_indexes in
let+ slot_indexes =
all_indexes
|> List.map (fun i ->
let+ is_index_present =
Bitset.mem bitset (Dal_slot_repr.Index.to_int i)
in
if is_index_present then [i] else [])
|> all_e
in
List.concat slot_indexes
in
let open Lwt_tzresult_syntax in
let* _initial_level = initial_level ctxt rollup in
let* subscribed_slots = subscribed_slots_at_level ctxt rollup level in
let*? result = to_dal_slot_index_list subscribed_slots in
return result
end