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
module Patch_legacy_contracts_for_J = struct
let patch_script (address, legacy_script_hash, patched_code) ctxt =
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 legacy_script_hash then (
let new_code = Script_repr.lazy_expr patched_code in
Logging.log Notice "Contract %s successfully patched" address ;
Storage.Contract.Code.update ctxt contract new_code
>>=? fun (ctxt, size_diff) ->
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
legacy_script_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
end
let prepare_first_block ctxt ~typecheck ~level ~timestamp =
Raw_context.prepare_first_block ~level ~timestamp 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 ->
Raw_level_repr.of_int32 level >>?= fun level ->
Storage.Tenderbake.First_level_of_protocol.init ctxt level >>=? fun ctxt ->
(match previous_protocol with
| Genesis param ->
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
?no_reward_cycles:param.no_reward_cycles
param.bootstrap_accounts
param.bootstrap_contracts
>>=? fun (ctxt, bootstrap_balance_updates) ->
Delegate_storage.init_first_cycles ctxt >>=? fun ctxt ->
let cycle = (Raw_context.current_level ctxt).cycle in
Delegate_storage.freeze_deposits_do_not_call_except_for_migration
~new_cycle:cycle
~balance_updates:[]
ctxt
>>=? fun (ctxt, deposits_balance_updates) ->
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
>>=? fun (ctxt, operation_results) ->
Storage.Pending_migration.Operation_results.init ctxt operation_results
>>=? fun ctxt ->
return
( ctxt,
commitments_balance_updates @ bootstrap_balance_updates
@ deposits_balance_updates )
| Ithaca_012 ->
Storage.Vote.Legacy_listings_size.remove ctxt >>= fun ctxt ->
Vote_storage.update_listings ctxt >>=? fun ctxt ->
Liquidity_baking_migration.Migration_from_Ithaca.update ctxt
>>=? fun ctxt -> return (ctxt, []))
>>=? fun (ctxt, balance_updates) ->
Storage.Tenderbake.First_level_legacy.remove ctxt >>= fun ctxt ->
Receipt_repr.group_balance_updates balance_updates >>?= fun balance_updates ->
Storage.Pending_migration.Balance_updates.add ctxt balance_updates
>>= fun ctxt ->
List.fold_right_es
Patch_legacy_contracts_for_J.patch_script
Legacy_script_patches_for_J.addresses_to_patch
ctxt
>>=? fun ctxt -> return ctxt
let prepare ctxt ~level ~predecessor_timestamp ~timestamp =
Raw_context.prepare ~level ~predecessor_timestamp ~timestamp ctxt
>>=? fun ctxt -> Storage.Pending_migration.remove ctxt