Source file init_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
let patch_script ctxt (address, hash, patched_code) =
Contract_repr.of_b58check address >>?= fun contract ->
Storage.Contract.Code.find ctxt contract >>=? fun (ctxt, code_opt) ->
Logging.log Notice "Patching %s... " address ;
match code_opt with
| Some old_code ->
let old_bin = Data_encoding.force_bytes old_code in
let old_hash = Script_expr_hash.hash_bytes [old_bin] in
if Script_expr_hash.equal old_hash hash then (
let new_code = Script_repr.lazy_expr patched_code in
Storage.Contract.Code.update ctxt contract new_code
>>=? fun (ctxt, size_diff) ->
Logging.log Notice "Contract %s successfully patched" address ;
let size_diff = Z.of_int size_diff in
Storage.Contract.Used_storage_space.get ctxt contract
>>=? fun prev_size ->
let new_size = Z.add prev_size size_diff in
Storage.Contract.Used_storage_space.update ctxt contract new_size
>>=? fun ctxt ->
if Z.(gt size_diff zero) then
Storage.Contract.Paid_storage_space.get ctxt contract
>>=? fun prev_paid_size ->
let paid_size = Z.add prev_paid_size size_diff in
Storage.Contract.Paid_storage_space.update ctxt contract paid_size
else return ctxt)
else (
Logging.log
Error
"Patching %s was skipped because its script does not have the \
expected hash (expected: %a, found: %a)"
address
Script_expr_hash.pp
hash
Script_expr_hash.pp
old_hash ;
return ctxt)
| None ->
Logging.log
Error
"Patching %s was skipped because no script was found for it in the \
context."
address ;
return ctxt
(** Converts {Storage.Stake.Staking_balance} from {Tez_repr} to
{Stake_repr.Full}. Remove me in P. *)
let migrate_staking_balance_for_o ctxt =
let open Lwt_result_syntax in
let convert ctxt delegate staking_balance =
let delegate_contract = Contract_repr.Implicit delegate in
let* {current_amount = own_frozen; initial_amount = _} =
Frozen_deposits_storage.get ctxt delegate_contract
in
let delegated =
Tez_repr.sub_opt staking_balance own_frozen
|> Option.value ~default:Tez_repr.zero
in
let staked_frozen = Tez_repr.zero in
return (Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated)
in
Storage.Stake.Staking_balance_up_to_Nairobi.fold
ctxt
~order:`Undefined
~init:(ok ctxt)
~f:(fun delegate staking_balance ctxt ->
let*? ctxt in
let* stake = convert ctxt delegate staking_balance in
Storage.Stake.Staking_balance.update ctxt delegate stake)
(** Clear staking balance snapshots to avoid storing legacy values.
This should do nothing if the migration happens at a cycle
boundary, which is usually the case.
*)
let clear_staking_balance_snapshots_for_o ctxt =
let open Lwt_result_syntax in
let*! ctxt =
Storage.Stake.Staking_balance_up_to_Nairobi.Snapshot.clear ctxt
in
Storage.Stake.Last_snapshot.update ctxt 0
(** Converts {Storage.Stake.Total_active_stake} and
{Storage.Stake.Selected_distribution_for_cycle} from {Tez_repr} to
{Stake_repr}.
Remove me in P. *)
let migrate_stake_distribution_for_o ctxt =
let open Lwt_result_syntax in
let convert =
let limit_of_delegation_over_baking =
Constants_storage.limit_of_delegation_over_baking ctxt
in
fun old_stake ->
let frozen =
Tez_repr.div_exn old_stake (limit_of_delegation_over_baking + 1)
in
match Tez_repr.sub_opt old_stake frozen with
| Some delegated -> Stake_repr.make ~frozen ~delegated
| None -> Stake_repr.make ~frozen:old_stake ~delegated:Tez_repr.zero
in
let* ctxt =
Storage.Stake.Total_active_stake_up_to_Nairobi.fold
ctxt
~order:`Undefined
~init:(ok ctxt)
~f:(fun cycle stake ctxt ->
let*? ctxt in
let stake = convert stake in
Storage.Stake.Total_active_stake.update ctxt cycle stake)
in
Storage.Stake.Selected_distribution_for_cycle_up_to_Nairobi.fold
ctxt
~order:`Undefined
~init:(ok ctxt)
~f:(fun cycle distr ctxt ->
let*? ctxt in
let distr = List.map (fun (pkh, stake) -> (pkh, convert stake)) distr in
Storage.Stake.Selected_distribution_for_cycle.update ctxt cycle distr)
(** Initializes the total supply when migrating from N to O
Uses an estimation of the total supply at the activation of O.
This value can be refined at the beginning of P to have a
perfectly accurate ammount on mainnet.
Remove me in P. *)
let initialize_total_supply_for_o chain_id ctxt =
let open Lwt_syntax in
if Chain_id.equal Constants_repr.mainnet_id chain_id then
Storage.Contract.Total_supply.add
ctxt
(Tez_repr.of_mutez_exn 975_000_000_000_000L)
else
let* total_supply =
Storage.Contract.fold
ctxt
~order:`Undefined
~f:(fun contract acc ->
let* full_balance =
Contract_storage.For_RPC.get_full_balance ctxt contract
in
match full_balance with
| Ok full_balance ->
return @@ Result.value ~default:acc Tez_repr.(acc +? full_balance)
| _ -> return acc)
~init:Tez_repr.zero
in
Storage.Contract.Total_supply.add ctxt total_supply
let migrate_pending_consensus_keys_for_o ctxt =
let open Lwt_result_syntax in
Storage.Delegates.fold
ctxt
~order:`Undefined
~init:ctxt
~f:(fun delegate ctxt ->
let delegate = Contract_repr.Implicit delegate in
let*! pending_cks =
Storage.Contract.Pending_consensus_keys_up_to_Nairobi.bindings
(ctxt, delegate)
in
List.fold_left_s
(fun ctxt (cycle, pk) ->
let*! ctxt =
Storage.Pending_consensus_keys.add (ctxt, cycle) delegate pk
in
Storage.Contract.Pending_consensus_keys_up_to_Nairobi.remove
(ctxt, delegate)
cycle)
ctxt
pending_cks)
let prepare_first_block chain_id ctxt ~typecheck_smart_contract
~typecheck_smart_rollup ~level ~timestamp ~predecessor =
Raw_context.prepare_first_block ~level ~timestamp chain_id ctxt
>>=? fun (previous_protocol, ctxt) ->
let parametric = Raw_context.constants ctxt in
( Raw_context.Cache.set_cache_layout
ctxt
(Constants_repr.cache_layout parametric)
>|= fun ctxt -> Raw_context.Cache.clear ctxt )
>>= fun ctxt ->
(match previous_protocol with
| Genesis param ->
Raw_level_repr.of_int32 level >>?= fun level ->
Storage.Tenderbake.First_level_of_protocol.init ctxt level
>>=? fun ctxt ->
Storage.Tenderbake.Forbidden_delegates.init
ctxt
Signature.Public_key_hash.Set.empty
>>=? fun ctxt ->
Storage.Contract.Total_supply.add ctxt Tez_repr.zero >>= fun ctxt ->
Storage.Block_round.init ctxt Round_repr.zero >>=? fun ctxt ->
let init_commitment (ctxt, balance_updates)
Commitment_repr.{blinded_public_key_hash; amount} =
Token.transfer
ctxt
`Initial_commitments
(`Collected_commitments blinded_public_key_hash)
amount
>>=? fun (ctxt, new_balance_updates) ->
return (ctxt, new_balance_updates @ balance_updates)
in
List.fold_left_es init_commitment (ctxt, []) param.commitments
>>=? fun (ctxt, commitments_balance_updates) ->
Storage.Stake.Last_snapshot.init ctxt 0 >>=? fun ctxt ->
Seed_storage.init ?initial_seed:param.constants.initial_seed ctxt
>>=? fun ctxt ->
Contract_storage.init ctxt >>=? fun ctxt ->
Bootstrap_storage.init
ctxt
~typecheck_smart_contract
~typecheck_smart_rollup
?no_reward_cycles:param.no_reward_cycles
param.bootstrap_accounts
param.bootstrap_contracts
param.bootstrap_smart_rollups
>>=? fun (ctxt, bootstrap_balance_updates) ->
Delegate_cycles.init_first_cycles ctxt >>=? fun ctxt ->
Vote_storage.init
ctxt
~start_position:(Level_storage.current ctxt).level_position
>>=? fun ctxt ->
Vote_storage.update_listings ctxt >>=? fun ctxt ->
Liquidity_baking_migration.init ctxt ~typecheck:typecheck_smart_contract
>>=? fun (ctxt, operation_results) ->
Storage.Pending_migration.Operation_results.init ctxt operation_results
>>=? fun ctxt ->
Sc_rollup_inbox_storage.init_inbox ~predecessor ctxt >>=? fun ctxt ->
Adaptive_issuance_storage.init ctxt >>=? fun ctxt ->
return (ctxt, commitments_balance_updates @ bootstrap_balance_updates)
| Nairobi_017
->
Raw_level_repr.of_int32 level >>?= fun level ->
Storage.Tenderbake.First_level_of_protocol.update ctxt level
>>=? fun ctxt ->
Storage.Tenderbake.Endorsement_branch.find ctxt >>=? fun opt ->
Storage.Tenderbake.Endorsement_branch.remove ctxt >>= fun ctxt ->
Storage.Tenderbake.Attestation_branch.add_or_remove ctxt opt
>>= fun ctxt ->
Storage.Tenderbake.Forbidden_delegates.init
ctxt
Signature.Public_key_hash.Set.empty
>>=? fun ctxt ->
migrate_staking_balance_for_o ctxt >>=? fun ctxt ->
clear_staking_balance_snapshots_for_o ctxt >>=? fun ctxt ->
migrate_stake_distribution_for_o ctxt >>=? fun ctxt ->
initialize_total_supply_for_o chain_id ctxt >>= fun ctxt ->
Remove_zero_amount_ticket_migration_for_o.remove_zero_ticket_entries ctxt
>>= fun ctxt ->
Adaptive_issuance_storage.init ctxt >>=? fun ctxt ->
migrate_pending_consensus_keys_for_o ctxt >>= fun ctxt ->
Sc_rollup_refutation_storage.migrate_clean_refutation_games ctxt
>>=? fun ctxt -> return (ctxt, []))
>>=? fun (ctxt, balance_updates) ->
List.fold_left_es patch_script ctxt Legacy_script_patches.addresses_to_patch
>>=? fun ctxt ->
Receipt_repr.group_balance_updates balance_updates >>?= fun balance_updates ->
Storage.Pending_migration.Balance_updates.add ctxt balance_updates
>>= fun ctxt -> return ctxt
let prepare ctxt ~level ~predecessor_timestamp ~timestamp =
Raw_context.prepare
~level
~predecessor_timestamp
~timestamp
~adaptive_issuance_enable:false
ctxt
>>=? fun ctxt ->
Adaptive_issuance_storage.set_adaptive_issuance_enable ctxt >>=? fun ctxt ->
Storage.Pending_migration.remove ctxt